Exemple #1
0
        //{
        //    _baseEntityName = baseEntityName;
        //    _name = name;

        //    _properties = new DynPropertyConfigurationCollection(this);

        //    foreach (DynPropertyConfiguration property in BaseEntityType.Properties)
        //    {
        //        DynPropertyConfiguration inheritedProperty = new DynPropertyConfiguration(property);

        //        if (property.IsInherited == false)
        //        {
        //            inheritedProperty.IsInherited = true;
        //            inheritedProperty.InheritEntityMappingName = BaseEntityType.Name;
        //        }

        //        _properties.Add(inheritedProperty);
        //    }
        //}

        /// <summary>
        /// 基于指定父类构造新的实体类型
        /// </summary>
        /// <param name="name">类名称</param>
        /// <param name="parentClass">父类</param>
        //ToDo: 需要清理,zml
        public DynEntityType(string name, string baseEntityName, string namespaceName)
        {
            _name = name;

            if (!string.IsNullOrEmpty(baseEntityName))
            {
                _baseEntityName = baseEntityName;
            }

            _nameSpace = namespaceName;

            _properties = new DynPropertyConfigurationCollection(this);

            //_properties = new DynPropertyConfigurationCollection(this);

            //foreach (DynPropertyConfiguration property in BaseEntityType.Properties)
            //{
            //    DynPropertyConfiguration inheritedProperty = new DynPropertyConfiguration(property);

            //    if (property.IsInherited == false)
            //    {
            //        inheritedProperty.IsInherited = true;
            //        inheritedProperty.InheritEntityMappingName = BaseEntityType.Name;
            //    }

            //    _properties.Add(inheritedProperty);
            //}
        }
Exemple #2
0
        /// <summary>
        /// 基于指定父类构造新的实体类型
        /// </summary>
        /// <param name="name">类名称</param>
        /// <param name="parentClass">父类</param>
        public DynEntityType(string name, string baseEntityName)
        {
            _name = name;

            if (!string.IsNullOrEmpty(baseEntityName))
            {
                _baseEntityName = baseEntityName;
            }
            _properties = new DynPropertyConfigurationCollection(this);
        }
Exemple #3
0
        public DynPropertyConfigurationCollection GetProperties()
        {
            DynPropertyConfigurationCollection properties = new DynPropertyConfigurationCollection(this);

            properties.AddRange(_properties);

            if (!string.IsNullOrEmpty(_baseEntityName) && BaseEntityType != null)
            {
                DynPropertyConfigurationCollection inheritedProperties = BaseEntityType.GetProperties();

                foreach (DynPropertyConfiguration inheritedProperty in inheritedProperties)
                {
                    inheritedProperty.IsInherited = true;
                    if (inheritedProperty.InheritEntityMappingName == null)
                    {
                        inheritedProperty.InheritEntityMappingName = BaseEntityType.Name;
                    }
                }

                properties.AddRange(inheritedProperties);
            }

            return(properties);
        }
Exemple #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);
        }