asp.net MVC example Project "Suteki.shop" Analysis of the IOC (control reversal)

Source: Internet
Author: User

In Suteki.shop, not using Microsoft's own Unity framework to achieve the IOC, but the use of the famous castle Windsor.

Because the Windsor is quoted, it is necessary to introduce it briefly. And I understand that this IOC container (Container) includes the following important concepts:

Container (Container): Windsor is a reverse control container. It is created on the basis of a microkernel that scans classes and tries to find which object references and object dependencies these classes use, and then supplies the dependency information to the class.

Component (Component): What we usually call the business logic unit and the corresponding function implementation, the component is a reusable unit of code. It should be implemented and exposed as a service. A component is a class that implements a service or interface.

Services (Service): That is, the corresponding component interface or n component by the business logic of the combination of business logic interface.

An interface is a specification of a service that creates an abstraction layer that you can easily replace with a service implementation.

Expansion Unit plug-in (facilities): provides (extensible) container to manage components.

We can use the component directly (as mentioned in the following section), or we can convert the component to the corresponding service interface.

Do you remember the service you mentioned in the last article? Plainly, it is a service. and suteki.shop do more "exaggerated", as long as the functional code with the nature of business logic can be considered as component or services, such as the previous articles mentioned in the Filter,modelbinder. Even the service component initialization auxiliary class (Windsorservicelocator) is also taken down.

To make it easier to understand, let's go to suteki.shop and see how it's done:

First, let's take a look at the entrance to the entire Suteki.shop project launch, which is also the starting point for Windsor IOC container initialization. This feature code is implemented in the Application_Start method in Global.asax (Suteki.shop\global.asax), and the following is the declaration of the method:

protected void Application_Start(object sender, EventArgs e)
{
RouteManager.RegisterRoutes(RouteTable.Routes);
InitializeWindsor();
}

The routemanager.registerroutes in the code is the binding to the route rule, and the content of the rule is hard-coded into Routemanager. There are a lot of information about route on the internet, there are many friends in the garden have written, here will not be explained.

The above method then runs Initializewindsor (), which is how the Windsor container is initialized:

/// <summary>
/// This web application uses the Castle Project's IoC container, Windsor see:
/// http://www.castleproject.org/container/index.html
/// </summary>
protected virtual void InitializeWindsor()
{
if (container == null)
{
// create a new Windsor Container
container = ContainerBuilder.Build ("Configuration\\Windsor.config");

WcfConfiguration.ConfigureContainer(container);

ServiceLocator.SetLocatorProvider(() => container.Resolve<IServiceLocator> ());
// set the controller factory to the Windsor controller factory (in MVC Contrib)
System.Web.Mvc.ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));
}
}

Note: The content in "Configuration\\windsor.config" is longer, mainly some XML configuration nodes. We can take the time to read it.

This method is the main content of today's lecture, the following is a description of the code.

The first is to determine whether the container (IWindsorContainer type) is empty, and if the container is empty, create and initialize the container. That is, calling the build method of the Containerbuilder (Suteki.shop\containerbuilder) class to load the default information from an external config file. We'll look at the implementation of the Build method here:

public static IWindsorContainer build (String Configpath)
{
var container = new WindsorContainer (new Xmlinterpreter (Configpath));

Register Handler Selectors
Container. Kernel.addhandlerselector (New Urlbasedcomponentselector (
typeof (Ibasecontrollerservice),
typeof (Iimagefileservice),
typeof (Iconnectionstringprovider)
));

Automatically register controllers
Container. Register (alltypes
. Of<controller> ()
. Fromassembly (assembly.getexecutingassembly ())
. Configure (c => c.lifestyle.transient.named (C.implementation.name.tolower ()));

Container. Register (
Component.for<iunitofworkmanager> (). Implementedby<linqtosqlunitofworkmanager> (). Lifestyle.transient,
Component.for<iformsauthentication> (). Implementedby<formsauthenticationwrapper> (),
Component.for<iservicelocator> (). Instance (new Windsorservicelocator (container)),
Component.for<authenticatefilter> (). Lifestyle.transient,
Component.for<unitofworkfilter> (). Lifestyle.transient,
Component.for<databinder> (). Lifestyle.transient,
Component.for<loadusingfilter> (). Lifestyle.transient,
Component.for<currentbasketbinder> (). Lifestyle.transient,
Component.for<productbinder> (). Lifestyle.transient,
Component.for<orderbinder> (). Lifestyle.transient,
Component.for<iordersearchservice> (). Implementedby<ordersearchservice> (). Lifestyle.transient,
Component.for<iemailbuilder> (). Implementedby<emailbuilder> (). Lifestyle.singleton
);

return container;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.