0

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! What 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!

1
  • Final solution: I made a mix of the code from below answers. I used @Carles ReleaseController method in @Darin's code. I also tweaked the UnityControllerFactoty to my needs. Commented Feb 22, 2011 at 9:25

2 Answers 2

3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I have tried that as well! I get ArgumentNullExecption at return _container.Resolve(controllerType) as IController; But the DI seems to working fine! Why is that?
@DarinDimitrov Do you know if its possible to include the edmx file - using database first approach - as a container.registertype in order to use it in Unity IOC??. thank you
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.