public void InvokeOnModifiedHandlers() { if (IsModified()) { OnEntityModified?.Invoke(this); } }
public async Task <object> Modify(string token, dynamic data, string json = "") { try { data = data ?? JsonConvert.DeserializeObject <dynamic>(json); if (data == null) { throw new Exception("解析Json对象失败"); } PropertyInfo[] pis = EntityTool.GetKeyProperty <T>(); if (data.GetType() == typeof(JObject)) { string str = data.ToString(); T t = JsonConvert.DeserializeObject <T>(str); if (t is BaseEntity) { (t as BaseEntity).Modifier = new UserService().GetByToken(token).Name; } JObject jObject = data as JObject; List <object> ids = new List <object>(); foreach (var p in pis) { PropertyInfo tp = t.GetType().GetProperty(p.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (tp == null) { throw new Exception("实体未包含主键:" + p.Name); } ids.Add(tp.GetValue(t)); } T model = await Get(ids.ToArray()); if (model == null) { throw new Exception("查找修改对象失败"); } foreach (var item in jObject) { PropertyInfo mp = model.GetType().GetProperty(item.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (mp != null) { PropertyInfo tp = t.GetType().GetProperty(item.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); mp.SetValue(model, tp.GetValue(t)); } } List <T> list = new List <T>() { model }; OnEntityModifing?.Invoke(ref list); int r = await Put(model); OnEntityModified?.Invoke(ref list); return(r); } else if (data.GetType() == typeof(JArray)) { string str = data.ToString(); JArray jarray = JsonConvert.DeserializeObject(str) as JArray; List <T> list = JsonConvert.DeserializeObject <List <T> >(str); List <T> modellist = new List <T>(); for (int i = 0; i < list.Count; i++) { var t = list[i]; JObject jObject = jarray[i] as JObject; List <object> ids = new List <object>(); if (t is BaseEntity) { (t as BaseEntity).Modifier = new UserService().GetByToken(token).Name; } foreach (var p in pis) { PropertyInfo tp = t.GetType().GetProperty(p.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (tp == null) { throw new Exception("实体未包含主键:" + p.Name); } ids.Add(tp.GetValue(t)); } T model = await Get(ids.ToArray()); if (model == null) { throw new Exception("查找修改对象失败"); } foreach (var item in jObject) { PropertyInfo mp = model.GetType().GetProperty(item.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (mp != null) { PropertyInfo tp = t.GetType().GetProperty(item.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); mp.SetValue(model, tp.GetValue(t)); } } modellist.Add(model); } OnEntityModifing?.Invoke(ref modellist); int r = await PutList(modellist); OnEntityModified?.Invoke(ref modellist); return(r); } throw new Exception("解析Json对象失败"); } catch (Exception e) { throw e; } }