-
Notifications
You must be signed in to change notification settings - Fork 0
Using Bailey.Web.Mvc
Using Bailey.Web.Mvc is fairly straight-forward. Let's see how:
-
Open your site's
Global.asax.csfile. -
Add the
Castle.Windsor.IContainerAccessorinterface to the root class. In the current (MVC5) template, this is namedMvcApplication. -
Implement the interface. I typically implement it like so:
private static IWindsorContainer __container; public IWindsorContainer Container { get { return __container; } } -
Add a
BootstrapContainer()method to theMvcApplicationclass. This lets us encapsulate the DI methods into one location in our class:private void BootstrapContainer() { } -
In
BootstrapContainer(), add the relevant code to instantiate the DI container:private void BootstrapContainer() { __container = new WindsorContainer(); }If you're using installers (and you should be!), tweak the line above slightly:
private void BootstrapContainer() { __container = new WindsorContainer().Install(FromAssembly.This()); } -
Assign the dependency resolver and controller factory:
private void BootstrapContainer() { __container = new WindsorContainer().Install(FromAssembly.This()); System.Web.Mvc.DependencyResolver.SetResolver(new WindsorDependencyResolver(__container)); ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(__container.Kernel)); } -
Lastly, call our
BootstrapContainer()method inApplication_Start():protected void Application_Start() { // ... BootstrapContainer(); // ... }
At this point, your application is setup to use the Bailey.Web.Mvc components. Good job!