博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
园子里的一个Dal类
阅读量:5046 次
发布时间:2019-06-12

本文共 10977 字,大约阅读时间需要 36 分钟。

public class DALHelper    {        public static List
Search
() where T : SH_SetBase { using (var db = new ShopContext()) { var dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { return dbSet.Where(o => !o.IsDelete).ToList(); } return dbSet.ToList(); } } public static List
Search
(Expression
> where) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(Expression.And(where.Body, invokedExpr), where.Parameters); return dbSet.Where(where3.Compile()).ToList(); } return dbSet.Where(where).ToList(); } } public static List
Search
(Expression
> where, PageContent pageContent) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(Expression.And(where.Body, invokedExpr), where.Parameters); where = where3; } pageContent.TotalLogs = Count
(where); return dbSet.Where(where.Compile()).Skip((pageContent.PageIndex - 1) * pageContent.PageSize).Take(pageContent.PageSize).ToList(); } } public static List
Search
(Expression
> include, Expression
> where) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(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
Search
(Expression
> include, Expression
> where, PageContent pageContent) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(Expression.And(where.Body, invokedExpr), where.Parameters); where = where3; } pageContent.TotalLogs = Count
(where); return dbSet.Include(include).Where(where.Compile()).Skip((pageContent.PageIndex - 1) * pageContent.PageSize).Take(pageContent.PageSize).ToList(); } } public static List
Search
(Expression
> where, PageContent pageContent, Expression
> order, bool isAsc) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(Expression.And(where.Body, invokedExpr), where.Parameters); where = where3; } pageContent.TotalLogs = Count
(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
Search
(Expression
> include, Expression
> where, PageContent pageContent, Expression
> order, bool isAsc) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(Expression.And(where.Body, invokedExpr), where.Parameters); where = where3; } pageContent.TotalLogs = Count
(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
Search
(Expression
> path1, Expression
> path2, Expression
> where, PageContent pageContent, Expression
> order, bool isAsc) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(Expression.And(where.Body, invokedExpr), where.Parameters); where = where3; } pageContent.TotalLogs = Count
(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
(Expression
> where) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(Expression.And(where.Body, invokedExpr), where.Parameters); return dbSet.FirstOrDefault(where3.Compile()) != null; } return dbSet.FirstOrDefault(where.Compile()) != null; } } public static int Count
(Expression
> where) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(Expression.And(where.Body, invokedExpr), where.Parameters); return dbSet.Count(where3.Compile()); } return dbSet.Count(where); } } public static decimal Sum
(Expression
> where, Expression
> selector) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(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
(Expression
> where, Expression
> selector) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(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
(Expression
> where) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; if (typeof(T).IsSubclassOf(typeof(SH_SetBase))) { Expression
> where2 = (o => (o as SH_SetBase).IsDelete == false); var invokedExpr = Expression.Invoke(where2, where.Parameters.Cast
()); Expression
> where3 = Expression.Lambda
>(Expression.And(where.Body, invokedExpr), where.Parameters); return dbSet.FirstOrDefault(where3.Compile()); } return dbSet.FirstOrDefault(where.Compile()); } } public static T Find
(long id) where T : SH_Base { using (ShopContext db = new ShopContext()) { DbSet
dbSet = GetDBSet(db, typeof(T)) as DbSet
; 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); } }

 

转载于:https://www.cnblogs.com/lzhp/p/4160213.html

你可能感兴趣的文章
爬虫基础
查看>>
jquery.lazyload延迟加载图片第一屏问题
查看>>
HDU 1011 Starship Troopers (树形DP)
查看>>
手把手教你写DI_1_DI框架有什么?
查看>>
.net常见的一些面试题
查看>>
OGRE 源码编译方法
查看>>
上周热点回顾(10.20-10.26)
查看>>
C#正则表达式引发的CPU跑高问题以及解决方法
查看>>
云计算之路-阿里云上:“黑色30秒”走了,“黑色1秒”来了,真相也许大白了...
查看>>
APScheduler调度器
查看>>
设计模式——原型模式
查看>>
【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.1.CSS框架和其他功能
查看>>
如何一个pdf文件拆分为若干个pdf文件
查看>>
web.xml中listener、 filter、servlet 加载顺序及其详解
查看>>
前端chrome浏览器调试总结
查看>>
获取手机验证码修改
查看>>
数据库连接
查看>>
python中数据的变量和字符串的常用使用方法
查看>>
等价类划分进阶篇
查看>>
delphi.指针.PChar
查看>>