Beispiel #1
0
        public static void RefTextNamingConvention(IModelInspector modelInspector, PropertyPath member, IPropertyMapper map)
        {
            var property = member.LocalMember as PropertyInfo;

            if (property.Name == "RefText")
            {
                var props       = member.GetContainerEntity(modelInspector).GetProperties();
                var refTextProp = props.Where(p => Attribute.IsDefined(p, typeof(RefTextAttribute), true));
                if (refTextProp.Count() == 1)
                {
                    map.Formula(refTextProp.First().Name);
                }
                else
                {
                    string s          = "";
                    var    signatures = props.Where(
                        p => Attribute.IsDefined(p, typeof(DomainSignatureAttribute), true) && p.PropertyType == typeof(String));
                    foreach (var prop in signatures)
                    {
                        s += prop.Name + "+'-'+";
                    }
                    //if (s == "") throw new NetArchException("Must define RefText, or DomainSignature on string props for a DomainObject class:" + member.GetContainerEntity(modelInspector).Name);
                    if (s == "")
                    {
                        map.Formula("'not defined'");
                    }
                    else
                    {
                        map.Formula(s.Remove(s.Length - 5));
                    }
                }
            }
        }
Beispiel #2
0
 public static void ManyToManyConvention(IModelInspector modelInspector, PropertyPath member, IManyToManyMapper map)
 {
     map.ForeignKey(
         string.Format("fk_{0}_{1}",
                       member.LocalMember.Name,
                       member.GetContainerEntity(modelInspector).Name));
 }
        public static void WithConventions(this ConventionModelMapper mapper, Configuration configuraiton)
        {
            Type baseEntityType = typeof(Entity);

            mapper.IsEntity((type, declared) => IsEntity(type));
            mapper.IsRootEntity((type, declared) => baseEntityType.Equals(type.BaseType));

            mapper.BeforeMapClass += (modelInspector, type, classCustomizer) =>
            {
                classCustomizer.Id(c => c.Column("Id"));
                classCustomizer.Id(c => c.Generator(Generators.Identity));
                //classCustomizer.Table(Inflector.Net.Inflector.Singularize(type.Name.ToString()));
            };
            mapper.BeforeMapManyToOne += (modelInspector, propertyPath, map) =>
            {
                map.Column(propertyPath.LocalMember.GetPropertyOrFieldType().Name + "Id");
                map.Cascade(Cascade.Persist);
            };
            mapper.BeforeMapBag += (modelInspector, PropertyPath, map) =>
            {
                map.Key(keyMapper => keyMapper.Column(PropertyPath.GetContainerEntity(modelInspector).Name + "Id"));
                map.Cascade(Cascade.All);
            };

            AddConventionOverrides(mapper);

            HbmMapping mapping =
                mapper.CompileMappingFor(typeof(Stack).Assembly.GetExportedTypes().Where(t => IsEntity(t)));

            configuraiton.AddDeserializedMapping(mapping, "DevelopmentStackMappings");
        }
Beispiel #4
0
        /// <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;
            var    one            = modelInspector.IsOneToOne(property.LocalMember);


            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);
        }
 private static void BeforeBag(IModelInspector modelinspector, PropertyPath member,
                               IBagPropertiesMapper propertycustomizer)
 {
     propertycustomizer.Key(x => x.Column(member.GetContainerEntity(modelinspector).Name + "Id"));
     propertycustomizer.Cascade(Cascade.All);
     propertycustomizer.Inverse(false);
     propertycustomizer.Lazy(CollectionLazy.NoLazy);
 }
Beispiel #6
0
 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);
 }
Beispiel #7
0
        /// <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.IsManyToManyItem(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 #8
0
 private void ManyToManyConvention(IModelInspector modelInspector, PropertyPath member, IManyToManyMapper map)
 {
     if (FkConstraintNaming != null)
     {
         map.ForeignKey(
             FkConstraintNaming(
                 member.LocalMember,
                 member.GetContainerEntity(modelInspector)));
     }
 }
