private async Task <List <TResult> > GetStatisDataAsync <TResult>(Func <IQueryable, Task <TResult> > access, IQueryable newSource = null)
        {
            newSource = newSource ?? _source;
            var tables = _shardingConfig.GetReadTables(_source);

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

            tasks = tables.Select(aTable =>
            {
                IDbAccessor db;
                if (_shardingDb.OpenedTransaction)
                {
                    db = _shardingDb.GetMapDbAccessor(aTable.conString, aTable.dbType, aTable.suffix);
                }
                else
                {
                    db = _dbFactory.GetDbAccessor(aTable.conString, aTable.dbType, null, aTable.suffix);
                }

                dbs.Add(db);
                var targetIQ = db.GetIQueryable <T>();
                var newQ     = newSource.ReplaceQueryable(targetIQ);

                return(access(newQ));
            }).ToList();
            var res = (await Task.WhenAll(tasks)).ToList();

            if (!_shardingDb.OpenedTransaction)
            {
                dbs.ForEach(x => x.Dispose());
            }

            return(res);
        }
        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 void Dispose()
 {
     _objList.ForEach(x =>
     {
         try
         {
             x.Dispose();
         }
         catch
         {
         }
     });
     _objList.Clear();
 }
Example #4
0
        public void Dispose()
        {
            _objList.ForEach(x =>
            {
                try
                {
                    x.Dispose();
                }
                catch (Exception ex)
                {
                    throw ex.InnerException;
                }
            });

            _objList.Dispose();
            _objList = null;
        }
        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.GetReadTables(_source);
            SynchronizedCollection <IDbAccessor> dbs   = new SynchronizedCollection <IDbAccessor>();
            List <Task <List <T> > >             tasks = tables.Select(aTable =>
            {
                IDbAccessor db;
                if (_shardingDb.OpenedTransaction)
                {
                    db = _shardingDb.GetMapDbAccessor(aTable.conString, aTable.dbType, aTable.suffix);
                }
                else
                {
                    db = _dbFactory.GetDbAccessor(aTable.conString, aTable.dbType, null, aTable.suffix);
                }
                dbs.Add(db);

                var targetIQ = db.GetIQueryable <T>();
                var newQ     = noPaginSource.ReplaceQueryable(targetIQ);
                return(newQ
                       .Cast <object>()
                       .Select(x => (T)x)
                       .ToListAsync());
            }).ToList();
            List <T> all = new List <T>();

            (await Task.WhenAll(tasks.ToArray())).ToList().ForEach(x => all.AddRange(x));

            if (!_shardingDb.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);
        }
        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);
        }