Example #1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="dbContextResolve">数据库上下文解析器</param>
 /// <param name="dbContextPool">数据库上下文池</param>
 /// <param name="repository">非泛型仓储</param>
 /// <param name="serviceProvider">服务提供器</param>
 public EFCoreRepository(
     Func <Type, IScoped, DbContext> dbContextResolve
     , IDbContextPool dbContextPool
     , IRepository repository
     , IServiceProvider serviceProvider) : base(dbContextResolve, dbContextPool, repository, serviceProvider)
 {
 }
Example #2
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContextLocator"></param>
        /// <param name="serviceProvider">服务提供器</param>
        public PrivateRepository(Type dbContextLocator, IServiceProvider serviceProvider) : base(dbContextLocator, serviceProvider)
        {
            // 初始化服务提供器
            ServiceProvider = serviceProvider;

            DbConnection  = Database.GetDbConnection();
            ChangeTracker = Context.ChangeTracker;
            Model         = Context.Model;

            // 内置多租户
            Tenant = DynamicContext.Tenant;

            // 设置提供器名称
            ProviderName = Database.ProviderName;

            //初始化实体
            Entities         = Context.Set <TEntity>();
            DetachedEntities = Entities.AsNoTracking();
            EntityType       = Entities.EntityType;

            // 初始化数据上下文池
            _dbContextPool = serviceProvider.GetService <IDbContextPool>();

            // 非泛型仓储
            _repository = serviceProvider.GetService <IRepository>();
        }
Example #3
0
        private bool Release(out IDbContextPool pool, out IDbContextPoolable context)
        {
            pool         = _contextPool;
            context      = Context;
            _contextPool = null;
            Context      = null;

            return(pool != null);
        }
    /// <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. Defaults to 1024.</param>
    public PooledDbContextFactory(DbContextOptions <TContext> options, int poolSize = DbContextPool <DbContext> .DefaultPoolSize)
    {
        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);
    }
Example #5
0
        /// <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 DbContextLease([NotNull] IDbContextPool contextPool, bool standalone)
        {
            _contextPool = contextPool;
            _standalone  = standalone;

            var context = _contextPool.Rent();

            Context = context;

            context.SetLease(this);
        }
        public override void Dispose()
        {
            if (_contextPool != null)
            {
                if (!_contextPool.Return(this))
                {
                    ((IDbContextPoolable)this).SetPool(null);
                    base.Dispose();
                }

                _contextPool = null;
            }
            else
            {
                base.Dispose();
            }
        }
        public void DbContextShouldNotBeCreatedFromPoolWhenSetUpWithoutPooling(
            ServiceCollection serviceCollection,
            [StubElsaContext] ElsaContext pooledContext,
            IDbContextPool <ElsaContext> pool)
        {
            serviceCollection
            .AddElsa(elsa =>
            {
                elsa
                .UseNonPooledEntityFrameworkPersistence((services, opts) => { opts.UseSqlite("Data Source=:memory:;Mode=Memory;"); },
                                                        ServiceLifetime.Transient);
            })
            .AddSingleton(pool);

            Mock.Get(pool).Setup(x => x.Rent()).Returns(pooledContext);

            var serviceProvider = serviceCollection.BuildServiceProvider();
            var contextFactory  = serviceProvider.GetRequiredService <IDbContextFactory <ElsaContext> >();

            using var context = contextFactory.CreateDbContext();
            Assert.NotSame(pooledContext, context);
        }
 public PooledApplicationDbContextFactory(IDbContextPool <TContextImpl> pool)
 {
     Guard.NotNull(pool, nameof(pool));
     _pool = pool;
 }
Example #9
0
 /// <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);
 /// <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);
     _lease.Context.SetLease(_lease);
 }
Example #11
0
 void IDbContextPoolable.SetPool(IDbContextPool contextPool)
 => throw new NotSupportedException(message: $"Pooling for this EF context must be avoided.{Environment.NewLine}\tContext:{this.FmtStr().GNLI2()}");
Example #12
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="dbContextPool"></param>
 public UnitOfWorkFilter(IDbContextPool dbContextPool)
 {
     _dbContextPool = dbContextPool;
 }
 /// <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 PooledDbContextFactory([NotNull] IDbContextPool <TContext> pool)
 => _pool = pool;
Example #14
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="dbContextResolve">数据库上下文解析器</param>
 /// <param name="dbContextPool">数据库上下文池</param>
 public SqlRepository(
     Func <Type, DbContext> dbContextResolve
     , IDbContextPool dbContextPool) : base(dbContextResolve, dbContextPool)
 {
 }
Example #15
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="dbContextResolve">数据库上下文解析器</param>
 /// <param name="dbContextPool">数据库上下文池</param>
 /// <param name="serviceProvider">服务提供器</param>
 public SqlRepository(
     Func <Type, DbContext> dbContextResolve
     , IDbContextPool dbContextPool
     , IServiceProvider serviceProvider) : base(dbContextResolve, dbContextPool, serviceProvider)
 {
 }
Example #16
0
 public PooledDbContextFactory(IDbContextPool <TContext> pool)
 => _pool = pool;
 public virtual void SetPool(IDbContextPool contextPool) => _contextPool = contextPool;