Beispiel #9
0
        private static void OnBeforeMapCollection(IModelInspector inspector, PropertyPath member,
                                                  ICollectionPropertiesMapper customizer)
        {
            customizer.Key(mapper =>
            {
                mapper.Column(columnMapper => columnMapper.Name(member.GetContainerEntity(inspector).Name + "Id"));
                if (inspector.IsManyToManyItem(member.LocalMember))
                {
                    mapper.ForeignKey(
                        $"FK_{member.GetContainerEntity(inspector).Name.ToUpper()}_{member.LocalMember.Name.ToUpper()}_{member.GetContainerEntity(inspector).Name + "Id"}");
                }
                else
                {
                    var a = member.LocalMember.GetPropertyOrFieldType();
                    if (a.IsEnumerable(out var elementType))
                    {
                        mapper.ForeignKey(
                            $"FK_{elementType.Name.ToUpper()}_{member.GetContainerEntity(inspector).Name.ToUpper()}");
                    }
                }
            });

            if (inspector.IsManyToManyItem(member.LocalMember))
            {
                customizer.Table(member.LocalMember.DeclaringType.Name + "_" + member.LocalMember.Name);
            }
            else
            {
                if (member.LocalMember.GetPropertyOrFieldType().IsEnumerable(out var elementType) &&
                    typeof(Entity).IsAssignableFrom(elementType))
                {
                    var parentRef = elementType
                                    .GetProperties()
                                    .Single(p => p.PropertyType == member.GetContainerEntity(inspector));

                    customizer.Key(o => o.Column($"{parentRef.Name}Id"));
                    customizer.Inverse(true);
                }

                customizer.Cascade(Cascade.All | Cascade.DeleteOrphans);
            }
        }
Beispiel #10
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);
            }
        }
        public void GetContainerEntityWhenPropertyIsInComponentThenReturnEntity()
        {
            var orm = new Mock <IDomainInspector>();

            orm.Setup(x => x.IsEntity(typeof(MainEntity))).Returns(true);
            orm.Setup(x => x.IsRootEntity(typeof(MainEntity))).Returns(true);
            orm.Setup(x => x.IsEntity(typeof(InheritedEntity))).Returns(true);
            var level0 = new PropertyPath(null, ForClass <InheritedEntity> .Property(p => p.Component));
            var path   = new PropertyPath(level0, ForClass <MyComponent> .Property(p => p.Something));

            path.GetContainerEntity(orm.Object).Should().Be(typeof(InheritedEntity));
        }
Beispiel #12
0
        private void OneToManyConvention(IModelInspector modelInspector, PropertyPath member, ICollectionPropertiesMapper map)
        {
            var inv = GetInverseProperty(member.LocalMember);

            if (inv == null && InverseFkColumnNaming != null)
            {
                var containerEntity = member.GetContainerEntity(modelInspector);
                foreach (var idProperty in containerEntity.GetProperties().Where(modelInspector.IsPersistentId))
                {
                    map.Key(x => x.Column(InverseFkColumnNaming(member.LocalMember, containerEntity, idProperty)));
                }
            }
        }
        /// <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);
            }
        }
        private static void OneToManyConvention(IModelInspector modelInspector,
                                                PropertyPath propertyPath,
                                                ISetPropertiesMapper setPropertiesMapper)
        {
            var propertyInfo = propertyPath.LocalMember.GetInverseProperty();

            if (propertyInfo == null)
            {
                setPropertiesMapper.Key(km => km.Column(string.Concat(propertyPath.GetContainerEntity(modelInspector).
                                                                      Name,
                                                                      ConventionNames.PrimaryKeyPostfix)));
                setPropertiesMapper.Cascade(Cascade.All | Cascade.DeleteOrphans);
                setPropertiesMapper.BatchSize(20);
                setPropertiesMapper.Inverse(true);
            }
        }
Beispiel #15
0
        public static void MapBag(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);
                var elementType           = member.LocalMember.GetPropertyOrFieldType().DetermineCollectionElementType();
                var genericCollectionType = typeof(DomainListType <>);
                var collectionType        = genericCollectionType.MakeGenericType(new[] { elementType });
                map.Type(collectionType);
            }
        }
Beispiel #16
0
 private void ReferenceConvention(IModelInspector modelInspector, PropertyPath member, IManyToOneMapper map)
 {
     if (FkColumnNaming != null)
     {
         foreach (var idProperty in member.LocalMember.GetPropertyOrFieldType().GetProperties().Where(modelInspector.IsPersistentId))
         {
             map.Column(k => k.Name(FkColumnNaming(member.LocalMember, idProperty)));
         }
     }
     if (FkConstraintNaming != null)
     {
         map.ForeignKey(
             FkConstraintNaming(
                 member.LocalMember,
                 member.GetContainerEntity(modelInspector)));
     }
 }