Friday, June 15, 2012

Unity Container

Unity Application Block is a dependency injection container which supports for,
  • Property Injection
  • Constructor Injection
  • Method Call Injection
Dependency injection is a design pattern which allows to injects the components at run time, rather than the compile time.

Property Injection

Below sample project is, the way to bind different database implementation(Oracle & SQL implementation) at run time, without compile the application again and again.

Create a solution and add projects as follow.

In the UnitLib project, declare the interfaces which we are going to bind objects at run time. It's better to define the interfaces in a separate project. Because dependency injection is pattern is about loose coupling and separation of behavior.
Implement the interfaces in the UnityLibImplOracle and UnityLibImplSQL projects. We should write the different implementation in separate projects. According to the sample, it's better to separate the Oracle implementation and the SQL implementation.
In the facade we can define the StudentFacade like below.
The real implementation of IUnityStudent's Register method will not be known till the run time.
In the run-time the concrete implementation of the IUnityStudent will be injected  by Unity, depending on it's configuration. In the type we should define the class of the property with the name space and then by separating comma, we should define the Dll file name. In mapTo xml tag define the concrete class that should be use in the run time.

The entry point for the application is like,
In the above configuration file, in the run-time the program will access the Oracle based implementation. By changing only the configuration file, in the run time we can load the implementation of the SQL.

In the above code we are calling the resolve no argument method. But we can pass the registration name and through that we can bind the related objects at the run time.

Constructor Injection

Create a class called Student
In the constructor after the object initialization, using setters, the FirstName will be set.

Method Injection

In the UserFacade class define the method that we need to inject at the run time.
We can configure the App.config file like below.
According to the Microsoft, If you are not sure which type of injection to use, the recommendation is that you use constructor injection. This is likely to satisfy almost all general requirements.

Monday, June 11, 2012

ASP.Net MVC Web API

Using Web API we can develop richer application for various clients. It allows to expose functionality/data in secure way over HTTP rather than through WCF or SOAP.
Lots of people have already developed applications using web API, examples like Google, Facebook, Twitter & etc.

Create a ASP.Net MVC internet Project.  And add a class called Student.
Add a Web API Controller class - StudentsController. Write action methods like below.
After we run the application we can access the API through the browser. We can request http://localhost:1965/api/students, it'll return back all the students in the database.
This is an API. No html associated with it. So we can use a tool like Fiddler.

By changing the Accept header in the HTTP Request Headers we can have json or xml response back. Web API's default response type is json.
Another feature in Web API is OData  support. To request an OData type of request, that the action method must have IQueryable type of return.
We can request http://localhost:1965/api/students?$filter=FirstName eq 'Dilan'. According to the filters the data will be return back from the action method.

Up to now we were retrieving the data from the data base. Now we are going to update the database.
To call that method, we can use Fiddler. In the request headers, we should add the content-type as json.  And then we can send Student object as json type as follows.
We can add more details to the response by adding custom Http status code, custom headers and etc. To do that, we should make few changes to the post method that we wrote.
In the below image, first line is without customizing the response. And second line is, after we add the HttpResponseMessage. The result type is different. And also if we check the headers, there will be location URL for newly created student.

Those are the major things we should know about the Web API.

If we add another no-argument action, and make a request to http://localhost:1965/api/students, this will give an error - Multiple actions were found that match the request.

Reason for that is, in the default project, routing for the api is configured for the controllers. Not for the actions. So we can configure it for actions as follows.
Then we can request for each specific actions like http://localhost:1965/api/students/Get.

The advantage of Web API is we can call it from any client like, Windows Service, WPF, Metro, iOS, Android and etc. Also we can integrate Web API with jquery, knockout and etc. So it makes really easier to develop richer applications.

Wednesday, June 6, 2012

Entity Framework with Inheritance

In Entity Framework(EF) code-first approach, there are three types of methods to handle the inheritance hierarchy.
  • Table per Hierarchy (TPH)
  • Table per Type (TPT)
  • Table per Concrete Class (TPC)
By default EF use TPH approach.

Table per Hierarchy (TPH)

In TPH, All data in the hierarchy will be saved in a single table. It uses Discriminator column to identify which record belongs to which sub type. Value of this column is the name of the sub type.

In the below sample I have a base class called Vehicle. Car and Bicycle classes are inherited from the Vehicle class. SportsCar class is inherited from the Car class.
According to the above class structure the database table design will be look like below. The Discriminator column will be added automatically through the EF.
After add some data, it'll be look like below. The value of the Discriminator column will be the name of the sub type.

Table per Type (TPT)

In TPT, EF will create table for each type. In this approach inheritance relationships are represented as Relational Foreign Key Associations.
The table for sub classes contains only the non-inherited properties along with the primary key. Primary key of each sub class is a foreign key for the base class.
To do this we can override the OnModelCreating method of DbContext and write some fluent API code.
The design of the tables will be as follow.


After add some data, it'll be like,

Table per Concrete Class (TPC)

In TPC, for each class, the EF will create a table. So all the properties and all the inherited properties will be mapped to the each table.
To do this we can override the OnModelCreating method of DbContext and write some fluent API code.
After add some data, tables'll be like.