コード例 #1
0
        void ApplyPropertyConvention(IModelInspector mi, PropertyPath type, IPropertyMapper map)
        {
            if (type.PreviousPath != null)
            {
                if (mi.IsComponent(((PropertyInfo)type.PreviousPath.LocalMember).PropertyType))
                {
                    map.Column(type.PreviousPath.LocalMember.Name + type.LocalMember.Name);
                }
            }

            if (type.LocalMember.GetCustomAttributes(typeof(UniqueAttribute), false).Any())
            {
                map.Unique(true);
            }

            var propertyInfo = type.LocalMember as PropertyInfo;

            if (propertyInfo != null)
            {
                if (propertyInfo.PropertyType == typeof(byte[]))
                {
                    map.Length(Int32.MaxValue);
                }

                return;
            }

            var fieldInfo = type.LocalMember as FieldInfo;

            if (fieldInfo != null && fieldInfo.FieldType == typeof(byte[]))
            {
                map.Length(Int32.MaxValue);
            }
        }
コード例 #2
0
        private void OnBeforeMapProperty(IModelInspector mi, PropertyPath member, IPropertyMapper map)
        {
            var type = member.LocalMember.GetPropertyOrFieldType();

            if (type == typeof(string))
            {
                if (member.LocalMember.Name == "FileName")
                {
                    map.Length(255);
                }
                else
                {
                    map.Type <DefaultStringType>();
                    map.Length(50);
                }
            }
            else if (type == typeof(byte[]))
            {
                map.Length(Int32.MaxValue / 2);
                map.Column(x => x.SqlType("varbinary(max)"));
            }

            if (member.LocalMember.Name == "DateCreated")
            {
                map.Update(false);
            }
        }
コード例 #3
0
 private void Name(IPropertyMapper propertyMapper)
 {
     propertyMapper.Column("Name");
     propertyMapper.Type(NHibernateUtil.String);
     propertyMapper.Length(100);
     propertyMapper.NotNullable(false);
 }
コード例 #4
0
        public static void MapStringLengthFromAttribute(IModelInspector modelinspector, PropertyPath member, IPropertyMapper propertycustomizer)
        {
            var propertyInfo = member.LocalMember as PropertyInfo;
            if (propertyInfo == null || propertyInfo.PropertyType != typeof(string))
            {
                return;
            }

            var attributes = propertyInfo.GetCustomAttributes(true);
            var attribute = attributes.FirstOrDefault(x => x.GetType().Name.IndexOf("Length") > -1);
            if (attribute == null)
            {
                return;
            }
            int value = 0;
            var possiblePropertyNames = new[] { "MaximumLength", "Length", "Max", "MaxLength" };
            foreach (var name in possiblePropertyNames)
            {
                var attributeProperty = attribute.GetType().GetProperty(name);
                if (attributeProperty != null)
                {
                    value = (int)attributeProperty.GetValue(attribute, null);
                    break;
                }
            }

            if (value == 0)
            {
                throw new InvalidOperationException(
                    string.Format("could not get the length of property {0}", propertyInfo.Name));
            }

            propertycustomizer.Length(value);
        }
コード例 #5
0
        void ApplyPropertyConvention(IModelInspector mi, PropertyPath type, IPropertyMapper map)
        {
            if (type.PreviousPath != null)
            {
                if (mi.IsComponent(((PropertyInfo)type.PreviousPath.LocalMember).PropertyType))
                {
                    map.Column(type.PreviousPath.LocalMember.Name + type.LocalMember.Name);
                }
            }

            if (type.LocalMember.DeclaringType != null)
            {
                var sagaMetadata = sagaMetaModel.FirstOrDefault(sm => sm.SagaEntityType == type.LocalMember.DeclaringType);

                if (sagaMetadata != null)
                {
                    SagaMetadata.CorrelationPropertyMetadata correlationProperty;
                    if (sagaMetadata.TryGetCorrelationProperty(out correlationProperty) && correlationProperty.Name == type.LocalMember.Name)
                    {
                        map.Unique(true);
                    }
                }
            }

            var propertyInfo = type.LocalMember as PropertyInfo;

            if (propertyInfo != null)
            {
                if (propertyInfo.PropertyType == typeof(byte[]))
                {
                    map.Length(int.MaxValue);
                }

                return;
            }

            var fieldInfo = type.LocalMember as FieldInfo;

            if (fieldInfo != null && fieldInfo.FieldType == typeof(byte[]))
            {
                map.Length(int.MaxValue);
            }
        }
