-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #24137
- Loading branch information
Showing
7 changed files
with
198 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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 Microsoft.EntityFrameworkCore.Internal; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Infrastructure | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// A factory returning pooled <see cref="DbContext" /> instances. Disposing the instance returned by this factory returns | ||
/// them to the internal pooling mechanism. | ||
/// </para> | ||
/// <para> | ||
/// A service of this type is registered in the dependency injection container by the | ||
/// <see cref="M:EntityFrameworkServiceCollectionExtensions.AddDbContextPool" /> methods. | ||
/// </para> | ||
/// </summary> | ||
public class PooledDbContextFactory<TContext> : IDbContextFactory<TContext> | ||
where TContext : DbContext | ||
{ | ||
private readonly IDbContextPool<TContext> _pool; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PooledDbContextFactory{TContext}" /> class. | ||
/// The maximum number of instances retained by the pool defaults to 128. | ||
/// </summary> | ||
/// <param name="options"> The options to use for contexts produced by this factory. </param> | ||
public PooledDbContextFactory(DbContextOptions<TContext> options) | ||
=> _pool = new DbContextPool<TContext>(options); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PooledDbContextFactory{TContext}" /> class. | ||
/// </summary> | ||
/// <param name="options"> The options to use for contexts produced by this factory. </param> | ||
/// <param name="poolSize"> Sets the maximum number of instances retained by the pool. </param> | ||
public PooledDbContextFactory(DbContextOptions<TContext> options, int poolSize) | ||
{ | ||
var optionsBuilder = new DbContextOptionsBuilder<TContext>(options); | ||
|
||
var extension = (options.FindExtension<CoreOptionsExtension>() ?? new CoreOptionsExtension()) | ||
.WithMaxPoolSize(poolSize); | ||
|
||
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension); | ||
|
||
_pool = new DbContextPool<TContext>(optionsBuilder.Options); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual TContext CreateDbContext() | ||
=> (TContext)new DbContextLease(_pool, standalone: true).Context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.