园子里的一个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 译 ...
随机推荐
- HANA初印象
今天手懒,看了一些SAP HANA 的一些外文介绍,不翻译了,直接剽窃过来,供参考. 1. HANA 是什么东西? “HANA doesn't actually mean anything, but ...
- Effective C++ 34
34.将文件间的编译依赖性降到最低. 对于一个大型程序,其结构是错综复杂的.当你对一个类进行一些改动时.改动的不是接口,而是类的实现,即仅仅是一些细节部分,但又一次生成程序时,所实用到这个类的的文件都 ...
- WGAN (原理解析)
在GAN的相关研究如火如荼甚至可以说是泛滥的今天,一篇新鲜出炉的arXiv论文<Wasserstein GAN>却在Reddit的Machine Learning频道火了,连Goodfel ...
- iphone5刷机教程
如果不想麻烦可以在越狱之后添加源,cydia.china3gpp.com打ios7的补丁就可以了 机器为iphone5 美国sprint有锁版 1. 首先备份需要的程序和数据(把各种缓存的影片删掉再备 ...
- Datagrid分页、排序、删除代码
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="fal ...
- destoon入门实例与常见问题
收集了一些destoon入门实例与常见问题,大家做个参考. 链接如下: destoon忘记后台密码怎么办?destoon找回管理员密码 忘记destoon管理员后台账号密码怎么办?解决方法 desto ...
- Atitti 过程导向 vs 结果导向 attlax的策
Atitti 过程导向 vs 结果导向 attilax的策略 1. 结果导向的问题 以结果为导向”的明显弊端2 1.1. 白猫黑猫的策略是错误的2 1.2. 为了目的不择手段,完全违背了程序正义原则2 ...
- 【Unity】7.1 Input类的方法和变量
分类:Unity.C#.VS2015 创建日期:2016-04-21 一.简介 在Input类中,Key与物理按键对应,例如键盘.鼠标.摇杆上的按键,其映射关系无法改变,程序员可以通过按键名称或者按键 ...
- hdu 2680 Choose the best route (dijkstra算法)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...
- Warning Template OS Linux: /etc/passwd has been changed on {HOST.NAME} {monitor:vfs.file.cksum[/etc/passwd].diff(0)}>0 Unknown
# ll -h /etc/passwd -rw-r--r-- 1 root root 1.5K Apr 15 16:10 /etc/passwd 让zabbix 可以登录服务器 # mkdir /ho ...