ASP.net MVC 构建layui管理后台(构造基础仓储)<1>

本文章为ASP.net MVC 构建layui管理后台,第一篇。

使用EF+ado.net 实体数据模型模式进行底层的数据库连接。

 

 

 

 在项目添加一个类库Model

 

 

 ASP.net MVC 构建layui管理后台(构造基础仓储)<1>_第1张图片

 

 

 在类库Model上添加一个ado.net 实体数据模型

ASP.net MVC 构建layui管理后台(构造基础仓储)<1>_第2张图片

 

 

 ASP.net MVC 构建layui管理后台(构造基础仓储)<1>_第3张图片

 

 

 ASP.net MVC 构建layui管理后台(构造基础仓储)<1>_第4张图片

ASP.net MVC 构建layui管理后台(构造基础仓储)<1>_第5张图片

 

 

 

 添加成功之后,效果如下

ASP.net MVC 构建layui管理后台(构造基础仓储)<1>_第6张图片

 

 

 

 

 

 

 

按照以上的步骤,已经完成了,对应数据库的连接,关联,下面我们来定义数据库的存储。

在Model类库下新建EFDbContext类,用于连接数据库

 /// 
        /// 连接数据库
        /// 
        /// 
        public static ZWSDBEntities GetDbContext()
        {
            ZWSDBEntities dbContext = CallContext.GetData("ZWSDBEntities") as ZWSDBEntities;
            if (dbContext == null)
            {
                dbContext = new ZWSDBEntities();
                CallContext.SetData("ZWSDBEntities", dbContext);
            }
            return dbContext;
        }

其中ZWSDBEntities是每个人的可能不一样,可以在下图中找

ASP.net MVC 构建layui管理后台(构造基础仓储)<1>_第7张图片

 

 

接下来定义两个接口IBaseEntityDAL,IBaseEntityService

 /// 
    /// 封装定义数据操作接口
    /// 
    /// 
    public interface IBaseEntityDAL where T : class
    {
        //! 检查是否存在持久实体对象
        bool HasInstance(Expressionbool>> where);
        bool HasRelevanceInstance(Expressionbool>> where);

        //! 提取单个对象的方法
        T GetByIntID(int id);
        T GetByStringID(string oid);
        T GetByGuid(Guid oid);
        T GetByLambda(Expressionbool>> where);

        //! 根据 id ,提取实例,如果没有,创建一个新的实例,并执行持久化
        T GetOrCreatePersistence(Guid id);
        //! 根据 id ,提取实例,如果没有,创建一个新的实例,但不执行持久化
        T GetOrCreateNotPersistence(Guid id);

        //! 添加对象的方法
        void Add(T bo);
        void AddOrUpdate(T bo, bool isForNewObject, ref int returnPoint);

        //! 删除对象的方法
        void Delete(T bo);
        void DeleteRelevenceObject(T2 relevanceBo);
        void DeleteRelevenceObjectsByLambda(Expressionbool>> where);

        //! 更新对象的方法
        void Update(T bo);

        //! 提取对象集合的方法
        IQueryable GetAll();
        IQueryable GetAllOrdered(Expressionbool>> order);
        IQueryable GetMany(Expressionbool>> where);
        IQueryable GetMany(Expressionobject>> selector, object val);
        IQueryable GetManyOrdered(Expressionbool>> where, Expression> order);
        IQueryable GetAllOrderedPaged(Expression> order, int page, int pageSize, ref int pageAmount, ref int boAmount, ref bool isDesc);
        //IQueryable GetDataPaging(DataTableModel dataTableModel, ref int dataAmount, string orderProperty, Expression> where = null);
        IQueryable GetAllCommonWay(
            ref int pageAmount,
            ref int boAmount,
            Expressionbool>> where = null,
            Expressionbool>> order = null,
            bool? isDesc = null,
            int? page = null,
            int? pageSize = null);

        //! 数据处理约束管理方法
        bool CanAdd(Expressionbool>> where);

        //! 用于处理一些附加对象的方法
        T2 GetRelevanceObject(Guid id);
        T2 GetRelevanceObject(Expressionbool>> where);
        T2 GetRelevanceObjectBySqlQuery(string SqlQureyString);

        IQueryable GetRelevanceObjects(Expressionbool>> where);
        IQueryable GetRelevanceObjects(
            Expressionbool>> where = null,
            Expressionbool>> order = null,
            bool? isDesc = null);

        void AddRelevanceObject(T2 relevanceBo);
        void UpdateRelevanceObject(T2 relevanceBo);
    }
