2017-08-01 2 views
1

Ich ging zu asp.net Kern vor kurzem und ich begann mit ConfigureServices anstelle von Windsor. Ich habe zwei Klassen MockDbContext und ProductionDbContext, beide erben von DbContext Klasse. Wie kann ich den folgenden Code in ConfigureServices schreiben?asp.net Kern Windsor vs ConfigureServices

if (true) { 
     container.Register(Component.For<DbContext>().UsingFactoryMethod(() => MockDbContext.GetMockDbContext()).LifeStyle.HybridPerWebRequestTransient()); 
} else { 
     container.Register(Component.For<DbContext>().ImplementedBy<ProductionDbContext>().LifeStyle.HybridPerWebRequestTransient()); 
} 

Ich versuchte mehrere Optionen ohne Erfolg. Es scheint, als ob ich eine Schnittstelle herstellen müsste, um beide Klassen zu unterscheiden.

Antwort

0
 if (true) 
     { 
      services.AddSingleton((_) => MockDbContext.GetMockDbContext()); 
     } else 
     { 
      services.AddScoped((_) => new ProductionDbContext()); 
     }