Skip to content
Closed
Show file tree
Hide file tree
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 @@ -307,7 +307,7 @@ public static IServiceCollection AddDbContextPool<TContextService, TContextImple
AddPoolingOptions<TContextImplementation>(serviceCollection, optionsAction, poolSize);

serviceCollection.TryAddSingleton<IDbContextPool<TContextImplementation>, DbContextPool<TContextImplementation>>();
serviceCollection.AddScoped<IScopedDbContextLease<TContextImplementation>, ScopedDbContextLease<TContextImplementation>>();
serviceCollection.AddScoped<IScopedDbContextLease<TContextImplementation>, PoolingScopedDbContextLease<TContextImplementation>>();

serviceCollection.AddScoped<TContextService>(
sp => sp.GetRequiredService<IScopedDbContextLease<TContextImplementation>>().Context);
Expand Down Expand Up @@ -369,6 +369,13 @@ public static IServiceCollection AddDbContext<TContext>(
where TContext : DbContext
=> AddDbContext<TContext, TContext>(serviceCollection, contextLifetime, optionsLifetime);

// public static IServiceCollection AddDbContext<TContext>(
// this IServiceCollection serviceCollection,
// ServiceLifetime contextLifetime,
// ServiceLifetime optionsLifetime = ServiceLifetime.Scoped)
// where TContext : DbContext
// => AddDbContext<TContext, TContext>(serviceCollection, contextLifetime, optionsLifetime);

/// <summary>
/// <para>
/// Registers the given context as a service in the <see cref="IServiceCollection" />.
Expand Down Expand Up @@ -780,6 +787,9 @@ public static IServiceCollection AddDbContextFactory<TContext, TFactory>(
typeof(TFactory),
lifetime));

serviceCollection.AddScoped<IScopedDbContextLease<TContext>, ScopedDbContextLease<TContext>>();
serviceCollection.AddScoped(sp => sp.GetRequiredService<IScopedDbContextLease<TContext>>().Context);

return serviceCollection;
}

Expand Down
56 changes: 56 additions & 0 deletions src/EFCore/Internal/PoolingScopedDbContextLease.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading.Tasks;

namespace Microsoft.EntityFrameworkCore.Internal
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public sealed class PoolingScopedDbContextLease<TContext> : IScopedDbContextLease<TContext>, IDisposable, IAsyncDisposable
where TContext : DbContext
{
private DbContextLease _lease;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public PoolingScopedDbContextLease(IDbContextPool<TContext> contextPool)
=> _lease = new DbContextLease(contextPool, standalone: false);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public TContext Context
=> (TContext)_lease.Context;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
void IDisposable.Dispose()
=> _lease.Release();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
ValueTask IAsyncDisposable.DisposeAsync()
=> _lease.ReleaseAsync();
}
}
13 changes: 7 additions & 6 deletions src/EFCore/Internal/ScopedDbContextLease.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ namespace Microsoft.EntityFrameworkCore.Internal
public sealed class ScopedDbContextLease<TContext> : IScopedDbContextLease<TContext>, IDisposable, IAsyncDisposable
where TContext : DbContext
{
private DbContextLease _lease;
private readonly IDbContextFactory<TContext> _factory;
private TContext? _context;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public ScopedDbContextLease(IDbContextPool<TContext> contextPool)
=> _lease = new DbContextLease(contextPool, standalone: false);
public ScopedDbContextLease(IDbContextFactory<TContext> factory)
=> _factory = factory;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -33,7 +34,7 @@ public ScopedDbContextLease(IDbContextPool<TContext> contextPool)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public TContext Context
=> (TContext)_lease.Context;
=> _context = _factory.CreateDbContext();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -42,7 +43,7 @@ public TContext Context
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
void IDisposable.Dispose()
=> _lease.Release();
=> _context!.Dispose();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -51,6 +52,6 @@ void IDisposable.Dispose()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
ValueTask IAsyncDisposable.DisposeAsync()
=> _lease.ReleaseAsync();
=> _context!.DisposeAsync();
}
}