public class DALHelper { public static ListSearch () 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); } }