[英]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.