园子里的一个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 - ) * 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 - ) * 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 - ) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
else
return dbSet.Where(where.Compile()).OrderByDescending(order.Compile()).Skip((pageContent.PageIndex - ) * 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 - ) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
else
return dbSet.Include(include).Where(where.Compile()).OrderByDescending(order.Compile()).Skip((pageContent.PageIndex - ) * 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 - ) * pageContent.PageSize).Take(pageContent.PageSize).ToList();
else
return dbSet.Include(path1).Include(path2).Where(where.Compile()).OrderByDescending(order.Compile()).Skip((pageContent.PageIndex - ) * 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 == )
{
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类的更多相关文章
- 创建一个接口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类另 ...
- Eclipse里选中一个变量后,这个类里的该变量不变色了?
Eclipse里选一个变量后,这个类里的该变量不变色了. 1.使用“Alt+Shift+O”对该提示功能的开/关切换 2.可以在以下设置选中后的文本提示颜色 window--> Prefere ...
- Entity Framework 6 Recipes 2nd Edition(11-6)译 -> 从一个”模型定义”函数里返回一个复杂类型
11-6.从一个”模型定义”函数里返回一个复杂类型 问题 想要从一个”模型定义”函数返回一个复杂类型 解决方案 假设我们有一个病人(patient)和他们访客(visit)的模型,如 Figure 1 ...
- PHP用单例模式实现一个数据库类
使用单例模式的出发点: 1.php的应用主要在于数据库应用, 所以一个应用中会存在大量的数据库操作, 使用单例模式, 则可以避免大量的new 操作消耗的资源. 2.如果系统中需要有一个类来全局控制某些 ...
- cookiecutter-flask生成的框架里边自带了一个CRUDMixin类
单元测试的必要性 之前曾经写过一篇讲单元测试的,正好最近也在实践和摸索.我似乎有种洁癖,就是我会严格遵守流程性的东西,比如测试,注释和文档等.目前就职的公司在我接手项目的时候是没有一行单元测试的,我挺 ...
- 使用libzplay库封装一个音频类
装载请说明原地址,谢谢~~ 前两天我已经封装好一个duilib中使用的webkit内核的浏览器控件和一个基于vlc的用于播放视频的视频控件,这两个控件可以分别用在放酷狗播放器的乐库功能和MV ...
- 微软在 .NET 3.5 新增了一个 HashSet 类,在 .NET 4 新增了一个 SortedSet 类,本文介绍他们的特性,并比较他们的异同。
微软在 .NET 3.5 新增了一个 HashSet 类,在 .NET 4 新增了一个 SortedSet 类,本文介绍他们的特性,并比较他们的异同. .NET Collection 函数库的 Has ...
- Away3d 基础 1 ---对一个简单类的解释
转自:http://www.cnblogs.com/nooon/archive/2009/05/16/1458334.html 原英文地址: http://www.flashmagazine.com/ ...
- 怎样从一个DLL中导出一个C++类
原文作者:Alex Blekhman 翻译:朱金灿 原文来源: http://www.codeproject.com/KB/cpp/howto_export_cpp_classes.aspx 译 ...
随机推荐
- 【C#】C#创建Windows Service服务
目录结构: contents structure [+] 创建Windows服务 配置 安装Windows服务 在Visual Studio中调试 常见问题 最近写了一个TCP连接的程序,由于这种通信 ...
- 【MySQL】mysql出现错误“ Every derived table must have its own alias”
Every derived table must have its own alias 这句话的意思是说每个派生出来的表都必须有一个自己的别名 一般在多表查询时,会出现此错误. 因为,进行嵌套查询的时 ...
- Java与C++Socket通讯注意事项
c++与java进行socket通信时注意事项 原文链接: http://my.oschina.net/ypimgt/blog/106439 因为java发送的都是网络字节序(big-endium), ...
- unity, Collider2D.bounds的一个坑
Note that this will be an empty bounding box if the collider is disabled or the game object is inact ...
- 四、s3c2440 裸机开发 通用异步收发器UARN
四.通用异步收发器UARN 原文地址 http://blog.csdn.net/woshidahuaidan2011/article/details/51137047 by jaosn Email: ...
- Shiro系列(3) - What is shiro?
什么是shiro? Shiro是apache的一个开源权限管理的框架,它实现用户身份认证,权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架 使用shiro来实现权限管理,可以非常有效的提高 ...
- TCP/IP 三次握手,温故知新
今天看网络编程.又又一次看了一遍三次握手.曾经只知道连接有三次握手.今天发现原来断开也有三次握手. 三次握手:指通信两方彼此交换三次信息. 三次握手是在存在数据报丢失.反复和延迟的情况下,确保通信两方 ...
- spring cloud 路由Zuul的高可用
Zuul的高可用非常关键,因为外部请求到后端微服务的流量都会经过Zuul.故而在生产环境中,我们一般都需要部署高可用的Zuul以避免单点故障. 笔者分两种场景讨论Zuul的高可用. Zuul客户端也注 ...
- (原创)如何使用boost.asio写一个简单的通信程序(一)
boost.asio相信很多人听说过,作为一个跨平台的通信库,它的性能是很出色的,然而它却谈不上好用,里面有很多地方稍不注意就会出错,要正确的用好asio还是需要花一番精力去学习和实践的,本文将通过介 ...
- 利用babel-cli搭建支持ES6的node环境
现在ES6盛行,开始大量使用ES6的特性敲代码,但限于Node.js本身对ES6的特性支持的不够完备,那么需要借助于其他工具来完成. 基本上,现在都直接写ES6的代码,然后使用babel-cli提供的 ...