Upgrading the default ASP.NET MVC project with IoC and the Unity Controller Factory


 

The first part is easy, just grab the Unity DLLs and reference them in your MVC project. At this point, you’re now free to use Unity to inject whatever you feel like. Rigging up the controller factory so that it will use Unity to resolve controllers is equally easy because the mvc contrib codeplex site has already created a class called UnityControllerFactory that does just this. Once you’ve added references to Unity and Mvc.Contrib.Unity from your MVC project, you can change your global.asax.cs file to look something like this:

if (_container == null)
            {
                _container = new UnityContainer();             
                ControllerBuilder.Current.SetControllerFactory(typeof(UnityControllerFactory));                               
               
                _container.RegisterType(typeof(HomeController), typeof(HomeController));
                _container.RegisterInstance(typeof(AccountController), new AccountController());               
            }

 

Via Upgrading the default ASP.NET MVC project with IoC and the Unity Controller Factory [The .NET Addict’s Blog]

Advertisements

One thought on “Upgrading the default ASP.NET MVC project with IoC and the Unity Controller Factory

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s