Click here to Skip to main content
12,633,390 members (31,999 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads

Stats

33.9K views
1.1K downloads
29 bookmarked
Posted

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

, 26 Mar 2012 CPOL
Explains basic information and steps for creating an MVC3 application with the Dependency Injection (DI) pattern.
MVC3Demo
Library
Microsoft.Practices.Unity.dll
Local.testsettings
MVC3Demo
App_Code
Content
themes
base
images
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
Controllers
Factories
Global.asax
Models
MVC3Demo.csproj.user
Properties
Scripts
Views
Home
Product
Shared
MVC3Demo.Test
Properties
MVC3Demo.vsmdi
TraceAndTestImpact.testsettings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.Unity;
using System.Web.Mvc;

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);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Arthanarieaswaran
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...

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