private void BeforeMappingCollectionConvention(IModelInspector inspector, PropertyPath member, ICollectionPropertiesMapper customizer)
        {
            if (inspector.IsManyToMany(member.LocalMember))
                customizer.Table(member.ManyToManyIntermediateTableName());

            customizer.Key(k => k.Column(DetermineKeyColumnName(inspector, member)));
        }
		protected virtual void MemberReadOnlyAccessor(IModelInspector modelInspector, PropertyPath member, IAccessorPropertyMapper propertyCustomizer)
		{
			if (MatchReadOnlyProperty(member.LocalMember))
			{
				propertyCustomizer.Access(Accessor.ReadOnly);
			}
		}
        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);
        }
 private void OnBeforeMapProperty(IModelInspector modelinspector, PropertyPath member, IPropertyMapper propertycustomizer)
 {
     if(member.LocalMember.Name == "Name")
     {
         propertycustomizer.Unique(true);
     }
 }
 public static void ManyToManyConvention(IModelInspector modelInspector, PropertyPath member, IManyToManyMapper map)
 {
     map.ForeignKey(
         string.Format("fk_{0}_{1}",
                member.LocalMember.Name,
                member.GetContainerEntity(modelInspector).Name));
 }
        private static string DetermineKeyColumnName(IModelInspector inspector, PropertyPath member)
        {
            var otherSideProperty = member.OneToManyOtherSideProperty();
            if (inspector.IsOneToMany(member.LocalMember) && otherSideProperty != null)
                return otherSideProperty.Name + ForeignKeyColumnPostfix;

            return member.Owner().Name + ForeignKeyColumnPostfix;
        }
 public static void MapStringAsVarchar(IModelInspector modelInspector, PropertyPath member, IPropertyMapper propertyCustomizer)
 {
     var propertyInfo = member.LocalMember as PropertyInfo;
     if (propertyInfo != null && propertyInfo.PropertyType == typeof(string))
     {
         propertyCustomizer.Type(NHibernateUtil.AnsiString);
     }
 }
 public static void ReferenceConvention(IModelInspector modelInspector, PropertyPath member, IManyToOneMapper map)
 {
     map.Column(k => k.Name(member.LocalMember.GetPropertyOrFieldType().Name + "Id"));
     map.ForeignKey(
         string.Format("fk_{0}_{1}",
                member.LocalMember.Name,
                member.GetContainerEntity(modelInspector).Name));
     map.Cascade(Cascade.All | Cascade.DeleteOrphans);
 }
        /// <summary>
        /// Returns the name of the foreign key in a relationship.
        /// </summary>
        /// <param name="inspector">The model inspector.</param>
        /// <param name="member">The entity property.</param>
        /// <returns>The name of the foreign key.</returns>
        public string ForeignKey(IModelInspector inspector, PropertyPath member, Type declaringType = null, Type idDeclaringType = null)
        {
            Requires.That(member, "member").IsNotNull();
            Requires.That(member.LocalMember, "member.LocalMember").IsNotNull();

            return ForeignKeyNameFormat.Formatted(Table(inspector, idDeclaringType ?? member.LocalMember.GetPropertyOrFieldType()),
                                              Table(inspector, declaringType ?? member.LocalMember.DeclaringType),
                                              Column(inspector, member, idDeclaringType));
        }
 /// <summary>
 /// Applies the <see cref="DateTimeOffsetSplitType"/> Composite user type to all <see cref="DateTimeOffset"/> fields in the mapping.
 /// </summary>
 /// <remarks>
 /// Allows the use of <see cref="DateTimeOffset"/> type with databases that do not natively support it.
 /// User: mapper.BeforeMapProperty += ModelMapperHelper.ApplyDateTimeOffsetSplitTypeToDateTimeOffset
 /// </remarks>
 public static void ApplyDateTimeOffsetSplitTypeToDateTimeOffset(IModelInspector inspector, PropertyPath property, IPropertyMapper mapper)
 {
     Type propertyType = property.LocalMember.GetPropertyOrFieldType();
     if (propertyType == typeof(DateTimeOffset) || propertyType == typeof(DateTimeOffset?))
     {
         mapper.Type(typeof(DateTimeOffsetSplitType), null);
         string columName = property.ToColumnName();
         mapper.Columns(n => n.Name(columName + "DateTime"), n => n.Name(columName + "Offset"));
     }
 }
		public PropertyPath(PropertyPath previousPath, MemberInfo localMember)
		{
			if (localMember == null)
			{
				throw new ArgumentNullException("localMember");
			}
			this.previousPath = previousPath;
			this.localMember = localMember;
			hashCode = localMember.GetHashCode() ^ (previousPath != null ? previousPath.GetHashCode() : 41);
		}
