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 classPerson { Public intId {Get;Set; } Public stringName {Get;Set; } Public intAge {Get;Set; } Public stringAddress {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 classpersonrepository:ipersonrepository {List<Person> person =NewList<person>(); Publicpersonrepository () {ADD (Newperson {Id =1, Name ="Joye.net1", age = -, Address ="Shanghai, China" }); ADD (Newperson {Id =2, Name ="Joye.net2", age = -, Address ="Shanghai, China" }); ADD (Newperson {Id =3, Name ="Joye.net3", age = -, Address ="Shanghai, China" }); } PublicIenumerable<person>GetAll () {returnPerson ; } PublicPerson Get (intID) {returnPerson. Find (p = = P.id = =ID); } PublicPerson Add (person item) {if(Item = =NULL) { Throw NewArgumentNullException ("Item"); } person. ADD (item); returnitem; } Public BOOLUpdate (person item) {if(Item = =NULL) { Throw NewArgumentNullException ("Item"); } intindex = person. FindIndex (p = p.id = =item. ID); if(Index = =-1) { return false; } person. RemoveAt (index); Person. ADD (item); return true; } Public BOOLDelete (intID) {person. RemoveAll (P= = P.id = =ID); return true; } }
Global attribute Injection
Public classMvcApplication:System.Web.HttpApplication {Private voidsetupresolverules (containerbuilder builder) {Builder. Registertype<PersonRepository> (). As<ipersonrepository>(); } protected voidApplication_Start () {varBuilder =NewContainerbuilder (); Setupresolverules (builder); Builder. Registercontrollers (assembly.getexecutingassembly ()). Propertiesautowired (); varcontainer =Builder. Build (); Dependencyresolver.setresolver (NewAutofacdependencyresolver (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