/// <summary>
        /// 移除一个模型对象
        /// </summary>
        /// <param name="value">要移除的模型对象</param>
        public virtual void Remove(IModelBase value)
        {
            if (value == null)//于艳辉添加
            {
                return;
            }
            Type type = value.GetType();

            while (type.BaseType != typeof(object))
            {
                ModelCollection modelCollection = this[type.Name];
                if (modelCollection != null &&
                    modelCollection.Contains(value.Rid))
                {
                    IModelBase model = modelCollection[value.Rid];
                    modelCollection.Remove(model);
                    modelCollection.RemoveAltKey(model);
                }
                type = type.BaseType;
            }
            if (ModelRemoved != null)
            {
                ModelRemoved(this, new ModelCacheChangedEventArgs(value));
            }
        }
        /// <summary>
        /// 存储一个模型对象,如果存根管理器中没有该对象则增加该对象
        /// </summary>
        /// <param name="value">要存储的模型对象</param>
        public virtual void Save(IModelBase value)
        {
            Type            type            = value.GetType();
            ModelCollection modelCollection = this[type];

            if (modelCollection == null)
            {
                return;
            }
            if (modelCollection.Contains(value.Rid))
            {
                IModelBase   model = modelCollection[value.Rid];
                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;
                }
                modelCollection.AddAltKey(value);
            }
            else
            {
                modelCollection.Add(value);
                modelCollection.AddAltKey(value);
                type = type.BaseType;
                while (type.BaseType != typeof(object) && type.Name != "ScadaModel")
                {
                    modelCollection = this[type.Name];
                    if (modelCollection.Contains(value.Rid))
                    {
                        IModelBase   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;
                }
            }
            value.Initialize();
            if (ModelSaved != null)
            {
                ModelSaved(this, new ModelCacheChangedEventArgs(value.GetType(), value.Rid));
            }
        }
 /// <summary>
 /// 获取模型对象的字典集合,如果没有该集合则创建该字典集合以及此类对象的父类模型对象的字典集合
 /// </summary>
 /// <param name="typeName">模型对象的类型名称</param>
 /// <returns>模型对象字典集合</returns>
 public ModelCollection this[string typeName]
 {
     get
     {
         ModelCollection modelCollection = null;
         typeDictionary.TryGetValue(typeName, out modelCollection);
         return(modelCollection);
     }
 }
 /// <summary>
 /// 通过远程传输的模型对象获取本地对应的模型对象
 /// </summary>
 /// <param name="value">远程传输来的模型对象</param>
 /// <returns>本地模型对象</returns>
 public IModelBase this[IModelBase value]
 {
     get
     {
         ModelCollection modelDictionary = this[value.GetType()];
         if (modelDictionary != null && modelDictionary.Contains(value.Rid))
         {
             return(modelDictionary[value.Rid]);
         }
         return(null);
     }
 }
 /// <summary>
 /// 获取一个模型对象
 /// </summary>
 /// <param name="type">模型对象的类型</param>
 /// <param name="altKey">模型对象的可选索引键</param>
 /// <returns>模型对象</returns>
 public IModelBase this[Type type, string altKey]
 {
     get
     {
         ModelCollection modelDictionary = this[type];
         if (modelDictionary != null)
         {
             return(modelDictionary[altKey]);
         }
         return(null);
     }
 }
 /// <summary>
 /// 获取一个模型对象
 /// </summary>
 /// <param name="type">模型对象的类型</param>
 /// <param name="rid">模型对象的Rid</param>
 /// <returns>模型对象</returns>
 public IModelBase this[Type type, int rid]
 {
     get
     {
         ModelCollection modelDictionary = this[type];
         if (modelDictionary != null && modelDictionary.Contains(rid))
         {
             return(modelDictionary[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 (IModelBase child in rootCollection)
     {
         int rid = (int)childProperty.GetValue(child, null);
         if (rid == parentRid)
         {
             this.Add(child);
         }
     }
 }
        private ModelCollection CreateModelCollection(Type type)
        {
            if (type.BaseType == typeof(object))
            {
                return(null);
            }
            ModelCollection modelCollection;

            if (!typeDictionary.TryGetValue(type.Name, out modelCollection))
            {
                modelCollection           = new ModelCollection();
                modelCollection.ModelType = type;
                typeDictionary.Add(type.Name, modelCollection);
                modelCollection.BaseCollection = CreateModelCollection(type.BaseType);
                return(modelCollection);
            }
            return(modelCollection);
        }
        /// <summary>
        /// 获取一对多关联中的子对象集合
        /// </summary>
        /// <param name="childTypeName">关联子对象类型名称</param>
        /// <param name="childPropertyName">关联子对象对应的成员属性名称</param>
        /// <returns>关联子对象集合</returns>
        public ICollection <IModelBase> 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);
            }
        }
        public ChidrenModelCollection()
        {
            ModelCollection collection = ModelCacheManager.Instance[typeof(T)];

            collection.CollectionChanged += new NotifyCollectionChangedEventHandler(ModelCacheCollection_CollectionChanged);
        }
Exemple #11
0
        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();
        }