Esempio n. 1
0
        public virtual string GetPrimaryKeyConstraintName(TypeDescriptor declaringTypeDescriptor, PropertyDescriptor[] primaryKeys)
        {
            var defaultName = VariableSubstituter.SedTransform("", NamingTransformsConfiguration.DefaultPrimaryKeyConstraintName, primaryKeys);

            if (namingTransformsConfiguration?.PrimaryKeyConstraintName == null)
            {
                return(defaultName);
            }

            return(VariableSubstituter.SedTransform(defaultName, namingTransformsConfiguration.PrimaryKeyConstraintName));
        }
Esempio n. 2
0
        public virtual string GetForeignKeyConstraintName(PropertyDescriptor propertyDescriptor)
        {
            var defaultName = VariableSubstituter.SedTransform("", NamingTransformsConfiguration.DefaultForeignKeyConstraintName, propertyDescriptor);

            if (namingTransformsConfiguration?.ForeignKeyConstraintName == null)
            {
                return(defaultName);
            }

            return(VariableSubstituter.SedTransform(defaultName, namingTransformsConfiguration.ForeignKeyConstraintName));
        }
Esempio n. 3
0
        public virtual string GetIndexConstraintName(IEnumerable <PropertyDescriptor> propertyDescriptors)
        {
            var defaultName = VariableSubstituter.SedTransform("", NamingTransformsConfiguration.DefaultIndexConstraintName, propertyDescriptors.ToArray());

            if (namingTransformsConfiguration?.IndexConstraintName == null)
            {
                return(defaultName);
            }

            return(VariableSubstituter.SedTransform(defaultName, namingTransformsConfiguration.IndexConstraintName));
        }
        public virtual string GetIndexConstraintName(TypeDescriptor typeDescriptor, string indexName, bool unique, PropertyDescriptor[] properties)
        {
            string transform;

            if (unique)
            {
                transform = this.namingTransformsConfiguration?.UniqueIndexConstraintName
                            ?? this.namingTransformsConfiguration?.IndexConstraintName
                            ?? NamingTransformsConfiguration.DefaultUniqueIndexConstraintName;
            }
            else
            {
                transform = this.namingTransformsConfiguration?.IndexConstraintName ?? NamingTransformsConfiguration.DefaultIndexConstraintName;
            }

            return(VariableSubstituter.SedTransform(indexName, transform, properties));
        }
