Exemplo n.º 1
0
        public IEnumerable <PropertyDeclarationModel> GetProperties(PropertyCategoryEnum propertyCategoryFlagsToInclude = PropertyCategoryEnum.PrimaryKey | PropertyCategoryEnum.EntityRef | PropertyCategoryEnum.EntityRefAsPrimaryKey | PropertyCategoryEnum.NonPk)
        {
            if (propertyCategoryFlagsToInclude.HasFlag(PropertyCategoryEnum.PrimaryKey))
            {
                foreach (var p in this.PkProperties)
                {
                    yield return(p);
                }
            }

            if (propertyCategoryFlagsToInclude.HasFlag(PropertyCategoryEnum.EntityRefAsPrimaryKey))
            {
                foreach (var p in this.PkEntityReferences)
                {
                    yield return(p);
                }
            }

            if (propertyCategoryFlagsToInclude.HasFlag(PropertyCategoryEnum.EntityRef))
            {
                foreach (var p in this.EntityReferences)
                {
                    yield return(p);
                }
            }

            if (propertyCategoryFlagsToInclude.HasFlag(PropertyCategoryEnum.NonPk))
            {
                foreach (var p in this.NonPkProperties)
                {
                    yield return(p);
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyDeclarationModel"/> class for the given <see cref="PropertyInfo"/> instance.
        /// </summary>
        /// <param name="pi">The property info.</param>
        /// <exception cref="System.ArgumentNullException">pi</exception>
        public PropertyDeclarationModel(PropertyInfo pi)
        {
            this.DeclaringProperty  = pi ?? throw new ArgumentNullException(nameof(pi));
            this.PkAttribute        = pi.GetCustomAttribute <PrimaryKeyAttribute>(true);
            this.EntityRefAttribute = pi.GetCustomAttribute <EntityReferenceAttribute>(true);
            var attributeList = pi.CustomAttributes.Where(attr =>
                                                          attr.AttributeType != typeof(PrimaryKeyAttribute) &&
                                                          attr.AttributeType != typeof(EntityReferenceAttribute))
                                .ToList() ?? new List <CustomAttributeData>();

            // if this is an entity reference
            if ((this.EntityRefAttribute != null)
                // if it cannot be null
                && this.EntityRefAttribute.Multiplicity == EntityReferenceMultiplicityEnum.One
                // and the Required attribute is not applied yet
                && !pi.CustomAttributes.Any(customAttribute => customAttribute.AttributeType == typeof(RequiredAttribute)))
            {
                // we add it to the property declaration model attributes
                attributeList.Add(Helper.RequiredAttributeData);
            }

            this.Attributes = attributeList.ToArray();

            if ((this.PkAttribute != null) && (this.EntityRefAttribute != null))
            {
                this.PropertyCategory = PropertyCategoryEnum.EntityRefAsPrimaryKey;
            }
            else if (this.PkAttribute != null)
            {
                this.PropertyCategory = PropertyCategoryEnum.PrimaryKey;
            }
            else if (this.EntityRefAttribute != null)
            {
                this.PropertyCategory = PropertyCategoryEnum.EntityRef;
            }
            else
            {
                this.PropertyCategory = PropertyCategoryEnum.NonPk;
            }

            this.Name             = pi.Name;
            this.TypeFriendlyName = pi.PropertyType.GetFriendlyTypeName();

            if (pi.CanRead)
            {
                this.GetMethod = pi.GetGetMethod();
                if (this.GetMethod.IsStatic)
                {
                    this.IsStatic = true;
                }
            }

            if (pi.CanWrite)
            {
                this.SetMethod = pi.GetSetMethod();
                if (this.SetMethod.IsStatic)
                {
                    this.IsStatic = true;
                }
            }
        }