Skip to content

Commit

Permalink
rename to ReplaceCoreServices
Browse files Browse the repository at this point in the history
  • Loading branch information
OdeToCode committed Sep 27, 2019
1 parent fc7cf96 commit 2f17838
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions All/test/TestTheTest/TestInMemory/CustomWebFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,29 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
//
// This will replace DbContextOptions
//
//AddCoreServices<SomeDbContext>(services, (p, o) =>
//ReplaceCoreServices<SomeDbContext>(services, (p, o) =>
//{
// o.UseInMemoryDatabase("DB");
//}, ServiceLifetime.Scoped);

});
}

private static void AddCoreServices<TContextImplementation>(IServiceCollection serviceCollection, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction, ServiceLifetime optionsLifetime) where TContextImplementation : DbContext
private static void ReplaceCoreServices<TContextImplementation>(IServiceCollection serviceCollection,
Action<IServiceProvider, DbContextOptionsBuilder> optionsAction,
ServiceLifetime optionsLifetime) where TContextImplementation : DbContext
{
serviceCollection.Add(new ServiceDescriptor(typeof(DbContextOptions<TContextImplementation>), (IServiceProvider p) => DbContextOptionsFactory<TContextImplementation>(p, optionsAction), optionsLifetime));
serviceCollection.Add(new ServiceDescriptor(typeof(DbContextOptions), (IServiceProvider p) => p.GetRequiredService<DbContextOptions<TContextImplementation>>(), optionsLifetime));
serviceCollection.Add(new ServiceDescriptor(typeof(DbContextOptions<TContextImplementation>),
(IServiceProvider p) => DbContextOptionsFactory<TContextImplementation>(p, optionsAction), optionsLifetime));
serviceCollection.Add(new ServiceDescriptor(typeof(DbContextOptions),
(IServiceProvider p) => p.GetRequiredService<DbContextOptions<TContextImplementation>>(), optionsLifetime));
}

private static DbContextOptions<TContext> DbContextOptionsFactory<TContext>(IServiceProvider applicationServiceProvider, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction) where TContext : DbContext
private static DbContextOptions<TContext> DbContextOptionsFactory<TContext>(IServiceProvider applicationServiceProvider,
Action<IServiceProvider, DbContextOptionsBuilder> optionsAction) where TContext : DbContext
{
DbContextOptionsBuilder<TContext> dbContextOptionsBuilder = new DbContextOptionsBuilder<TContext>(new DbContextOptions<TContext>(new Dictionary<Type, IDbContextOptionsExtension>()));
DbContextOptionsBuilder<TContext> dbContextOptionsBuilder = new DbContextOptionsBuilder<TContext>(
new DbContextOptions<TContext>(new Dictionary<Type, IDbContextOptionsExtension>()));
dbContextOptionsBuilder.UseApplicationServiceProvider(applicationServiceProvider);
optionsAction?.Invoke(applicationServiceProvider, dbContextOptionsBuilder);
return dbContextOptionsBuilder.Options;
Expand Down

0 comments on commit 2f17838

Please sign in to comment.