Autofac注入方式

Autofac三种注入方式

l 构造函数注入

l 属性注入

l 方法注入

 

实践

项目结构

在上节Autofac的快速入门的项目上改造。

 

 

 

添加ITestServiceB 和 TestServiceB

public interface ITestServiceB

    {

        void Show();

}

public class TestServiceB : ITestServiceB

    {

        private ITestServiceA _testServiceA;    

 

        public TestServiceB()

        {

            Console.WriteLine($"{this.GetType().Name} 被构造了...");

        }

        public void SetService(ITestServiceA testServiceA)

        {

            _testServiceA = testServiceA;

        }

 

        public void Show()

        {

            _testServiceA.Show();

            Console.WriteLine($"This is a {this.GetType().Name} Instance...");

        }

    }

 

添加ITestServiceC 和 TestServiceC

public interface ITestServiceC

    {

        void Show();

}

public class TestServiceC : ITestServiceC

    {

        private ITestServiceA _testServiceA;

        public TestServiceC(ITestServiceA iTestServiceA)

        {

            _testServiceA = iTestServiceA;

            Console.WriteLine($"{this.GetType().Name} 被构造了...");

        }

 

        public void Show()

        {

            _testServiceA.Show();

            Console.WriteLine($"This is a {this.GetType().Name} Instance...");

        }

    }

 

 

添加ITestServiceD 和 TestServiceD

public interface ITestServiceB

    {

        void Show();

}

public class TestServiceD : ITestServiceD

    {

        public ITestServiceA TestServiceA { get; set; }

        public ITestServiceB TestServiceB { get; set; }

        public ITestServiceC TestServiceC { get; set; }

 

        public TestServiceD()

        {

            Console.WriteLine($"{this.GetType().Name} 被构造了...");

        }

 

        public void Show()

        {

            TestServiceA.Show();

            //TestServiceB.Show();

            TestServiceC.Show();

            Console.WriteLine($"This is a {this.GetType().Name} Instance...");

        }

    }

 

构造函数注入

构造器注入是默认行为,不需要设置。

var builder = new ContainerBuilder();

                builder.RegisterType<TestServiceA>().As<ITestServiceA>();

                builder.RegisterType<TestServiceC>().As<ITestServiceC>();

                var container = builder.Build();

                // 获取服务实例

                var testService = container.Resolve<ITestServiceC>();

                testService.Show();

 

  运行:

 

属性注入

提供属性的服务,注册时候必须使用PropertiesAutowired方法。

var builder = new ContainerBuilder();

                builder.RegisterType<TestServiceA>().As<ITestServiceA>();                builder.RegisterType<TestServiceD>().As<ITestServiceD>().PropertiesAutowired();

                var container = builder.Build();

                // 获取服务实例

                var testService = container.Resolve<ITestServiceD>();

                testService.Show();

 

 

方法注入

提供方法注入,需要在注册服务的时候,使用OnActivated方法。

                var builder = new ContainerBuilder();

                builder.RegisterType<TestServiceA>().As<ITestServiceA>();

                builder.RegisterType<TestServiceB>().OnActivated(e =>

                         e.Instance.SetService(e.Context.Resolve<ITestServiceA>())

                ).As<ITestServiceB>();

                var container = builder.Build();

 

                // 获取服务实例

                var testService = container.Resolve<ITestServiceB>();

                testService.Show();

 

 

 运行:

 

 

总结

通过构造函数传入接口进行获取实例,该方法可以极大减少构造函数传入的实例过多所导致的构造函数参数臃肿。

posted on   itjeff  阅读(146)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示