コード例 #6
0
        protected override void OnBeforeMapProperty(IModelInspector modelInspector, PropertyPath member, IPropertyMapper propertyCustomizer)
        {
            base.OnBeforeMapProperty(modelInspector, member, propertyCustomizer);

            Type memberType = member.MemberType();

            if (typeof(AbstractIdentification).IsAssignableFrom(memberType))
            {
                AbstractIdentification identification = (AbstractIdentification)Activator.CreateInstance(memberType, string.Empty);
                propertyCustomizer.Length(identification.StandardMaxLength);
            }
        }
        /// <summary>
        /// Sets the mapper to use:
        ///  1) a properties StringLength attribute if it has one for the databases field size.
        ///  2) non-nullable types to be not nullable in the database.
        ///  3) creates indexes based on the index attributes of properties.
        /// </summary>
        private void OnMapperOnBeforeMapProperty(
            IModelInspector inspector,
            PropertyPath member,
            IPropertyMapper customizer)
        {
            // Get all the custom attributes.
            var customAttributes = member.LocalMember.GetCustomAttributes(false);

            // For all types check for index attributes and add indexes if required.
            var indexAttributes = customAttributes.OfType <IndexAttribute>();

            foreach (var indexAttribute in indexAttributes)
            {
                string indexPrefix = member.GetContainerEntity(inspector).Name;
                if (indexAttribute.Unique)
                {
                    string indexName = $"UI_{indexPrefix}_{indexAttribute.Name}";
                    customizer.UniqueKey(indexName);
                }
                else
                {
                    string indexName = $"IX_{indexPrefix}_{indexAttribute.Name}";
                    customizer.Index(indexName);
                }
            }

            // For string types check for string length attribute and set field length if required
            Type memberType = member.LocalMember.GetPropertyOrFieldType();

            if (memberType == typeof(string))
            {
                StringLengthAttribute stringlengthAttribute =
                    (StringLengthAttribute)
                    customAttributes.FirstOrDefault(x => x.GetType() == typeof(StringLengthAttribute));
                int length = this.DefaltStringLength;
                if (stringlengthAttribute != null && stringlengthAttribute.MaximumLength > 0)
                {
                    length = stringlengthAttribute.MaximumLength;
                }
                customizer.Length(length);
            }

            // For all types if the type is not nullable then set not nullable to true.
            if (!IsNullable(memberType))
            {
                customizer.NotNullable(true);
            }
        }
