What is dependency injection?
We introduce the entity as a practical example as follows
Public class Product { publicintgetset;} Public string Get Set ; } Public decimal Get Set ; } }
The EF implementation is as follows
Public classProductcontext:dbcontext { PublicProductcontext ():Base("Productcontext") { } Publicdbset< product> Products {Get;Set; } } Public classproductrepo_ef {PrivateProductcontext _ctx =NewProductcontext (); PublicIenumerable<models.product >GetAll () {return_ctx. Products.tolist (); } PublicModels. Product GetProduct (intID) {return_ctx. Products.firstordefault (x=>x.id==ID); } }
In the controller
Public classproductscontroller:apicontroller{//This line is the source of the problem.Productrepository _repository =Newproductrepository (); PublicIenumerable<product>Get () {return_repository. GetAll (); } PublicIhttpactionresult Get (intID) {varProduct =_repository. GetByID (ID); if(Product = =NULL) { returnNotFound (); } returnOk (product); }}
Our productcontroller is dependent on the productrespository to provide the data, that is, our image, the controller relies on the implementation of _respository. So if our repo changes and we're not going to adopt EF's approach to SQL Server, we want to change the implementation for MySQL, and if a lot of controllers are dependent on repo implementations, the amount of code churn will be huge and easy to make mistakes. This will be unthinkable in the real industrial code. So we have to use the loosely coupled implementations that have been injected since.
Public Interfaceiproductrepository{IEnumerable<Product>GetAll (); Product GetById (intID); } Public classproductrepository:iproductrepository{//} Public classproductscontroller:apicontroller{Privateiproductrepository _repository; PublicProductsController (iproductrepository repository) {_repository=repository; } }
method to expose the implementation. When the controller instance is created, the first method is used to obtain the controller instance, if NULL is used to create an instance in the call to the parameterless construct. So we just need to extend the implementation for Idependencyresolver with an external IOC container. External IOC containers are commonly used with Unity AUTOFAC Ninject and so on. We use AUTOFAC as our implementation here. It is also the main IOC container because of its complete documentation, high efficiency and powerful functions. You can use AUTOFAC to create your own, or you can take the AUTOFAC official extension for WEBAPI. Install AUTOFAC and Autofac.extentions.webapi through NuGet. will automatically have a autofacwebapidependencyresolver implementation.
In the Global.asax
var New Containerbuilder (); builder. Registerapicontrollers (assembly.getexecutingassembly ()); // The only change in this place is a seamless link to the database. Builder. Registertype<productrepo_ef> (). As<iproductrepo>(); var container = Builder. Build (); var New Autofacwebapidependencyresolver (container); = resolver;
Here we use EF and ef to MySQL two repo implementations, that is, to provide data through two kinds of databases. Note that using EF to MySQL requires the MySQL database to be installed and in the NuGet package
And there are two types of connection strings for MySQL and SQL Server in Web. config. This way, when we need to replace different repo implementations, we only need to
Builder. registertype<productrepo_ef> (). as<Iproductrepo> ();
Replaced by
Builder. registertype<Productrepo_mysql> (). as<Iproductrepo> ();Can.Our code churn only happens in this place where it is injected.
AUTOFAC implementation of WEBAPI Dependency injection (EF and MySQL)