Autofac支持配置文件

Autofac支持配置文件

前面一节介绍了Autofac注入方式,这节介绍Autofac支持配置文件。

环境

Win10+VS2020+.NET 5.0 +Autofac 6.3.0

 

介绍

autofac是比较简单易用的IOC容器。下面我们展示如何通过json配置文件,来进行控制反转。

步骤:

  1. Nuget引入程序集:

l Autofac.Extensions.DependencyInjection

l Autofac.Configuration

l Autofac

2.准备配置文件

3.读取配置文件,根据配置文件信息,生成抽象和映射信息

 

 

实践

项目结构

在上节Autofac的注入方式的项目上改造。

 

 

 

引入程序集

<ItemGroup>

    <PackageReference Include="Autofac" Version="6.3.0" />

    <PackageReference Include="Autofac.Configuration" Version="6.0.0" />

    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />

  </ItemGroup>

 

 

准备配置文件

在根目录下添加名称为autofac”的Json文件,设置为“始终复制”。

 

 

 

代码如下:

{

"components": [

{

"type": "Yak.Autofac.Api.Demo.Services.TestServiceA,Yak.Autofac.Api.Demo",

"services": [

{

"type": "Yak.Autofac.Api.Demo.Services.Interface.ITestServiceA,Yak.Autofac.Api.Demo"

}

],

"instanceScope": "single-instance", //生命周期

"injectProperties": true // 属性注入

}

,

{

"type": "Yak.Autofac.Api.Demo.Services.TestServiceB,Yak.Autofac.Api.Demo",

"services": [

{

"type": "Yak.Autofac.Api.Demo.Services.Interface.ITestServiceB,Yak.Autofac.Api.Demo"

}

],

"instanceScope": "single-instance", //生命周期

"injectProperties": true // 属性注入

},

{

"type": "Yak.Autofac.Api.Demo.Services.TestServiceC,Yak.Autofac.Api.Demo",

"services": [

{

"type": "Yak.Autofac.Api.Demo.Services.Interface.ITestServiceC,Yak.Autofac.Api.Demo"

}

],

"instanceScope": "single-instance", //生命周期

"injectProperties": true // 属性注入

},

{

"type": "Yak.Autofac.Api.Demo.Services.TestServiceD,Yak.Autofac.Api.Demo",

"services": [

{

"type": "Yak.Autofac.Api.Demo.Services.Interface.ITestServiceD,Yak.Autofac.Api.Demo"

}

],

"instanceScope": "single-instance", //生命周期

"injectProperties": true // 属性注入

}

]

}

 

 

 

 

生成抽象和映射信息

修改StartupConfigureServices方法,读取配置文件,根据配置文件信息,生成抽象和映射信息,代码如下

ContainerBuilder containerBuilder = new ContainerBuilder();

                {

                    //读取配置文件,把配置关系装载到ContainerBuilder

                    IConfigurationBuilder config = new ConfigurationBuilder();

                    IConfigurationSource autofacJsonConfigSource = new JsonConfigurationSource()

                    {

                        Path = "autofac.json",

                        Optional = false,//boolean,默认就是false,可不写

                        ReloadOnChange = true,//同上

                    };

                    config.Add(autofacJsonConfigSource);

                    ConfigurationModule module = new ConfigurationModule(config.Build());

                    containerBuilder.RegisterModule(module);

                }

                IContainer container = containerBuilder.Build();

                ITestServiceD testServiceD = container.Resolve<ITestServiceD>();

                testServiceD.Show();

 

运行

运行程序:

 

 

 

 

支持配置文件优势

支持配置文件有什么好处呢?再添加一个类TestServiceUpdate ,代码如下:

 public class TestServiceUpdate : ITestServiceA

    {

        public TestServiceUpdate()

        {

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

        }

 

        public void Show()

        {

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

        }

    }

 

把配置文件中TestServiceUpdate修改为TestServiceUpdate 。

 

 

 

运行结果:

 

 

 

 

 

发现不用修改代码只要配置就可以实现类的调换了,是不是很方便。

 

 

总结

Autofac支持配置文件太强大了,不用修改代码只要配置就可以实现类的调换了。

posted @ 2022-05-17 23:19  春光牛牛  阅读(415)  评论(0编辑  收藏  举报