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 scoped application services to be accessed from the context #9009

Merged
merged 1 commit into from Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 36 additions & 22 deletions src/EFCore/EntityFrameworkServiceCollectionExtensions.cs
Expand Up @@ -54,19 +54,21 @@ public static class EntityFrameworkServiceCollectionExtensions
/// </para>
/// </param>
/// <param name="contextLifetime"> The lifetime with which to register the DbContext service in the container. </param>
/// <param name="optionsLifetime"> The lifetime with which to register the DbContextOptions service in the container. </param>
/// <returns>
/// The same service collection so that multiple calls can be chained.
/// </returns>
public static IServiceCollection AddDbContext<TContext>(
[NotNull] this IServiceCollection serviceCollection,
[CanBeNull] Action<DbContextOptionsBuilder> optionsAction = null,
ServiceLifetime contextLifetime = ServiceLifetime.Scoped)
ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
ServiceLifetime optionsLifetime = ServiceLifetime.Scoped)
where TContext : DbContext
=> AddDbContext<TContext>(
serviceCollection,
optionsAction == null
? (Action<IServiceProvider, DbContextOptionsBuilder>)null
: (p, b) => optionsAction.Invoke(b), contextLifetime);
: (p, b) => optionsAction.Invoke(b), contextLifetime, optionsLifetime);

/// <summary>
/// Registers the given context as a service in the <see cref="IServiceCollection" /> and enables DbContext pooling.
Expand Down Expand Up @@ -149,7 +151,8 @@ public static class EntityFrameworkServiceCollectionExtensions

CheckContextConstructors<TContext>();

AddCoreServices<TContext>(serviceCollection,
AddCoreServices<TContext>(
serviceCollection,
(sp, ob) =>
{
optionsAction(sp, ob);
Expand All @@ -158,19 +161,12 @@ public static class EntityFrameworkServiceCollectionExtensions
.WithMaxPoolSize(poolSize);

((IDbContextOptionsBuilderInfrastructure)ob).AddOrUpdateExtension(extension);
});

serviceCollection.TryAdd(
new ServiceDescriptor(
typeof(DbContextPool<TContext>),
typeof(DbContextPool<TContext>),
ServiceLifetime.Singleton));
},
ServiceLifetime.Singleton,
ServiceLifetime.Singleton);

serviceCollection.Add(
new ServiceDescriptor(
typeof(TContext),
_ => _.GetService<DbContextPool<TContext>>().Rent(),
ServiceLifetime.Scoped));
serviceCollection.TryAddSingleton<DbContextPool<TContext>>();
serviceCollection.AddScoped(p => p.GetService<DbContextPool<TContext>>().Rent());

return serviceCollection;
}
Expand All @@ -193,14 +189,16 @@ public static class EntityFrameworkServiceCollectionExtensions
/// <typeparam name="TContext"> The type of context to be registered. </typeparam>
/// <param name="serviceCollection"> The <see cref="IServiceCollection" /> to add services to. </param>
/// <param name="contextLifetime"> The lifetime with which to register the DbContext service in the container. </param>
/// <param name="optionsLifetime"> The lifetime with which to register the DbContextOptions service in the container. </param>
/// <returns>
/// The same service collection so that multiple calls can be chained.
/// </returns>
public static IServiceCollection AddDbContext<TContext>(
[NotNull] this IServiceCollection serviceCollection,
ServiceLifetime contextLifetime)
ServiceLifetime contextLifetime,
ServiceLifetime optionsLifetime = ServiceLifetime.Scoped)
where TContext : DbContext
=> AddDbContext<TContext>(serviceCollection, (Action<IServiceProvider, DbContextOptionsBuilder>)null, contextLifetime);
=> AddDbContext<TContext>(serviceCollection, (Action<IServiceProvider, DbContextOptionsBuilder>)null, contextLifetime, optionsLifetime);

/// <summary>
/// <para>
Expand Down Expand Up @@ -250,23 +248,30 @@ public static class EntityFrameworkServiceCollectionExtensions
/// </para>
/// </param>
/// <param name="contextLifetime"> The lifetime with which to register the DbContext service in the container. </param>
/// <param name="optionsLifetime"> The lifetime with which to register the DbContextOptions service in the container. </param>
/// <returns>
/// The same service collection so that multiple calls can be chained.
/// </returns>
public static IServiceCollection AddDbContext<TContext>(
[NotNull] this IServiceCollection serviceCollection,
[CanBeNull] Action<IServiceProvider, DbContextOptionsBuilder> optionsAction,
ServiceLifetime contextLifetime = ServiceLifetime.Scoped)
ServiceLifetime contextLifetime = ServiceLifetime.Scoped,
ServiceLifetime optionsLifetime = ServiceLifetime.Scoped)
where TContext : DbContext
{
Check.NotNull(serviceCollection, nameof(serviceCollection));

if (contextLifetime == ServiceLifetime.Singleton)
{
optionsLifetime = ServiceLifetime.Singleton;
}

if (optionsAction != null)
{
CheckContextConstructors<TContext>();
}

AddCoreServices<TContext>(serviceCollection, optionsAction);
AddCoreServices<TContext>(serviceCollection, optionsAction, contextLifetime, optionsLifetime);

serviceCollection.TryAdd(new ServiceDescriptor(typeof(TContext), typeof(TContext), contextLifetime));

Expand All @@ -275,15 +280,24 @@ public static class EntityFrameworkServiceCollectionExtensions

private static void AddCoreServices<TContext>(
IServiceCollection serviceCollection,
Action<IServiceProvider, DbContextOptionsBuilder> optionsAction)
Action<IServiceProvider, DbContextOptionsBuilder> optionsAction,
ServiceLifetime contextLifetime,
ServiceLifetime optionsLifetime)
where TContext : DbContext
{
serviceCollection
.AddMemoryCache()
.AddLogging();

serviceCollection.TryAddSingleton(p => DbContextOptionsFactory<TContext>(p, optionsAction));
serviceCollection.AddSingleton<DbContextOptions>(p => p.GetRequiredService<DbContextOptions<TContext>>());
serviceCollection.TryAdd(new ServiceDescriptor(
typeof(DbContextOptions<TContext>),
p => DbContextOptionsFactory<TContext>(p, optionsAction),
optionsLifetime));

serviceCollection.Add(new ServiceDescriptor(
typeof(DbContextOptions),
p => p.GetRequiredService<DbContextOptions<TContext>>(),
optionsLifetime));
}

private static DbContextOptions<TContext> DbContextOptionsFactory<TContext>(
Expand Down