实体类调用泛型父类中的静态方法中执行CRUD——第二版
1 using System; 2 using System.Collections.Generic; 3 using System.Data.Entity; 4 using System.Linq; 5 6 namespace WebHub { 7 public abstract class HubContext<DB, T> 8 where DB : DbContext, new() 9 where T : class { 10 public static void Remove(object idOrObj) { 11 using (var db = new DB()) { 12 if (idOrObj.GetType() == typeof(T)) { 13 db.Set<T>().Remove(idOrObj as T); 14 db.SaveChanges(); 15 } else { 16 var t = db.Set<T>().Find(idOrObj); 17 db.Set<T>().Remove(t); 18 db.SaveChanges(); 19 } 20 } 21 } 22 public static T Add(T t) { 23 using (var db = new DB()) { 24 db.Set<T>().Add(t); 25 db.SaveChanges(); 26 return t; 27 } 28 } 29 public static T Add(T t, Action failCallback) { 30 using (var db = new DB()) { 31 db.Set<T>().Add(t); 32 try { 33 db.SaveChanges(); 34 } catch (Exception) { 35 failCallback(); 36 return default(T); 37 } 38 return t; 39 } 40 } 41 public static TR Execute<TR>(object id, Func<T, TR> func) { 42 var props = typeof(T).GetProperties(); 43 using (var db = new DB()) { 44 var t = db.Set<T>().Find(id); 45 if (t == null) { 46 return default(TR); 47 }//TODO:if no "ToList()" below, won't work.WHY? 48 var oldValues = props.Select(p => p.GetValue(t, null) ?? string.Empty).ToList(); 49 TR r; 50 try { 51 r = func(t); 52 } catch { 53 return default(TR); 54 } 55 var newValues = props.Select(p => p.GetValue(t, null) ?? string.Empty).ToList(); 56 57 for (int i = 0; i < props.Count(); i++) { 58 if (newValues[i].GetHashCode() != oldValues[i].GetHashCode()) { 59 db.SaveChanges(); 60 } 61 } 62 return r; 63 } 64 } 65 66 public static int Execute(object id, Action<T> action) { 67 var props = typeof(T).GetProperties(); 68 using (var db = new DB()) { 69 var t = db.Set<T>().Find(id); 70 if (t == null) { 71 return 0; 72 } 73 var oldValues = props.Select(p => p.GetValue(t, null) ?? string.Empty).ToList(); 74 try { 75 action(t); 76 } catch { 77 return 0; 78 } 79 var newValues = props.Select(p => p.GetValue(t, null) ?? string.Empty).ToList(); 80 for (int i = 0; i < props.Count(); i++) { 81 if (newValues[i].GetHashCode() != oldValues[i].GetHashCode()) { 82 var r = db.SaveChanges(); 83 return r; 84 } 85 } 86 return 0; 87 } 88 } 89 public static T First() { 90 using (var db = new DB()) { 91 var r = db.Set<T>().FirstOrDefault(); 92 return r; 93 } 94 } 95 public static T First(object id) { 96 using (var db = new DB()) { 97 return db.Set<T>().Find(id); 98 } 99 } 100 public static T First(Func<T, bool> predicate) { 101 using (var db = new DB()) { 102 var r = db.Set<T>().FirstOrDefault(predicate); 103 return r; 104 } 105 } 106 public static T First<P>(Func<T, bool> predicate, Func<T, P> orderby) { 107 var r = First(predicate, orderby, false); 108 return r; 109 } 110 public static T First<P>(Func<T, bool> predicate, Func<T, P> orderby, bool desc) { 111 using (var db = new DB()) { 112 T r; 113 if (desc) { 114 r = db.Set<T>().Where(predicate).OrderByDescending(orderby).FirstOrDefault(); 115 } else { 116 r = db.Set<T>().Where(predicate).OrderBy(orderby).FirstOrDefault(); 117 } 118 return r; 119 } 120 } 121 public static int Count() { 122 using (var db = new DB()) { 123 return db.Set<T>().Count(); 124 } 125 } 126 public static int Count(Func<T, bool> prediacate) { 127 using (var db = new DB()) { 128 return db.Set<T>().Count(prediacate); 129 } 130 } 131 public static IEnumerable<T> All() { 132 using (var db = new DB()) { 133 var r = db.Set<T>().ToList(); 134 return r; 135 } 136 } 137 public static bool Exists(object id) { 138 using (var db = new DB()) { 139 var r = db.Set<T>().Find(id); 140 return r != null; 141 } 142 } 143 public static bool Exists(Func<T, bool> where) { 144 using (var db = new DB()) { 145 var r = db.Set<T>().Any(where); 146 return r; 147 } 148 } 149 public static IEnumerable<T> Find(Func<T, bool> where) { 150 using (var db = new DB()) { 151 var rr = db.Set<T>().Where(where).ToList(); 152 return rr; 153 } 154 } 155 public static IEnumerable<T> Find<P>(Func<T, bool> where, Func<T, P> orderby) { 156 using (var db = new DB()) { 157 var r = db.Set<T>().Where(where).OrderBy(orderby).ToList(); 158 return r; 159 } 160 } 161 public static IEnumerable<T> Find(Func<T, bool> where, int skip, int take) { 162 using (var db = new DB()) { 163 return db.Set<T>().Where(where).Skip(skip).Take(take).ToList(); 164 } 165 } 166 public static IEnumerable<T> Find(Func<T, bool> where, int skip, int take, out int all) { 167 using (var db = new DB()) { 168 all = db.Set<T>().Count(where); 169 return db.Set<T>().Where(where).Skip(skip).Take(take).ToList(); 170 } 171 } 172 public static IEnumerable<T> Find<P>(Func<T, bool> where, Func<T, P> orderby, int skip, int take) { 173 using (var db = new DB()) { 174 var r = db.Set<T>().Where(where).OrderBy(orderby).Skip(skip).Take(take).ToList(); 175 return r; 176 } 177 } 178 public static IEnumerable<T> Find<P>(Func<T, bool> where, Func<T, P> orderby, int skip, int take, out int all) { 179 using (var db = new DB()) { 180 all = db.Set<T>().Count(where); 181 var r = db.Set<T>().Where(where).OrderBy(orderby).Skip(skip).Take(take).ToList(); 182 return r; 183 } 184 } 185 public static IEnumerable<T> Find<P>(Func<T, bool> where, Func<T, P> orderby, DataPager p) { 186 using (var db = new DB()) { 187 p.Total = db.Set<T>().Count(where); 188 var r = db.Set<T>().Where(where).OrderBy(orderby).Skip(p.Current * p.Size).Take(p.Size).ToList(); 189 return r; 190 } 191 } 192 } 193 }