大龄程序员谈架构经验 内行看门道

原创2022-10-15 17:01·码农阿峰

前言

孔乙己显出极高兴的样子,将两个指头的长指甲敲着柜台,点头说:“对呀,对呀!......回字有四样写法,你知道么?”

大家好,我是44岁的大龄程序员码农阿峰。阿峰从事编程二十年了,虽然没有成为架构师,却也用过很多种架构。我觉得一招鲜走遍天,架构师常用的那几招我还是会的,听我来说道说道。我以为至少有这几招:

  1. 模板方法设计模式
  2. 反射
  3. 不重复造轮子,集众家所长

架构经验总结

1).模板方法设计模式的运用

在一个方法中定义了一个算法的骨架或者步骤,而将一些步骤延迟到子类中去实现。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某一些步骤。

   #父类:

namespace Repository
{
    /// 
    /// 数据库存储泛型基类
    /// 
    /// 
    public class BaseRepositoryT> : SimpleClientT> where T : class, new()
    {
        public ITenant itenant = null;//多租户事务
        public BaseRepository(ISqlSugarClient context = null) : base(context)
        {
            //通过特性拿到ConfigId
            var configId = typeof(T).GetCustomAttribute()?.configId;
            if (configId != null)
            {
                Context = DbScoped.SugarScope.GetConnectionScope(configId);//根据类传入的ConfigId自动选择
            }
            else
            {
                Context = context ?? DbScoped.SugarScope.GetConnectionScope(0);//没有默认db0
            } 
            itenant = DbScoped.SugarScope;//设置租户接口
        }

        #region add

        /// 
        /// 插入实体
        /// 
        /// 
        /// 
        public int Add(T t)
        {
            return Context.Insertable(t).IgnoreColumns(true).ExecuteCommand();
        }

        public int Insert(List t)
        {
            return Context.Insertable(t).ExecuteCommand();
        }
        public int Insert(T parm, Expressionobject>> iClumns = null, bool ignoreNull = true)
        {
            return Context.Insertable(parm).InsertColumns(iClumns).IgnoreColumns(ignoreNullColumn: ignoreNull).ExecuteCommand();
        }
        public IInsertable Insertable(T t)
        {
            return Context.Insertable(t);
        }
        #endregion add

        #region update
        public IUpdateable Updateable(T entity)
        {
            return Context.Updateable(entity);
        }
        public int Update(T entity, bool ignoreNullColumns = false)
        {
            return Context.Updateable(entity).IgnoreColumns(ignoreNullColumns).ExecuteCommand();
        }

        public int Update(T entity, Expressionobject>> expression, bool ignoreAllNull = false)
        {
            return Context.Updateable(entity).UpdateColumns(expression).IgnoreColumns(ignoreAllNull).ExecuteCommand();
        }

        /// 
        /// 根据实体类更新指定列 eg:Update(dept, it => new { it.Status }, f => depts.Contains(f.DeptId));只更新Status列,条件是包含
        /// 
        /// 
        /// 
        /// 
        /// 
        public int Update(T entity, Expressionobject>> expression, Expression> where)
        {
            return Context.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand();
        }

        public int Update(SqlSugarClient client, T entity, Expressionobject>> expression, Expression> where)
        {
            return client.Updateable(entity).UpdateColumns(expression).Where(where).ExecuteCommand();
        }

        /// 
        ///
        /// 
        /// 
        /// 
        /// 默认为true
        /// 
        public int Update(T entity, List list = null, bool isNull = true)
        {
            if (list == null)
            {
                list = new List()
            {
                "Create_By",
                "Create_time"
            };
            }
            return Context.Updateable(entity).IgnoreColumns(isNull).IgnoreColumns(list.ToArray()).ExecuteCommand();
        }

        /// 
        /// 更新指定列 eg:Update(w => w.NoticeId == model.NoticeId, it => new SysNotice(){ Update_time = DateTime.Now, Title = "通知标题" });
        /// 
        /// 
        /// 
        /// 
        public int Update(Expression> where, Expression> columns)
        {
            return Context.Updateable().SetColumns(columns).Where(where).RemoveDataCache().ExecuteCommand();
        }
        #endregion update

