Steps with Ninject:
- Download Ninject
- Reference the Ninject.dll library
- Next we have to stop making ASP.NET MVC call controller classes directly, and instead call the controllers through the Dependency Injection framework.
In order to do this, we do the following:
* Create a subclass of ASP.NET MVC's DefaultControllerFactory class, overriding the GetControllerInstance method. (To make this call existing controllers like normal, return new StandardKernel.Get(controllerType)
* Inside the Global.asax.cs file's Application_Start() method, set the new DefaultControllerFactory class's subclass as the current controller factory (i.e. ControllerBuilder.Current.SetControllerFactory(new MyNewClassName());
Here's my subclass:
public class NinjectControllerFactory : DefaultControllerFactory { private IKernel kernel = new StandardKernel(new SportsStoreServices()); protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) { if(controllerType == null) { return null; } return (IController) kernel.Get(controllerType); } private class SportsStoreServices : NinjectModule { public override void Load() { Bind() .To () .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString); Bind () .ToSelf().WithConstructorArgument("numberOfItems",int.Parse(ConfigurationManager.AppSettings["ProductsPerPage"])); } } }
And here's the relevant bits in my web.config:
<connectionstrings> <add connectionstring="Server=.\SQLEXPRESS;Database=SportsStore;Trusted_Connection=yes;" name="AppDb"> </add></connectionstrings> <appsettings> <add key="ProductsPerPage" value="5"> </add></appsettings>
Yeah, so that's pretty much it - now my controller takes a IProductsRepository, as well as a ItemsPerPage (an int wrapper) object which I inject. The cool thing I find with this is that now I can make my configuration in my web.config; so I can make changes to my application without writing a line of code :)
No comments:
Post a Comment