Example #1
0
        /// <summary>
        /// 把一个实体数据拷贝到另一个实体
        /// </summary>
        /// <param name="dataEntity"></param>
        /// <param name="newEntity"></param>
        /// <param name="copyHandler"></param>
        /// <param name="clearPrimaryKeyValue"></param>
        /// <param name="onlyDbProperty"></param>
        /// <param name="onlyDirtyProperty"></param>
        public static void CopyData(this IDataEntityBase dataEntity, IDataEntityBase newEntity, Func <string, string, bool> copyHandler = null, bool clearPrimaryKeyValue = false, bool onlyDbProperty = false, bool onlyDirtyProperty = false)
        {
            IDataEntityType dataEntityType = dataEntity.GetDataEntityType();
            IDataEntityType type2          = newEntity.GetDataEntityType();

            if (copyHandler == null)
            {
                copyHandler = (dtName, propName) => true;
            }
            IEnumerable <IDataEntityProperty> dirtyProperties = dataEntityType.GetDirtyProperties(dataEntity);

            foreach (ISimpleProperty property in type2.Properties.GetSimpleProperties(onlyDbProperty))
            {
                if (copyHandler(type2.Name, property.Name))
                {
                    IDataEntityProperty dpOldProperty = null;
                    TryGetOldProperty(property, dataEntityType, out dpOldProperty);
                    if ((!onlyDirtyProperty || dirtyProperties.Contains <IDataEntityProperty>(dpOldProperty)) && !(property.IsReadOnly || (dpOldProperty == null)))
                    {
                        property.SetValue(newEntity, dpOldProperty.GetValue(dataEntity));
                    }
                }
            }
            if (clearPrimaryKeyValue)
            {
                ISimpleProperty primaryKey = type2.PrimaryKey;
                if (primaryKey != null)
                {
                    primaryKey.ResetValue(newEntity);
                }
                type2.SetDirty(newEntity, true);
            }
            foreach (IComplexProperty property4 in type2.Properties.GetComplexProperties(onlyDbProperty))
            {
                IDataEntityProperty property5 = null;
                TryGetOldProperty(property4, dataEntityType, out property5);
                IDataEntityBase base2 = property5.GetValue(dataEntity) as IDataEntityBase;
                if (base2 != null)
                {
                    IDataEntityBase base3;
                    if (property4.IsReadOnly)
                    {
                        base3 = property4.GetValue(newEntity) as IDataEntityBase;
                        if (base3 == null)
                        {
                            throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,只读的属性却返回了NULL值。", "014009000001633", SubSystemType.SL, new object[0]));
                        }
                        base2.CopyData(base3, copyHandler, false, onlyDbProperty, false);
                    }
                    else
                    {
                        base3 = property4.ComplexPropertyType.CreateInstance() as IDataEntityBase;
                        base2.CopyData(base3, copyHandler, clearPrimaryKeyValue, onlyDbProperty, false);
                        property4.SetValue(newEntity, base3);
                    }
                }
            }
            foreach (ICollectionProperty property6 in type2.Properties.GetCollectionProperties(onlyDbProperty))
            {
                IDataEntityProperty property7 = null;
                TryGetOldProperty(property6, dataEntityType, out property7);
                object obj2 = property7.GetValue(dataEntity);
                if (obj2 != null)
                {
                    IEnumerable enumerable2 = obj2 as IEnumerable;
                    if (enumerable2 == null)
                    {
                        throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值不支持枚举。", "014009000001634", SubSystemType.SL, new object[0]));
                    }
                    object obj3 = property6.GetValue(newEntity);
                    if (obj3 == null)
                    {
                        if (property6.IsReadOnly)
                        {
                            throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值为null。", "014009000001635", SubSystemType.SL, new object[0]));
                        }
                        obj3 = Activator.CreateInstance(property6.PropertyType);
                        property6.SetValue(newEntity, obj3);
                    }
                    IList list = obj3 as IList;
                    if (list == null)
                    {
                        throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值不支持IList。", "014009000001636", SubSystemType.SL, new object[0]));
                    }
                    list.Clear();
                    foreach (IDataEntityBase base4 in enumerable2)
                    {
                        IDataEntityBase base5 = property6.CollectionItemPropertyType.CreateInstance() as IDataEntityBase;
                        base4.CopyData(base5, copyHandler, clearPrimaryKeyValue, onlyDbProperty, false);
                        list.Add(base5);
                    }
                }
            }
        }