Beispiel #1
0
        /// <summary>
        ///   Creates and initializes the model mapper.
        /// </summary>
        /// <returns> The model mapper. </returns>
        protected virtual ModelMapper CreateModelMapper()
        {
            var mapper = new ConventionModelMapper();

            mapper.BeforeMapClass += (inspector, type, customizer) =>
            {
                customizer.Table(NamingConvention.Table(inspector, type));

                PropertyPath property = inspector.FindPersistentId(type);

                if (property != null)
                {
                    customizer.Id(property.LocalMember, map =>
                    {
                        map.Column(
                            NamingConvention.Column
                                (inspector,
                                property));

                        ApplyIdConventions(map,
                                           property
                                           .
                                           LocalMember
                                           as
                                           PropertyInfo);
                    });
                }
            };

            mapper.BeforeMapJoinedSubclass += (inspector, type, customizer) =>
            {
                var id = inspector.FindPersistentId(type.BaseType);


                customizer.Table(NamingConvention.Table(inspector, type));
                customizer.Key(
                    key =>
                {
                    key.Column(NamingConvention.KeyColumn(inspector, id,
                                                          type.BaseType));
                    key.ForeignKey(NamingConvention.ForeignKey(inspector, id, type, type.BaseType));
                });
            };

            mapper.BeforeMapProperty += (inspector, member, customizer) =>
            {
                if (!inspector.IsPersistentId(member.LocalMember) &&
                    !inspector.IsPersistentProperty(member.LocalMember))
                {
                    return;
                }

                customizer.Column(NamingConvention.Column(inspector, member));

                Type type = member.LocalMember.GetPropertyOrFieldType();

                ApplyPropertyConventions(customizer, member, type, GetMemberAttributes(member.LocalMember));
            };

            mapper.BeforeMapManyToOne += (inspector, member, customizer) =>
            {
                customizer.Column(NamingConvention.Column(inspector, member));
                customizer.ForeignKey(NamingConvention
                                      .ForeignKey(inspector, member));
                customizer.Index(NamingConvention.Index(inspector, member));

                Type type = member.LocalMember.GetPropertyOrFieldType();

                ApplyManyToOneConventions(customizer, member, type, GetMemberAttributes(member.LocalMember));
            };

            mapper.BeforeMapBag += (inspector, member, customizer) =>
            {
                var inverse = GetLikelyInverseProperty(member);

                var keyColumn = inverse == null
                                                               ? NamingConvention.KeyColumn(inspector, member)
                                                               : NamingConvention.Column(inspector, inverse);


                customizer.Key(key => key.Column(keyColumn));

                ApplyBagConventions(customizer, member);
            };

            mapper.BeforeMapSet += (inspector, member, customizer) =>
            {
                var inverse = GetLikelyInverseProperty(member);

                var keyColumn = inverse == null
                                                               ? NamingConvention.KeyColumn(inspector, member)
                                                               : NamingConvention.Column(inspector, inverse);

                customizer.Key(key => key.Column(keyColumn));

                ApplySetConventions(customizer, member);
            };

            mapper.BeforeMapList += (inspector, member, customizer) =>
            {
                var inverse = GetLikelyInverseProperty(member);

                var keyColumn = inverse == null
                                                                ? NamingConvention.KeyColumn(inspector, member)
                                                                : NamingConvention.Column(inspector, inverse);

                customizer.Key(key => key.Column(keyColumn));
                customizer.Index(index => index.Column(NamingConvention.IndexColumn(inspector, member)));

                ApplyListConventions(customizer, member);
            };

            mapper.BeforeMapMap += (inspector, member, customizer) =>
            {
                var inverse = GetLikelyInverseProperty(member);

                var keyColumn = inverse == null
                                                               ? NamingConvention.KeyColumn(inspector, member)
                                                               : NamingConvention.Column(inspector, inverse);

                // TODO: figure out how to set the map key column (not just the foreign key)

                customizer.Key(key => key.Column(keyColumn));

                ApplyMapConventions(customizer, member);
            };


            mapper.IsEntity(IsEntity);
            mapper.IsRootEntity(IsRootEntity);
            //mapper.IsTablePerClassHierarchy(IsTablePerClassHierarchy);

            return(mapper);
        }