Exemplo n.º 1
0
        /// <summary>
        /// 基于实体类型构造对象
        /// </summary>
        /// <param name="entityType"></param>
        public DynEntity(string typeName)
        {
            // 这里应该是从数据库中加载
            DynEntityType entityType = DynEntityTypeManager.GetEntityType(typeName);

            if (entityType == null)
            {
                throw new ApplicationException("实体类型不能为空");
            }

            this.PropertyChanged += new PropertyChangedHandler(Entity_PropertyChanged);
            isAttached            = false;

            _entityType = entityType;

            int normalCnt = 0;
            int queryCnt  = 0;

            foreach (DynPropertyConfiguration entityProperty in _entityType.GetProperties())
            {
                if (entityProperty.IsQueryProperty == true)
                {
                    if (entityProperty.IsArray)
                    {
                        _queryPropertyNameIdMap.Add("_" + entityProperty.Name, queryCnt++);
                        _queryPropertyValues.Add(null);
                    }
                    else
                    {
                        QueryDynAttribute qa        = entityProperty.GetPropertyQueryAttribute();
                        DynQueryDescriber describer = new DynQueryDescriber(qa, entityProperty, entityProperty.PropertyOriginalEntityType, _entityType.Name);

                        _normalPropertyNameIdMap.Add("_" + entityProperty.Name + "_" + describer.RelatedForeignKey, normalCnt++);
                        _fkPropertyNameNormalPropertyNameMap.Add("_" + entityProperty.Name + "_" + describer.RelatedForeignKey, entityProperty.Name);
                        _normalPropertyValues.Add(null);

                        _queryPropertyNameIdMap.Add("_" + entityProperty.Name, queryCnt++);
                        _queryPropertyValues.Add(null);
                    }
                }
                else
                {
                    _normalPropertyNameIdMap.Add("_" + entityProperty.Name, normalCnt++);
                    _normalPropertyValues.Add(null);
                }
            }
        }
        public QueryDynAttribute GetPropertyQueryAttribute()
        {
            QueryDynAttribute queryDynAttribute = null;

            foreach (EntityDynAttribute attr in _attributes)
            {
                if (typeof(QueryDynAttribute).IsAssignableFrom(attr.GetType()))
                {
                    queryDynAttribute = attr as QueryDynAttribute;
                }
            }
            //QueryDynAttribute qa = GetPropertyAttribute(typeof(QueryDynAttribute));
            //if (qa == null) qa = GetPropertyAttribute<FkQueryDynAttribute>(dynEntityProperty);
            //if (qa == null) qa = GetPropertyAttribute<CustomQueryDynAttribute>(dynEntityProperty);
            //if (qa == null) qa = GetPropertyAttribute<PkReverseQueryDynAttribute>(dynEntityProperty);
            //if (qa == null) qa = GetPropertyAttribute<FkReverseQueryDynAttribute>(dynEntityProperty);
            //if (qa == null) qa = GetPropertyAttribute<ManyToManyQueryDynAttribute>(dynEntityProperty);

            return(queryDynAttribute);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Set the property value
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        public void SetProportyValue(string propertyName, object propertyValue)
        {
            string entityPropertyName = propertyName;

            bool isFKProperty = false;

            string normalPropertyName = null;

            if (_fkPropertyNameNormalPropertyNameMap.TryGetValue("_" + propertyName, out normalPropertyName))
            {
                entityPropertyName = normalPropertyName;
                isFKProperty       = true;
            }

            DynPropertyConfiguration entityProperty = GetEntityProperty(entityPropertyName);

            if (entityProperty.IsQueryProperty == true && isFKProperty == false)
            {
                if (entityProperty.IsArray)
                {
                    OnPropertyChanged(propertyName, _queryPropertyValues[_queryPropertyNameIdMap[propertyName]], propertyValue);

                    int index = 0;
                    if (_queryPropertyNameIdMap.TryGetValue("_" + propertyName, out index))
                    {
                        _queryPropertyValues[index] = propertyValue;
                    }
                }
                else
                {
                    QueryDynAttribute qa        = entityProperty.GetPropertyQueryAttribute();
                    DynQueryDescriber describer = new DynQueryDescriber(qa, entityProperty, entityProperty.PropertyOriginalEntityType, _entityType.Name);

                    OnQueryOnePropertyChanged(propertyName, GetPropertyValue(propertyName), propertyValue);
                    _queryPropertyValues[_queryPropertyNameIdMap["_" + propertyName]] = propertyValue;
                    if (propertyValue == null)
                    {
                        //OnPropertyChanged(propertyName, _normalPropertyValues[_queryPropertyNameIdMap["_" + propertyName + "_" + describer.RelatedForeignKey]], null);
                        //_normalPropertyValues[_queryPropertyNameIdMap["_" + propertyName + "_" + describer.RelatedForeignKey]] = null;
                        OnPropertyChanged(propertyName, _normalPropertyValues[_queryPropertyNameIdMap["_" + propertyName]], null);
                        _normalPropertyValues[_queryPropertyNameIdMap["_" + propertyName]] = null;
                    }
                    else
                    {
                        //OnPropertyChanged(propertyName, _normalPropertyValues[_queryPropertyNameIdMap["_" + propertyName + "_" + describer.RelatedForeignKey]], (propertyValue as DynEntity).GetPropertyValue(describer.RelatedForeignKey));
                        //_normalPropertyValues[_queryPropertyNameIdMap["_" + propertyName + "_" + describer.RelatedForeignKey]] = (propertyValue as DynEntity).GetPropertyValue(describer.RelatedForeignKey);
                        OnPropertyChanged(propertyName, _normalPropertyValues[_queryPropertyNameIdMap["_" + propertyName]], (propertyValue as DynEntity).GetPropertyValue(describer.RelatedForeignKey));
                        _normalPropertyValues[_queryPropertyNameIdMap["_" + propertyName]] = (propertyValue as DynEntity).GetPropertyValue(describer.RelatedForeignKey);
                    }
                }
            }
            else
            {
                OnPropertyChanged(propertyName, GetPropertyValue(propertyName), propertyValue);

                int index = 0;
                if (_normalPropertyNameIdMap.TryGetValue("_" + propertyName, out index))
                {
                    _normalPropertyValues[index] = propertyValue;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// new an EntityConfiguration from an EntityType
        /// </summary>
        /// <param name="entityType"></param>
        /// <returns></returns>
        public static EntityConfiguration EntityType2EntityConfiguration(DynEntityType entityType)
        {
            EntityConfiguration ec = new EntityConfiguration();

            ec.Name = entityType.FullName;

            EntityDynAttribute attr = entityType.GetCustomAttribute(typeof(MappingNameDynAttribute));

            if (attr != null)
            {
                ec.MappingName = (attr as MappingNameDynAttribute).Name;
            }
            else
            {
                ec.MappingName = entityType.Name;
            }

            entityType.MappingName = ec.MappingName;


            attr = entityType.GetCustomAttribute(typeof(CommentDynAttribute));
            if (attr != null)
            {
                ec.Comment = (attr as CommentDynAttribute).Content;
            }
            else
            {
                ec.Comment = "";
            }

            entityType.Comment = ec.Comment;

            attr = entityType.GetCustomAttribute(typeof(BatchUpdateDynAttribute));
            if (attr != null)
            {
                ec.BatchSize = (attr as BatchUpdateDynAttribute).BatchSize;
            }
            else
            {
                ec.BatchSize = 0;
            }

            entityType.BatchSize = ec.BatchSize;

            if (string.IsNullOrEmpty(entityType.BaseEntityName))
            {
                ec.BaseEntity = null;
            }

            else
            {
                ec.BaseEntity = entityType.Namespace + "." + entityType.BaseEntityName;
            }

            attr = entityType.GetCustomAttribute(typeof(CustomDataDynAttribute));
            if (attr != null)
            {
                ec.CustomData = (attr as CustomDataDynAttribute).Data;
            }
            else
            {
                ec.CustomData = "";
            }

            entityType.CustomData = ec.CustomData;

            attr = entityType.GetCustomAttribute(typeof(AdditionalSqlScriptDynAttribute));
            if (attr != null)
            {
                ec.AdditionalSqlScript = (attr as AdditionalSqlScriptDynAttribute).Sql;
            }
            else
            {
                ec.AdditionalSqlScript = "";
            }

            entityType.AdditionalSqlScript = ec.AdditionalSqlScript;

            attr = entityType.GetCustomAttribute(typeof(ReadOnlyDynAttribute));
            if (attr != null)
            {
                ec.IsReadOnly = true;
            }
            else
            {
                ec.IsReadOnly = false;
            }
            entityType.IsReadOnly = ec.IsReadOnly;

            attr = entityType.GetCustomAttribute(typeof(AutoPreLoadDynAttribute));
            if (attr != null)
            {
                ec.IsAutoPreLoad = true;
            }
            else
            {
                ec.IsAutoPreLoad = false;
            }
            entityType.IsAutoPreLoad = ec.IsAutoPreLoad;

            attr = entityType.GetCustomAttribute(typeof(RelationDynAttribute));
            if (attr != null)
            {
                ec.IsRelation = true;
            }
            else
            {
                ec.IsRelation = false;
            }
            entityType.IsRelation = ec.IsRelation;

            //根据DynPropertyConfigurationCollection构造PropertyConfiguration
            List <PropertyConfiguration> pcList = new List <PropertyConfiguration>();
            //TODO:此处如果支持继承关系需要调整
            DynPropertyConfigurationCollection allProperties = entityType.GetProperties();

            foreach (DynPropertyConfiguration property in allProperties)
            {
                PropertyConfiguration pc = new PropertyConfiguration();

                pc.IsInherited = property.IsInherited;
                pc.InheritEntityMappingName = property.InheritEntityMappingName;

                attr            = property.GetPropertyAttribute(typeof(PrimaryKeyDynAttribute));
                pc.IsPrimaryKey = (attr != null);

                IndexPropertyDynAttribute ipa = property.GetPropertyAttribute(typeof(IndexPropertyDynAttribute)) as IndexPropertyDynAttribute;
                if (ipa != null)
                {
                    pc.IsIndexProperty           = true;
                    pc.IsIndexPropertyDesc       = ipa.IsDesc;
                    property.IsIndexProperty     = true;
                    property.IsIndexPropertyDesc = ipa.IsDesc;
                }

                attr         = property.GetPropertyAttribute(typeof(NotNullDynAttribute));
                pc.IsNotNull = (attr != null);

                attr = property.GetPropertyAttribute(typeof(SerializationIgnoreDynAttribute));
                pc.IsSerializationIgnore = (attr != null);

                RelationKeyDynAttribute rka = property.GetPropertyAttribute(typeof(RelationKeyDynAttribute)) as RelationKeyDynAttribute;
                if (rka != null)
                {
                    pc.IsRelationKey           = true;
                    pc.RelatedType             = GetOutputNamespace(rka.RelatedType) + "." + rka.RelatedType;
                    pc.RelatedForeignKey       = rka.RelatedPk;
                    property.IsRelationKey     = true;
                    property.RelatedType       = pc.RelatedType;
                    property.RelatedForeignKey = rka.RelatedPk;
                }

                FriendKeyDynAttribute fka = property.GetPropertyAttribute(typeof(FriendKeyDynAttribute)) as FriendKeyDynAttribute;
                if (fka != null)
                {
                    pc.IsFriendKey             = true;
                    pc.RelatedForeignKey       = fka.RelatedEntityPk;
                    pc.RelatedType             = GetOutputNamespace(fka.RelatedEntityType) + "." + fka.RelatedEntityType;
                    property.IsFriendKey       = true;
                    property.RelatedForeignKey = fka.RelatedEntityPk;
                    property.RelatedType       = pc.RelatedType;
                }

                MappingNameDynAttribute mna = property.GetPropertyAttribute(typeof(MappingNameDynAttribute)) as MappingNameDynAttribute;
                if (mna != null)
                {
                    pc.MappingName       = mna.Name;
                    property.MappingName = mna.Name;
                }

                CommentDynAttribute ca = property.GetPropertyAttribute(typeof(CommentDynAttribute)) as CommentDynAttribute;
                if (ca != null)
                {
                    pc.Comment       = ca.Content;
                    property.Comment = ca.Content;
                }

                attr = property.GetPropertyAttribute(typeof(CustomDataDynAttribute));
                if (attr != null)
                {
                    pc.CustomData = ((CustomDataDynAttribute)attr).Data;
                }

                pc.IsReadOnly = property.IsReadOnly;
                pc.Name       = property.Name;


                SqlTypeDynAttribute sta = property.GetPropertyAttribute(typeof(SqlTypeDynAttribute)) as SqlTypeDynAttribute;
                if (sta != null && (!string.IsNullOrEmpty(sta.Type)))
                {
                    pc.SqlType               = sta.Type;
                    pc.SqlDefaultValue       = sta.DefaultValue;
                    property.SqlType         = sta.Type;
                    property.SqlDefaultValue = sta.DefaultValue;
                }

                QueryDynAttribute qa = property.GetPropertyQueryAttribute();
                if (qa != null)
                {
                    pc.IsQueryProperty = true;
                    pc.QueryType       = qa.QueryType.ToString();

                    property.PropertyOriginalEntityType = property.PropertyType;
                    property.IsQueryProperty            = true;


                    string propertyEntityType = property.PropertyType;
                    //if (property.IsArray)
                    //{
                    //    propertyEntityType = property.PropertyType;
                    //}
                    DynQueryDescriber describer = new DynQueryDescriber(qa, property, propertyEntityType, entityType.Name);

                    pc.IsLazyLoad         = describer.LazyLoad;
                    pc.QueryOrderBy       = describer.OrderBy;
                    pc.QueryWhere         = describer.Where;
                    property.IsLazyLoad   = describer.LazyLoad;
                    property.QueryOrderBy = describer.OrderBy;
                    property.QueryWhere   = describer.Where;

                    if (describer.RelationType != null)
                    {
                        pc.RelationType = GetOutputNamespace(describer.RelationType) + "." + describer.RelationType;
                    }
                    pc.IsContained             = describer.Contained;
                    pc.RelatedForeignKey       = describer.RelatedForeignKey;
                    property.IsContained       = describer.Contained;
                    property.RelatedForeignKey = describer.RelatedForeignKey;
                    if (describer.RelatedType != null)
                    {
                        pc.RelatedType       = GetOutputNamespace(describer.RelatedType) + "." + describer.RelatedType;
                        property.RelatedType = pc.RelatedType;
                    }

                    if (property.IsArray)
                    {
                        pc.PropertyType = GetOutputNamespace(property.PropertyType) + "." + property.PropertyType + "ArrayList";
                    }
                    else
                    {
                        pc.PropertyType = GetOutputNamespace(property.PropertyType) + "." + property.PropertyType;
                    }
                    property.PropertyType = pc.PropertyType;

                    if (qa.QueryType == QueryType.FkReverseQuery)
                    {
                        if (string.IsNullOrEmpty(describer.RelatedForeignKeyType) != true)
                        {
                            pc.PropertyMappingColumnType = describer.RelatedForeignKeyType ?? typeof(string).ToString();
                        }
                        else
                        {
                            pc.PropertyMappingColumnType = string.IsNullOrEmpty(describer.RelatedForeignKeyType) ? "" : typeof(string).ToString();
                        }
                        property.PropertyMappingColumnType = pc.PropertyMappingColumnType;
                    }

                    if (qa.QueryType == QueryType.FkQuery)
                    {
                        pc.RelatedForeignKey = describer.RelatedForeignKey;
                        pc.RelatedType       = GetOutputNamespace(describer.RelatedType) + "." + describer.RelatedType;

                        property.RelatedForeignKey = describer.RelatedForeignKey;
                        property.RelatedType       = pc.RelatedType;
                    }
                }
                else
                {
                    pc.PropertyType = property.PropertyType;
                }

                pcList.Add(pc);
            }
            ec.Properties = pcList.ToArray();

            return(ec);
        }