[Asp.Net Core]Autofac多种注入

Posted 厦门德仔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Asp.Net Core]Autofac多种注入相关的知识,希望对你有一定的参考价值。

具体实现

    [Intercept(typeof(CustomAutofacAop))]
    public class TestServiceA : ITestServiceA
    
       
        public TestServiceA()
        
            Console.WriteLine($"this.GetType().Name被构造。。。");
        

        public virtual void Show()
        
            Console.WriteLine("A123456");
        
    
    public class TestServiceB : ITestServiceB
    

        public ITestServiceA _iTestServiceA = null;

        public void SetService(ITestServiceA iTestServiceA)
        
            _iTestServiceA = iTestServiceA;
        

        public TestServiceB(ITestServiceA iTestServiceA)
        
            Console.WriteLine($"this.GetType().Name被构造。。。");
        


        public void Show()
        
            //_iTestServiceA.Show();
            Console.WriteLine($"This is TestServiceB B123456");
        
    
    public class TestServiceC : ITestServiceC
    
        public TestServiceC(ITestServiceB iTestServiceB)
        
            Console.WriteLine($"this.GetType().Name被构造。。。");
        
        public void Show()
        
            Console.WriteLine("C123456");
        
    

    public class TestServiceD : ITestServiceD
    
        public string strin  get; set; 
        public ITestServiceA _iTestServiceA  get; set; 
        public ITestServiceB _iTestServiceB  get; set;  
        public ITestServiceC _iTestServiceC  get; set; 

        public TestServiceD()
        
            Console.WriteLine($"this.GetType().Name被构造。。。");
        
        public void Show()
        
            _iTestServiceA.Show();
            Console.WriteLine("D123456");
        
    

1.构造函数注入

                ContainerBuilder containerBuilder = new ContainerBuilder(); //创建容器
                containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>();
                containerBuilder.RegisterType<TestServiceB>().As<ITestServiceB>();
                containerBuilder.RegisterType<TestServiceC>().As<ITestServiceC>();
                IContainer container = containerBuilder.Build(); //得到容器
                ITestServiceB testServiceB = container.Resolve<ITestServiceB>(); //获取服务
                testServiceB.Show();

2.属性注入

                ContainerBuilder containerBuilder = new ContainerBuilder(); //创建容器
                containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>();
                containerBuilder.RegisterType<TestServiceB>().As<ITestServiceB>();
                containerBuilder.RegisterType<TestServiceC>().As<ITestServiceC>();
                containerBuilder.RegisterType<TestServiceD>().As<ITestServiceD>().PropertiesAutowired();
                IContainer container = containerBuilder.Build(); //得到容器
                ITestServiceD testServiceD = container.Resolve<ITestServiceD>(); //获取服务
                testServiceD.Show();

3.方法注入

                ContainerBuilder containerBuilder = new ContainerBuilder(); //创建容器
                containerBuilder.RegisterType<TestServiceA>().As<ITestServiceA>();
                containerBuilder.RegisterType<TestServiceB>().OnActivated(e =>
                e.Instance.SetService(e.Context.Resolve<ITestServiceA>())).As<ITestServiceB>();

                containerBuilder.RegisterType<TestServiceC>().As<ITestServiceC>();
                containerBuilder.RegisterType<TestServiceD>().As<ITestServiceD>().PropertiesAutowired();
                IContainer container = containerBuilder.Build(); //得到容器
                ITestServiceB testServiceB = container.Resolve<ITestServiceB>(); //获取服务
                testServiceB.Show();

以上是关于[Asp.Net Core]Autofac多种注入的主要内容,如果未能解决你的问题,请参考以下文章

[Asp.Net Core]Autofac单抽象多实例属性注入

[Asp.Net Core]Autofac单抽象多实例属性注入

ASP.NET Core Web 应用程序系列- 在ASP.NET Core中使用Autofac替换自带DI进行批量依赖注入(MVC当中应用)

Autofac - ASP.NET Core 中的动作过滤器中的属性注入

Asp.net Core AutoFac根据程序集实现依赖注入

[Asp.Net Core]Autofac单抽象多实现构造函数注入