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

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

How to Get Along Without naming services?

// 获取没有命名的服务,把 namd 去掉即可
var servicenamed = this.AutofacContainer.Resolve<IMyService>();
servicenamed.ShowCode();

// Autofac 容器获取实例的方式是一组 Resolve 方法
var service = this.AutofacContainer.ResolveNamed<IMyService>("service2");
service.ShowCode();

Start the program, the output is as follows:

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

Next, to explain the properties of injection

builder.RegisterType<MyNameService>();
// 只需要在注册方法加上 PropertiesAutowired 即可
builder.RegisterType<MyServiceV2>().As<IMyService>().PropertiesAutowired();

Obtain it from the inside and the service ShowCode

var servicenamed = this.AutofacContainer.Resolve<IMyService>();
servicenamed.ShowCode();

Start the program, the output is as follows:

MyServiceV2.ShowCode:11318800,NameService是否为空:False

Is not empty, the registration is successful

Next, the scene presentation AOP, it refers to a case where an undesirable change in the original class, some logic embedded in performing the methods of making it possible to insert any method performed in the logic section

namespace DependencyInjectionAutofacDemo.Services
{
    /// <summary>
    /// IInterceptor 是 Autofac 的面向切面的最重要的一个接口,它可以把逻辑注入到方法的切面里面去
    /// </summary>
    public class MyInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            // 方法执行前
            Console.WriteLine($"Intercept before,Method:{invocation.Method.Name}");
            // 具体方法的执行,如果这句话不执行,相当于把切面的方法拦截掉,让具体类的方法不执行
            invocation.Proceed();
            // 方法执行后,也就是说可以在任意的方法执行后,插入执行逻辑,并且决定原有的方法是否执行
            Console.WriteLine($"Intercept after,Method:{invocation.Method.Name}");
        }
    }
}

How to start section?

// 把拦截器注册到容器里面
builder.RegisterType<MyInterceptor>();
// 注册 MyServiceV2,并且允许它属性注册 (PropertiesAutowired)
// 开启拦截器需要使用 InterceptedBy 方法,并且注册类型 MyInterceptor
// 最后还要执行一个开关 EnableInterfaceInterceptors 允许接口拦截器
builder.RegisterType<MyServiceV2>().As<IMyService>().PropertiesAutowired().InterceptedBy(typeof(MyInterceptor)).EnableInterfaceInterceptors();

Interceptor two types, one is the interface interceptor, interceptor, and class

Commonly used is the interface interceptors, when the service type is time interface, you need to use this way

If there is no class-based interface design, but when the implementation class, you need to use an interceptor class

A case where the class method interceptor needs to design a virtual method, like this type allows overloaded, it may intercept the specific methods

Start the program, the output is as follows:

Intercept before,Method:ShowCode
MyServiceV2.ShowCode:31780825,NameService是否为空:True
Intercept after,Method:ShowCode

Then all of a sudden see the use of container

// Autofac 具备给子容器进行命名的特性,可以把以服务注入到子容器中,并且是特定命名的子容器,这就意味着在其他的子容器是获取不到这个对象的
builder.RegisterType<MyNameService>().InstancePerMatchingLifetimeScope("myscope");

Create a child container of a myscope

using (var myscope = AutofacContainer.BeginLifetimeScope("myscope"))
{
    var service0 = myscope.Resolve<MyNameService>();
    using (var scope = myscope.BeginLifetimeScope())
    {
        var service1 = scope.Resolve<MyNameService>();
        var service2 = scope.Resolve<MyNameService>();
        Console.WriteLine($"service1=service2:{service1 == service2}");
        Console.WriteLine($"service1=service0:{service1 == service0}");
    }
}

Start the program, the output is as follows:

service1=service2:True
service1=service0:True

This means myscope child below the container, and then create the life cycle of any child regardless of the vessel, the resulting objects are the same

The advantage of this way is not desirable when the object is created at the root of the container, and it is within a desired predetermined range in the case of single-mode embodiment, it can be used in this way

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/12355351.html
Recommended