Ejemplo n.º 1
0
        public virtual IDataEntityType GetDataEntityType(object dataEntity)
        {
            if (dataEntity == null)
            {
                return(null);
            }
            IDataEntityBase base2 = dataEntity as IDataEntityBase;

            if (base2 != null)
            {
                return(base2.GetDataEntityType());
            }
            Type type = dataEntity.GetType();

            if (!type.IsPrimitive && !(type == typeof(string)))
            {
                return(null);
            }
            return(type.GetDataEntityType());
        }
Ejemplo n.º 2
0
        public static T GetPrimaryKeyValue <T>(this IDataEntityBase dataEntity, bool throwOnError = true)
        {
            if (dataEntity == null)
            {
                if (throwOnError)
                {
                    throw new ArgumentNullException("dataEntity");
                }
                return(default(T));
            }
            ISimpleProperty primaryKey = dataEntity.GetDataEntityType().PrimaryKey;

            if (primaryKey != null)
            {
                return((T)primaryKey.GetValue(dataEntity));
            }
            if (throwOnError)
            {
                throw new NotSupportedException(string.Format(ResManager.LoadKDString("实体类型{0}沒有定义主键,无法获取。", "014009000001732", SubSystemType.SL, new object[0]), dataEntity.GetDataEntityType().Name));
            }
            return(default(T));
        }
Ejemplo n.º 3
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);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private void CopyData(IDataEntityType dt, object dataEntity, object newEntity, bool clearPrimaryKeyValue)
        {
            IDataEntityType objA = dt;
            IDataEntityType objB = dt;

            if (dataEntity is IDataEntityBase)
            {
                objA = (dataEntity as IDataEntityBase).GetDataEntityType();
            }
            if (newEntity is IDataEntityBase)
            {
                objB = (newEntity as IDataEntityBase).GetDataEntityType();
            }
            if ((dataEntity is DynamicObject) && object.ReferenceEquals(objA, objB))
            {
                ((DynamicObject)newEntity).DataStorage = ((DynamicObject)dataEntity).DataStorage.MemberClone();
            }
            else
            {
                foreach (ISimpleProperty property in dt.Properties.GetSimpleProperties(this._onlyDbProperty))
                {
                    IDataEntityProperty dpOldProperty = null;
                    this.TryGetOldProperty(property, objA, out dpOldProperty);
                    if (!property.IsReadOnly && (dpOldProperty != null))
                    {
                        property.SetValue(newEntity, dpOldProperty.GetValue(dataEntity));
                    }
                }
            }
            if (clearPrimaryKeyValue)
            {
                ISimpleProperty primaryKey = dt.PrimaryKey;
                if (primaryKey != null)
                {
                    primaryKey.ResetValue(newEntity);
                }
                objB.SetDirty(newEntity, true);
            }
            foreach (IComplexProperty property4 in dt.Properties.GetComplexProperties(this._onlyDbProperty))
            {
                if (!property4.IsRefrenceObject())
                {
                    IDataEntityProperty property5 = null;
                    this.TryGetOldProperty(property4, objA, out property5);
                    object obj2 = property5.GetValue(dataEntity);
                    if (obj2 != null)
                    {
                        object          obj3;
                        IDataEntityType dataEntityType;
                        IDataEntityBase base2 = obj2 as IDataEntityBase;
                        if (base2 != null)
                        {
                            dataEntityType = base2.GetDataEntityType();
                        }
                        else
                        {
                            dataEntityType = property4.ComplexPropertyType;
                        }
                        if (property4.IsReadOnly)
                        {
                            obj3 = property4.GetValue(newEntity);
                            if (obj3 == null)
                            {
                                throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,只读的属性却返回了NULL值。", "014009000001633", SubSystemType.SL, new object[0]));
                            }
                            this.CopyData(dataEntityType, obj2, obj3, false);
                        }
                        else
                        {
                            if (property4.IsDbIgnore())
                            {
                                obj3 = this.Clone(dataEntityType, obj2, false);
                            }
                            else
                            {
                                obj3 = this.Clone(dataEntityType, obj2, this._clearPrimaryKeyValue);
                            }
                            property4.SetValue(newEntity, obj3);
                        }
                    }
                }
            }
            foreach (ICollectionProperty property6 in dt.Properties.GetCollectionProperties(this._onlyDbProperty))
            {
                IDataEntityProperty property7 = null;
                this.TryGetOldProperty(property6, objA, out property7);
                object obj4 = property7.GetValue(dataEntity);
                if (obj4 != null)
                {
                    IEnumerable enumerable = obj4 as IEnumerable;
                    if (enumerable == null)
                    {
                        throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值不支持枚举。", "014009000001634", SubSystemType.SL, new object[0]));
                    }
                    if (newEntity is DynamicObject)
                    {
                        ((DynamicObject)newEntity).DataStorage.SetLocalValue((DynamicProperty)property6, null);
                    }
                    object obj5 = property6.GetValue(newEntity);
                    if (obj5 == null)
                    {
                        if (property6.IsReadOnly)
                        {
                            throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值为null。", "014009000001635", SubSystemType.SL, new object[0]));
                        }
                        obj5 = Activator.CreateInstance(property6.PropertyType);
                        property6.SetValue(newEntity, obj5);
                    }
                    IList list = obj5 as IList;
                    if (list == null)
                    {
                        throw new ORMDesignException("??????", ResManager.LoadKDString("哦,真不幸,集合的属性返回值不支持IList。", "014009000001636", SubSystemType.SL, new object[0]));
                    }
                    list.Clear();
                    foreach (object obj6 in enumerable)
                    {
                        IDataEntityType collectionItemPropertyType;
                        IDataEntityBase base3 = obj6 as IDataEntityBase;
                        if (base3 == null)
                        {
                            collectionItemPropertyType = property6.CollectionItemPropertyType;
                        }
                        else
                        {
                            collectionItemPropertyType = base3.GetDataEntityType();
                        }
                        list.Add(this.Clone(collectionItemPropertyType, obj6));
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public object Clone(IDataEntityBase dataEntity)
        {
            IDataEntityType dataEntityType = dataEntity.GetDataEntityType();

            return(this.Clone(dataEntityType, dataEntity));
        }
Ejemplo n.º 6
0
 // Methods
 public static object Clone(this IDataEntityBase dataEntity, bool onlyDbProperty = false, bool clearPrimaryKeyValue = true)
 {
     return(new CloneUtils(onlyDbProperty, clearPrimaryKeyValue).Clone(dataEntity));
 }