protected ShardingDbContext(ContextScope scope, DbTransactionContext dbTran) : base(dbTran.Connection)
        {
            Scope = scope;
            UseTransaction(dbTran); //设置Scope后才可调用

            CheckTableCreate(dbTran.ConnectionString);
        }
Example #2
0
        /// <summary>
        /// 获取或新建实例
        /// </summary>
        private T GetDb(ContextScope scope)
        {
            var type    = typeof(T);
            var typeKey = type.Namespace + type.Name + scope.TablePostfix;

            return(GetComponent(() => Create(scope), typeKey));
        }
Example #3
0
        /// <summary>
        /// 创建新的Db实例
        /// </summary>
        private T Create(ContextScope scope)
        {
            if (scope.ScopeType != _dbConfig.ScopeType)
            {
                throw new Exception($"{typeof(T).Name} ShardScope is {_dbConfig.ScopeType}, Can't use {scope.ScopeType}!");
            }

            if (_dbConfig.DbConstructor.NewDb != null)
            {
                return(_dbConfig.DbConstructor.NewDb(scope));
            }

            //反射创建
            return(Activator.CreateInstance(typeof(T), scope) as T);
        }
        /*/// <summary>
         * /// 创建DbCreater(主要用于子类调用,以确保静态构造函数被调用-RegDb)
         * /// </summary>
         * protected static ShardingDbCreater<T> Creater<T>(BaseServiceComponent service) where T : ShardingDbContext
         * {
         *  return new ShardingDbCreater<T>(service);
         * }*/

        #endregion

        #region Ctor

        //执行顺序Ctor、OnConfiguring、OnModelCreating

        protected ShardingDbContext(ContextScope scope, string connectionString) : base(connectionString)
        {
            Scope = scope;
            CheckTableCreate(connectionString);
        }
Example #5
0
 /// <summary>
 /// DateTime.Today 在当前ScopeType下的ContextScope。
 /// </summary>
 public static ContextScope Today(this ShardScopeType scopeType)
 {
     return(ContextScope.Create(scopeType, DateTime.Today));
 }
Example #6
0
 /// <summary>
 /// 按Scope获取ShardingDb
 /// </summary>
 public T By(ContextScope scope)
 {
     return(GetDb(scope));
 }
Example #7
0
 /// <summary>
 /// 按时间范围创建Scope,范围错误时,可能为空。
 /// </summary>
 public List <ContextScope> Scope(DateTime start, DateTime end, bool avoidFutureTime = true)
 {
     return(ContextScope.Create(_dbConfig.ScopeType, start, end, avoidFutureTime));
     //if (scopes.IsNullOrEmpty()) scopes = scopes.NullableAdd(Scope(start));
 }
Example #8
0
 /// <summary>
 /// 按时间创建Scope
 /// </summary>
 public ContextScope Scope(DateTime date)
 {
     return(ContextScope.Create(_dbConfig.ScopeType, date));
 }