Integration of dependency injection container autofac with MVC

Source: Internet
Author: User

Autofac is a dependency injection (Di, dependency injection) Container applied to the. NET platform. It is close to and fits with the C # language. As application systems become increasingly large and complex, the use of autofac containers to manage the relationships between components can be "flat" and the complex class dependencies, with good adaptability and convenience.
In this blog post, we will apply autofac to establish a traditional ASP with dependency injection.. NET page and the service/middle layer to establish the link between "rendering" and "control.

So how can we implant dependency injection into ASP. NET?
ASP. NET page lifecycle is ASP. net worker process control, which basically cut off the idea that we want to implant the service/middle layer through constructor injection in this process, we must complete the implantation process before entering the page lifecycle. Based on this consideration, we can use the "Property injection" method based on the ihttpmodule. The implementation details are as follows:
1. reference the corresponding DLL

Download autofac. Check whether the version you downloaded is the same as the. NET version of your project.

Add the following reference to an ASP. NET application:
Autofac. dll
Autofac. Integration. Web. dll
Autofac. Integration. Web. MVC. dll

2. Set web. config
Next, modify the Web. config file and create an HTTP module. Because the control reversal container dynamically initializes the object, the purpose of this module is to release the created object as appropriate.
<Configuration>
<System. Web>
<Httpmodules>
<! -- Set under IIS6 -->

<Httpmodules>
<Add name = "containerdisposal" type = "autofac. Integration. Web. containerdisposalmodule, autofac. Integration. Web"/>
</Httpmodules>
</System. Web>
<System. webserver>

 

<! -- Iis7 settings -->

<System. webserver>
<Modules>
<Add name = "containerdisposal" type = "autofac. Integration. Web. containerdisposalmodule, autofac. Integration. Web"/>
</Modules>
<Validation validateintegratedmodeconfiguration = "false"/>
</System. webserver>

Note:
Containerdisposalmodule. When the page request ends, autofac releases the previously created component/service.
Propertyinjectionmodule, which injects dependencies into the page before the page lifecycle.

3. modify the contents of the Global. asax page and enable the control reversal container in the application_start event so that. Net MVC can be integrated with autofac.

Because some namespaces are required to use autofac, the following references must be added:

 

Using system. reflection;
Using autofac;
Using autofac. Integration. Web;
Using autofac. Integration. Web. MVC;

 

Set the icontainerprovideraccessor interface in the mvcapplication class, and add the following code at the class level:

Static icontainerprovider _ containerprovider;

Public icontainerprovider containerprovider
{
Get {return _ containerprovider ;}
}

 

Add the following code at the beginning of the application_start method:

VaR builder = new containerbuilder ();

Setupresolverules (builder );

Builder. registercontrollers (assembly. getexecutingassembly ());

_ Containerprovider = new containerprovider (builder. Build ());

Controllerbuilder. Current. setcontrollerfactory (New autofaccontrollerfactory (_ containerprovider ));

 

4. Get service/intermediate layer for page properties

Put a public attribute in the background of the. ASPX page to receive the service/intermediate layer.

// Receives the service/intermediate layer attributes
Public iempmng somedependency {Get; set ;}
Protected void page_load (Object sender, eventargs E)
{
Lblemp. Text = This. somedependency. selectoneemp ();
}

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.