• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

.Net Core 中同一接口多个实现依赖注入的问题

开发技术 开发技术 4小时前 2次浏览

创建一个接口,然后提供两个实现的类

需要添加 Microsoft.Extensions.DependencyInjection nuget包的引用

 1 void Main()
 2 {
 3     var provider = new ServiceCollection()
 4     .AddScoped<ILogCore, NlogCore>()
 5     .AddScoped<ILogCore, SerilogCore>()
 6     .BuildServiceProvider();
 7 
 8 
 9     var nlog = provider.GetService<ILogCore>();
10     var serilog = provider.GetService<ILogCore>();
11     nlog.WriteLog();
12     serilog.WriteLog();
13 }
14 
15 public interface ILogCore
16 {
17     void WriteLog();
18 }
19 
20 public class NlogCore : ILogCore
21 {
22     public void WriteLog()
23     {
24         Console.WriteLine("Writing logs by nlog");
25     }
26 }
27 
28 public class SerilogCore : ILogCore
29 {
30     public void WriteLog()
31     {
32         Console.WriteLine("Writing logs by serilog");
33     }
34 }

运行结果如下:

Writing logs by serilog
Writing logs by serilog

按照上面的写法,看到的结果并不是我们想要的,这个后面的注入覆盖了前面的,那我们该如何做到一个接口的多个实现呢?可以对代码进行稍微的调整,利用另一个扩展的方法:

 1 void Main()
 2 {
 3     var provider = new ServiceCollection()
 4     .AddScoped<NlogCore>()
 5     .AddScoped<SerilogCore>()
 6     .AddScoped(factory =>
 7     {
 8         Func<string, ILogCore> accessor = key =>
 9         {
10 
11             if (key.Equals("nlog"))//根据传入的key值,然后返回相应的实现类的实例
12             {
13                 return factory.GetService<NlogCore>();
14             }
15             else if (key.Equals("serilog"))
16             {
17                 return factory.GetService<SerilogCore>();
18             }
19             else
20             {
21                 throw new ArgumentNullException($"{key} is not supported!");
22             }
23         };
24 
25 
26         return accessor;
27     })
28     .BuildServiceProvider();
29     
30     var logProvider = provider.GetService<Func<string, ILogCore>>();
31     var nlog = logProvider("nlog");
32     var serilog = logProvider("serilog");
33     nlog.WriteLog();
34     serilog.WriteLog();
35 }
36 
37 public interface ILogCore
38 {
39     void WriteLog();
40 }
41 
42 public class NlogCore : ILogCore
43 {
44     public void WriteLog()
45     {
46         Console.WriteLine("Writing logs by nlog");
47     }
48 }
49 
50 public class SerilogCore : ILogCore
51 {
52     public void WriteLog()
53     {
54         Console.WriteLine("Writing logs by serilog");
55     }
56 }

输出结果:

Writing logs by nlog
Writing logs by serilog

这个正是我们想要的结果,这是通过使用微软提供的IOC容器进行的依赖注入,如果换成Autofac又当如何实现呢?

Autofac版同一接口多个实现的依赖注入的解决方案

需要添加以下nuget包的引用:

Microsoft.Extensions.DependencyInjection

Autofac.Extensions.DependencyInjection

还是以上面的代码为例,直接上代码:

 1 void Main()
 2 {
 3     var collections = new ServiceCollection();
 4     var containerBuilder = new ContainerBuilder();
 5     containerBuilder.Populate(collections);
 6     containerBuilder.RegisterType<NlogCore>().Keyed<ILogCore>(LogTargetEnum.Nlog);
 7     containerBuilder.RegisterType<SerilogCore>().Keyed<ILogCore>(LogTargetEnum.Serilog);
 8     var container = containerBuilder.Build();
 9     var serivceProvider = new AutofacServiceProvider(container);
10 
11     var logProvider = serivceProvider.GetService<IIndex<LogTargetEnum, ILogCore>>();
12     var nlog = logProvider[LogTargetEnum.Nlog];
13     var serilog = logProvider[LogTargetEnum.Serilog];
14     
15     nlog.WriteLog();
16     serilog.WriteLog();
17 }
18 
19 public interface ILogCore
20 {
21     void WriteLog();
22 }
23 
24 public class NlogCore : ILogCore
25 {
26     public void WriteLog()
27     {
28         Console.WriteLine("Writing logs by nlog");
29     }
30 }
31 
32 public class SerilogCore : ILogCore
33 {
34     public void WriteLog()
35     {
36         Console.WriteLine("Writing logs by serilog");
37     }
38 }
39 
40 public enum LogTargetEnum
41 {
42     Nlog,
43     Serilog
44 }

 

输出结果:

Writing logs by nlog
Writing logs by serilog

也能达到我们想要的结果

当然也还有其他的方式达到我们的目的,其他的方式请大家自己摸索和学习,也可以在下面留言,大家共同学习和进步!

 


程序员灯塔
转载请注明原文链接:.Net Core 中同一接口多个实现依赖注入的问题
喜欢 (0)