Beispiel #1
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="serviceProvider"></param>
        public EFCoreShardingBootstrapper(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            _shardingOptions = serviceProvider.GetService <IOptions <EFCoreShardingOptions> >().Value;

            Cache.ServiceProvider = serviceProvider;
        }
 public LogicDeleteDbAccessor(IDbAccessor db, EFCoreShardingOptions shardingOptions)
 {
     FullDbAccessor = db;
     _logicDelete   = shardingOptions.LogicDelete;
     _deletedField  = shardingOptions.DeletedField;
     _keyField      = shardingOptions.KeyField;
 }
        public static EFCoreShardingOptions BuildOption(this IOptionsMonitor <EFCoreShardingOptions> optionsSnapshot, string optionName)
        {
            if (optionName.IsNullOrEmpty())
            {
                return(optionsSnapshot.CurrentValue);
            }
            else
            {
                var selfOption    = optionsSnapshot.Get(optionName).DeepClone();
                var defaultOption = new EFCoreShardingOptions();
                var globalOption  = optionsSnapshot.CurrentValue;

                foreach (var aProperty in typeof(EFCoreShardingOptions).GetProperties())
                {
                    var selfValue    = aProperty.GetValue(selfOption);
                    var defaultValue = aProperty.GetValue(defaultOption);
                    var globalValue  = aProperty.GetValue(globalOption);

                    var value = Equals(selfValue, defaultValue) ? globalValue : selfValue;
                    aProperty.SetValue(selfOption, value);
                }

                return(selfOption);
            }
        }
        public IDbAccessor GetDbAccessor(DbContextParamters dbContextParamters, string optionName = null)
        {
            EFCoreShardingOptions eFCoreShardingOptions = _optionsMonitor.BuildOption(optionName);

            var dbContext = GetDbContext(dbContextParamters, eFCoreShardingOptions);

            return(GetProvider(dbContextParamters.DbType).GetDbAccessor(dbContext));
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="contextOptions"></param>
        /// <param name="paramter"></param>
        /// <param name="shardingOptions"></param>
        public GenericDbContext(DbContextOptions contextOptions, DbContextParamters paramter, EFCoreShardingOptions shardingOptions)
            : base(contextOptions)
        {
            DbContextOption = contextOptions;
            Paramter        = paramter;
            ShardingOption  = shardingOptions;

            Database.SetCommandTimeout(ShardingOption.CommandTimeout);
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="serviceProvider"></param>
        public EFCoreShardingBootstrapper(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            _shardingOptions = serviceProvider.GetService <IOptions <EFCoreShardingOptions> >().Value;

            DiagnosticListener.AllListeners.Subscribe(
                new DiagnosticObserver(serviceProvider.GetService <ILoggerFactory>(),
                                       _shardingOptions.MinCommandElapsedMilliseconds));

            Cache.ServiceProvider = serviceProvider;
        }
        public GenericDbContext GetDbContext(DbContextParamters dbContextParamters, EFCoreShardingOptions eFCoreShardingOptions)
        {
            if (eFCoreShardingOptions == null)
            {
                eFCoreShardingOptions = _optionsMonitor.BuildOption(null);
            }

            AbstractProvider provider = GetProvider(dbContextParamters.DbType);

            DbConnection dbConnection = provider.GetDbConnection();

            dbConnection.ConnectionString = dbContextParamters.ConnectionString;

            DbContextOptionsBuilder builder = new DbContextOptionsBuilder();

            builder.UseLoggerFactory(_loggerFactory);

            provider.UseDatabase(builder, dbConnection);
            builder.ReplaceService <IModelCacheKeyFactory, GenericModelCacheKeyFactory>();
            builder.ReplaceService <IMigrationsModelDiffer, ShardingMigration>();

            return(new GenericDbContext(builder.Options, dbContextParamters, eFCoreShardingOptions));
        }
Beispiel #8
0
 public DbFactory(ILoggerFactory loggerFactory, IOptions <EFCoreShardingOptions> shardingOptions)
 {
     _loggerFactory   = loggerFactory;
     _shardingOptions = shardingOptions.Value;
 }