首页 > 代码库 > 园子里的一个Dal类
园子里的一个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); } }
园子里的一个Dal类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。