[ASP.NET MVC ] Controller Factory and Ninject

As an example of a custom controller factory, let’s create a DI (Dependency Injection) controller factory using Ninject.

1. Add Ninject to the MVC Project

  • Tools -> Library Package Manager -> Add Library Package Reference
  • Select “Ninject” on the “Online” tab

“Ninject” will be installed.

2. Creating a Custom Controller Factory

Let’s create “NinjectControllerFactory” that derives from “System.Web.Mvc.DefaultControllerFactory“.

  • Override the “GetControllerInstance()” method to retrieve an controller instance.
public class NinjectControllerFactory : DefaultControllerFactory
{
  private IKernel ninjectKernel;
  public NinjectControllerFactory()
  {
    ninjectKernel = new StandardKernel();
    AddBindings();
  }

  protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
  {
    return (controllerType == null) ? null : (IController)ninjectKernel.Get(controllerType);
  }

  private void AddBindings()
  {
    // put additional bindings here
    // ninjectKernel.Bind<...>().To<...>();
  }
}

3. Registering a Custom Controller Factory

In the “Application_Start()” method in the “Global.asax”

  • Get the “ControllerBuilder” object using the “ControllerBuilder.Current” property
  • Call the “SetControllerFactory()” method
protected void Application_Start()
{
  ...
  ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}

4. Modifying the Controller Class

Whenever the routing system figures out what kind (type) of Controller is required, it calls the “GetControllerInstance()” method of the custome factory.

Ninject knows how to create an instance.

Suppose your controll class requires a parameter (such as Data Access object). I think, you know why we need to use DI(Dependency Injection).

public class ProductController : Controller
{
  private IProductAccessor accessor;
  public ProductController(IProductAccessor productAccessor)
  {
    accessor = productAccessor;
  }
}

5. DI Binding

Once you have a concreate class for the interface, you need to bind them using “ninjectKernel.Bind”

Check the “AddBindings()” method in the custom factory.

private void AddBindings()
{
  // put additional bindings here
  ninjectKernel.Bind<IProductAccessor>().To<DBProductAccessor>();
}

Now whenever the “ProductController” controller is created, the instance of “DBProductAccessor” is created and passed to the constructor of the controller class.

 

3 thoughts on “[ASP.NET MVC ] Controller Factory and Ninject

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s