Online big guy's original address:
Https://www.cnblogs.com/liupeng/p/4806184.html
78227628?locationnum=2&fps=1
A. Files that need to be referenced, install using NuGet
The first one for AUTOFAC
The second MVC extension for AUTOFAC
II. Structure of the project
The table name used for the test is syssample, and 2-6 is its BLL, DAL, IBLL, Idal, Models project.
Third, the relationship
Iv. Use of
1. Constructor injection
(1) Automatic injection. The BLL and Controller are all injected by AUTOFAC Unified
A. Create a unified inherited interface, idependency
B. References. The BLL, IBLL, DAL, idal four projects all refer to the Apps.autofacbase project where Idependency is located.
C. Inheritance: Both ISYSSAMPLEBLL and isyssamplerepository inherit this interface
D. Constructors. SYSSAMPLEBLL and Syssamplecontroller constructors are modified.
A reference to the E.web project. The web needs to add a reference to the BLL, IBLL, DAL, idal four items. Otherwise, the injected information for unreferenced items is missing when auto-injection.
Modification of the F.application_start () method
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Reflection;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Optimization;usingSystem.Web.Routing;usingapps.autofacbase;usingAPPS.BLL;usingApps.dal;usingAPPS.IBLL;usingApps.idal;usingAUTOFAC;usingAUTOFAC.INTEGRATION.MVC;namespaceapps.web{ Public classMvcApplication:System.Web.HttpApplication {protected voidApplication_Start () {arearegistration.registerallareas (); Routeconfig.registerroutes (routetable.routes); //Enable JSCSS compressionBundletable.enableoptimizations =true; Bundleconfig.registerbundles (Bundletable.bundles); #regionAutomatic injection//Create a container instance of the AUTOFAC management registration class varBuilder =NewContainerbuilder (); Assembly[] Assemblies= Directory.GetFiles (AppDomain.CurrentDomain.RelativeSearchPath,"*.dll"). Select (Assembly.LoadFrom). ToArray (); //register all types that implement the Idependency interfaceType BaseType =typeof(idependency); Builder. Registerassemblytypes (Assemblies). Where (Type= Basetype.isassignablefrom (type) &&!type. IsAbstract). Asself (). Asimplementedinterfaces (). Propertiesautowired (). Instanceperlifetimescope (); //Registering MVC typesBuilder. Registercontrollers (Assemblies). Propertiesautowired (); Builder. Registerfilterprovider (); varcontainer =Builder. Build (); Dependencyresolver.setresolver (NewAutofacdependencyresolver (container)); #endregion } }}
View Code
(2) Manual injection
There is no need to create idependency interfaces.
You need to list all the classes, interfaces, and callers that need to be injected.
Modification of the Application_Start () method
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Reflection;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Optimization;usingSystem.Web.Routing;usingapps.autofacbase;usingAPPS.BLL;usingApps.dal;usingAPPS.IBLL;usingApps.idal;usingApps.Web.Controllers;usingAUTOFAC;usingAUTOFAC.INTEGRATION.MVC;namespaceapps.web{ Public classMvcApplication:System.Web.HttpApplication {protected voidApplication_Start () {arearegistration.registerallareas (); Routeconfig.registerroutes (routetable.routes); //Enable JSCSS compressionBundletable.enableoptimizations =true; Bundleconfig.registerbundles (Bundletable.bundles); #regionManual injection//Create a container instance of the AUTOFAC management registration class varBuilder =NewContainerbuilder (); /*IBLL that require dependency injection*/ //relationship of classes and interfacesBuilder. Registertype<syssamplerepository> (). As<isyssamplerepository>(); Builder. Registertype<SysSampleBLL> (). As<isyssamplebll>(); //injected by the caller. Builder. Registertype<syssamplebll> (). Instanceperdependency ();//The DAL layer is called in the SYSSAMPLEBLL//(not recommended) registration of single controller controllersBuilder. Registertype<syssamplecontroller> (). Instanceperdependency ();//Syssamplecontroller called the BLL layer//(recommended) Use the Registercontrollers extension method provided by AUTOFAC to register all controllers in the assembly once//Builder. Registercontrollers (assembly.getexecutingassembly ()). Propertiesautowired (); //generate a specific instance varcontainer =Builder. Build (); //The following is an extension that uses MVC to change the way in which MVC is injected.Dependencyresolver.setresolver (NewAutofacdependencyresolver (container)); #endregion } }}
View Code
2. Attribute Injection
(1) Automatic injection.
A. with 1. (1) The automatic injection configuration of the constructor injection is mostly consistent,
B. Where modifications are required. When injecting callers, you need to add them to the back. Propertiesautowired ();
Refers to an object that modifies the constructor to a property.
(2) Manual injection
A. With 2. (1) In the same vein, most of the
B. Where modifications are required. Baidu said when injecting the caller, you need to add at the back. Propertiesautowired ();
However, the call still fails when it is actually called. No further study was carried out.
Five, remarks
For the convenience of writing and viewing, the current use is. Automatic injection + attribute injection.
The use of AUTOFAC in MVC