I just installed Ninject.MVC3 0.3. I am using ASP.NET MVC 3 beta.
I added this code to my Global.asax.cs file:
public static void RegisterServices(IKernel kernel) { kernel.Bind<IProductRepository>().To<SqlProductRepository>(); } public void SetupDependencyInjection() { IKernel kernel = new StandardKernel(); RegisterServices(kernel); DependencyResolver.SetResolver(new Ninject.Mvc3.NinjectServiceLocator(kernel)); }
And I added the call to SetupDependencyInjection() to the Application_Start() function so that it looks like this:
protected void Application_Start() { SetupDependencyInjection(); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
IProductRepository and SqlProductRepository are the classes that I created in my Mods folder, and I added a constructor dependency to my HomeController . Here is the code:
private IProductRepository products; public HomeController(IProductRepository productRepository) { products = productRepository; }
I added some breakpoints and launched the application and it works like a charm. Hope this helps.
gligoran
source share