.NET Core development actual combat (Lesson 7: Enhancing the capacity of the container with Autofac) - Study Notes (a)

07 | Autofac container with enhanced capabilities: the ability to introduce Aspect Oriented Programming (AOP) of

This section explains the use of third-party framework to extend dependency injection container

We need to introduce a third party vessel components under what circumstances?

In most cases, enough to use the default container assembly

When you need some very special scenario is as follows:

1, based on the injection name: the need to put a name to distinguish the service in accordance with the time it's different implementations

2, the injection properties: property directly to the service registration to a certain class to the inside, without the need to define a constructor, such as before the reference and constructors FromService

3, sub-containers: it can be understood as previously talked about Scope, but actually can achieve some special sub-frame container with a third party

4, based on dynamic proxy AOP: the need to inject additional actions in the service, the ability of dynamic proxies can be used

.NET Core dependency injection framework, its core extension point IserviceProviderFactory

Dependency injection container to third class are used as an extension point, to inject themselves to the entire framework

That is to say in the use of these dependency injection framework, without concern that someone's characteristics, what it was like someone interface, only need to focus on the core of the official definition of it, without direct rely on these frameworks

Source link:
https://github.com/witskeeper/geektime/tree/master/samples/DependencyInjectionAutofacDemo

service

namespace DependencyInjectionAutofacDemo.Services
{
    public interface IMyService
    {
        void ShowCode();
    }
    public class MyService : IMyService
    {
        public void ShowCode()
        {
            Console.WriteLine($"MyService.ShowCode:{GetHashCode()}");
        }
    }

    public class MyServiceV2 : IMyService
    {
        /// <summary>
        /// 用于演示属性注入的方式
        /// </summary>
        public MyNameService NameService { get; set; }

        public void ShowCode()
        {
            // 默认情况下,NameService 为空,如果注入成功,则不为空
            Console.WriteLine($"MyServiceV2.ShowCode:{GetHashCode()},NameService是否为空:{NameService == null}");
        }
    }


    public class MyNameService
    { 
        
    }
}

Let's look at how to integrate Autofac

Autofac using .NET because it is one of the oldest communities inside the container frame

It has two packages:

Autofac.Extensions.DependencyInjection

Autofac.Extras.DynamicProxy

The introduction of these two packages, you can use it to achieve said before four abilities

After introduction of these two packages, the need to add a line of code in Program

.UseServiceProviderFactory(new AutofacServiceProviderFactory())

UseServiceProviderFactory is the entry for registering a third-party container

There is also a change in Startup, we need to add a ConfigureContainer method, it is the participation of ContainerBuilder Autofac

public void ConfigureContainer(ContainerBuilder builder)

There are two ConfigureServices, a default, one ConfigureContainer

After registration into default container service, in fact, is to succeed Autofac, and then perform ConfigureContainer

Autofac way of registration and registration of different ways before, to register specific implementation, and then tell it wanted to mark it as the type of services which, contrary to the previous wording

builder.RegisterType<MyService>().As<IMyService>();

Next is the name registered, when you need to register a service several times, and with different names to distinguish as they can in this way, the argument is a service name

builder.RegisterType<MyServiceV2>().Named<IMyService>("service2");

How to use it?

public ILifetimeScope AutofacContainer { get; private set; }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // 注册根容器
    this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
    
    // Autofac 容器获取实例的方式是一组 Resolve 方法
    var service = this.AutofacContainer.ResolveNamed<IMyService>("service2");
    service.ShowCode();
    
    ...

Start the program, the output is as follows:

MyServiceV2.ShowCode:61566768,NameService是否为空:True

Creative Commons License

This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing.

Welcome to reprint, use, repost, but be sure to keep the article signed by Zheng Ziming (containing links: http://www.cnblogs.com/MingsonZheng/), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification .

If you have any questions, please contact me ([email protected]).

Guess you like

Origin www.cnblogs.com/MingsonZheng/p/12348263.html