1
Project
Introduce AUTOFAC and AUTOFAC MVC two assemblies into the project first
Then we create a AutoFacConfig.cs class under the App_start folder in the MVC (UI layer)
Using system;using system.collections.generic;using system.linq;using system.web;namespace FB. Cms. mvcsite.app_start{using AUTOFAC; Using AUTOFAC.INTEGRATION.MVC; Using System.Reflection; Using SYSTEM.WEB.MVC; <summary>////This class is a class of my own definition, mainly used to initialize AUTOFAC container related data///</summary> public class Autofacconfig { public static void Register () {//Initialize AUTOFAC related features/* 1.0 tell AUTOFAC to initialize data warehousing layer F B.cms. An object instance of all classes in the Repository.dll. These object instances are stored in the AUTOFAC container in the form of their interfaces 2.0 tells AUTOFAC to initialize the object instance of all classes in the business logic layer FB.CMS.Services.dll. These object instances are stored in the AUTOFAC container in the form of their interfaces 3.0 replace the MVC default controller factory with the AUTOFAC Factory *///First step: Construct a AUTOFAC builder capacity Containerbuilder builder = new Autofac.containerbuilder (); Step two: Tell the AUTOFAC controller factory where the controller class is created to find (the default controller factory is to scan all the assemblies in the bin directory)//2.1 load the FB.CMS.MvcSite.dll assembly from the currently running Bin directory Assembly Controllerass = Assembly.Load ("FB. Cms. Mvcsite "); 2.2 Tell AutThe OFAC controller factory, the creation of the controller is looked up from controllerass (note: The Registercontrollers () method is a variable parameter, if the creation of your controller class needs to be found in multiple assemblies, then we will use the assembly Controllerbss=assembly.load ("Required assembly name") loads the required assembly and then controllerass the array into the Registercontrollers () method) Builder . Registercontrollers (Controllerass); Step three: Tell the AUTOFAC container, create an object instance of the specified class in the project, store it as an interface (in fact, create an object instance of all the classes in both the data warehousing layer and the business logic layer, and then save it in the form of its interface to the AUTOFAC container memory. Of course, if there is a need, you can create an object instance of all the classes of the other assembly, which only needs to be specified by US)//3.1 load the data warehousing layer FB.CMS.Repository this assembly. Assembly Repositoryass = Assembly.Load ("FB. Cms. Repository "); 3.2 Reflection scans all classes in this FB.CMS.Repository.dll assembly to get a collection of all classes in this assembly. type[] Rtypes = Repositoryass.gettypes (); 3.3 tells the AUTOFAC container to create an object instance builder that rtypes all classes in this collection. Registertypes (rtypes). Asimplementedinterfaces (); Indicates that the created Rtypes object instance of all classes in this collection is saved as its interface//3.4 loading the business logic layer FB.CMS.Services the assembly. Assembly Servicesass = Assembly.Load ("FB. Cms. Services "); 3.5 Reflection scans all the classes in this FB.CMS.Services.dll assembly,Get a collection of all the classes in this assembly. type[] Stypes = Servicesass.gettypes (); 3.6 tells the AUTOFAC container to create an object instance builder that stypes all classes in this collection. Registertypes (stypes). Asimplementedinterfaces (); Indicates that the created Stypes object instance of all classes in this collection is saved as its interface//Fourth step: Create a real AUTOFAC work container var container = Builder. Build (); We have created an object instance of all classes of the specified assembly and saved it in the AUTOFAC container memory as its interface. So how are we going to get it? Gets an object instance of its implementation class from within the AUTOFAC container based on the specified interface//assuming I want to get the object instance of the implementation class of isysfunctionservices this interface, how to take it? var obj = container. Resolve<isysfunctionservices> (); It can be taken in this form only when there is a special need. In general, it is not necessary to take this, because AUTOFAC will automatically work (that is, will automatically go to the class parameter of the constructor to find the same parameter type as the key in the container, and inject the object into the class, in fact, the object is assigned to the parameters of the constructor)//Fifth step: Replace the controller factory in the current container The default controller factory for MVC. (ie: do not MVC default controller factory, replace with the controller factory in the AUTOFAC container) is used here to give the AUTOFAC work container to the MVC bottom (need using SYSTEM.WEB.MVC;) Dependencyresolver.setresolver (new Autofacdependencyresolver (container)); We know that the controller is created to call the MVC default controller factory, the default controller factory is the parameterless constructor that calls the Controller class//But if we want to use the AUTOFAC automatic factory, we will pass the objectThe constructor is injected into the class, then this constructor needs to take parameters//If we delete the controller's parameterless constructor, preserve the constructor with parameters, MVC default controller factory to create the control when//will call the parameterless constructor, but this time found no The parameterless constructor then reports the "no parameterless constructor for this object" error//Now that we have a bug, if we keep the parameterless constructor, is it possible to declare a constructor with parameters? The answer is yes, but when the controller is created, the MVC default controller factory calls the parameterless constructor, which does not call the parameter constructor//This time, we can only replace AUTOFAC its controller factory to call the MVC default controller factory (Controller by AUTOFAC's Controller factory to create)//and AUTOFAC controller factory only scans the constructor with parameters when creating the control, and injects the object into the constructor with parameters//autofacdependencyresolver this controller factory is inherited The Idependencyresolver interface, and the Idependencyresolver interface is the MVC thing//MVC the default controller factory name is: Defaultcontrollerfactory//With Body reference: Http://www.cnblogs.com/artech/archive/2012/04/01/controller-activation-032.html}}}
We then call this class in the Application_Start () method in the Global.asax file.
Using FB. Cms. Mvcsite.app_start;using system;using system.collections.generic;using system.linq;using System.Web;using System.web.http;using system.web.mvc;using system.web.optimization;using system.web.routing;namespace FB. Cms. mvcsite{//Note: For instructions on enabling IIS6 or IIS7 Classic mode,//please visit http://go.microsoft.com/? linkid=9394801 public class MvcApplication:System.Web.HttpApplication {protected void Application_Start () {Arearegistration.registerallareas (); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (Bundletable.bundles); First: Initialize the AUTOFAC function when the site is started/* 1.0 tells AUTOFAC to initialize an object instance of all classes in the data warehousing layer FB.CMS.Repository.dll. These object instances are stored in the AUTOFAC container in the form of their interfaces 2.0 tells AUTOFAC to initialize the object instance of all classes in the business logic layer FB.CMS.Services.dll. These object instances are stored in the AUTOFAC container in the form of their interfaces And then call here next this class Autofacconfig.register (); } }}
Using the Home controller
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;namespace Fb. Cms. mvcsite.controllers{ using FB. Cms. iservices; public class Homecontroller:controller { isysfunctionservices dal; Public HomeController (isysfunctionservices dal)//dependency constructor for object injection { this.dal = dal;// Initializes the HomeController controller class's Dal property in the constructor (the type of this Dal property is isysfunctionservices) } public ActionResult Index () { var a = dal. Querywhere (r = r.fid > 20). ToList (); Query return View ();}}}
AUTOFAC (Control inversion IOC vs. Dependency injection di)