Beispiel #12
0
 private void IdBagTableName(IModelInspector modelInspector, PropertyPath member, IIdBagPropertiesMapper propertyCustomizer)
 {
     if (member.LocalMember.IsComponentCollection(Mapper.ModelInspector))
     {
         propertyCustomizer.Table(member.GetRootMember().DeclaringType.Name + member.ToColumnName());
     }
     else
     {
         propertyCustomizer.Table(BidirectionAssociation.AnalizeManyToMany(member.LocalMember).ManyToManyTablename);
     }
 }
Beispiel #13
0
 public static void OneToManyConvention(IModelInspector modelInspector, PropertyPath member, IBagPropertiesMapper map)
 {
     var inv = member.LocalMember.GetInverseProperty();
     if (inv == null)
     {
         map.Key(x => x.Column(member.GetContainerEntity(modelInspector).Name + "Id"));
         map.Cascade(Cascade.All | Cascade.DeleteOrphans);
         map.BatchSize(20);
         map.Inverse(true);
     }
 }
Beispiel #14
0
        private static void MapEnumAsString(IModelInspector modelInspector, PropertyPath member, IPropertyMapper propertyCustomizer)
        {
            var propertyType = member.LocalMember.GetPropertyOrFieldType();

            if (propertyType.IsEnumOrNullableEnum() == false) return;

            var enumType = propertyType.IsEnum ? propertyType : propertyType.GetGenericArguments().Single();

            var type = typeof(EnumStringType<>).MakeGenericType(enumType);

            propertyCustomizer.Type(type, null);
        }
		protected virtual void ComponentParentNoSetterToField(IModelInspector modelInspector, PropertyPath member, IComponentAttributesMapper componentMapper)
		{
			System.Type componentType = member.LocalMember.GetPropertyOrFieldType();
			IEnumerable<MemberInfo> persistentProperties =
				MembersProvider.GetComponentMembers(componentType).Where(p => ModelInspector.IsPersistentProperty(p));

			MemberInfo parentReferenceProperty = GetComponentParentReferenceProperty(persistentProperties, member.LocalMember.ReflectedType);
			if (parentReferenceProperty != null && MatchNoSetterProperty(parentReferenceProperty))
			{
				componentMapper.Parent(parentReferenceProperty, cp => cp.Access(Accessor.NoSetter));
			}
		}
Beispiel #16
0
        private void NameKeyColumn(IModelInspector modelinspector, PropertyPath member, IBagPropertiesMapper propertycustomizer)
        {
            var association = BidirectionAssociation.AnalyzeManyToOne(member);

            var columnName = association.IsBidirectional
                           ? association.ColumnNameOnCollectionSide
                           : member.GetRootMember().ReflectedType.Name + "Id";

            propertycustomizer.Key(keyMapper => keyMapper.Column(columnName));

            propertycustomizer.Inverse(association.IsBidirectional);
        }
		public bool Equals(PropertyPath other)
		{
			if (ReferenceEquals(null, other))
			{
				return false;
			}
			if (ReferenceEquals(this, other))
			{
				return true;
			}
			return hashCode == other.GetHashCode();
		}
		public bool Equals(PropertyPath other)
		{
			if (ReferenceEquals(null, other))
			{
				return false;
			}
			if (ReferenceEquals(this, other))
			{
				return true;
			}
			return Equals(previousPath, other.previousPath) && localMember.Equals(other.localMember);
		}
		protected virtual new void BeforeMapSet(IModelInspector modelInspector, PropertyPath member, ISetPropertiesMapper propertyCustomizer)
		{
			if (modelInspector.IsManyToMany(member.LocalMember) == true)
			{
				propertyCustomizer.Key(x => x.Column(member.LocalMember.DeclaringType.Name + "_Id"));

				Type sourceType = member.LocalMember.DeclaringType;
				Type destinationType = member.LocalMember.GetPropertyOrFieldType().GetGenericArguments().First();
				String [] names = new Type[] { sourceType, destinationType }.Select(x => x.Name).OrderBy(x => x).ToArray();

				//set inverse on the relation of the alphabetically first entity name
				propertyCustomizer.Inverse(sourceType.Name == names.First());
				//set mapping table name from the entity names in alphabetical order
				propertyCustomizer.Table(String.Join("_", names));
			}
		}
