public static void AppendModel(ModelBase item, out Exception e) { e = null; try { ModelMapping mp = new ModelMapping(item.GetType()); ModelFactoryCollection.AppendModel(item, mp, out e); if (e == null) { ModelCacheManager.Instance.Save(item); } else { return; } } catch (Exception ex) { e = ex; } }
/// <summary> /// 移除一个模型对象 /// </summary> /// <param name="value">要移除的模型对象</param> public virtual void Remove(ModelBase value) { Type type = value.GetType(); while (type != null) { ModelCollection modelCollection = this[type.Name]; if (modelCollection != null && modelCollection.Contains(value.Rid)) { ModelBase model = modelCollection[value.Rid]; modelCollection.Remove(model); modelCollection.RemoveAltKey(model); } if (modelCollection.ModelField.Parent != null) { type = modelCollection.ModelField.Parent.ModelType; } else { type = null; } } }
/// <summary> /// 存储一个模型对象,如果存根管理器中没有该对象则增加该对象 /// </summary> /// <param name="value">要存储的模型对象</param> public virtual void Save(ModelBase value) { Type type = value.GetType(); ModelCollection modelCollection = this[type]; if (modelCollection == null) { return; } if (modelCollection.Contains(value.Rid)) { bool altKeyChanged = false; ModelBase model = modelCollection[value.Rid]; if (model.AlternateKey != value.AlternateKey) { modelCollection.RemoveAltKey(model); altKeyChanged = true; } ModelMapping mf = modelCollection.ModelField; foreach (PropertyFieldPair pfp in mf.PropertyFields) { object obj = pfp.Property.GetValue(value, null); pfp.Property.SetValue(model, obj, null); } while (mf.Parent != null) { foreach (PropertyFieldPair pfp in mf.Parent.PropertyFields) { object obj = pfp.Property.GetValue(value, null); pfp.Property.SetValue(model, obj, null); } mf = mf.Parent; } if (altKeyChanged) { modelCollection.AddAltKey(value); } } else { modelCollection.Add(value); modelCollection.AddAltKey(value); while (modelCollection.ModelField.Parent != null) { modelCollection = this[modelCollection.ModelField.Parent.ModelType]; if (modelCollection.Contains(value.Rid)) { ModelBase model = modelCollection[value.Rid]; ModelMapping mf = modelCollection.ModelField; foreach (PropertyFieldPair pfp in mf.PropertyFields) { object obj = pfp.Property.GetValue(model, null); pfp.Property.SetValue(value, obj, null); } modelCollection.Remove(value.Rid); modelCollection.RemoveAltKey(value); } modelCollection.Add(value); modelCollection.AddAltKey(value); type = type.BaseType; } } }