zoukankan      html  css  js  c++  java
  • 园子里的一个Dal类

    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);
            }
        }
  • 相关阅读:
    二叉树中序遍历及后序遍历(下)
    完全二叉树和三序遍历算法(先序)(上)
    分布式调度——zookeeper
    Linux版JDK环境安装
    Redis的事务
    Redis的持久化——RDB与AOF
    分布式缓存——Redis
    非关系型数据库
    分布式存储——MySQL
    分布式系统
  • 原文地址:https://www.cnblogs.com/lzhp/p/4160213.html
Copyright © 2011-2022 走看看