After creating an empty MVC 4 project, the first thing I realised was there were some framework changes in comparison to MVC 3. Most obvious changes happen in Global.asax.cs
, which moves all the configuration setting registers into separate class files in the folder App_Start
.
This actually prevents me from using Ninject.MVC3
like I used to do in MVC 3. Even more unfortunately is that Ninject package for MVC 4 is not out yet, that why this alternative solution is used.
NOTE: By the time I am updating this blog, Ninject for MVC 4 has been available for a white, and I am sure there are cleaner and better ways to implement dependency injection. But carry on reading if you are still interested in the though process, and have fun!
Add ‘Ninject.MVC3’ package
You will see NinjectWebCommon.cs
file is generated inside ‘App_Start’ folder, which was meant to be part of package adding process.
Modify ‘Global.asax.cs’
- Inherit
MvcApplication
fromNinjectHttpApplication
instead ofHttpApplication
- Implement
OnApplicationStarted
method inherited fromNinjectHttpApplication
, and move all registers inApplication_Start
this method to support the normal usage of those functions - Implement
CreateKernel
method and register your own services
namespace Ninject.MVC4
{
// TODO: Remove this auto-generated class declaration
//public class MvcApplication : System.Web.HttpApplication
//{
// protected void Application_Start()
// {
// AreaRegistration.RegisterAllAreas();
//
// WebApiConfig.Register(GlobalConfiguration.Configuration);
// FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
// RouteConfig.RegisterRoutes(RouteTable.Routes);
// BundleConfig.RegisterBundles(BundleTable.Bundles);
// AuthConfig.RegisterAuth();
// }
//}
public class MvcApplication : NinjectHttpApplication
{
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IFlorentinesService>().ToDefaultChannel();
}
}
}
Modify controller
Introduce service into controller constructor before consumption.
namespace MVC4.Controllers
{
public class HomeController : Controller
{
private ITestService service;
public HomeController(ITestService service)
{
this. service = service;
}
}
}
Delete ‘NinjectWebCommon.cs’
By specifying client endpoint, the service injection is done.