Skip to content

BaseRepository的类增加了其他Model的调用方法,方便不同库的操作。 #340

Description

@xcanel

IBaseRepository接口:

        //使用其他Model的方法
        Task<List<T>> Query<T>();
        Task<List<T>> Query<T>(Expression<Func<T, bool>> whereExpression);
        Task<T> QueryById<T>(object objId);
        Task<int> Add<T>(T model) where T : class, new();
        Task<int> Add<T>(List<T> listEntity) where T : class, new();
        Task<bool> Update<T>(T model) where T : class, new();
        Task<int> Update<T>(List<T> listEntity) where T : class, new();

BaseRepository类:

        private SqlSugarClient _dbBase_Other;

        private ISqlSugarClient dbOther<T>()
        {
            if (_dbBase_Other == null)
                _dbBase_Other = _dbBase;

            /* 如果要开启多库支持,
                 * 1、在appsettings.json 中开启MutiDBEnabled节点为true,必填
                 * 2、设置一个主连接的数据库ID,节点MainDB,对应的连接字符串的Enabled也必须true,必填
                 */
            if (Appsettings.app(new string[] { "MutiDBEnabled" }).ObjToBool())
            {
                if (typeof(T)
                    .GetTypeInfo()
                    .GetCustomAttributes(typeof(SugarTable), true)
                    .FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable
                    && !string.IsNullOrEmpty(sugarTable.TableDescription))
                {
                    _dbBase_Other.ChangeDatabase(sugarTable.TableDescription.ToLower());
                }
                else
                {
                    _dbBase_Other.ChangeDatabase(MainDb.CurrentDbConnId.ToLower());
                }
            }
            return _dbBase_Other;
        }
        
        //使用其他Model的方法

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async Task<List<T>> Query<T>()
        {
            return await dbOther<T>().Queryable<T>().ToListAsync();
        }

        public async Task<List<T>> Query<T>(Expression<Func<T, bool>> whereExpression)
        {
            return await dbOther<T>().Queryable<T>()
                .WhereIF(whereExpression != null, whereExpression)
                .ToListAsync();
        }

        public async Task<T> QueryById<T>(object objId)
        {
            return await dbOther<T>().Queryable<T>().In(objId).SingleAsync();
        }

        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public async Task<int> Add<T>(T entity) where T : class, new()
        {
            var insert = dbOther<T>().Insertable<T>(entity);
            return await insert.ExecuteReturnIdentityAsync();
        }


        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="insertColumns">指定只插入列</param>
        /// <returns>返回自增量列</returns>
        public async Task<int> Add<T>(List<T> listEntity) where T : class, new()
        {
            if (listEntity != null && listEntity.Count > 0)
                return await dbOther<T>().Insertable(listEntity.ToArray()).ExecuteCommandAsync();
            else
                return 0;
        }

        public async Task<bool> Update<T>(T model) where T : class, new()
        {
            return await dbOther<T>().Updateable(model).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <returns></returns>
        public async Task<int> Update<T>(List<T> listEntity) where T : class, new()
        {
            if (listEntity != null && listEntity.Count > 0)
                return await dbOther<T>().Updateable(listEntity.ToArray()).ExecuteCommandAsync();
            else
                return 0;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public ISugarQueryable<T> SqlQueryableByDB<T>(string sql) where T : class, new()
        {
            return dbOther<T>().SqlQueryable<T>(sql);
        }

IBaseServices接口:

        Task<List<T>> Query<T>();
        Task<List<T>> Query<T>(Expression<Func<T, bool>> whereExpression);
        Task<T> QueryById<T>(object objId);
        Task<int> Add<T>(T model) where T : class, new();
        Task<int> Add<T>(List<T> listEntity) where T : class, new();
        Task<bool> Update<T>(T model) where T : class, new();
        Task<int> Update<T>(List<T> listEntity) where T : class, new();

BaseServices类:

        public async Task<List<T>> Query<T>()
        {
            return await BaseDal.Query<T>();
        }

        public async Task<List<T>> Query<T>(Expression<Func<T, bool>> whereExpression)
        {
            return await BaseDal.Query<T>(whereExpression);
        }

        public async Task<T> QueryById<T>(object objId)
        {
            return await BaseDal.QueryById<T>(objId);
        }

        public async Task<int> Add<T>(T model) where T : class, new()
        {
            return await BaseDal.Add<T>(model);
        }

        public async Task<int> Add<T>(List<T> listEntity) where T : class, new()
        {
            return await BaseDal.Add<T>(listEntity);
        }

        public async Task<bool> Update<T>(T model) where T : class, new()
        {
            return await BaseDal.Update<T>(model);
        }

        public async Task<int> Update<T>(List<T> listEntity) where T : class, new()
        {
            return await BaseDal.Update<T>(listEntity);
        }

使用:
Model中加分库的连接
image
可以使用其他Model的Services查询。
image

主要是懒的写多个Service,我就这样处理了。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions