/// <summary> /// 创建对象集合 /// </summary> /// <param name="type">指定类型</param> /// <returns></returns> internal ModelCollection CreateModelCollection(Type type) { if (!type.IsSubclassOf(typeof(ModelBase))) { return(null); } ModelCollection modelCollection; if (!typeDictionary.TryGetValue(type.Name, out modelCollection)) { modelCollection = new ModelCollection(); modelCollection.ModelType = type; typeDictionary.Add(type.Name, modelCollection); /* 田濛2012/5/15 添加插入表明字典 */ DbTableAttribute dbTableAttribute = Attribute.GetCustomAttribute(type, typeof(DbTableAttribute)) as DbTableAttribute; if (dbTableAttribute != null) { if (!string.IsNullOrWhiteSpace(dbTableAttribute.TableName)) { tableDictionary.Add(dbTableAttribute.TableName, modelCollection); } else { tableDictionary.Add(type.Name, modelCollection); } } ///////////////////////// if (modelCollection.ModelField.Parent != null) { modelCollection.BaseCollection = CreateModelCollection(modelCollection.ModelField.Parent.ModelType); } return(modelCollection); } return(modelCollection); }
/// <summary> /// 通过远程传输的模型对象获取本地对应的模型对象 /// </summary> /// <param name="value">远程传输来的模型对象</param> /// <returns>本地模型对象</returns> public ModelBase this[ModelBase value] { get { ModelCollection modelDictionary = this[value.GetType()]; if (modelDictionary != null && modelDictionary.Contains(value.Rid)) { return(modelDictionary[value.Rid]); } return(null); } }
internal void BuildFromRootModelCollection(ModelCollection rootCollection, PropertyInfo childProperty, int parentRid) { m_childProperty = childProperty; this.rootCollection = rootCollection; this.parentRid = parentRid; rootCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(RootCollection_CollectionChanged); foreach (ModelBase child in rootCollection) { int rid = (int)childProperty.GetValue(child, null); if (rid == parentRid) { this.Add(child); } } }
/// <summary> /// 获取一个模型对象 /// </summary> /// <param name="type">模型对象的类型</param> /// <param name="altKey">模型对象的可选索引键</param> /// <returns>模型对象</returns> public ModelBase this[Type type, string altKey] { get { try { ModelCollection modelDictionary = this[type]; return(modelDictionary[altKey]); } catch { return(null); } } }
/// <summary> /// 获取一个模型对象 /// </summary> /// <param name="typeName">模型对象的类型名称</param> /// <param name="altKey">模型对象的可选索引键</param> /// <returns>模型对象</returns> public ModelBase this[string typeName, string altKey] { get { try { ModelCollection modelDictionary = this[typeName]; return(modelDictionary[altKey]); } catch (Exception e) { return(null); } } }
/// <summary> /// 获取一个模型对象 /// </summary> /// <param name="type">模型对象的类型</param> /// <param name="rid">模型对象的Rid</param> /// <returns>模型对象</returns> public ModelBase this[Type type, int rid] { get { try { ModelCollection modelDictionary = this[type]; return(modelDictionary[rid]); } catch (Exception e) { return(null); } } }
/// <summary> /// 获取一个模型对象 /// </summary> /// <param name="typeName">模型对象的类型名称</param> /// <param name="rid">模型对象的Rid</param> /// <returns>模型对象</returns> public ModelBase this[string typeName, int rid] { get { try { ModelCollection modelDictionary = this[typeName]; return(modelDictionary[rid]); } catch { return(null); } } }
/// <summary> /// 获取一对多关联中的子对象集合 /// </summary> /// <param name="childTypeName">关联子对象类型名称</param> /// <param name="childPropertyName">关联子对象对应的成员属性名称</param> /// <returns>关联子对象集合</returns> public ICollection <ModelBase> this[string childTypeName, string childPropertyName] { get { ChildrenModelCollection children = new ChildrenModelCollection(); ModelCollection rootCollection = ModelCacheManager.Instance[childTypeName]; if (rootCollection == null) { return(children); } Type childType = rootCollection.ModelType; PropertyInfo property = childType.GetProperty(childPropertyName); if (property == null || property.PropertyType != typeof(int)) { return(children); } children.BuildFromRootModelCollection(rootCollection, property, model.Rid); return(children); } }
/// <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; } } }
internal static void AddChildrenToList(ModelBase parentModel, List <ModelBase> targetList) { if (parentModel == null) { return; } Type type = parentModel.GetType(); ChildrenModelAttribute[] childrenAttributes = Attribute.GetCustomAttributes(type, typeof(ChildrenModelAttribute)) as ChildrenModelAttribute[]; foreach (ChildrenModelAttribute childrenAttribute in childrenAttributes) { ModelCollection rootCollection = ModelCacheManager.Instance[childrenAttribute.m_childType]; if (rootCollection == null) { return; } string protertyName = string.IsNullOrWhiteSpace(childrenAttribute.m_protertyName) ? parentModel.GetType().Name : childrenAttribute.m_protertyName; PropertyInfo property = childrenAttribute.m_childType.GetProperty(protertyName); if (property == null || property.PropertyType != typeof(int)) { return; } foreach (ModelBase child in rootCollection) { int rid = (int)property.GetValue(child, null); if (rid == parentModel.Rid) { targetList.Add(child); AddChildrenToList(child, targetList); } } } }
public void FindDifference(ModelCollection <T> target, out T[] notInTarget, out T[] notInThis, out T[] diffInTarget) { if (target.Count == 0 || this.Count == 0) { notInTarget = this.ToArray(); notInThis = target.ToArray(); diffInTarget = new T[0]; return; } List <T> thisList = new List <T>(); List <T> targetList = new List <T>(); List <T> diffList = new List <T>(); int indexThis = 0; int indexTarget = 0; while (indexThis < this.Count && indexTarget < target.Count) { T itemThis = this.Items[indexThis]; T itemTarget = target.Items[indexTarget]; if (itemThis.Rid < itemTarget.Rid) { thisList.Add(itemThis); indexThis++; } else if (itemThis.Rid > itemTarget.Rid) { targetList.Add(itemTarget); indexTarget++; } else { indexThis++; indexTarget++; foreach (PropertyInfo property in typeof(T).GetProperties()) { if (property.IsDefined(typeof(DataMemberAttribute), false)) { object obj1 = property.GetValue(itemThis, null); object obj2 = property.GetValue(itemTarget, null); if (obj1 == null && obj2 == null) { continue; } else if (obj1 != null && !obj1.Equals(obj2)) { diffList.Add(itemTarget); } } } } } for (int i = indexThis; i < this.Count; i++) { thisList.Add(this.Items[i]); } for (int i = indexTarget; i < target.Count; i++) { targetList.Add(target.Items[i]); } notInTarget = thisList.ToArray(); notInThis = targetList.ToArray(); diffInTarget = diffList.ToArray(); }
/// <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; } } }