Using Ninject in MVC 3

Previously I have chatted about how to use Structured Map and Unity for Dependency Injections. Here is the link

https://kevww.wordpress.com/tag/dependency-injection/

Today I am going to discuss how to use Ninject for your DI container.

First of all, let’s add Ninject and Ninject.MVC3 to our web application using Nuget. Here is the command you can use to install them in Package Manager Console in VS2010.

PM> Install-Package Ninject

PM> Install-Package Ninject.MVC3 -Version 2.2.2.0

Then let’s get started. And modify the Global.asax to use NinjectHttpApplication instead of HttpApplication. It overrides CreateKernel to create a kernel and load all the modules.

public class MvcApplication : NinjectHttpApplication
{
   public static void RegisterGlobalFilters(GlobalFilterCollection filters)
   {
       filters.Add(new HandleErrorAttribute());
   }
 
   public static void RegisterRoutes(RouteCollection routes)
   {
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
       routes.MapRoute(
           "Default", // Route name
           "{controller}/{action}/{id}", // URL with parameters
           new
           {
               controller = "Home",
               action = "Index",
               id = UrlParameter.Optional
           });
   }
 
   protected override IKernel CreateKernel()
   {
       var kernel = new StandardKernel();
       kernel.Load(Assembly.GetExecutingAssembly());
       return kernel;
   }
 
   protected override void OnApplicationStarted()
   {
       base.OnApplicationStarted();
 
       AreaRegistration.RegisterAllAreas();
       RegisterGlobalFilters(GlobalFilters.Filters);
       RegisterRoutes(RouteTable.Routes);
   }
}

The only thing that has to be done is to add dependencies for controller constructor and configure the Ninject bindings for its dependencies. The controller itself will be found by Ninject even without adding a binding.

public class HomeController : Controller
{
    private readonly IWelcomeMessageService welcomeMessageService;
 
    public HomeController(IWelcomeMessageService welcomeMessageService)
    {
        this.welcomeMessageService = welcomeMessageService;
    }
 
    public void Index()
    {
        ViewModel.Message = this.welcomeMessageService.TodaysWelcomeMessage;
        return View();
    }
}
 
public class WelcomeMessageServiceModule : NinjectModule
{
    public override void Load()
    {
         this.Bind().To();
    }
}

Ninject provides contextual bindings. It allows you to register more than one binding for a type. There are 3 ways.

1. add condition to the bindings.

interface ICondition {
  bool Matches(T obj);
}

Bind().To();
Bind().To().Only(When.Context.Service.Name.StartsWith("Foo"));

2. Create Attribute

public class RangeAttribute : Attribute {}

public class Swordsman {
  [Inject] public IWeapon Weapon { get; set; }
}

public class Ninja {
  [Inject] public IWeapon MeleeWeapon { get; set; }
  [Inject, Range] public IWeapon RangeWeapon { get; set; }
}

3. Create Tags

public class Ninja {
  [Inject] public IWeapon MeleeWeapon { get; set; }
  [Inject, Tag("range")] public IWeapon RangeWeapon { get; set; }
}

Bind().To();
Bind().To().Only(When.Context.Tag == "range");

Here are some references you can read more about Ninject.

http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/ ( This blog post shows you how to use Ninject for Attribute bindings.)

http://bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html

http://ninject.codeplex.com/wikipage?title=User%20Guide&referringTitle=Home (Ninject Home)

Advertisements

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