namespace Benchmarks.Model; using System.Collections.Concurrent; using Microsoft.EntityFrameworkCore; public sealed class DatabaseContextPool where TContext : DbContext { private readonly ConcurrentBag _pool = new(); private readonly Func _factory; private readonly int _maxSize; public DatabaseContextPool(Func factory, int maxSize) { _factory = factory; _maxSize = maxSize; } public TContext Rent() { if (_pool.TryTake(out var ctx)) { ctx.ChangeTracker.Clear(); return ctx; } return _factory(); } public void Return(TContext context) { if (_pool.Count >= _maxSize) { context.Dispose(); return; } context.ChangeTracker.Clear(); _pool.Add(context); } }