Click here to Skip to main content
13,549,170 members
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 23 Mar 2012
Licenced CPOL

Basics of ASP.NET MVC 3: Part-III (Dependency Injection)

, 26 Mar 2012
Rate this:
Please Sign up or sign in to vote.
Explains basic information and steps for creating an MVC3 application with the Dependency Injection (DI) pattern.


This article explains Dependency Injection (DI) in MVC 3 applications.


This article explains the code from Basics of MVC3 Part - 1 and Part - 2, so please have a look at these articles before starting MVC 3 Dependency Injection (Part – 3).

The Dependency Injection (DI) Design Pattern

The Dependency Injection (DI) design pattern is based on separating component behavior from dependency resolution without object intervention.

This pattern is a particular implementation of Inversion of Control, where the consumer object receives its dependencies inside constructor properties or arguments.

The advantages of using the Dependency Injection pattern and Inversion of Control are the following:

  • Reduces class coupling
  • Increases code reuse
  • Improves code maintainability
  • Improves application testing

Implementing DI in the MVC3Demo project

Create a UnityControllerFactory Class

Create a UnityControllerFactory (custom class) for Unity. This class implements the IControllerFactory interface, extending the CreateController and ReleaseController methods to work with Unity.

The CreateController method creates an instance of the controllers available in the project.

Please download Unity Application Block 2.0 and install it. Then refer to Microsoft.Practices.Unity in the MVC3Demo project.

Step 1

Add a new folder named Factories and add a class UnityControllerFactory.cs.

namespace MVC3Demo.Factories
    public class UnityControllerFactory : IControllerFactory
        private IUnityContainer _container;
        private IControllerFactory _factory;

        public UnityControllerFactory(IUnityContainer container)
            : this(container, new DefaultControllerFactory()) { }

        public UnityControllerFactory(IUnityContainer container, IControllerFactory factory)
            _container = container;
            _factory = factory;

        public IController CreateController(System.Web.Routing.RequestContext requestContext, 
                                            string controllerName)
                return _container.Resolve<IController>(controllerName);
            catch (Exception)

                return _factory.CreateController(requestContext, controllerName);

        public System.Web.SessionState.SessionStateBehavior 
               System.Web.Routing.RequestContext requestContext, string controllerName)
            return System.Web.SessionState.SessionStateBehavior.Default;

        public void ReleaseController(IController controller)

Create Interface for ProductModel

Step 2

Right click the ProductModel class and click the Refactor->Extract Interface menu.

and click OK.

Refactory ProductController Class

Refer to the IProductModel interface instead of the ProductModel concrete class in the constructor of the ProductController class.

IProductModel model = null;
IList<ProductModel> AllProducts = null;

public ProductController(IProductModel _model)
    model = _model;
    AllProducts = model.GetAllProducts();

No Parameterless Constructor Error

Build and run the MVC3Demo application, it gives No Prameterless constructor defined for this object. The ProductController constructor has an argument IProductModel.

Configuring UnityContainer

Step 3

Configure the Interface and its concrete class using RegisterType and create an instance of the custom controller factory UnityControllerFactory and pass this into ControllerBuilder.

Please see the code below.

private void RegisterType()
    UnityContainer container = new UnityContainer();
    container.RegisterType<IProductModel, ProductModel>();
    container.RegisterType<IController, ProductController>("Product");

    UnityControllerFactory factory = new UnityControllerFactory(container);



Create this method in Global.asax.cs and call the RegisterType method at the end of the Application_Start() event.

Please refer to the attached code zip for detailed application code.


I hope this article helps to understand some of the basics of Dependency Injection (DI) in MVC 3 applications.




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead
India India
Artha is a Technical Lead in Windows Phone, WPF [MVVM, PRISM], ASP.NET [MVC 3.0 & 4.0], C#.NET, VB.NET and ASP.

Windows Phone

Microsoft Developer Network

Published Tools in Visual Studio Galleries

Published Source Codes in CodePlex

Microsoft Virtual Academy Profile

Published Articles in Code Project

Microsoft Certified Professional Developer (MCPD) Microsoft ASP.NET Developer 3.5 (070-564, 070-536, 070-562, 070 315)

You may also be interested in...

Comments and Discussions

SuggestionArticle Name Suggestion Pin
AspDotNetDev23-Mar-12 12:11
protectorAspDotNetDev23-Mar-12 12:11 
GeneralRe: Article Name Suggestion Pin
Arthanarieaswaran26-Mar-12 18:26
memberArthanarieaswaran26-Mar-12 18:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 26 Mar 2012
Article Copyright 2012 by Arthanarieaswaran
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid