Unity Frame Work in ASP.Net MVC Application

No.of Views881
Bookmarked1 times
Downloads 
Votes0
By  Dhananjay Kumar   On  08 May 2010 09:05:46
Tag : ASP.NET , ASP.NET MVC
This article will explain step by step that how to inject dependency using UNITY framework in ASP.Net MVC Application
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will explain step by step that how to inject dependency using UNITY framework in ASP.Net MVC Application

Step 1

Create a MVC application


Step 2

Add Unity references to the MVC application.

Image Loading

Step 3

Idea here is to create a custom controller factory.  This custom controller factory will create the controller and inject the dependencies in that. Add a class to the project. Feel free to give any name; here name of the class is CustomUnityControllerFactory.

 

Image Loading

Extend this CustomUnityControllerFactory class from DefaultControllerFactory class.

 

Image Loading

Create reference of IUnityContainer. Don’t forget to add include namespace Unity.

 

Image Loading

Create the constructor for CustomUnityControllerFactory and pass reference of Unity Container in that.

 

Image Loading

Override GetControllerInstance of  DefaultControllerFactory. Here we will pass type of the controller , which we are going to create.  In parameter of overrideen function we are passing type of the controller to be instanciated.

 

Image Loading
 
So the complete CustomUnityControllerFactory class will look like below.
 

CustomUnityControllerFactory.cs

 
Image Loading

Step 4

Now we need to do something at start up of the application. For that open Global. asax file. We need to modify Application.Start() method.
a.    Create instance of IUnityContainer.
b.    Create instance of CustomUnityControllerFactory and pass instance of unity container in that.
c.    Set the CustomUnityControllerFactory as the default controller factory to be used. For this call Current method on ControllerBuilder and pass instance of CustomUnityControllerFactory in that. (see below code in blue)
 

Image Loading

d.    Compile the application.

Step 5

Now it is time to check that, how Custom Unity Container Factory is working. To test that, we will create a service interface and concrete service class in Model. 
a.    Right click on Model and add an interface
 

Image Loading

Interface contains the below code.

 

Image Loading

b.    Right click on Model and add a class and implement the Message() method.

 

Image Loading

Class contains the below code

 

Image Loading

Step 6

Go to Application_Start() and register the type
 

Image Loading

So, now the complete code at Application Start () will look like

 

Image Loading

Step 7

Now put a breakpoint at constructor of HomeController and run the application. We could see while debugging that dependency has been injected.

 

Image Loading

Now, you can see that dependency has been injected.  Now normal routing will process. For Routing and other details of MVP read my articles here.

Conclusion

I showed how to inject dependency using Unity in MVC application.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top