开发者

What is the right way to implement Unity in ASP.Net MVC?

Ok, there are so many results in Google when you search for this topic; NerdDinner,CodeClimber,CodeProject etc.. but they all seems to be not working as expected! Either they give error during build or runtime! Wha开发者_JAVA百科t is the right way to implement Unity 2.0 in ASP.Net MVC 2? I am simply not able to get this working!

Your help and thoughts are highly appreciated. Thanks!


Try writing a simple controller factory using Unity which is capable of resolving the controller instances:

public class UnityControllerFactory : DefaultControllerFactory 
{
    private readonly IUnityContainer _container; 
    public UnityControllerFactory(IUnityContainer container) 
    { 
        _container = container; 
    }

    protected override IController GetControllerInstance(
        RequestContext requestContext, 
        Type controllerType
    ) 
    { 
        if (controllerType == null)
        {
            throw new ArgumentNullException("controllerType");
        }
        if (!typeof(IController).IsAssignableFrom(controllerType))
        {
            throw new ArgumentException("Type requested is not a controller", "controllerType");
        }
        return _container.Resolve(controllerType) as IController; 
    }
}

and then hook it up in the Application_Start event in Global.asax:

protected void Application_Start()
{
    ...
    var container = new UnityContainer();
    // TODO: Configure the container here with your controllers

    var factory = new UnityControllerFactory(container);
    ControllerBuilder.Current.SetControllerFactory(factory);
}


This is my version of the UnityControllerFactory. It uses reflection to get the controllers from the calling assembly and register them in the container.

using System;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace WebProveidorsMVC.DI.ControllerFactories
{
    public class UnityControllerFactory : DefaultControllerFactory
    {
        private readonly IUnityContainer _container;

        public UnityControllerFactory()
        {
            _container=new UnityContainer();

            ((UnityConfigurationSection) ConfigurationManager.GetSection("unity")).Configure(_container);

            var controllerTypes = from t in Assembly.GetCallingAssembly().GetTypes()
                                  where typeof(IController).IsAssignableFrom(t)
                                  select t;

            foreach (var t in controllerTypes)
                _container.RegisterType(t, t.FullName);
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            if (controllerType != null)
                return (IController)_container.Resolve(controllerType);
            return null;
        }

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

Then in my ApplicationStart method I register it like this:

ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