Click here to Skip to main content
15,881,803 members
Articles / Web Development / CSS

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

Rate me:
Please Sign up or sign in to vote.
4.83/5 (4 votes)
26 Mar 2012CPOL2 min read 50.7K   1.1K   29   2
Explains basic information and steps for creating an MVC3 application with the Dependency Injection (DI) pattern.

Overview

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

Introduction

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.

C#
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)
        {
            try
            {
                return _container.Resolve<IController>(controllerName);
            }
            catch (Exception)
            {

                return _factory.CreateController(requestContext, controllerName);
            }
        }

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

        public void ReleaseController(IController controller)
        {
            _container.Teardown(controller);
        }
    }
}

Create Interface for ProductModel

Step 2

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

Image 1

and click OK.

Refactory ProductController Class

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

C#
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.

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

    UnityControllerFactory factory = new UnityControllerFactory(container);

    ControllerBuilder.Current.SetControllerFactory(factory);

}

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.

Conclusion

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

References

  1. http://msdn.microsoft.com/en-us/gg618491
  2. http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=9093
  3. http://msdn.microsoft.com/en-us/library/ff663144.aspx

License

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


Written By
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)

Comments and Discussions

 
SuggestionArticle Name Suggestion Pin
AspDotNetDev23-Mar-12 12:11
protectorAspDotNetDev23-Mar-12 12:11 
GeneralRe: Article Name Suggestion Pin
Arthanarieaswaran Shanmugaraj26-Mar-12 18:26
Arthanarieaswaran Shanmugaraj26-Mar-12 18:26 
Thanks for your suggestion, I have changed the article title.

Artha

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.