First, preface
AUTOFAC is one of the most popular IOC frameworks in the. NET field, and Microsoft's Orchad open source program uses the AUTOFAC of Autofac,nopcommerce open source programs.
Orchad and Nopcommerce in the AUTOFAC when the package, read the source of all know AUTAFAC use simple, powerful.
Recommended download Orchad and Nopcommerce Learn the source: attached
http://www.orchardproject.net/
http://www.nopcommerce.com/
In contrast to the other IOC:
Unity: The Microsoft Patterns&practicest Team developed an IOC dependency injection framework that supports AOP crosscutting concerns.
MEF(Managed Extensibility Framework): a framework for extending. NET applications that can develop plug-in systems.
spring.net: Dependency Injection, aspect-oriented programming (AOP), data access abstraction, and ASP.
postsharp: Implement static AOP crosscutting concerns, simple and powerful, without any changes to the method of target interception.
AUTOFAC: The most popular Dependency injection and IOC framework, lightweight and high-performance, with virtually no intrusion into project code.
The following describes the use of AUTOFAC
Second, the use of AUTOFAC
Create a new MVC project that installs AUTOFAC with NuGet and requires the installation of AUTOFAC and Autofac ASP. MVC5 intergration
After the installation is complete, the reference is more Autofac.dll and Autofac.Intergration.MVC, if you are using AUTOFAC in Webapi, you need to install AUTOFAC ASP. Web API2.2 Intergration Can.
Create a new person entity class
public class person {public int Id {get; set;} public string Name {get; set;} public int Age {get; set;} public string Address {get; set;} }
Create a new person storage interface
Public interface ipersonrepository { ienumerable<person> GetAll (); Person Get (int id); Person ADD (person item); BOOL Update (person item); BOOL Delete (int id); }
New implementation
public class Personrepository:ipersonrepository {list<person> person = new list<person> (); Public Personrepository () {Add (new person {Id = 1, Name = ' joye.net1 ', age =, Address = "Shanghai, China"} ); ADD (new person {Id = 2, Name = ' Joye.net2 ', age =, Address = "Shanghai, China"}); ADD (new person {Id = 3, Name = ' joye.net3 ', age =, Address = "Shanghai, China"}); } public ienumerable<person> GetAll () {return person; Public person Get (int id) {return person. Find (p = = P.id = = Id); Public person ADD (person item) {if (item = = null) {throw new Argume Ntnullexception ("item"); } person. ADD (item); return item; public bool Update (person Item) {if (item = = null) {throw new Argum Entnullexception ("item"); } int index = person. FindIndex (p = p.id = = Item. ID); if (index = =-1) {return false; } person. RemoveAt (index); Person. ADD (item); return true; } public bool Delete (int id) {person. RemoveAll (p = = P.id = = Id); return true; } }
Global attribute Injection
public class MvcApplication:System.Web.HttpApplication { private void Setupresolverules (Containerbuilder Builder) { Builder. Registertype<personrepository> (). As<ipersonrepository> (); } protected void Application_Start () { var builder = new Containerbuilder (); Setupresolverules (builder); Builder. Registercontrollers (assembly.getexecutingassembly ()). Propertiesautowired (); var container = Builder. Build (); Dependencyresolver.setresolver (new Autofacdependencyresolver (container)); Arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); } }
Best access to data results;
Iii. Summary
The paper just gives a simple injection implementation, the rest can be studied by themselves, the constructor injection, method injection
Generic injection, all assembly injection, can be seen,
It is also possible to download the two open source projects at the beginning of the article to study the AUTOFAC injection method.
NET is one of the most popular IOC frameworks in the world of AUTOFAC