asp.net 核心 Windsor 与 ConfigureServices
asp.net core Windsor vs ConfigureServices
我最近去了 asp.net 核心,我开始使用 ConfigureServices 而不是 Windsor。我有两个 classes MockDbContext 和 ProductionDbContext,它们都继承自 DbContext class。如何在ConfigureServices中编写如下代码?
if (true) {
container.Register(Component.For<DbContext>().UsingFactoryMethod(() => MockDbContext.GetMockDbContext()).LifeStyle.HybridPerWebRequestTransient());
} else {
container.Register(Component.For<DbContext>().ImplementedBy<ProductionDbContext>().LifeStyle.HybridPerWebRequestTransient());
}
我尝试了几个选项都没有成功。看来我必须制作界面来区分两者 classes.
if (true)
{
services.AddSingleton((_) => MockDbContext.GetMockDbContext());
} else
{
services.AddScoped((_) => new ProductionDbContext());
}
我最近去了 asp.net 核心,我开始使用 ConfigureServices 而不是 Windsor。我有两个 classes MockDbContext 和 ProductionDbContext,它们都继承自 DbContext class。如何在ConfigureServices中编写如下代码?
if (true) {
container.Register(Component.For<DbContext>().UsingFactoryMethod(() => MockDbContext.GetMockDbContext()).LifeStyle.HybridPerWebRequestTransient());
} else {
container.Register(Component.For<DbContext>().ImplementedBy<ProductionDbContext>().LifeStyle.HybridPerWebRequestTransient());
}
我尝试了几个选项都没有成功。看来我必须制作界面来区分两者 classes.
if (true)
{
services.AddSingleton((_) => MockDbContext.GetMockDbContext());
} else
{
services.AddScoped((_) => new ProductionDbContext());
}