Saturday, July 26, 2014

Hidden Secrets of Interfaces in .NET C#

Interface contains definitions for a group of related functionalties that a class or a struct can implement. Interface in C# is similar to an abstract class in which all methods are abstract (Why C# interface methods not declared as abstract). 

Interface can contain methods, properties, events, indexers but it can't contain constants, fields (why interface can't have fields), operators, instance constructors or types. Interface members are automatically public and cannot be static. (interfaces in MSDN)

When a class or struct implements an interface, it must provide an implementation for all the members that interface defines. If you implement an interface method in a deriving class it must be public. When a base list contains base class and interfaces, base class must come first in the list. 

Methods in interfaces can have optional parameters. But they are not enforced in implementing classes

Any class or struct that implements IEquatable<T> interface must contain definition for an Equals method. Also note that Reference types doesn't benefit much when using IEquatable.

Importance of an interface
Covered in SO, SO, Here 

An interface can inherit from one or more base interfaces. See Composition over Inheritance.

Explicit interface implementation 

Problem : Class implements 2 interfaces has a member with same signature
Solution : Explicit interface implementation



Friday, July 25, 2014

How to Asynchronous Programming in C#

async and await are the main keywords in asynchronous programming. You can use asynchronous with things such as downloads, uploads etc.


Below example shows you how to get a specific Http Header asynchronously.

  • Name of async method by convention ends with async. 
  • Return type is one of the following,
    • Task<TResult>
    • Task
    • void
  • Method has at least one await expression
Resources

https://msdn.microsoft.com/en-us/library/hh191443.aspx

Wednesday, July 9, 2014

Covariance and Contravariance in Generics

Covariance

Enables you to use more specific type than the type originally specified.

IEnumerable<Derived> d = new List<Derived>();
IEnumerable<Base> b = d;

Contravariance

Enables you to use more generic type than the type originally specified.

Action<Base> b = (target) => { Console.WriteLine(target.GetType().Name); };
Action<Derived> d = b;
d(new Derived());
Examples from MSDN.


Resources
https://msdn.microsoft.com/en-us/library/ee207183.aspx
http://stackoverflow.com/questions/2662369/covariance-and-contravariance-real-world-example

Tuesday, July 8, 2014

Introduction to Entity Framework

Entity Framework (EF) is an object-relational mapper (ORM) for .NET. Entity Framework can be used to work with relational data using domain-specific objects. Entity Framework eliminates need for data access code that programmers usually need to write. The codes you write with Entity Framework will be converted to SQL queries. 

You can view them using ((System.Data.Objects.ObjectQuery)result).ToTraceString(); 
ObjectQuery class is the base class for queries against a conceptual model. There's also the ObjectQuery<T> generic class. 

Architecture of Entity Framework



Above diagram shows the high-level architecture of the Entity Framework. Conceptual model contains model classes and there relationshps this is independent from the actual tables in the database. For details of other components in the above diagram see this.

You can also check Entity Framework Overview available in MSDN

Workflows

In Entity Framework there are 3 workflows developers can use to start with. They are Model First, Database First and Code first. All of these has its own pros and cons so understanding the differences correctly is important.

Model First

Using Model First approach developers can create the Database model using ORM designer in Visual Studio. Here the designer relies on the .edmx file designer to maintain the design specifics. 

The physical database will be then generated from the model.

Database First

Using this approach you can reverse engineer a model from an existing database. The classes are automatically generated from EDMX file. Read Entity Framework Database First Example for steps.

In the wizard for creating the model, you'll get checkboxes to select Pluralize or singularize generated object names option. Read more about it here.  

Code First

Create classes first and generate Database from the classes directly. No use of Entity designer at all. 

Choosing the suitable workflow

Understanding which workflow to use is important before you start developing data access applications using EF. 

http://blog.smartbear.com/development/choosing-the-right-entity-framework-workflow/
http://msdn.microsoft.com/en-us/library/vstudio/cc853327%28v=vs.110%29.aspx  

Data Persisting in Entity Framework

Entity Framework use Unit of Work pattern to persist data. 

Monday, July 7, 2014

What is ASP.NET Core

ASP.NET Core (previously known as ASP.NET VNext and then ASP.NET 5is the next version of ASP.NET. Unlike its predecessors, Core is is open source available on github.

Here are some of the features

  • Open source and cross platform
  • Can run on .NET core or on full .NET framework
  • Leander and modular
  • ASP.NET Core is no longer based on System.Web.dll it is now based on NuGet packages
  • Built-in dependency injection
  • New light-weight and modular HTTP request pipeline
Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook