【发布时间】:2020-02-06 18:12:59
【问题描述】:
我有一个店铺,有以下2种店铺
interface IStore {}
class AStore : IStore {}
class BStore : IStore {}
那我有一个服务
public Service : IService
{
public Service(IStore store) {}
}
表示我们可以根据 enum Store 的条件传递 AStore 或 BStore
enum Store { A, B }
if Store.A => return AStore
if Store.B => return BStore
在控制器中,我有一个操作将 Store 作为参数传递并获取 Service 实例以继续执行某些操作
例如
public Task Get(Store store)
{
// How I can get Service instance with Store parameter which depends on IStore?
}
问题是如何利用 ASP.Net Core 中的 DI 来解决依赖于 IStore 的 Service?
【问题讨论】:
-
一种方法是使用带有自定义委托的键控服务。 stackoverflow.com/questions/55494896/…