/// 
    /// 封装定义数据操作接口
    /// 
    /// 
    public interface IBaseEntityService where T : class
    {
        /// 
        /// 检查是否存在持久实体对象
        /// 
        /// 
        /// 
        bool HasInstance(Expressionbool>> where);
        bool HasRelevanceInstance(Expressionbool>> where);


        T GetByIntID(int id);
        /// 
        /// 提取单个对象的方法(ID)
        /// 
        /// ID
        /// 
        T GetByStringID(string oid);
        /// 
        ///提取单个对象的方法(GID)
        /// 
        /// 
        /// 
        T GetByGuid(Guid oid);
        T GetByLambda(Expressionbool>> where);

        //! 根据 id ,提取实例,如果没有,创建一个新的实例,并执行持久化
        T GetOrCreatePersistence(Guid id);
        //! 根据 id ,提取实例,如果没有,创建一个新的实例,但不执行持久化
        T GetOrCreateNotPersistence(Guid id);

        //! 添加对象的方法
        void Add(T bo);
        void AddOrUpdate(T bo, bool isForNewObject, params Expressionobject>>[] updatedProperties);

        //! 删除对象的方法
        void Delete(T bo);

        void DeleteRelevenceObject(T2 relevanceBo);
        void DeleteRelevenceObjectsByLambda(Expressionbool>> where);

        //! 更新对象的方法
        void Update(T bo);

        void Update(T entity, params Expressionobject>>[] updatedProperties);

        //! 提取对象集合的方法
        IQueryable GetAll();
        IQueryable GetAllOrdered(Expressionbool>> order);
        IQueryable GetMany(Expressionbool>> where);
        IQueryable GetMany(Expressionobject>> selector, object val);
        IQueryable GetManyOrdered(Expressionbool>> where, Expression> order);

        //! 按照分页方式提取对象集合的方法
        IQueryable GetAllOrderedPaged(string orderProeprtyName, int page, int pageSize, ref int pageAmount, ref int boAmount, ref bool isDesc);
        IQueryable GetAllOrderedPaged(Expression> order, int page, int pageSize, ref int pageAmount, ref int boAmount, ref bool isDesc);

        //IQueryable GetDataPaging(GridPager page, ref int dataAmount, Expression> where = null);
        IQueryable GetManyOrderedPaged(Expressionbool>> where, Expression> order, int page, int pageSize, ref int pageAmount, ref int boAmount, ref bool isDesc);
        IQueryable GetManyOrderedByProertyNamePaged(Expressionbool>> where, string orderProeprtyName, int page, int pageSize, ref int pageAmount, ref int boAmount, ref bool isDesc);
        IQueryable GetAllCommonWay(
            ref int pageAmount,
            ref int boAmount,
            Expressionbool>> where = null,
            Expressionbool>> order = null,
            bool? isDesc = null,
            int? page = null,
            int? pageSize = null);

        //! 数据处理约束管理方法
        bool CanAdd(Expressionbool>> where);

        //! 用于处理一些附加对象的方法
        T2 GetRelevanceObject(Guid id);
        T2 GetRelevanceObject(Expressionbool>> where);
        T2 GetRelevanceObjectBySqlQuery(string SqlQureyString);

        IQueryable GetRelevanceObjects(Expressionbool>> where);
        IQueryable GetRelevanceObjects(
            Expressionbool>> where = null,
            Expressionbool>> order = null,
            bool? isDesc = null);

        IQueryable GetManyRelevanceObjectsOrderedByProertyNamePaged(Expressionbool>> where, string orderProeprtyName, int page, int pageSize, ref int pageAmount, ref int boAmount, ref bool isDesc);

        void AddRelevanceObject(T2 relevanceBo);
        void UpdateRelevanceObject(T2 relevanceBo);
    }

