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

2021年09月16日 阅读数:2
这篇文章主要向大家介绍.Net Core 中同一接口多个实现依赖注入的问题,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

建立一个接口,而后提供两个实现的类ide

须要添加 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 }

运行结果以下:ui

Writing logs by serilog
Writing logs by serilog

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

 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 }

输出结果:code

Writing logs by nlog
Writing logs by serilog

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

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

须要添加如下nuget包的引用:get

Microsoft.Extensions.DependencyInjectionstring

Autofac.Extensions.DependencyInjectionit

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

 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

也能达到咱们想要的结果

固然也还有其余的方式达到咱们的目的,其余的方式请你们本身摸索和学习,也能够在下面留言,你们共同窗习和进步!