Beispiel #20
0
        private void IdBagHiloGenerator(IModelInspector modelInspector, PropertyPath member, IIdBagPropertiesMapper propertyCustomizer)
        {
            var hiloRowName = member.LocalMember.IsComponentCollection(Mapper.ModelInspector)
                                  ? member.GetRootMember().DeclaringType.Name + member.ToColumnName()
                                  : BidirectionAssociation.AnalizeManyToMany(member.LocalMember).ManyToManyTablename;

            propertyCustomizer.Id(idMap =>
            {
                idMap.Generator(new EntityHighLowGeneratorDef(hiloRowName));

                idMap.Column("Id");
                idMap.Type((IIdentifierType)NHibernateUtil.Int64);
            });

            Mapper.AddHiLoScript(EntityHighLowGenerator.GetInsertFor(hiloRowName));
        }
		/// <summary>
		/// Provide the list of progressive-paths
		/// </summary>
		/// <param name="source"></param>
		/// <returns>
		/// Given a path as : Pl1.Pl2.Pl3.Pl4.Pl5 returns paths-sequence as:
		/// Pl5
		/// Pl4.Pl5
		/// Pl3.Pl4.Pl5
		/// Pl2.Pl3.Pl4.Pl5
		/// Pl1.Pl2.Pl3.Pl4.Pl5
		/// </returns>
		public static IEnumerable<PropertyPath> InverseProgressivePath(this PropertyPath source)
		{
			if (source == null)
			{
				throw new ArgumentNullException("source");
			}
			PropertyPath analizing = source;
			var returnLocalMembers = new List<MemberInfo>(10);
			do
			{
				returnLocalMembers.Add(analizing.LocalMember);
				PropertyPath progressivePath = null;
				for (int i = returnLocalMembers.Count - 1; i >= 0; i--)
				{
					progressivePath = new PropertyPath(progressivePath, returnLocalMembers[i]);
				}
				yield return progressivePath;
				analizing = analizing.PreviousPath;
			} while (analizing != null);
		}
        /// <summary>
        /// Returns the name of the table column for an entity's property.
        /// </summary>
        /// <param name="inspector">The model inspector.</param>
        /// <param name="member">The entity property.</param>
        /// <param name="declaringType"> </param>
        /// <returns>The name of the table column.</returns>
        public string Column(IModelInspector inspector, PropertyPath member, Type declaringType = null)
        {
            Requires.That(member, "member").IsNotNull();
            Requires.That(member.LocalMember, "member.LocalMember").IsNotNull();

            var localName = member.LocalMember.Name.Underscore();

            if (declaringType != null)
            {
                return KeyColumnFormat.Formatted(declaringType.Name.Underscore(), localName);
            }

            var type = member.LocalMember.GetPropertyOrFieldType();

            if (inspector.IsEntity(type)) // is a foreign key
            {
                var id = inspector.FindPersistentId(type);

                if (id != null) return KeyColumnFormat.Formatted(localName, Column(inspector, id));
            }

            return localName;
        }
        /// <summary>
        /// Maps a map relationship
        /// </summary>
        /// <param name="modelInspector">The model inspector</param>
        /// <param name="member">The member to map</param>
        /// <param name="collectionRelationElementCustomizer">The property mapper </param>
        private void MapMapKey(IModelInspector modelInspector, PropertyPath member, IMapKeyMapper mapper)
        {
            string columName = namingEngine.ToElementKeyColumnName(member.LocalMember);

            mapper.Column(columName);
        }
        /// <summary>
        /// Maps a many to one relationship
        /// </summary>
        /// <param name="modelInspector">The model inspector</param>
        /// <param name="property">The property to map</param>
        /// <param name="mapper">The property mapper</param>
        private void MapManyToOne(IModelInspector modelInspector, PropertyPath property, IManyToOneMapper mapper)
        {
            Type targetEntityType = property.LocalMember.GetPropertyOrFieldType();
            Type sourceEntityType = property.GetContainerEntity(modelInspector);
            MemberInfo member = property.PreviousPath != null ? property.PreviousPath.LocalMember : property.LocalMember;

            var targetEntityIDProperty = modelInspector.GetIdentifierMember(targetEntityType);
            var foreignKeyProperty = property.LocalMember;

            string columnName = null;
            string foreignKeyName = null;

            if (MatchOneToOneComponent(property, modelInspector))
            {
                columnName = namingEngine.ToComponentForeignKeyColumnName(foreignKeyProperty, member, targetEntityIDProperty);
                foreignKeyName = namingEngine.ToForeignKeyName(sourceEntityType, targetEntityType, member, targetEntityIDProperty);
            }
            else
            {
                columnName = namingEngine.ToForeignKeyColumnName(property.LocalMember, targetEntityIDProperty);
                foreignKeyName = namingEngine.ToForeignKeyName(sourceEntityType, targetEntityType, foreignKeyProperty, targetEntityIDProperty);
            }

            mapper.Column(columnName);
            mapper.ForeignKey(foreignKeyName);
        }
        /// <summary>
        /// Maps a many to many relationship
        /// </summary>
        /// <param name="modelInspector">The model inspector</param>
        /// <param name="property">The property to map</param>
        /// <param name="mapper">The property mapper</param>
        private void MapManyToMany(IModelInspector modelInspector, PropertyPath property, IManyToManyMapper mapper)
        {
            Type sourceType = property.LocalMember.DeclaringType;
            Type targetType = property.LocalMember.GetPropertyOrFieldType().DetermineCollectionElementType();

            var primaryKeyProperty = modelInspector.GetIdentifierMember(targetType);
            var foreignKeyProperty = property.LocalMember;
            string foreignKeyColumnName = namingEngine.ToManyToManyForeignKeyColumnName(targetType, primaryKeyProperty);
            string foreignKeyName = namingEngine.ToManyToManyForeignKeyName(sourceType, targetType, targetType, primaryKeyProperty);

            mapper.Column(foreignKeyColumnName);
            mapper.ForeignKey(foreignKeyName);
        }
        /// <summary>
        /// Maps a collection of components or entities
        /// </summary>
        /// <param name="modelInspector">The model inspector</param>
        /// <param name="property">The property to map</param>
        /// <param name="mapper">The collections mapper</param>
        private void MapCollection(IModelInspector modelInspector, PropertyPath property, ICollectionPropertiesMapper mapper)
        {
            Type sourceType = property.GetContainerEntity(modelInspector);
            Type targetType = property.LocalMember.GetPropertyOrFieldType().DetermineCollectionElementType();

            var primaryKeyProperty = modelInspector.GetIdentifierMember(sourceType);
            var foreignKeyProperty = property.LocalMember;
            string foreignKeyColumnName = null;
            string foreignKeyName = null;
            string tableName = null;
            string schemaName = null;

            if (modelInspector.IsEntity(targetType))
            {
                // Entity Relationship Mapping
                if (modelInspector.IsManyToMany(property.LocalMember))
                {
                    // Many to many
                    foreignKeyColumnName = namingEngine.ToManyToManyForeignKeyColumnName(sourceType, primaryKeyProperty);
                    foreignKeyName = namingEngine.ToManyToManyForeignKeyName(sourceType, targetType, sourceType, primaryKeyProperty);
                    tableName = namingEngine.ToManyToManyTableName(sourceType, targetType);
                    schemaName = namingEngine.ToSchemaName(sourceType, targetType);
                }
                else
                {
                    // One to Many
                    foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty);
                    foreignKeyName = namingEngine.ToForeignKeyName(targetType, sourceType, sourceType, primaryKeyProperty);
                }
            }
            else if (IsElement(targetType))
            {
                // Element mapping
                foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty);
                foreignKeyName = namingEngine.ToComponentForeignKeyName(targetType, sourceType, foreignKeyProperty, primaryKeyProperty);
                tableName = namingEngine.ToElementTableName(sourceType, targetType, property.LocalMember);
                schemaName = namingEngine.ToSchemaName(sourceType, targetType);
            }
            else
            {
                // Component Relationship Mapping
                foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty);
                foreignKeyName = namingEngine.ToComponentForeignKeyName(targetType, sourceType, foreignKeyProperty, primaryKeyProperty);
                tableName = namingEngine.ToComponentTableName(sourceType, targetType, property.LocalMember);
                schemaName = namingEngine.ToSchemaName(sourceType, targetType);
            }

            // Mapping
            mapper.Schema(schemaName);
            mapper.Table(tableName);
            mapper.Key(k =>
            {
                k.Column(foreignKeyColumnName);
                k.ForeignKey(foreignKeyName);
            });
        }
