internal static List <ContextScope> Create(ShardScopeType scopeType, DateTime start, DateTime end, bool avoidFutureTime) { var scopes = new List <ContextScope>(); //时间检查,防止创建太多分表 var tmpEnd = end; if (avoidFutureTime) { var now = DateTime.Now; tmpEnd = end > now ? now : end; //避免未来时间 } else { var shardEnd = MaxShardEndDate; //分表时间上限 if (tmpEnd > shardEnd) { tmpEnd = shardEnd; } } //时间倒序查找 while (tmpEnd >= start) { var scp = Create(scopeType, tmpEnd); scopes.Add(scp); tmpEnd = scp.PrevScopeEnd; } return(scopes); }
internal static ContextScope Create(ShardScopeType scopeType, DateTime date) { switch (scopeType) { case ShardScopeType.Quarter: return(new QuarterScope(date)); case ShardScopeType.Year: return(new YearScope(date)); default: throw new ArgumentOutOfRangeException(nameof(scopeType), scopeType, "未实现ScopeType的创建"); } }
private static readonly Dictionary <string, ShardingDbConfig> DbTypeConfigs = new Dictionary <string, ShardingDbConfig>(); //派生DbContext的配置信息 /// <summary> /// 注册Db类型,并设置Db类型信息(设置后可使用ShardingDbCreater) /// </summary> protected static void RegDb <T>(ShardScopeType scopeType, Func <ContextScope, T> funCreate, Func <ContextScope, DbTransactionContext, T> createInTran = null) where T : ShardingDbContext { //add new config var dbType = typeof(T); var conf = new ShardingDbTypeDefine <T>(scopeType) { DbConstructor = new ShardDbConstructor <T> { NewDb = funCreate, NewDbInTran = createInTran } }; DbTypeConfigs.Add(dbType.Name, conf); #region Reflect table entity var dbSetType = typeof(DbSet <>); foreach (var prop in dbType.GetProperties()) { var propType = prop.PropertyType; if (!propType.IsGenericType || propType.GetGenericTypeDefinition() != dbSetType) { continue; } var entType = propType.GetGenericArguments()[0]; var shardAtt = entType.GetCustomAttribute <ShardingTableAttribute>(); if (shardAtt == null) { continue; } //分表的Entity conf.ShardEntitys.Add(new EntityTypeSet { EntityType = entType, TableBaseName = shardAtt.Name }); } #endregion }
public ShardingDbTypeDefine(ShardScopeType scopeType) { ScopeType = scopeType; }
/// <summary> /// DateTime.Today 在当前ScopeType下的ContextScope。 /// </summary> public static ContextScope Today(this ShardScopeType scopeType) { return(ContextScope.Create(scopeType, DateTime.Today)); }