码迷,mamicode.com
首页 > 其他好文 > 详细

园子里的一个Dal类

时间:2014-12-12 18:33:41      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   ar   color   sp   on   div   

public class DALHelper
    {
        public static List<T> Search<T>() where T : SH_SetBase
        {
            using (var db = new ShopContext())
            {
                var dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    return dbSet.Where(o => !o.IsDelete).ToList();
                }
                return dbSet.ToList();
            }
        }

        public static List<T> Search<T>(Expression<Func<T, bool>> where) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    return dbSet.Where(where3.Compile()).ToList();
                }
                return dbSet.Where(where).ToList();
            }
        }

        public static List<T> Search<T>(Expression<Func<T, bool>> where, PageContent pageContent) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    where = where3;
                }
                pageContent.TotalLogs = Count<T>(where);
                return dbSet.Where(where.Compile()).Skip((pageContent.PageIndex - 1) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
            }
        }

        public static List<T> Search<T>(Expression<Func<T, object>> include, Expression<Func<T, bool>> where) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    return dbSet.Include(include).Where(where3.Compile()).ToList();
                }
                return dbSet.Include(include).Where(where).ToList();
            }
        }

        public static List<T> Search<T>(Expression<Func<T, object>> include, Expression<Func<T, bool>> where, PageContent pageContent) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    where = where3;
                }
                pageContent.TotalLogs = Count<T>(where);
                return dbSet.Include(include).Where(where.Compile()).Skip((pageContent.PageIndex - 1) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
            }
        }

        public static List<T> Search<T>(Expression<Func<T, bool>> where, PageContent pageContent, Expression<Func<T, object>> order, bool isAsc) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    where = where3;
                }
                pageContent.TotalLogs = Count<T>(where);
                if (isAsc)
                    return dbSet.Where(where.Compile()).OrderBy(order.Compile()).Skip((pageContent.PageIndex - 1) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
                else
                    return dbSet.Where(where.Compile()).OrderByDescending(order.Compile()).Skip((pageContent.PageIndex - 1) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
            }
        }

        public static List<T> Search<T>(Expression<Func<T, object>> include, Expression<Func<T, bool>> where, PageContent pageContent, Expression<Func<T, object>> order, bool isAsc) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    where = where3;
                }
                pageContent.TotalLogs = Count<T>(where);
                if (isAsc)
                    return dbSet.Include(include).Where(where.Compile()).OrderBy(order.Compile()).Skip((pageContent.PageIndex - 1) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
                else
                    return dbSet.Include(include).Where(where.Compile()).OrderByDescending(order.Compile()).Skip((pageContent.PageIndex - 1) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
            }
        }

        public static List<T> Search<T>(Expression<Func<T, object>> path1, Expression<Func<T, object>> path2, Expression<Func<T, bool>> where, PageContent pageContent, Expression<Func<T, object>> order, bool isAsc) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    where = where3;
                }
                pageContent.TotalLogs = Count<T>(where);
                if (isAsc)
                    return dbSet.Include(path1).Include(path2).Where(where.Compile()).OrderBy(order.Compile()).Skip((pageContent.PageIndex - 1) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
                else
                    return dbSet.Include(path1).Include(path2).Where(where.Compile()).OrderByDescending(order.Compile()).Skip((pageContent.PageIndex - 1) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
            }
        }

        public static bool Exist<T>(Expression<Func<T, bool>> where) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    return dbSet.FirstOrDefault(where3.Compile()) != null;
                }
                return dbSet.FirstOrDefault(where.Compile()) != null;
            }
        }

        public static int Count<T>(Expression<Func<T, bool>> where) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    return dbSet.Count(where3.Compile());
                }
                return dbSet.Count(where);
            }
        }

        public static decimal Sum<T>(Expression<Func<T, bool>> where, Expression<Func<T, decimal>> selector) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    return dbSet.Where(where3.Compile()).Sum(selector.Compile());
                }
                return dbSet.Where(where.Compile()).Sum(selector.Compile());
            }
        }

        public static int Sum<T>(Expression<Func<T, bool>> where, Expression<Func<T, int>> selector) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    return dbSet.Where(where3.Compile()).Sum(selector.Compile());
                }
                return dbSet.Where(where.Compile()).Sum(selector.Compile());
            }
        }

        public static T SearchObject<T>(Expression<Func<T, bool>> where) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    Expression<Func<T, bool>> where2 = (o => (o as SH_SetBase).IsDelete == false);
                    var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast<Expression>());
                    Expression<Func<T, bool>> where3 = Expression.Lambda<Func<T, bool>>(Expression.And(where.Body, invokedExpr), where.Parameters);
                    return dbSet.FirstOrDefault(where3.Compile());
                }
                return dbSet.FirstOrDefault(where.Compile());
            }
        }

        public static T Find<T>(long id) where T : SH_Base
        {
            using (ShopContext db = new ShopContext())
            {
                DbSet<T> dbSet = GetDBSet(db, typeof(T)) as DbSet<T>;
                if (typeof(T).IsSubclassOf(typeof(SH_SetBase)))
                {
                    SH_SetBase model = dbSet.Find(id) as SH_SetBase;
                    if (model != null && !model.IsDelete)
                        return model as T;
                }
                return dbSet.Find(id) as T;
            }
        }

        public static bool Save(SH_Base model)
        {
            using (ShopContext db = new ShopContext())
            {
                object dbSet = GetDBSet(db, model);
                if (model.ID == 0)
                {
                    CallMethod(dbSet, "Add", new object[] { model });
                }
                else
                {
                    CallMethod(dbSet, "Attach", new object[] { model });
                    db.Entry(model).State = EntityState.Modified;
                }
                if (model.GetType().IsSubclassOf(typeof(SH_SetBase)))
                {
                    ((SH_SetBase)model).LastUpdateTime = DateTime.Now;
                    ((SH_SetBase)model).IsDelete = false;
                }
                else
                {
                    ((SH_LogBase)model).LogTime = DateTime.Now;
                }
                db.SaveChanges();
                return true;
            }
        }

        public static bool Delete(SH_Base model)
        {
            using (ShopContext db = new ShopContext())
            {
                if (model.GetType().IsSubclassOf(typeof(SH_SetBase)))
                {
                    ((SH_SetBase)model).LastUpdateTime = DateTime.Now;
                    ((SH_SetBase)model).IsDelete = true;
                    db.Entry(model).State = EntityState.Modified;
                    db.SaveChanges();
                    return true;
                }

                object dbSet = GetDBSet(db, model);
                CallMethod(dbSet, "Remove", new object[] { model });
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                return true;
            }
        }

        private static object GetDBSet(ShopContext db, SH_Base model)
        {
            string modelName = ObjectContext.GetObjectType(model.GetType()).Name;
            modelName = modelName.Replace("SH_", "");
            Type type = db.GetType();
            PropertyInfo property = type.GetProperty(modelName);
            object dbSet = property.GetValue(db);
            return dbSet;
        }

        private static object GetDBSet(ShopContext db, Type type)
        {
            type = ObjectContext.GetObjectType(type);
            string modelName = type.Name;
            modelName = modelName.Replace("SH_", "");
            PropertyInfo property = db.GetType().GetProperty(modelName);
            object dbSet = property.GetValue(db);
            return dbSet;
        }

        private static object CallMethod(object obj, string methodName, object[] parms)
        {
            Type type = obj.GetType();
            MethodInfo methodInfo = type.GetMethod(methodName);
            return methodInfo.Invoke(obj, parms);
        }
    }

 

园子里的一个Dal类

标签:des   style   blog   io   ar   color   sp   on   div   

原文地址:http://www.cnblogs.com/lzhp/p/4160213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!