Beispiel #27
0
 // derived from: https://gist.github.com/NOtherDev/1569982
 internal static System.Type CollectionElementType(
     this NHibernate.Mapping.ByCode.PropertyPath member
     ) =>
 member.LocalMember.GetPropertyOrFieldType()
 .DetermineCollectionElementOrDictionaryValueType();
		protected virtual void MemberToFieldAccessor(IModelInspector modelInspector, PropertyPath member, IAccessorPropertyMapper propertyCustomizer)
		{
			if (MatchPropertyToField(member.LocalMember))
			{
				propertyCustomizer.Access(Accessor.Field);
			}
		}
        protected virtual void ComponentParentNoSetterToField(IModelInspector modelInspector, PropertyPath member, IComponentAttributesMapper componentMapper)
        {
            System.Type componentType = member.LocalMember.GetPropertyOrFieldType();
            IEnumerable <MemberInfo> persistentProperties =
                MembersProvider.GetComponentMembers(componentType).Where(p => ModelInspector.IsPersistentProperty(p));

            MemberInfo parentReferenceProperty = GetComponentParentReferenceProperty(persistentProperties, member.LocalMember.ReflectedType);

            if (parentReferenceProperty != null && MatchNoSetterProperty(parentReferenceProperty))
            {
                componentMapper.Parent(parentReferenceProperty, cp => cp.Access(Accessor.NoSetter));
            }
        }
 protected virtual void MemberReadOnlyAccessor(IModelInspector modelInspector, PropertyPath member, IAccessorPropertyMapper propertyCustomizer)
 {
     if (MatchReadOnlyProperty(member.LocalMember))
     {
         propertyCustomizer.Access(Accessor.ReadOnly);
     }
 }
 /// <summary>
 /// Maps a property according the naming conventions configuration
 /// </summary>
 /// <param name="modelInspector">The model inspector</param>
 /// <param name="property">The property</param>
 /// <param name="mapper">The property mapper</param>
 private void MapProperty(IModelInspector modelInspector, PropertyPath property, IPropertyMapper mapper)
 {
     if (MatchOneToOneComponent(property, modelInspector))
     {
         mapper.Column(this.namingEngine.ToComponentColumnName(property.LocalMember, property.PreviousPath.LocalMember));
     }
     else
     {
         mapper.Column(this.namingEngine.ToColumnName(property.LocalMember));
     }
 }
        /// <summary>
        /// Indicates if a given property matches a one to one component relationship
        /// </summary>
        /// <param name="property">The property</param>
        /// <param name="modelInspector">An instance of the current model inspector</param>
        /// <returns>True if the property matches a one to one component relationship, false if not</returns>
        private bool MatchOneToOneComponent(PropertyPath property, IModelInspector modelInspector)
        {
            bool result = false;
            if (modelInspector.IsComponent(property.LocalMember.DeclaringType))
            {
                result = (property.PreviousPath != null) && !property.PreviousPath.LocalMember.GetPropertyOrFieldType().IsGenericCollection();
            }

            return result;
        }
Beispiel #33
0
 private static void SetNotNullable(IModelInspector modelInspector, PropertyPath member, IPropertyMapper propertyCustomizer)
 {
     propertyCustomizer.NotNullable(member.IsNotNullable());
 }