Esempio n. 5
0
        public PropertyDescriptor(TypeDescriptor declaringTypeDescriptor, Type ownerType, PropertyInfo propertyInfo)
        {
            this.OwnerType               = ownerType;
            this.PropertyInfo            = propertyInfo;
            this.DeclaringTypeDescriptor = declaringTypeDescriptor;

            this.ValueRequiredAttribute            = propertyInfo.GetFirstCustomAttribute <ValueRequiredAttribute>(true);
            this.DefaultValueAttribute             = propertyInfo.GetFirstCustomAttribute <DefaultValueAttribute>(true);
            this.BackReferenceAttribute            = propertyInfo.GetFirstCustomAttribute <BackReferenceAttribute>(true);
            this.RelatedDataAccessObjectsAttribute = propertyInfo.GetFirstCustomAttribute <RelatedDataAccessObjectsAttribute>(true);
            this.PersistedMemberAttribute          = propertyInfo.GetFirstCustomAttribute <PersistedMemberAttribute>(true);
            this.ComputedMemberAttribute           = propertyInfo.GetFirstCustomAttribute <ComputedMemberAttribute>(true);
            this.ComputedTextMemberAttribute       = propertyInfo.GetFirstCustomAttribute <ComputedTextMemberAttribute>(true);
            this.ForeignObjectConstraintAttribute  = propertyInfo.GetFirstCustomAttribute <ForeignObjectConstraintAttribute>(true);

            if (this.PersistedMemberAttribute == null)
            {
                this.PersistedMemberAttribute = (PersistedMemberAttribute)this.ComputedMemberAttribute ?? this.ComputedTextMemberAttribute;
            }

            if (this.PropertyType.IsIntegerType(true) || this.PropertyType.GetUnwrappedNullableType() == typeof(Guid))
            {
                this.AutoIncrementAttribute = propertyInfo.GetFirstCustomAttribute <AutoIncrementAttribute>(true);
            }

            var named = this.PersistedMemberAttribute ?? this.BackReferenceAttribute ?? (NamedMemberAttribute)this.RelatedDataAccessObjectsAttribute;

            if (named != null)
            {
                this.PersistedName = VariableSubstituter.SedTransform(VariableSubstituter.Substitute(named.Name ?? this.PropertyName, this), this.DeclaringTypeDescriptor.TypeDescriptorProvider.Configuration.NamingTransforms?.PersistedMemberName);
                this.PrefixName    = VariableSubstituter.SedTransform(VariableSubstituter.Substitute(named.PrefixName ?? this.PropertyName, this), this.DeclaringTypeDescriptor.TypeDescriptorProvider.Configuration.NamingTransforms?.PersistedMemberPrefixName);
                this.SuffixName    = VariableSubstituter.SedTransform(VariableSubstituter.Substitute(named.SuffixName ?? this.PropertyName, this), this.DeclaringTypeDescriptor.TypeDescriptorProvider.Configuration.NamingTransforms?.PersistedMemberSuffixName);
            }

            this.PrimaryKeyAttribute        = this.PropertyInfo.GetFirstCustomAttribute <PrimaryKeyAttribute>(true);
            this.OrganizationIndexAttribute = this.PropertyInfo.GetFirstCustomAttribute <OrganizationIndexAttribute>(true);
            this.IsPrimaryKey    = this.PrimaryKeyAttribute != null && this.PrimaryKeyAttribute.IsPrimaryKey;
            this.UniqueAttribute = this.PropertyInfo.GetFirstCustomAttribute <UniqueAttribute>(true);
            this.IndexAttributes = this.PropertyInfo.GetCustomAttributes(typeof(IndexAttribute), true).OfType <IndexAttribute>().ToReadOnlyCollection();

            foreach (var attribute in this.IndexAttributes)
            {
                if (attribute.indexNameIfPropertyOrPropertyIfClass != null)
                {
                    attribute.IndexName = attribute.indexNameIfPropertyOrPropertyIfClass;
                }
            }

            if (this.IndexAttributes.Any(c => c.IndexName == null && c.IncludeOnly))
            {
                throw new InvalidDataAccessModelDefinitionException($"Property {this.DeclaringTypeDescriptor.TypeName}.{this.PropertyInfo.Name} defines an index no (non-includeonly) properties.");
            }

            var expression = this.ComputedMemberAttribute?.GetSetLambdaExpression(this.DeclaringTypeDescriptor.TypeDescriptorProvider.Configuration, this)?.Body.StripConvert();

            if (expression?.NodeType == ExpressionType.Assign)
            {
                var assignmentExpression = expression as BinaryExpression;

                if (assignmentExpression.Left.NodeType == ExpressionType.MemberAccess)
                {
                    var memberAccess = assignmentExpression.Left as MemberExpression;

                    this.ComputedMemberAssignTarget    = memberAccess.Member as PropertyInfo;
                    this.ComputedMemberAssignmentValue = assignmentExpression.Right;
                }
            }

            if (this.ValueRequired && this.DefaultValueAttribute != null)
            {
                throw new InvalidDataAccessModelDefinitionException($"Property '{propertyInfo.DeclaringType.Name}.{this.PropertyName}' can't have ValueRequiredAttribute.Required = true and DefaultValueAttribute");
            }

            var implicitDefault = this.DeclaringTypeDescriptor.TypeDescriptorProvider.Configuration.ValueTypesAutoImplicitDefault;

            if (this.DefaultValueAttribute != null || (implicitDefault && !this.IsAutoIncrement && !this.ValueRequired))
            {
                this.HasDefaultValue = true;

                if (this.DefaultValueAttribute != null)
                {
                    this.DefaultValue = this.DefaultValueAttribute.Value;
                }
                else
                {
                    this.HasImplicitDefaultValue = true;
                    this.DefaultValue            = this.PropertyType.GetDefaultValue();
                }

                try
                {
                    try
                    {
                        this.DefaultValue = Convert.ChangeType(this.DefaultValue, this.PropertyType);
                    }
                    catch (InvalidCastException)
                    {
                        var converter = System.ComponentModel.TypeDescriptor.GetConverter(this.PropertyType);

                        this.DefaultValue = converter.ConvertFrom(this.DefaultValue);
                    }
                }
                catch (InvalidCastException)
                {
                    throw new InvalidDataAccessObjectModelDefinition($"The property '{propertyInfo.DeclaringType.Name}.{this.PropertyName}' has an incompatible default value");
                }
            }
        }
 public virtual string GetPrimaryKeyConstraintName(TypeDescriptor declaringTypeDescriptor, PropertyDescriptor[] primaryKeys)
 {
     return(VariableSubstituter.SedTransform("", this.namingTransformsConfiguration?.PrimaryKeyConstraintName ?? NamingTransformsConfiguration.DefaultPrimaryKeyConstraintName, primaryKeys));
 }
 public virtual string GetForeignKeyConstraintName(PropertyDescriptor propertyDescriptor)
 {
     return(VariableSubstituter.SedTransform("", this.namingTransformsConfiguration?.ForeignKeyConstraintName ?? NamingTransformsConfiguration.DefaultForeignKeyConstraintName, propertyDescriptor));
 }