Autofac基本使用

1、创建控制台应用程序,Nuget引入Autofac

2、创建接口IMicrophone

1 namespace Business.IServices
2 {
3     public interface IMicrophone
4     {
5        
6     }
7 }

3、创建Microphone类

 1 using Business.IServices;
 2 
 3 namespace Business.Services
 4 {
 5     public class Microphone : IMicrophone
 6     {
 7         public Microphone()
 8         {
 9             Console.WriteLine($"{this.GetType().Name}被构造..");
10         }
11     }
12 }

4、修改Programs.cs

 1 using Autofac;
 2 using Business.IServices;
 3 using Business.Services;
 4 {
 5     ContainerBuilder containerBuilder = new ContainerBuilder();
 6     containerBuilder.RegisterType<Microphone>().As<IMicrophone>();  //配置抽象与具体的关系
 7     IContainer container = containerBuilder.Build();  //创建容器
 8     IMicrophone microphone = container.Resolve<IMicrophone>();  //获取对象实例
 9 
10 }

5、设置断点,运行程序

可以发现microphone实例被创建

 6、扩展,Autofac的其它注册方式

 1 using Autofac;
 2 using Business.IServices;
 3 using Business.Services;
 4 using System.Reflection;
 5 {
 6     ContainerBuilder containerBuilder = new ContainerBuilder();
 7     containerBuilder.RegisterType<Microphone>().As<IMicrophone>();  //配置抽象与具体的关系
 8     IContainer container = containerBuilder.Build();  //创建容器
 9     IMicrophone microphone = container.Resolve<IMicrophone>();  //获取对象实例
10 
11 }
12 
13 {
14     ContainerBuilder containerBuilder = new ContainerBuilder();
15     containerBuilder.RegisterInstance(new Microphone());   //注册具体的实例
16     IContainer container = containerBuilder.Build();
17     IMicrophone microphone = container.Resolve<Microphone>();
18 }
19 
20 {
21     //注册一段业务逻辑
22     ContainerBuilder containerBuilder = new ContainerBuilder();
23     containerBuilder.RegisterType<Microphone>().As<IMicrophone>();
24     containerBuilder.Register<IPower>(context =>
25     {
26         IMicrophone microphone = context.Resolve<IMicrophone>();  //注册具体的实例
27         IPower power = new Power(microphone);  //创建对像
28         return power;
29     });
30     IContainer container = containerBuilder.Build();
31     IPower power = container.Resolve<IPower>();//注册具体的实例
32 }
33 {
34     //注册泛型
35     ContainerBuilder containerBuilder = new ContainerBuilder();
36     containerBuilder.RegisterGeneric(typeof(List<>)).As(typeof(List<>));
37     containerBuilder.RegisterType<Microphone>().As<IMicrophone>();
38     IContainer container = containerBuilder.Build();
39     IList<IMicrophone> microphones = container.Resolve<IList<IMicrophone>>();
40 }
41 {
42     //注册程序集
43     ContainerBuilder containerBuilder = new ContainerBuilder();
44     Assembly interfaceAssembly = Assembly.LoadFrom("Business.IServices.dll");
45     Assembly serviceAssembly = Assembly.LoadFrom("Business.Services.dll");
46     containerBuilder.RegisterAssemblyTypes(interfaceAssembly, serviceAssembly).AsImplementedInterfaces();
47     IContainer container = containerBuilder.Build();
48     IEnumerable<IMicrophone> microphones = container.Resolve<IEnumerable<IMicrophone>>();
49 }

 

7、默认情况下,Autofac会调用参数最多的构造函数,在指定调用哪个构造函数可以使用:UsingConstructor

1  ContainerBuilder containerBuilder = new ContainerBuilder();
2     containerBuilder.RegisterType<Microphone>().As<IMicrophone>();
3     containerBuilder.RegisterType<Power>()
4         .UsingConstructor(typeof(IMicrophone))    //选择只有一个参数构造函数
5         .As<IPower>();
6     IContainer container = containerBuilder.Build();
7     IPower power = container.Resolve<IPower>();   //power构造函数的参数Microphone对象自动创建,默认支持构造函数注入

 

posted @ 2022-05-21 23:05  ziff123  阅读(683)  评论(0编辑  收藏  举报