コード例 #8
0
        public static void BeforeMapProperty(
            IModelInspector modelInspector, PropertyPath member,
            IPropertyMapper map)
        {
            var prop  = member.LocalMember as PropertyInfo;
            var field = member.LocalMember as FieldInfo;

            if (
                prop?.PropertyType == typeof(TimeSpan) ||
                prop?.PropertyType == typeof(TimeSpan?) ||
                field?.FieldType == typeof(TimeSpan) ||
                field?.FieldType == typeof(TimeSpan?)
                )
            {
                map.Type <TimeSpanFormattedType>();
                map.Length(TimeSpanFormattedType.MaxLength);
            }
        }
        protected override void OnBeforeMapProperty(IModelInspector modelInspector, PropertyPath member, IPropertyMapper propertyCustomizer)
        {
            Type reflectedType = member.LocalMember.ReflectedType;

            if (reflectedType == null)
            {
                return;
            }

            propertyCustomizer.Column(GetColumnName(modelInspector, member));

            bool required =
                member.LocalMember
                .GetCustomAttributes()
                .OfType <RequiredAttribute>()
                .Any();

            // Getting tableType of reflected object
            Type memberType = member.MemberType();

            bool notNullable = required || (memberType != null && memberType.IsPrimitive) || memberType == typeof(DateTime);

            propertyCustomizer.NotNullable(notNullable);

            StringLengthAttribute stringLengthAttribute =
                member.LocalMember
                .GetCustomAttributes()
                .OfType <StringLengthAttribute>()
                .FirstOrDefault();

            if (stringLengthAttribute != null)
            {
                if (stringLengthAttribute.MaximumLength > 0)
                {
                    propertyCustomizer.Length(stringLengthAttribute.MaximumLength);
                }
                else
                {
                    propertyCustomizer.Type(NHibernateUtil.StringClob);
                }
            }
        }
        protected override void OnBeforeMapProperty(IModelInspector modelInspector, PropertyPath member, IPropertyMapper propertyCustomizer)
        {
            Type reflectedType = member.LocalMember.ReflectedType;
            if (reflectedType == null)
            {
                return;
            }

            propertyCustomizer.Column(GetColumnName(modelInspector, member));

            bool required =
                member.LocalMember
                      .GetCustomAttributes()
                      .OfType<RequiredAttribute>()
                      .Any();

            // Getting tableType of reflected object
            Type memberType = member.MemberType();

            bool notNullable = required || (memberType != null && memberType.IsPrimitive) || memberType == typeof(DateTime);
            propertyCustomizer.NotNullable(notNullable);

            StringLengthAttribute stringLengthAttribute =
                member.LocalMember
                      .GetCustomAttributes()
                      .OfType<StringLengthAttribute>()
                      .FirstOrDefault();
            if (stringLengthAttribute != null)
            {
                if (stringLengthAttribute.MaximumLength > 0)
                {
                    propertyCustomizer.Length(stringLengthAttribute.MaximumLength);
                }
                else
                {
                    propertyCustomizer.Type(NHibernateUtil.StringClob);
                }
            }
        }
コード例 #11
0
        /// <summary>
        /// Sets the mapper to use:
        ///  1) a properties StringLength attribute if it has one for the databases field size.
        ///  2) non-nullable types to be not nullable in the database.
        ///  3) creates indexes based on the index attributes of properties.
        /// </summary>
        private void OnMapperOnBeforeMapProperty(IModelInspector inspector, PropertyPath member, IPropertyMapper customizer)
        {
            // Get all the custom attributes.
            var customAttributes = member.LocalMember.GetCustomAttributes(false);

            // For all types check for index attributes and add indexes if required.
            var indexAttributes = customAttributes.OfType<IndexAttribute>();
            foreach (var indexAttribute in indexAttributes)
            {
                string indexPrefix = member.GetContainerEntity(inspector).Name;
                if (indexAttribute.Unique)
                {
                    string indexName = string.Format("UI_{0}_{1}", indexPrefix, indexAttribute.Name);
                    customizer.UniqueKey(indexName);
                }
                else
                {
                    string indexName = string.Format("IX_{0}_{1}", indexPrefix, indexAttribute.Name);
                    customizer.Index(indexName);
                }
            }

            // For string types check for string length attribute and set field length if required
            Type memberType = member.LocalMember.GetPropertyOrFieldType();
            if (memberType == typeof(string))
            {
                StringLengthAttribute stringlengthAttribute = (StringLengthAttribute)customAttributes.FirstOrDefault(x => x.GetType() == typeof(StringLengthAttribute));
                int length = DefaltStringLength;
                if (stringlengthAttribute != null && stringlengthAttribute.MaximumLength > 0)
                {
                    length = stringlengthAttribute.MaximumLength;
                }
                customizer.Length(length);
            }

            // For all types if the type is not nullable then set not nullable to true.
            if (!IsNullable(memberType))
            {
                customizer.NotNullable(true);
            }
        }