public IConfigInit UseDatabase <TRepository>(string conString, DatabaseType dbType) where TRepository : class, IRepository { if (ShardingConfig.ServiceDescriptors != null) { ShardingConfig.ServiceDescriptors.AddScoped(_ => { IRepository repository = DbFactory.GetRepository(conString, dbType); if (ShardingConfig.LogicDelete) { repository = new LogicDeleteRepository(repository); } if (typeof(TRepository) == typeof(IRepository)) { return((TRepository)repository); } else { return(repository.ActLike <TRepository>()); } }); } return(this); }
private async Task <List <TResult> > GetStatisDataAsync <TResult>(Func <IQueryable, Task <TResult> > access, IQueryable newSource = null) { newSource = newSource ?? _source; var tables = ShardingConfig.ConfigProvider.GetReadTables(_absTableName, _absDbName); List <Task <TResult> > tasks = new List <Task <TResult> >(); SynchronizedCollection <IRepository> dbs = new SynchronizedCollection <IRepository>(); tasks = tables.Select(aTable => { IRepository db; if (_repository.OpenedTransaction) { db = _repository.GetMapRepository(aTable.conString, aTable.dbType); } else { db = DbFactory.GetRepository(aTable.conString, aTable.dbType); } dbs.Add(db); var targetTable = MapTable(aTable.tableName); var targetIQ = db.GetIQueryable(targetTable); var newQ = newSource.ChangeSource(targetIQ); return(access(newQ)); }).ToList(); var res = (await Task.WhenAll(tasks)).ToList(); if (!_repository.OpenedTransaction) { dbs.ForEach(x => x.Dispose()); } return(res); }
public IConfigInit UseDatabase<TRepository>(string conString, DatabaseType dbType) where TRepository : class, IRepository { if (ShardingConfig.ServiceDescriptors != null) { ShardingConfig.ServiceDescriptors.AddScoped(_ => { ILoggerFactory loggerFactory = _.GetService<ILoggerFactory>(); IRepository repository = DbFactory.GetRepository(conString, dbType, loggerFactory); if (ShardingConfig.LogicDelete) repository = new LogicDeleteRepository(repository); if (typeof(TRepository) == typeof(IRepository)) return (TRepository)repository; else return repository.ActLike<TRepository>(); }); } return this; }
public async Task <List <T> > ToListAsync() { //去除分页,获取前Take+Skip数量 int?take = _source.GetTakeCount(); int?skip = _source.GetSkipCount(); skip = skip == null ? 0 : skip; var(sortColumn, sortType) = _source.GetOrderBy(); var noPaginSource = _source.RemoveTake().RemoveSkip(); if (!take.IsNullOrEmpty()) { noPaginSource = noPaginSource.Take(take.Value + skip.Value); } //从各个分表获取数据 var tables = ShardingConfig.ConfigProvider.GetReadTables(_absTableName, _absDbName); SynchronizedCollection <IRepository> dbs = new SynchronizedCollection <IRepository>(); List <Task <List <T> > > tasks = tables.Select(aTable => { var targetTable = MapTable(aTable.tableName); IRepository db; if (_repository.OpenedTransaction) { db = _repository.GetMapRepository(aTable.conString, aTable.dbType); } else { db = DbFactory.GetRepository(aTable.conString, aTable.dbType); } dbs.Add(db); var targetIQ = db.GetIQueryable(targetTable); var newQ = noPaginSource.ChangeSource(targetIQ); return(newQ .Cast <object>() .Select(x => x.ChangeType <T>()) .ToListAsync()); }).ToList(); List <T> all = new List <T>(); (await Task.WhenAll(tasks.ToArray())).ToList().ForEach(x => all.AddRange(x)); if (!_repository.OpenedTransaction) { dbs.ForEach(x => x.Dispose()); } //合并数据 var resList = all; if (!sortColumn.IsNullOrEmpty() && !sortType.IsNullOrEmpty()) { resList = resList.AsQueryable().OrderBy($"{sortColumn} {sortType}").ToList(); } if (!skip.IsNullOrEmpty()) { resList = resList.Skip(skip.Value).ToList(); } if (!take.IsNullOrEmpty()) { resList = resList.Take(take.Value).ToList(); } return(resList); }