.net Autofac taken over by the IOC under core3.1

As we all know, .net core IOC container that comes naturally, but he does not really powerful functionality, and there are pit: When injected into the construction of his default constructor parameters to find the least.

Here, I explain how to use Autofac to take over the IOC, as to why to choose Autofac, this fact, I am also with the public, but back when I talked about using Autofac achieve AOP, perhaps you can find his benefits

The first step, the lead pack

 

 

 The second step, modify startup.cs file

First, define a new method within the class

 

 

 

void ConfigureContainer public (ContainerBuilder Builder)
{
// add the service registration here
builder.RegisterType <ProductRepository> () As < IProductRepository> ();. // Register
}

In this method, the completion of service registration. Sign up for this service, on Autofac the API if there is a need to learn more about, you can review Autofac Guide

 

The third step is to modify the Program.cs file

 

 

 UseServiceProviderFactory (new AutofacServiceProviderFactory ()) // use AutoFac do IOC and AOP

The fourth step, the official use

For IOC, although there are many Sao Autofac posture, but I still recommend using the constructor injection here, if you want to ask why, in fact, the key is, you do not have to pull the bag in each class library, package, then dragged around the project dependence of the assembly will produce a certain coupling.

 

Guess you like

Origin www.cnblogs.com/shapman/p/12232924.html