两个操作类,BaseEntityDAL,BaseEntityService

 /// 
    /// 基于 EF CodeFirst 独立的针对 IBaseEntityService 方法的实现,其实际实现时通过继承的方式处理
    /// 
    /// 实现基础操作方法的基类
    /// 
    public abstract class BaseEntityDAL where T : class
    {
        protected readonly DbContext _Context;
        protected readonly IDbSet _DbSet;

        public BaseEntityDAL(DbContext context)
        {
            this._Context = context;
            _DbSet = _Context.Set();
        }

        /// 
        /// 根据条件检查数据是否已存在
        /// 
        /// Lamda表达式
        /// true:为不存在
        public bool HasInstance(Expressionbool>> where)
        {
            if (_DbSet.Where(where).FirstOrDefault() == null)
                return true;
            else
                return false;
        }

        public bool HasRelevanceInstance(Expressionbool>> where)
        {
            var dbSet = _Context.Set(typeof(T2)) as IQueryable; ;
            if (dbSet.Where(where).FirstOrDefault() != null)
                return true;
            else
                return false;
        }
        public T GetByStringID(string oid)
        {
            return _DbSet.Find(oid);
        }

        public T GetByGuid(Guid oid)
        {
            return _DbSet.Find(oid);
        }

        /// 
        /// 根据Lambda表达式获取一条数据
        /// 
        /// 
        /// 
        public T GetByLambda(Expressionbool>> where)
        {
            try
            {
                return _DbSet.Where(where).FirstOrDefault();
            }
            catch (Exception e)
            {
                return null;
            }
        }


        //根据Guid获取一条数据,如果该数据不存在将创建一条数据,然后返回
        public T GetOrCreatePersistence(Guid id)
        {
            var fObject = this.GetByGuid(id);
            if (fObject != null)
                return fObject;
            else
            {
                fObject = Activator.CreateInstance();
                this.Add(fObject);
                return fObject;
            }
        }

        public T GetOrCreateNotPersistence(Guid id)
        {
            var fObject = this.GetByGuid(id);
            if (fObject != null)
                return fObject;
            else
            {
                fObject = Activator.CreateInstance();
                return fObject;
            }
        }

        public void Add(T bo)
        {
            _DbSet.Add(bo);
            _Context.SaveChanges();
        }
        public void DelayedAdd(T bo)
        {
            _DbSet.Add(bo);
        }
        public int AllSaveChanges()
        {
            return _Context.SaveChanges();

        }
        public void AddOrUpdate(T bo, bool isForNewObject, ref int returnPoint)
        {
            if (isForNewObject)
            {
                this.Add(bo);
                returnPoint = 1;
            }
            else
            {
                this.Update(bo);
            }
        }
        /// 
        /// 物理删除
        /// 
        /// 
        public void DeleteEntity(T bo)
        {
            _DbSet.Remove(bo);
            _Context.SaveChanges();
        }
        /// 
        /// 物理删除
        /// 
        /// 
        public void DeleteByIds(params object[] ids)
        {
            if (ids != null)
            {
                string strIds = string.Join(",", ids);
                _Context.Database.ExecuteSqlCommand(string.Format("delete {0}  where  ID  in ('{1}')  ", typeof(T).Name, strIds));
            }
        }
        /// 
        /// 逻辑删除(使用时确保表中 含有用于标记逻辑删除的字段 IsDelete)
        /// 
        /// 
        public void Delete(T bo)
        {
            PropertyInfo[] properties = typeof(T).GetProperties();
            for (int i = properties.Length; i > 0; i--)
            {
                string fn = properties[i - 1].Name;
                PropertyInfo pinfo = typeof(T).GetProperty(fn);
                if ("IsDelete" == fn)
                {
                    pinfo.SetValue(bo, true, null);
                    break;
                }
            }
            _DbSet.Attach(bo);
            _Context.Entry(bo).State = EntityState.Modified;
            _Context.SaveChanges();
        }
        public void DeleteRelevenceObject(T2 relevanceBo)
        {
            var dbSet = _Context.Set(typeof(T2));
            dbSet.Remove(relevanceBo);
            _Context.SaveChanges();
        }

        public void DeleteRelevenceObjectsByLambda(Expressionbool>> where)
        {
            var dbSet = _Context.Set(typeof(T2));

            var tempCollection = dbSet as IQueryable;
            foreach (var item in tempCollection.Where(where))
                dbSet.Remove(item);

            _Context.SaveChanges();
        }

        public void Update(T bo)
        {
            _DbSet.Attach(bo);
            _Context.Entry(bo).State = EntityState.Modified;
            _Context.SaveChanges();
        }

        public IQueryable GetAll()
        {
            return _DbSet as IQueryable;
        }

        //获取所有数据,根据条件排序
        public IQueryable GetAllOrdered(Expressionbool>> order)
        {
            return _DbSet.OrderBy(order);
        }

        public IQueryable GetMany(Expressionbool>> where)
        {
            return _DbSet.Where(where) as IQueryable;
        }

        public IQueryable GetMany(Expressionobject>> selector, object val)
        {
            //var queryBuilder = CustomLinq.Func((selector, val) =>
            //      from c in _DbSet.ToExpandable()
            //      where selector.Expand(c).IndexOf(val) != -1
            //      select c);
            return _DbSet as IQueryable;
        }

        public IQueryable GetManyOrdered(Expressionbool>> where, Expression> order)
        {
            return _DbSet.Where(where).OrderBy(order);
        }

        /// 
        /// 分页获取数据
        /// 
        /// 
        /// 表格参数
        /// 总条数
        /// 要排序的对象名称
        /// 搜索条件
        /// 
        //public IQueryable GetDataPaging(DataTableModel dataTableModel, ref int dataAmount, string orderProperty, Expression> where = null)
        //{
        //    var searchResult = _DbSet as IQueryable;
        //    if (where != null)
        //        searchResult = searchResult.Where(where);

        //    dataAmount = searchResult.Count();
        //    var isDesc = dataTableModel.OrderDir == "desc";
        //    searchResult = searchResult.OrderBy(orderProperty, isDesc).Skip(dataTableModel.Start).Take(dataTableModel.Length);
        //    return searchResult;
        //}

        public IQueryable GetAllOrderedPaged(Expression> order, int page, int pageSize, ref int pageAmount, ref int boAmount, ref bool isDesc)
        {
            var searchResult = _DbSet as IQueryable;
            boAmount = searchResult.Count();
            pageAmount = boAmount / pageSize;
            if (boAmount > pageAmount * pageSize)
                pageAmount = pageAmount + 1;
            return searchResult.OrderBy(order).Skip((page - 1) * pageSize).Take(pageSize);
        }
        public IQueryable GetAllCommonWay(
            ref int pageAmount,
            ref int boAmount,
            Expressionbool>> where = null,
            Expressionbool>> order = null,
            bool? isDesc = null,
            int? page = null,
            int? pageSize = null)
        {
            pageAmount = 0;
            boAmount = 0;
            var searchResult = _DbSet as IQueryable;


            if (where != null && order != null)
            {
                searchResult = searchResult.Where(where);
                if (isDesc != null)
                    searchResult = searchResult.OrderByDescending(order);
                else
                    searchResult = searchResult.OrderBy(order);
            }
            else
            {
                if (where != null)
                {
                    searchResult = searchResult.Where(where);
                }
                else
                {
                    if (order != null)
                    {
                        if (isDesc != null)
                            searchResult = searchResult.OrderByDescending(order);
                        else
                            searchResult = searchResult.OrderBy(order);
                    }
                }
            }

            boAmount = searchResult.Count();
            if (pageAmount == -1)
            {
                pageAmount = -1;
            }

            if (page != null)
            {
                var p = (int)page;
                var ps = (int)pageSize;
                pageAmount = searchResult.Count() / ps;
                searchResult = searchResult.Skip((p - 1) * ps).Take(ps);
            }

            return searchResult;
        }

        public bool CanAdd(Expressionbool>> where)
        {
            var count = _DbSet.Where(where).Count();
            if (count > 0)
                return false;
            else
                return true;
        }

        public T2 GetRelevanceObject(Guid id)
        {
            var dbSet = _Context.Set(typeof(T2));
            return (T2)dbSet.Find(id);
        }

        public T2 GetRelevanceObjectBySqlQuery(string SqlQureyString)
        {
            var id = _Context.Database.SqlQuery(SqlQureyString).FirstOrDefault();
            if (id != null)
            {
                return GetRelevanceObject(id);
            }
            else
                return default(T2);
        }

        public T2 GetRelevanceObject(Expressionbool>> where)
        {
            var dbSet = _Context.Set(typeof(T2)) as IQueryable;
            return (T2)dbSet.Where(where).FirstOrDefault();
        }

        public IQueryable GetRelevanceObjects(Expressionbool>> where)
        {
            var dbSet = _Context.Set(typeof(T2));
            var searchResult = dbSet as IQueryable;

            return searchResult.Where(where);
        }

        public IQueryable GetRelevanceObjects(
            Expressionbool>> where = null,
            Expressionbool>> order = null,
            bool? isDesc = null)
        {
            var dbSet = _Context.Set(typeof(T2));
            var searchResult = dbSet as IQueryable;
            if (where != null && order != null)
            {
                searchResult = searchResult.Where(where);
                if (isDesc != null)
                    searchResult = searchResult.OrderByDescending(order);
                else
                    searchResult = searchResult.OrderBy(order);
            }
            else
            {
                if (where != null)
                {
                    searchResult = searchResult.Where(where);
                }
                else
                {
                    if (order != null)
                    {
                        if (isDesc != null)
                            searchResult = searchResult.OrderByDescending(order);
                        else
                            searchResult = searchResult.OrderBy(order);
                    }
                }
            }

            return searchResult;
        }


        public void AddRelevanceObject(T2 relevanceBo)
        {
            var dbSet = _Context.Set(typeof(T2));
            dbSet.Add(relevanceBo);
            _Context.SaveChanges();
        }

        public void UpdateRelevanceObject(T2 relevanceBo)
        {
            var tempBo = Activator.CreateInstance(typeof(T2));
            tempBo = relevanceBo;
            var dbSet = _Context.Set(typeof(T2));
            dbSet.Attach(tempBo);
            _Context.Entry(tempBo).State = EntityState.Modified;
            _Context.SaveChanges();
        }


        ///// 
        ///// 从缓存中获取所有数据
        ///// 
        ///// 
        //public ICollection GetAllByCache()
        //{
        //    var key = typeof(T).Name + "List";
        //    var obj = this.GetByCache(key);
        //    if (obj == null)
        //    {
        //        obj = this.GetAll().ToList();
        //        this.SetInCache(key, obj);
        //    }
        //    return obj as ICollection;
        //}
        ///// 
        ///// 从缓存中获取单个实体
        ///// 
        ///// 
        ///// 
        //public T GetByCache(Guid id)
        //{
        //    var key = id.ToString();
        //    var obj = this.GetByCache(key);
        //    if (obj == null)
        //    {
        //        obj = this.GetByGuid(id);
        //        this.SetInCache(key, obj);
        //    }
        //    return obj as T;
        //}

        ///// 
        ///// 移除缓存中的数据(集合)
        ///// 
        //public void RemoveAllCache()
        //{
        //    var key = typeof(T).Name + "List";
        //    this.RemoveCache(key);
        //}
        ///// 
        ///// 移除缓存中的数据(单个实体)
        ///// 
        ///// 
        //public void RemoveCache(Guid id)
        //{
        //    var key = id.ToString();
        //    this.RemoveCache(key);
        //}
        /// 
        /// 设置缓存
        /// 
        /// 
        /// 
        //public void SetInCache(string key, object val)
        //{
        //    CacheHelper.Set(key, val);
        //}
        ///// 
        ///// 从缓存中获取数据
        ///// 
        ///// 
        ///// 
        //public object GetByCache(string key)
        //{
        //    return CacheHelper.Get(key);
        //}
        ///// 
        ///// 从缓存中移除数据
        ///// 
        ///// 
        //public void RemoveCache(string key)
        //{
        //    CacheHelper.Remove(key);
        //}
    }
 /// 
    /// 基于 EF CodeFirst 独立的针对 IBaseEntityService 方法的实现,其实际实现时通过继承的方式处理
    /// 
    public abstract class BaseEntityService where T : class
    {
        private  readonly DbContext _Context;
        private  readonly IDbSet _DbSet;

        public BaseEntityService(DbContext context)
        {
            this._Context = context;
            _DbSet = _Context.Set();
        }
        /// 
        /// 根据条件判断是否数据存在
        /// 
        /// 判断条件
        /// 
        public  bool HasInstance(Expressionbool>> where)
        {
            if (_DbSet.Where(where).FirstOrDefault() != null)
                return true;
            else
                return false;
        }
        /// 
        /// 根据条件判断是否数据存在
        /// 
        /// 需要判断实体
        /// 判断条件
        /// 
        public bool HasRelevanceInstance(Expressionbool>> where)
        {
            var dbSet = _Context.Set(typeof(T2)) as IQueryable; ;
            if (dbSet.Where(where).FirstOrDefault() != null)
                return true;
            else
                return false;
        }
        /// 
        /// 根据主键ID获取实体
        /// 
        /// 
        /// 
        public T GetByIntID(int id)
        {
            return _DbSet.Find(id);
        }
        /// 
        /// 根据主键ID获取实体
        /// 
        /// 
        /// 
        public T GetByStringID(string oid)
        {
            return _DbSet.Find(oid);
        }
        /// 
        /// 根据主键ID获取实体
        /// 
        /// 
        /// 
        public T GetByGuid(Guid oid)
        {
            return _DbSet.Find(oid);
        }
        /// 
        /// 根据条件获取实体
        /// 
        /// 刷选条件
        /// 
        public T GetByLambda(Expressionbool>> where)
        {

            return _DbSet.Where(where).FirstOrDefault();
        }

        public T GetOrCreatePersistence(Guid id)
        {
            var fObject = this.GetByGuid(id);
            if (fObject != null)
                return fObject;
            else
            {
                fObject = Activator.CreateInstance();
                this.Add(fObject);
                return fObject;
            }
        }

        public T GetOrCreateNotPersistence(Guid id)
        {
            var fObject = this.GetByGuid(id);
            if (fObject != null)
                return fObject;
            else
            {
                fObject = Activator.CreateInstance();
                return fObject;
            }
        }
       /// 
       /// 添加数据
       /// 
       /// 实体
        public void Add(T bo)
        {
                _DbSet.Add(bo);
                _Context.SaveChanges();
        }
        /// 
        /// 添加或者更新数据
        /// 
        /// 实体
        /// 是否新增
        /// 过滤字段
        public void AddOrUpdate(T bo, bool isForNewObject=true, params Expressionobject>>[] updatedProperties)
        {
            if (isForNewObject)
            {
                this.Add(bo);
            }
            else
            {
                this.Update(bo,updatedProperties);
            }
        }
        /// 
        /// 删除数据
        /// 
        /// 实体
        public void Delete(T bo)
        {
            _DbSet.Remove(bo);
            _Context.SaveChanges();
        }

        /// 
        /// 根据stringID删除数据
        /// 
        /// 
        public void Delete(string id)
        {
            _DbSet.Remove(GetByStringID(id));
            _Context.SaveChanges();
        }
        /// 
        /// 根据主键ID删除数据(int)
        /// 
        /// 
        public void Delete(int id)
        {
            _DbSet.Remove(GetByIntID(id));
            _Context.SaveChanges();
        }

        //public void DeleteByLambda(Expression> where)
        //{
        //    _DbSet.Delete(where);
        //    _Context.SaveChanges();
        //}
        /// 
        /// 删除数据
        /// 
        /// 
        /// 
        public void DeleteRelevenceObject(T2 relevanceBo)
        {
            var dbSet = _Context.Set(typeof(T2));
            dbSet.Remove(relevanceBo);
            _Context.SaveChanges();
        }
        /// 
        /// 根据刷选添加循环删除
        /// 
        /// 需要删除的实体
        /// 删除条件
        public void DeleteRelevenceObjectsByLambda(Expressionbool>> where)
        {
            var dbSet = _Context.Set(typeof(T2));

            var tempCollection = dbSet as IQueryable;
            foreach (var item in tempCollection.Where(where))
                dbSet.Remove(item);

            _Context.SaveChanges();
        }
        /// 
        /// 更新实体
        /// 
        /// 实体
        public void Update(T bo)
        {
            _DbSet.Attach(bo);
            _Context.Entry(bo).State = EntityState.Modified;
            _Context.SaveChanges();
        }
        /// 
        /// 更新实体,并过滤不需要更新列
        /// 
        /// 实体
        /// 过滤不需要更实体数组
        public void Update(T entity, params Expressionobject>>[] updatedProperties)
        {
            _DbSet.Attach(entity);
            var dbEntityEntry = _Context.Entry(entity);
            _Context.Entry(entity).State = EntityState.Modified;
            if (updatedProperties.Any())
            {
                foreach (var property in updatedProperties)
                {
                    dbEntityEntry.Property(property).IsModified = false;
                }
            }
         
            _Context.SaveChanges();

        }
            /// 
            /// 获取实体所有信息
            /// 
            /// 
            public IQueryable GetAll()
        {
            return _DbSet as IQueryable;
        }
        /// 
        /// 获取实体信息并排序
        /// 
        /// 
        /// 
        public IQueryable GetAllOrdered(Expressionbool>> order)
        {
            return _DbSet.OrderBy(order);
        }
        /// 
        /// 筛选获取实体
        /// 
        /// 
        /// 
        public IQueryable GetMany(Expressionbool>> where)
        {
            return _DbSet.Where(where) as IQueryable;
        }

        public IQueryable GetMany(Expressionobject>> selector, object val)
        {
            //var queryBuilder = CustomLinq.Func((selector, val) =>
            //      from c in _DbSet.ToExpandable()
            //      where selector.Expand(c).IndexOf(val) != -1
            //      select c);
            return _DbSet as IQueryable;
        }

        public IQueryable GetManyOrdered(Expressionbool>> where, Expression> order)
        {
            return _DbSet.Where(where).OrderBy(order);
        }

        public IQueryable GetAllOrderedPaged(string orderProeprtyName, int page, int pageSize, ref int pageAmount, ref int boAmount, ref bool isDesc)
        {
            var searchResult = _DbSet as IQueryable;
            boAmount = searchResult.Count();
            pageAmount = boAmount / pageSize;
            return searchResult;
        }

        public IQueryable GetAllOrderedPaged(Expression> order, int page, int pageSize, ref int pageAmount, ref int boAmount, ref bool isDesc)
        {
            var searchResult = _DbSet as IQueryable;
            boAmount = searchResult.Count();
            pageAmount = boAmount / pageSize;
            if (boAmount > pageAmount * pageSize)
                pageAmount = pageAmount + 1;
            return searchResult.OrderBy(order).Skip((page - 1) * pageSize).Take(pageSize);
        }

        public IQueryable GetManyOrderedPaged(Expressionbool>> where, Expression> order, int page, int pageSize, ref int pageAmount, ref int boAmount, ref bool isDesc)
        {
            var searchResult = _DbSet.Where(where) as IQueryable;
            boAmount = searchResult.Count();
            pageAmount = boAmount / pageSize;
            if (boAmount > pageAmount * pageSize)
                pageAmount = pageAmount + 1;

            if (!isDesc)
                return searchResult.OrderBy(order).Skip((page - 1) * pageSize).Take(pageSize);
            else
                return searchResult.OrderByDescending(order).Skip((page - 1) * pageSize).Take(pageSize);
        }

        ///// 
        ///// 分页获取数据
        ///// 
        ///// 分页基础类
        ///// 输出总数量
        ///// 筛选条件
        ///// 
        //public IQueryable GetDataPaging(GridPager page, ref int dataAmount, Expression> where = null)
        //{
        //    var searchResult = _DbSet as IQueryable;
        //    if (where != null)
        //        searchResult = searchResult.Where(where);
        //    dataAmount = searchResult.Count();
        //    var isDesc = page.order == "desc";
        //    searchResult = searchResult.OrderBy(page.sort, isDesc).Skip(page.thisCount).Take(page.rows);
        //    return searchResult;
        //}

        public IQueryable GetManyOrderedByProertyNamePaged(
            Expressionbool>> where,
            string orderProeprtyName,
            int page,
            int pageSize,
            ref int pageAmount,
            ref int boAmount,
            ref bool isDesc
            )
        {
            if (where != null)
            {
                var searchResult = _DbSet.Where(where) as IQueryable;
                boAmount = searchResult.Count();
                pageAmount = boAmount / pageSize;
                if (boAmount > pageAmount * pageSize)
                    pageAmount = pageAmount + 1;

                return searchResult;
            }
            else
            {
                var searchResult = _DbSet as IQueryable;
                boAmount = searchResult.Count();
                pageAmount = boAmount / pageSize;
                if (boAmount > pageAmount * pageSize)
                    pageAmount = pageAmount + 1;

                return searchResult;
            }

        }


        public IQueryable GetAllCommonWay(
            ref int pageAmount,
            ref int boAmount,
            Expressionbool>> where = null,
            Expressionbool>> order = null,
            bool? isDesc = null,
            int? page = null,
            int? pageSize = null)
        {
            pageAmount = 0;
            boAmount = 0;
            var searchResult = _DbSet as IQueryable;


            if (where != null && order != null)
            {
                searchResult = searchResult.Where(where);
                if (isDesc != null)
                    searchResult = searchResult.OrderByDescending(order);
                else
                    searchResult = searchResult.OrderBy(order);
            }
            else
            {
                if (where != null)
                {
                    searchResult = searchResult.Where(where);
                }
                else
                {
                    if (order != null)
                    {
                        if (isDesc != null)
                            searchResult = searchResult.OrderByDescending(order);
                        else
                            searchResult = searchResult.OrderBy(order);
                    }
                }
            }

            boAmount = searchResult.Count();
            if (pageAmount == -1)
            {
                pageAmount = -1;
            }

            if (page != null)
            {
                var p = (int)page;
                var ps = (int)pageSize;
                pageAmount = searchResult.Count() / ps;
                searchResult = searchResult.Skip((p - 1) * ps).Take(ps);
            }

            return searchResult;
        }

        public bool CanAdd(Expressionbool>> where)
        {
            var count = _DbSet.Where(where).Count();
            if (count > 0)
                return false;
            else
                return true;
        }

        public T2 GetRelevanceObject(Guid id)
        {
            var dbSet = _Context.Set(typeof(T2));
            return (T2)dbSet.Find(id);
        }

        public T2 GetRelevanceObjectBySqlQuery(string SqlQureyString)
        {
            var id = _Context.Database.SqlQuery(SqlQureyString).FirstOrDefault();
            if (id != null)
            {
                return GetRelevanceObject(id);
            }
            else
                return default(T2);
        }

        public T2 GetRelevanceObject(Expressionbool>> where)
        {
            var dbSet = _Context.Set(typeof(T2)) as IQueryable;
            return (T2)dbSet.Where(where).FirstOrDefault();
        }

        public IQueryable GetRelevanceObjects(Expressionbool>> where)
        {
            var dbSet = _Context.Set(typeof(T2));
            var searchResult = dbSet as IQueryable;

            return searchResult.Where(where);
        }

        public IQueryable GetRelevanceObjects(
            Expressionbool>> where = null,
            Expressionbool>> order = null,
            bool? isDesc = null)
        {
            var dbSet = _Context.Set(typeof(T2));
            var searchResult = dbSet as IQueryable;
            if (where != null && order != null)
            {
                searchResult = searchResult.Where(where);
                if (isDesc != null)
                    searchResult = searchResult.OrderByDescending(order);
                else
                    searchResult = searchResult.OrderBy(order);
            }
            else
            {
                if (where != null)
                {
                    searchResult = searchResult.Where(where);
                }
                else
                {
                    if (order != null)
                    {
                        if (isDesc != null)
                            searchResult = searchResult.OrderByDescending(order);
                        else
                            searchResult = searchResult.OrderBy(order);
                    }
                }
            }

            return searchResult;
        }

        public IQueryable GetManyRelevanceObjectsOrderedByProertyNamePaged(
            Expressionbool>> where,
            string orderProeprtyName,
            int page,
            int pageSize,
            ref int pageAmount,
            ref int boAmount,
            ref bool isDesc)
        {
            var dbSet = _Context.Set(typeof(T2));

            if (where != null)
            {
                var searchResult = (dbSet as IQueryable).Where(where) as IQueryable;
                boAmount = searchResult.Count();
                pageAmount = boAmount / pageSize;
                if (boAmount > pageAmount * pageSize)
                    pageAmount = pageAmount + 1;

                return searchResult;
            }
            else
            {
                var searchResult = dbSet as IQueryable;
                boAmount = searchResult.Count();
                pageAmount = boAmount / pageSize;
                if (boAmount > pageAmount * pageSize)
                    pageAmount = pageAmount + 1;

                return searchResult;
            }

        }

        public void AddRelevanceObject(T2 relevanceBo)
        {
            var dbSet = _Context.Set(typeof(T2));
            dbSet.Add(relevanceBo);
            _Context.SaveChanges();
        }

        public void UpdateRelevanceObject(T2 relevanceBo)
        {
            var tempBo = Activator.CreateInstance(typeof(T2));
            tempBo = relevanceBo;
            var dbSet = _Context.Set(typeof(T2));
            dbSet.Attach(tempBo);
            _Context.Entry(tempBo).State = EntityState.Modified;
            _Context.SaveChanges();
        }



    }

通过以上步骤,一个基本简单的仓储完成了。

接下来一般的话,是使用三层Model,DAL,BLL,由于我个人比较懒,直接跳过了DAL层,在BLL层进行业务处理。

新建一个ArticleBLL,对业务的操作。

 public class ArticleBLL : BaseEntityService, IBaseEntityService
    {
        public ArticleBLL() : base(EFDbContext.GetDbContext()) { }
    }

 

ASP.net MVC 构建layui管理后台(构造基础仓储)<1>_第8张图片

 

 此刻,基本的仓储完成,下篇将介绍如果结合layui完成菜单的添加

你可能感兴趣的:(ASP.net MVC 构建layui管理后台(构造基础仓储)<1>)