Click here to Skip to main content
15,887,776 members
Articles / MVC / MVC3

Using the ControllerActivator in MVC 3

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
18 Oct 2010CPOL1 min read 18.1K   2   2
How you can use the ControllerActivator in order to activate controllers using your own behavior

In the previous post, I showed how to use the DependencyResolver in order to bring Dependency Injection behavior to a MVC 3 application. In this post, I’ll show you how you can use the ControllerActivator in order to activate controllers using your own behavior.

The IControllerActivator

In MVC 3 beta, a new interface was introduced – the IControllerActivator. This interface is an injection point in order to create our own behavior in order to activate controllers. That interface is discoverable using the dependency resolver. If you don’t implement the interface and register it in the dependency resolver, a default behavior which is to use the Activator.CreateInstance will activate controllers.

The interface includes only one method which is the Create method. This method has the same signature like the DefaultControllerFactory’s GetControllerInstance method.

Here is a simple way to implement the IControllerActivator using the previous post’s DependencyResolver:

C#
public class UnityControllerActivator : IControllerActivator
{
  #region IControllerActivator Members
 
  public IController Create(RequestContext requestContext, Type controllerType)
  {
    return DependencyResolver.Current.GetService(controllerType) as IController;      
  }
 
  #endregion
}

As you can see, I use the current DependencyResolver to get the Unity behavior. Of course, you can use other behaviors to implement the controller activation. Here is how the InitContainer method from the previous post will look like now:

C#
private static IUnityContainer InitContainer()    
{
  var container = new UnityContainer();        
 
  // Register the relevant types for the         
  // container here through classes or configuration 

  container.RegisterType<IControllerActivator, UnityControllerActivator>();                
  container.RegisterType<IMessageService, MessageService>();

  return container;
}

I just added the registration of the IControllerActivator to the container. Now if you will debug the application and set a breakpoint in the Create method, you will see that all controller activations will pass through the IControllerActivator. When running the application, you will get the same result as in the previous post:

HomeController Result

Summary

In this post, I showed how to hook up your own controller activation behavior in MVC 3. This can be achieved by using the new IControllerActivator interface. The example I showed is using the Unity IoC and could be implemented in other ways as well.

License

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


Written By
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

Comments and Discussions

 
GeneralMy vote of 1 Pin
Christian Del Bianco18-Sep-14 8:31
Christian Del Bianco18-Sep-14 8:31 
Generalwhat is the point of this ? Pin
zebula817-Jun-11 10:46
zebula817-Jun-11 10:46 

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.