Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a DbContext to have a derived service implementation registered #28

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ public static class ServiceCollectionExtensions
/// <param name="logger">The <see cref="ILoggerFactory"/>implementation.</param>
public static void AddSqlServerDbContextFactory<TDataContext>(this IServiceCollection services, string nameOrConnectionString = null, ILoggerFactory logger = null)
where TDataContext : DbContext
{
services.AddSqlServerDbContextFactory<TDataContext, TDataContext>(nameOrConnectionString, logger);
}

/// <summary>
/// Configures the resolution of <typeparamref name="TContextService"/>'s factory.
/// </summary>
/// <typeparam name="TContextService">The DbContext service type.</typeparam>
/// <typeparam name="TContextImplementation">The DbContent implementation type.</typeparam>
/// <param name="services"></param>
/// <param name="nameOrConnectionString">Name or connection string of the context. (Optional)</param>
/// <param name="logger">The <see cref="ILoggerFactory"/>implementation.</param>
public static void AddSqlServerDbContextFactory<TContextService, TContextImplementation>(this IServiceCollection services, string nameOrConnectionString = null, ILoggerFactory logger = null)
where TContextService : class
where TContextImplementation : DbContext, TContextService
{
if (string.IsNullOrEmpty(nameOrConnectionString))
{
Expand All @@ -31,7 +46,7 @@ public static void AddSqlServerDbContextFactory<TDataContext>(this IServiceColle
nameOrConnectionString = configuration.GetConnectionString("DefaultConnection");
}

AddDbContextFactory<TDataContext>(services, (provider, builder) =>
AddDbContextFactory<TContextService, TContextImplementation>(services, (provider, builder) =>
builder.UseSqlServer(nameOrConnectionString)
.UseLoggerFactory(logger)
);
Expand All @@ -46,7 +61,20 @@ public static void AddSqlServerDbContextFactory<TDataContext>(this IServiceColle
public static void AddDbContextFactory<TDataContext>(this IServiceCollection services,
Action<DbContextOptionsBuilder> options)
where TDataContext : DbContext
=> AddDbContextFactory<TDataContext>(services, (provider, builder) => options.Invoke(builder));
=> AddDbContextFactory<TDataContext, TDataContext>(services, (provider, builder) => options.Invoke(builder));

/// <summary>
/// Configures the resolution of <typeparamref name="TContextService"/>'s factory.
/// </summary>
/// <typeparam name="TContextService">The DbContext service type.</typeparam>
/// <typeparam name="TContextImplementation">The DbContext implementation type.</typeparam>
/// <param name="services"></param>
/// <param name="options">The DbContext options.</param>
public static void AddDbContextFactory<TContextService, TContextImplementation>(this IServiceCollection services,
Action<DbContextOptionsBuilder> options)
where TContextService : class
where TContextImplementation : DbContext, TContextService
=> AddDbContextFactory<TContextService, TContextImplementation>(services, (provider, builder) => options.Invoke(builder));

/// <summary>
/// Configures the resolution of <typeparamref name="TDataContext"/>'s factory.
Expand All @@ -57,18 +85,33 @@ public static void AddDbContextFactory<TDataContext>(this IServiceCollection ser
public static void AddDbContextFactory<TDataContext>(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction)
where TDataContext : DbContext
{
AddCoreServices<TDataContext>(services, optionsAction, ServiceLifetime.Scoped);
services.AddDbContextFactory<TDataContext, TDataContext>(optionsAction);
}

/// <summary>
/// Configures the resolution of <typeparamref name="TContextService"/>'s factory.
/// </summary>
/// <typeparam name="TContextService">The DbContext service type.</typeparam>
/// <typeparam name="TContextImplementation">The DbContext implementation type.</typeparam>
/// <param name="services"></param>
/// <param name="optionsAction">Service provider and DbContext options.</param>
public static void AddDbContextFactory<TContextService, TContextImplementation>(this IServiceCollection services, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction)
where TContextService : class
where TContextImplementation : DbContext, TContextService
{
AddCoreServices<TContextService, TContextImplementation>(services, optionsAction, ServiceLifetime.Scoped);
var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetService<DbContextOptions<TDataContext>>();
var options = serviceProvider.GetService<DbContextOptions<TContextImplementation>>();

services.AddScoped<Func<TDataContext>>(ctx => () => (TDataContext)Activator.CreateInstance(typeof(TDataContext), options));
services.AddScoped<Func<TContextService>>(ctx => () => (TContextService)Activator.CreateInstance(typeof(TContextImplementation), options));
}

private static void AddCoreServices<TContextImplementation>(
private static void AddCoreServices<TContextService, TContextImplementation>(
IServiceCollection serviceCollection,
Action<IServiceProvider, DbContextOptionsBuilder> optionsAction,
ServiceLifetime optionsLifetime)
where TContextImplementation : DbContext
where TContextService : class
where TContextImplementation : DbContext, TContextService
{
serviceCollection
.AddMemoryCache()
Expand Down