        public DbResult UseTran(Action action)
        {
            try
            {
                var result = Context.Ado.UseTran(() => action());
                return result;
            }
            catch (Exception ex)
            {
                Context.Ado.RollbackTran();
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        public IStorageable Storageable(T t)
        {
            return Context.Storageable(t);
        }
        public IStorageable Storageable(List t)
        {
            return Context.Storageable(t);
        }
        /// 
        /// 
        /// 
        /// 
        /// 增删改查方法
        /// 
        public DbResult UseTran(SqlSugarClient client, Action action)
        {
            try
            {
                var result = client.AsTenant().UseTran(() => action());
                return result;
            }
            catch (Exception ex)
            {
                client.AsTenant().RollbackTran();
                Console.WriteLine(ex.Message);
                throw;
            }
        }

        public bool UseTran2(Action action)
        {
            var result = Context.Ado.UseTran(() => action());
            return result.IsSuccess;
        }

        #region delete
        public IDeleteable Deleteable()
        {
            return Context.Deleteable();
        }

        /// 
        /// 批量删除
        /// 
        /// 
        /// 
        public int Delete(object[] obj)
        {
            return Context.Deleteable().In(obj).ExecuteCommand();
        }
        public int Delete(object id)
        {
            return Context.Deleteable(id).ExecuteCommand();
        }
        public int DeleteTable()
        {
            return Context.Deleteable().ExecuteCommand();
        }
        public bool Truncate()
        {
            return Context.DbMaintenance.TruncateTable();
        }
        #endregion delete

        #region query

        public bool Any(Expression> expression)
        {
            return Context.Queryable().Where(expression).Any();
        }

        public ISugarQueryable Queryable()
        {
            return Context.Queryable();
        }

        public (List, int) QueryableToPage(Expression> expression, int pageIndex = 0, int pageSize = 10)
        {
            int totalNumber = 0;
            var list = Context.Queryable().Where(expression).ToPageList(pageIndex, pageSize, ref totalNumber);
            return (list, totalNumber);
        }

        public (List, int) QueryableToPage(Expression> expression, string order, int pageIndex = 0, int pageSize = 10)
        {
            int totalNumber = 0;
            var list = Context.Queryable().Where(expression).OrderBy(order).ToPageList(pageIndex, pageSize, ref totalNumber);
            return (list, totalNumber);
        }

        public (List, int) QueryableToPage(Expression> expression, Expressionobject>> orderFiled, string orderBy, int pageIndex = 0, int pageSize = 10)
        {
            int totalNumber = 0;

            if (orderBy.Equals("DESC", StringComparison.OrdinalIgnoreCase))
            {
                var list = Context.Queryable().Where(expression).OrderBy(orderFiled, OrderByType.Desc).ToPageList(pageIndex, pageSize, ref totalNumber);
                return (list, totalNumber);
            }
            else
            {
                var list = Context.Queryable().Where(expression).OrderBy(orderFiled, OrderByType.Asc).ToPageList(pageIndex, pageSize, ref totalNumber);
                return (list, totalNumber);
            }
        }

        public List SqlQueryToList(string sql, object obj = null)
        {
            return Context.Ado.SqlQuery(sql, obj);
        }

        /// 
        /// 根据主值查询单条数据
        /// 
        /// 主键值
        /// 泛型实体
        public T GetId(object pkValue)
        {
            return Context.Queryable().InSingle(pkValue);
        }
        /// 
        /// 根据条件查询分页数据
        /// 
        /// 
        /// 
        /// 
        public PagedInfo GetPages(Expression> where, PagerInfo parm)
        {
            var source = Context.Queryable().Where(where);

            return source.ToPage(parm);
        }

        public PagedInfo GetPages(Expression> where, PagerInfo parm, Expressionobject>> order, OrderByType orderEnum = OrderByType.Asc)
        {
            var source = Context.Queryable().Where(where).OrderByIF(orderEnum == OrderByType.Asc, order, OrderByType.Asc).OrderByIF(orderEnum == OrderByType.Desc, order, OrderByType.Desc);

            return source.ToPage(parm);
        }

        public PagedInfo GetPages(Expression> where, PagerInfo parm, Expressionobject>> order, string orderByType)
        {
            return GetPages(where, parm, order, orderByType == "desc" ? OrderByType.Desc : OrderByType.Asc);
        }

        /// 
        /// 查询所有数据(无分页,请慎用)
        /// 
        /// 
        public List GetAll(bool useCache = false, int cacheSecond = 3600)
        {
            return Context.Queryable().WithCacheIF(useCache, cacheSecond).ToList();
        }

        #endregion query

        /// 
        /// 此方法不带output返回值
        

文章来源于互联网:大龄程序员谈架构经验 内行看门道

THE END
分享
二维码