public IConfigInit UseDatabase <TDbAccessor>(string conString, DatabaseType dbType) where TDbAccessor : class, IDbAccessor
        {
            if (ShardingConfig.ServiceDescriptors != null)
            {
                ShardingConfig.ServiceDescriptors.AddScoped(_ =>
                {
                    ILoggerFactory loggerFactory = _.GetService <ILoggerFactory>();

                    IDbAccessor repository = DbFactory.GetDbAccessor(conString, dbType, loggerFactory);
                    if (ShardingConfig.LogicDelete)
                    {
                        repository = new LogicDeleteDbAccessor(repository);
                    }

                    if (typeof(TDbAccessor) == typeof(IDbAccessor))
                    {
                        return((TDbAccessor)repository);
                    }
                    else
                    {
                        return(repository.ActLike <TDbAccessor>());
                    }
                });
            }

            return(this);
        }
Ejemplo n.º 2
0
        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, _source);

            List <Task <TResult> > tasks             = new List <Task <TResult> >();
            SynchronizedCollection <IDbAccessor> dbs = new SynchronizedCollection <IDbAccessor>();

            tasks = tables.Select(aTable =>
            {
                IDbAccessor db;
                if (_repository.OpenedTransaction)
                {
                    db = _repository.GetMapDbAccessor(aTable.conString, aTable.dbType);
                }
                else
                {
                    db = DbFactory.GetDbAccessor(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);
        }
Ejemplo n.º 3
0
        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, _source);
            SynchronizedCollection <IDbAccessor> dbs   = new SynchronizedCollection <IDbAccessor>();
            List <Task <List <T> > >             tasks = tables.Select(aTable =>
            {
                var targetTable = MapTable(aTable.tableName);
                IDbAccessor db;
                if (_repository.OpenedTransaction)
                {
                    db = _repository.GetMapDbAccessor(aTable.conString, aTable.dbType);
                }
                else
                {
                    db = DbFactory.GetDbAccessor(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);
        }