• 如何在Asp.Net Core中注册同一接口的多个实现?


    have services that are derived from the same interface.

    public interface IService { }
    public class ServiceA : IService { }
    public class ServiceB : IService { } 
    public class ServiceC : IService { }
    
    • 1
    • 2
    • 3
    • 4

    Typically, other IoC containers like Unity allow you to register concrete implementations by some Key that distinguishes them.

    In ASP.NET Core, how do I register these services and resolve them at runtime based on some key?

    I don’t see any Add Service methods that take a key or name parameter, which would typically be used to distinguish the concrete implementation.

    public void ConfigureServices(IServiceCollection services)
    {
    // How do I register services of the same interface?
    }

    public MyController:Controller
    {
    public void DoSomething(string key)
    {
    // How do I resolve the service by key?
    }
    }

    Is the Factory pattern the only option here?

    Update1
    I have gone though the article here that shows how to use the factory pattern to get service instances when we have multiple concrete implementations. However, it is still not a complete solution. When I call the _serviceProvider.GetService() method, I cannot inject data into the constructor.

    For example consider this:

    public class ServiceA : IService
    {
         private string _efConnectionString;
         ServiceA(string efconnectionString)
         {
           _efConnecttionString = efConnectionString;
         } 
    }
    
    public class ServiceB : IService
    {    
       private string _mongoConnectionString;
       public ServiceB(string mongoConnectionString)
       {
          _mongoConnectionString = mongoConnectionString;
       }
    }
    
    public class ServiceC : IService
    {    
        private string _someOtherConnectionString
        public ServiceC(string someOtherConnectionString)
        {
          _someOtherConnectionString = someOtherConnectionString;
        }
    }
    
    • 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

    How can _serviceProvider.GetService() inject the appropriate connection string? In Unity, or any other IoC library, we can do that at type registration. I can use IOption, however, that will require me to inject all settings. I cannot inject a particular connection string into the service.

    Also note that I am trying to avoid using other containers (including Unity) because then I have to register everything else (e.g., Controllers) with the new container as well.

    Also, using the factory pattern to create service instances is against DIP, as it increases the number of dependencies a client has details here.

    So, I think the default DI in ASP.NET Core is missing two things:

    The ability to register instances using a key
    The ability to inject static data into constructors during registration

  • 相关阅读:
    MYSQL(四)DQL-----对表中数据进行查询
    js基础笔记学习252事件委派3
    面经-并发-HashTable与ConcurrentHashMap比较
    【Unity入门计划】CreatorKitFPS:第一人称射击3D小游戏
    keepalived+haproxy配置集群和负载均衡
    SpringBoot自动装配原理(简单易懂)
    Python运维脚本:提高工作效率
    Edge浏览器下载文件提示 “无法安全下载” 的解决方法
    带你了解“JSON解析”
    TikTok为什么无法关注其他人?
  • 原文地址:https://blog.csdn.net/qq_42980269/article/details/134013566