园子里的一个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);
        }
    }
时间: 2024-11-08 18:23:44

园子里的一个Dal类的相关文章

Eclipse里选中一个变量后,这个类里的该变量不变色了?

Eclipse里选一个变量后,这个类里的该变量不变色了. 1.使用“Alt+Shift+O”对该提示功能的开/关切换 2.可以在以下设置选中后的文本提示颜色  window--> Preferences--> General--> Editors--> Text Editors--> Annotations Eclipse里选中一个变量后,这个类里的该变量不变色了?

创建一个接口Shape,其中有抽象方法area,类Circle 、Rectangle实现area方法计算其面积并返回。又有Star实现Shape的area方法,其返回值是0,Star类另有一返回值boolean型方法isStar;在main方法里创建一个Vector,根据随机数的不同向其中加入Shape的不同子类对象(如是1,生成Circle对象;如是2,生成Rectangle对象;如是3,生成S

题目补充: 创建一个接口Shape,其中有抽象方法area,类Circle .Rectangle实现area方法计算其面积并返回. 又有Star实现Shape的area方法,其返回值是0,Star类另有一返回值boolean型方法isStar: 在main方法里创建一个Vector,根据随机数的不同向其中加入Shape的不同子类对象(如是1,生成Circle对象: 如是2,生成Rectangle对象:如是3,生成Star对象).然后将Vector中元素依次取出,判断其是否为Star类.如是返回其

PHP用单例模式实现一个数据库类

使用单例模式的出发点: 1.php的应用主要在于数据库应用, 所以一个应用中会存在大量的数据库操作, 使用单例模式, 则可以避免大量的new 操作消耗的资源. 2.如果系统中需要有一个类来全局控制某些配置信息, 那么使用单例模式可以很方便的实现. 这个可以参看ZF的FrontController部分. 3.在一次页面请求中, 便于进行调试, 因为所有的代码(例如数据库操作类db)都集中在一个类中, 我们可以在类中设置钩子, 输出日志,从而避免到处var_dump, echo. 创造单例注意: 1

MyBatis里字段到枚举类的映射

一.简介 我们在用MyBatis里,很多时间有这样一个需求:bean里有个属性是枚举,在DB存储时我们想存的枚举的代号,从DB拿出来时想直接映射成目标枚举类型,也即代号字段与Java枚举类的相互类型转换. 当然,你可以为每个枚举写一个MyEnumTypeHandler,但问题是要为每个类都写一个TypeHandler,过于繁琐. 有了泛型,一个通用的TypeHandler直接搞定. 二.源码 源码详见:spring-mybatis-test package com.adu.spring_test

在python里如何动态添加类的动态属性呢?

body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif; font-size: 10.5pt; line-height: 1.5;

创建一个Fraction类(分数)实现分数的加减乘除,比较大小、约分等方法。要求:为类添加属性

//在.h文件里 {     NSInteger _numerator;     //分子     NSInteger _denominator;   //分母     }   //属性 @property (nonatomic) NSInteger numerator; @property (nonatomic) NSInteger denominator; //初始化   - (id)initwithNumerator:(NSInteger)numerator denominator:(NS

不能继承于QObject的类就一定不能使用信号槽?(用一个代理类进行发射就行了)

首先不能继承QObject的情况在开发中遇到得并不多,笔者在一年多的Qt项目开发中只遇到两三次.而且都是因为引进了第三方库导致编译过程中报错. 要想解决这个问题其实不难,因为笔者遇到的问题都是想定义一个信号,定义的槽情况没有遇到过,可以提出一个想法,有需要的朋友试一下. 要想实现信号发送,可以定义一个信号发射器类继承于QObject,然后在想要发送信息的类里定义一个信号发射器: 图1 图2 只要在想触发信号的地方调用信号发射器的sendMsg()函数就可以让信号发射器将信号发出. 要想实现槽的话

实体类作为另一个实体类的属性

如果一个实体类作为另一个实体类的属性,如果对该属性赋值的时候直接赋一个对象就行了,如果有多个对象同时赋给那个属性 就可以用List集合去接收,就像微信开发里面的图文消息 ,如果回复的是多条图文消息就用list集合去装t它们. 今天早上被技术总监说了一顿,就是他将很多种类都写在一个文件里面,而我一般都是将类写在一个单独的文件里面,我很奇怪这种写法.在QQ群里和人讨论了一下,觉得类应该是单独的建一个文件去写,这样不管是后期维护,还是查阅修改,都会很方便.还有一种所有实体类加partial.需要手动改

ReSharper的功能真的很强大主要是针对代码规范和优化,园子里介绍的也不少,如果你没有安装,那我只能表示你们会相见恨晚

二.ReSHarper 代码规范.单元测试.... ReSharper的功能真的很强大,主要是针对代码规范和优化,园子里介绍的也不少,如果你没有安装,那我只能表示你们会相见恨晚! 1.像命名不规范,foreach转化为linq, 用var来命名,出名的Ctrl+alt+Enter 自动修改,清除多余的变量和引用,Ctrl+K+D(这是VS自带的,搞错了)文档对齐,Ctrl+R+R 命名替换 等等我就不再多说了.用过的人都知道. 2.关键是这个货居然还能调整CSS. 比如: 比自己敲要来的快,当然