Usually use inject more, today contact to Castle Windsor. This article is to experience the application process in ASP.
Visual Studio 2012 creates an ASP. NET MVC 4 Web site.
Install Castle Windsor through NuGet.
Create a folder called IOC under the current project.
In ASP. Defaultcontrollerfactory creates a controller instance for us each time we request it, we need to customize a class derived from defaultcontrollerfactory and let Castle Windsor help us generate a controller instance.
Using System.Web;
Using SYSTEM.WEB.MVC;
Using Castle.microkernel;
Namespace Mycastlewindsor.ioc
{
public class Windsorcontrollerfactory:defaultcontrollerfactory
{
Private ReadOnly Ikernel _kernel;
Public windsorcontrollerfactory (Ikernel kernel)
{
_kernel = kernel;
}
protected override IController GetControllerInstance (System.Web.Routing.RequestContext requestcontext, System.Type Controllertype)
{
if (Controllertype = = null)
{
Throw New HttpException (404,string. Format ("current request for {0} does not exist", requestContext.HttpContext.Request.Path));
}
Return (IController) _kernel. Resolve (Controllertype);
}
public override void Releasecontroller (IController Controller)
{
_kernel. Releasecomponent (Controller);
Base. Releasecontroller (Controller);
}
}
}
You now need to generate the Castle Windsor instance in the global, assign the constructor to the custom controllerfactory, and destroy the Castle Windsor instance at the end of the application.
public class MvcApplication:System.Web.HttpApplication
{
Private IWindsorContainer _container;
protected void Application_Start ()
{
Arearegistration.registerallareas ();
Webapiconfig.register (globalconfiguration.configuration);
Filterconfig.registerglobalfilters (globalfilters.filters);
Routeconfig.registerroutes (routetable.routes);
Bundleconfig.registerbundles (Bundletable.bundles);
Initializing an IOC container
_container = new WindsorContainer (). Install (Fromassembly.this ());
Completing the registration in the Iwindsorinstaller interface
ControllerBuilder.Current.SetControllerFactory (New WindsorControllerFactory (_container. Kernel));
}
protected void Application_End ()
{
_container. Dispose ();
}
}
Now, you need to tell Castle Windsor under what conditions and how to register the dependency. The Castle Windsor provides a Iwindsorinstaller interface. Create a class that implements the interface under the IOC folder.
Using SYSTEM.WEB.MVC;
Using Castle.MicroKernel.Registration;
Using Mycastlewindsor.controllers;
Namespace Mycastlewindsor.ioc
{
public class Controllerinstaller:iwindsorinstaller
{
public void Install (Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
Container. Register (classes.fromthisassembly ()//Where to find the interface or class
. Basedon<icontroller> ()//Implement IController interface
. If (component.isinsamenamespaceas
. If (t = = T.name.endswith ("Controller"))//End with "Controller"
. Configure (c = c.lifestyleperwebrequest ()));//Create a controller instance per request
}
}
}
In fact, in the global file, when the Install (Fromassembly.this ()) method is run, the Iwindsorinstaller install interface method is executed.
For example
Suppose you have an interface:
public interface Icontactmanager
{}
The implementation class for the interface:
public class Contactmanager:icontactmanager
{}
In a controller, the dependency is injected through the constructor, which we want to write:
Private Icontactmanager Contactmanager;
Public HomeController (Icontactmanager Contactmanager)
{
This.contactmanager = Contactmanager;
}
Now you need to register Icontactmanager and Contactmanager. Dependencies can be registered by reflection.
Using SYSTEM.WEB.MVC;
Using Castle.MicroKernel.Registration;
Using Mycastlewindsor.controllers;
Namespace Mycastlewindsor.ioc
{
public class Controllerinstaller:iwindsorinstaller
{
public void Install (Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
Container. Register (classes.fromthisassembly ()
. Where (t = t.name.endswith ("Manager"))
. Withservicedefaultinterfaces ()
. Configure (c = c.lifestyletransient ()));
}
}
}
The application of Castle Windsor is as follows:
→ Inherit the controllerfactory of ASP. Castle Windsor through the constructor function to parse out the IController type controller.
→ Register dependencies by Reflection by implementing the Iwindsorinstaller interface of the Castle Windsor.
→ Initialize the instance of Castle Windsor in the global file and register the custom controllerfactory.
Using Castle Windsor in ASP.