简体   繁体   English

在多个构造函数参数中注入具有相同接口的不同实现

[英]Inject different implementations that have same interface in multiple constructor parameters

My goal is to create an object that contains different implementations of an interface and at runtime select the implementation to use. 我的目标是创建一个包含接口的不同实现的对象,并在运行时选择要使用的实现。 I'm using the Dependency injection in ASP.NET Core. 我在ASP.NET Core中使用依赖注入。

Code: 码:

public interface IStateRepository : IDbReadRepository<IState> { }

public interface IDbReadRepository<T> : IBaseRepository
{
     IReadOnlyList<T> GetAll();
}

public interface IBaseRepository
{
    IUserContext UserContext { get; set; }
}

namespace MvcOpinionatedTemplate.Repositories.Dapper
{
    public class StateRepository : BaseDbRepository, IStateRepository
    {
        public StateRepository(IUserContext userContext, IDbConnection dbConnection) : base(userContext, dbConnection) { }

        public IReadOnlyList<IState> GetAll()
        {
            return _dbConnection.Query<State>("SELECT * FROM State").ToList();
        }
    }
}

namespace Template.Repositories.Local
{
    public class StateRepository : BaseRepository, IStateRepository
    {
        public StateRepository(IUserContext userContext) : base(userContext) { }

        public IReadOnlyList<IState> GetAll()
        {
            var filePath = Path.Combine(AppContext.BaseDirectory, @"Local\json\states.json");

            return JsonConvert.DeserializeObject<List<State>>(File.ReadAllText(filePath));
        }
}


namespace MvcOpinionatedTemplate.Repositories.Collections
{
    public class StateRepositories
    {
        public IStateRepository Local { get; }

        public IStateRepository SqlServer { get; }

        public StateRepositories(IStateRepository local, IStateRepository sqlServer)
        {
            Local = local;
            SqlServer = sqlServer;
        }
    }
}

What I'd like to do is set in the Startup.ConfigureServices(): 我想做的是在Startup.ConfigureServices()中设置的:

services.AddTransient<StateRepositories, XXXXX>

I tried this: 我尝试了这个:

services.AddTransient<StateRepositories>(s => new StateRepositories(new Repositories.Local.StateRepository(--UserContext--), new Repositories.Dapper.StateRepository(-UserContext--)));

The problem is how to have DI populate UserContext. 问题是如何让DI填充UserContext。 I have it defined Startup.ConfigureServices(): 我定义了Startup.ConfigureServices():

services.AddScoped<IUserContext, UserContext>();

How do have DI populate UserContext for the StateRepositories implementations? DI如何为StateRepositories实现填充UserContext? Or is there a better approach to achieve my goal? 还是有更好的方法实现我的目标?

You can register your IStateRepository separately and then inject IEnumerable<IStateRepository> which injects all implementations of IStateRepository . 您可以分别注册IStateRepository ,然后注入IEnumerable<IStateRepository> ,后者注入IStateRepository所有实现。

public interface IStateRepository
{
}

public class LocalRepository : IStateRepository
{
}

public class DapperRepository : IStateRepository
{
}

services.AddTransient<IStateRepository, LocalRepository>()
        .AddTransient<IStateRepository, DapperRepository>()
        .AddTransient<StateRepositories>();

public class StateRepositories
{
    public IStateRepository Local { get; }

    public IStateRepository SqlServer { get; }

    public StateRepositories(IEnumerable<IStateRepository> repositories)
    {
        Local = repositories.OfType<LocalRepository>().FirstOrDefault();
        SqlServer = repositories.OfType<DapperRepository>().FirstOrDefault();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有条件/上下文DI的情况下将不同的实现注入相同的接口? - How to inject different implementations to same interface without conditional/context DI? 使用相同的接口但构造函数参数不同的多个单例注册 - Register multiple singletons with same interface but different constructor parameters 注册多个具有相同接口但构造函数参数值不同的单例 - Register multiple singletons with same interface but different constructor parameters values 相同接口的 StructureMap,但具有不同构造函数参数的多个实例 - StructureMap for same interface but with multiple instances for different constructor parameters 使用多个相同类型的参数注入构造函数 - Inject constructor with multiple same typed parameters 在运行时将命令的不同实现注入命令 - Inject different implementations of an Interface to a command at runtime 如何将一个接口的多个实现注入一个实现中 - How to Inject multiple Implementations of an Interface into one Implementation Autofac 具有相同接口的多个实现 - Autofac with multiple implementations of the same interface 将多个实现注册到同一接口 - Register multiple implementations to same interface 将不同的实现注入相同的接口,然后在正确的项目/程序集中选择正确的实现 - Inject different implementations into same interface then pick up right implementation in right project / assembly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM