EntityFrameWork 5.0数据访问层封装

稍作整理, 留以后备用

1.数据访问层DAL 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.Entity;

using System.Data.Linq;
using System.Data.Objects;
using System.Reflection;
using System.Threading;
using DiaryNodeApp.Model;

namespace DiaryNodeApp.DAL
{
    public class SysDbDAL: DbContext
    {
        DbContext entities = null;
        public SysDbDAL()
        {
            entities = new DiaryNodeDBEntities();
        }
        public SysDbDAL(string nameOrConnectionString)
        {
            //"name=DiaryNodeDBEntities"
            entities = new DbContext(nameOrConnectionString);
        }


        #region 根据SQL语句查询数据
        public IEnumerable<T> ExecuteQuery<T>( string query, params object[] parms)
        {
            try
            {
               return entities.Database.SqlQuery<T>(query, parms);
               
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public IEnumerable<T> ExecuteQuery<T>(string query)
        {
            return ExecuteQuery<T>(string.Empty, query, new object[] { });
        }
        #endregion

        #region 执行操作类型SQl语句
        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="sqlCommand"></param>
        /// <returns></returns>
        public int ExecuteSqlCommand(string sqlCommand, params object[]parms)
        {
            return entities.Database.ExecuteSqlCommand(sqlCommand, parms);
        }
        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="sqlCommand"></param>
        /// <returns></returns>
        public void ExecuteSqlCommand(string sqlCommand)
        {
            ExecuteSqlCommand(sqlCommand);
        }
        #endregion


        #region 私有方法
        private DbSet<T> GetTable<T>() where T : class
        {
            try
            {
               
                DbSet<T> customers = entities.Set<T>();
                return customers;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        #endregion

        #region 统计指定条件的数据量
        /// <summary>
        /// 统计指定条件的数据量
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        public virtual int Count<T>(Expression<Func<T, bool>> query) where T : class
        {
            var table = GetTable<T>();
            return (from t in table
                    select t).Where(query).Count();
        }
        #endregion


        #region 获取单个实体
        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <typeparam name="T">泛型类型参数</typeparam>
        /// <param name="express">查询条件</param>
        /// <returns></returns>
        public virtual T GetSingleEntity<T>(Expression<Func<T, bool>> query) where T : class
        {
            try
            {
                var table = GetTable<T>();
                return (from t in table
                        select t).Where(query).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        #region 更新实体
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <typeparam name="T">泛型类型参数</typeparam>
        /// <param name="entity">更改后的实体</param>
        public virtual bool UpdateEntitys<T>(T entity) where T : class
        {
            object propertyValue = null;
            T entityFromDB = GetSingleEntity<T>(s => s == entity);
            if (null == entityFromDB)
                return false;
            PropertyInfo[] properties = entityFromDB.GetType().GetProperties();
            foreach (PropertyInfo property in properties)
            {
                propertyValue = null;
                if (null != property.GetSetMethod())
                {
                    PropertyInfo entityProperty =
                          entity.GetType().GetProperty(property.Name);
                    if (entityProperty.PropertyType.BaseType ==
                        Type.GetType("System.ValueType") ||
                        entityProperty.PropertyType ==
                        Type.GetType("System.String"))

                        propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);
                    if (propertyValue == null)
                    {
                        Thread.Sleep(50);
                        propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);
                    }
                    if (null != propertyValue)
                        property.SetValue(entityFromDB, propertyValue, null);
                }
            }

            entities.SaveChanges();
            return true;
        }

        #endregion


        #region 获取相关的实体信息
        /// <summary>
        /// 分页_获取指定页的数据集合
        /// </summary>
        /// <typeparam name="T">泛型类型参数</typeparam>
        /// <param name="query">查询条件</param>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="pageNum">当前页号</param>
        /// <param name="pageTotal">总页数</param>
        /// <param name="datasTotal">总数据量</param>
        /// <returns></returns>
        public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query, int pageSize, int pageNum, out int pageTotal, out int datasTotal) where T : class
        {
            var table = GetTable<T>();

            datasTotal = (from t in table
                          select t).Where(query).Count();

            pageTotal = datasTotal / pageSize + 1;

            return (from t in table
                    select t).Where(query).Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
        }
        /// <summary>
        /// 分页_获取指定页的数据集合
        /// </summary>
        /// <typeparam name="T">泛型类型参数</typeparam>
        /// <param name="query">查询条件</param>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="pageNum">当前页号</param>
        /// <param name="pageTotal">总页数</param>
        /// <param name="datasTotal">总数据量</param>
        /// <returns></returns>
        public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query, Func<T, object> orderByDesc, int pageSize, int pageNum, out int pageTotal, out int datasTotal) where T : class
        {
            var table = GetTable<T>();

            datasTotal = (from t in table
                          select t).Where(query).Count();

            pageTotal = (int)Math.Ceiling((double)datasTotal / pageSize);
            return (from t in table
                    select t).Where(query).OrderByDescending(orderByDesc).Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
        }
        /// <summary>
        /// 获取指定条件的实体集合

        /// </summary>
        /// <typeparam name="T">泛型类型参数</typeparam>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query) where T : class
        {
            var table = GetTable<T>();
            return (from t in table
                    select t).Where(query).ToList();
        }

        /// <summary>
        /// 获取指定条件的实体集合

        /// </summary>
        /// <typeparam name="T">泛型类型参数</typeparam>
        /// <param name="query">查询条件</param>      
        /// <param name="orderAsc"></param>       
        /// <returns></returns>
        public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query, bool isAsc, Func<T, object> order) where T : class
        {
            var table = GetTable<T>();
            if (isAsc)
                return (from t in table
                        select t).Where(query).OrderBy(order).ToList();
            return (from t in table
                    select t).Where(query).OrderByDescending(order).ToList();
        }

        public virtual List<T> GetAllEntity<T>() where T : class
        {
            var table = GetTable<T>();
            return (from t in table
                    select t).ToList();
        }
        #endregion


        #region 新增实体
        /// <summary>
        /// 新增单个实体
        /// </summary>
        /// <typeparam name="T">泛型类型参数</typeparam>
        /// <param name="entity">待插入的实体</param>
        /// <returns></returns>
        public virtual void InsertEntitys<T>(T entity) where T : class
        {
            var table = GetTable<T>();

            table.Add(entity);
           
            entities.SaveChanges();
        }

        /// <summary>
        /// 批量新增实体
        /// </summary>
        /// <typeparam name="T">泛型类型参数</typeparam>
        /// <param name="entityList">待添加的实体集合</param>
        /// <returns></returns>
        public virtual void BatchInsertEntity<T>(List<T> entityList) where T : class
        {
            if (entityList.Count > 0)
            {
                var table = GetTable<T>();
                foreach (var item in entityList)
                {
                    table.Add(item);
                   
                }
                entities.SaveChanges();
            }
        }
        #endregion

        #region 删除实体
        /// <summary>
        /// 根据条件删除指定实体
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="query">条件</param>
        /// <returns>bool</returns>
        public virtual void DeleteEntitys<T>(Expression<Func<T, bool>> query) where T : class
        {
            var table = GetTable<T>();
            var toDeletedColl = table.Where(query);
            if (toDeletedColl != null && toDeletedColl.Count() > 0)
            {
                foreach (var item in toDeletedColl)
                {
                    table.Remove(item);
                   
                }
                entities.SaveChanges();
            }
        }
        #endregion
    }
}
View Code

 

2.业务逻辑BLL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DiaryNodeApp.Model;
using DiaryNodeApp.DAL;
using System.Linq.Expressions;

namespace DiaryNodeApp.BLL
{
    public class SysDbBLL
    {
        private SysDbDAL dal { get; set; }
        public SysDbBLL()
        {
            dal = new SysDbDAL();
        }
        public SysDbBLL(string entityName)
        {
            dal = new SysDbDAL(entityName);
        }
        
        public bool Insert<T>(T entity) where T : class
        {
            dal.InsertEntitys(entity);
            return true;
        }

        public bool Delete<T>(Expression<Func<T, bool>> query) where T : class
        {
            dal.DeleteEntitys(query);
            return true;
        }

        public bool Update<T>(T entity) where T : class
        {
            dal.UpdateEntitys(entity);
            return true;
        }
        public T GetSingle<T>(Expression<Func<T, bool>> query) where T : class
        {
            return dal.GetSingleEntity(query);
          
        }

        public List<T> GetAll<T>(Expression<Func<T, bool>> query) where T : class
        {
            return dal.GetAllEntity(query);
          
        }
    }
}
View Code

 

posted @ 2018-03-14 17:10  兴想事成  阅读(241)  评论(0编辑  收藏  举报