diff --git a/src/Extensions/EFCore.DbContextFactory/Extensions/ServiceCollectionExtensions.cs b/src/Extensions/EFCore.DbContextFactory/Extensions/ServiceCollectionExtensions.cs index d24fac4..8a833c7 100644 --- a/src/Extensions/EFCore.DbContextFactory/Extensions/ServiceCollectionExtensions.cs +++ b/src/Extensions/EFCore.DbContextFactory/Extensions/ServiceCollectionExtensions.cs @@ -23,6 +23,21 @@ public static class ServiceCollectionExtensions /// The implementation. public static void AddSqlServerDbContextFactory(this IServiceCollection services, string nameOrConnectionString = null, ILoggerFactory logger = null) where TDataContext : DbContext + { + services.AddSqlServerDbContextFactory(nameOrConnectionString, logger); + } + + /// + /// Configures the resolution of 's factory. + /// + /// The DbContext service type. + /// The DbContent implementation type. + /// + /// Name or connection string of the context. (Optional) + /// The implementation. + public static void AddSqlServerDbContextFactory(this IServiceCollection services, string nameOrConnectionString = null, ILoggerFactory logger = null) + where TContextService : class + where TContextImplementation : DbContext, TContextService { if (string.IsNullOrEmpty(nameOrConnectionString)) { @@ -31,7 +46,7 @@ public static void AddSqlServerDbContextFactory(this IServiceColle nameOrConnectionString = configuration.GetConnectionString("DefaultConnection"); } - AddDbContextFactory(services, (provider, builder) => + AddDbContextFactory(services, (provider, builder) => builder.UseSqlServer(nameOrConnectionString) .UseLoggerFactory(logger) ); @@ -46,7 +61,20 @@ public static void AddSqlServerDbContextFactory(this IServiceColle public static void AddDbContextFactory(this IServiceCollection services, Action options) where TDataContext : DbContext - => AddDbContextFactory(services, (provider, builder) => options.Invoke(builder)); + => AddDbContextFactory(services, (provider, builder) => options.Invoke(builder)); + + /// + /// Configures the resolution of 's factory. + /// + /// The DbContext service type. + /// The DbContext implementation type. + /// + /// The DbContext options. + public static void AddDbContextFactory(this IServiceCollection services, + Action options) + where TContextService : class + where TContextImplementation : DbContext, TContextService + => AddDbContextFactory(services, (provider, builder) => options.Invoke(builder)); /// /// Configures the resolution of 's factory. @@ -57,18 +85,33 @@ public static void AddDbContextFactory(this IServiceCollection ser public static void AddDbContextFactory(this IServiceCollection services, Action optionsAction) where TDataContext : DbContext { - AddCoreServices(services, optionsAction, ServiceLifetime.Scoped); + services.AddDbContextFactory(optionsAction); + } + + /// + /// Configures the resolution of 's factory. + /// + /// The DbContext service type. + /// The DbContext implementation type. + /// + /// Service provider and DbContext options. + public static void AddDbContextFactory(this IServiceCollection services, Action optionsAction) + where TContextService : class + where TContextImplementation : DbContext, TContextService + { + AddCoreServices(services, optionsAction, ServiceLifetime.Scoped); var serviceProvider = services.BuildServiceProvider(); - var options = serviceProvider.GetService>(); + var options = serviceProvider.GetService>(); - services.AddScoped>(ctx => () => (TDataContext)Activator.CreateInstance(typeof(TDataContext), options)); + services.AddScoped>(ctx => () => (TContextService)Activator.CreateInstance(typeof(TContextImplementation), options)); } - private static void AddCoreServices( + private static void AddCoreServices( IServiceCollection serviceCollection, Action optionsAction, ServiceLifetime optionsLifetime) - where TContextImplementation : DbContext + where TContextService : class + where TContextImplementation : DbContext, TContextService { serviceCollection .AddMemoryCache()