Esempio n. 1
0
        private static HbmMapping GetMappings(IEnumerable <Assembly> mappingsAssemblies)
        {
            //Using the built-in auto-mapper
            var mapper = new ConventionModelMapper();

            DefineBaseClass(mapper);
            var allEntities = new List <Type>();

            foreach (var mappingsAssembly in mappingsAssemblies)
            {
                allEntities.AddRange(mappingsAssembly.GetTypes().Where(
                                         t => BaseEntityToIgnore.IsAssignableFrom(t) &&
                                         t != BaseEntityToIgnore &&
                                         !t.IsInterface
                                         ).ToList());
            }
            mapper.AddAllManyToManyRelations(allEntities);
            mapper.ApplyNamingConventions();
            if (MapAllEnumsToStrings)
            {
                mapper.MapAllEnumsToStrings();
            }
            if (AutoMappingOverride != null)
            {
                AutoMappingOverride(mapper);
            }
            OverrideByClassMapping(mapper, mappingsAssemblies);

            var mapping = mapper.CompileMappingFor(allEntities);

            //ShowOutputXmlMappings(mapping);
            return(mapping);
        }
Esempio n. 2
0
        private static HbmMapping GetMappings(Type baseEntityToIgnore, Assembly mappingsAssembly, string mappingsNamespace,
                                              bool mapAllEnumsToStrings, Action <ModelMapper> autoMappingOverride, bool showLogs, string outputXmlMappingsFile)
        {
            //Using the built-in auto-mapper
            var mapper = new ConventionModelMapper();

            DefineBaseClass(mapper, new[] { baseEntityToIgnore });
            var allEntities = mappingsAssembly.GetTypes().Where(t => t.Namespace == mappingsNamespace).ToList();

            mapper.AddAllManyToManyRelations(allEntities);
            mapper.ApplyNamingConventions();
            if (mapAllEnumsToStrings)
            {
                mapper.MapAllEnumsToStrings();
            }
            if (autoMappingOverride != null)
            {
                autoMappingOverride(mapper);
            }

            var mapping = mapper.CompileMappingFor(allEntities);

            showOutputXmlMappings(mapping, showLogs, outputXmlMappingsFile);
            return(mapping);
        }
Esempio n. 3
0
        public static Configuration Init(Configuration configuration, ISessionStorage storage,
                                         Type[] baseEntityToIgnore,
                                         Type[] allEntities,
                                         Action <ModelMapper> autoMappingOverride,
                                         bool showLogs = false)
        {
            InitStorage(storage);

            try
            {
                var mapper = new ConventionModelMapper();

                DefineBaseClass(mapper, baseEntityToIgnore);
                mapper.AddAllManyToManyRelations(allEntities);
                mapper.ApplyNamingConventions();
                mapper.MapAllEnumsToStrings();
                if (autoMappingOverride != null)
                {
                    autoMappingOverride(mapper);
                }

                var mapping = mapper.CompileMappingFor(allEntities);
                showOutputXmlMappings(mapping, showLogs, "mappings.xml");

                configuration.AddDeserializedMapping(mapping, null);
                var sessionFactory = configuration.BuildSessionFactory();

                return(AddConfiguration(DefaultFactoryKey, sessionFactory, configuration, string.Empty));
            }
            catch
            {
                // If this NHibernate config throws an exception, null the Storage reference so
                // the config can be corrected without having to restart the web application.
                Storage = null;
                throw;
            }
        }
Esempio n. 4
0
        private HbmMapping GetMappings()
        {
            //Using the built-in auto-mapper
            var mapper = new ConventionModelMapper();

            DefineBaseClass(mapper);
            var allEntities =
                MappingsAssemblies.SelectMany(a => a.GetTypes()).Where(t => t.Namespace == MappingsNamespace).ToList();

            mapper.AddAllManyToManyRelations(allEntities);
            mapper.ApplyNamingConventions();
            if (MapAllEnumsToStrings)
            {
                mapper.MapAllEnumsToStrings();
            }
            if (AutoMappingOverride != null)
            {
                AutoMappingOverride(mapper);
            }
            var mapping = mapper.CompileMappingFor(allEntities);

            ShowOutputXmlMappings(mapping);
            return(mapping);
        }
Esempio n. 5
0
        public static HbmMapping GetIdentityMappings(List <Type> entities)
        {
            var mapper = new ConventionModelMapper();

            // 1. Antes de mapear as propriedades, verificar atributos;
            mapper.BeforeMapProperty += (insp, prop, map) =>
            {
                var stringLength = prop.LocalMember.GetAttribute <StringLengthAttribute>();
                if (stringLength != null)
                {
                    map.Length(stringLength.MaximumLength);
                }

                var required = prop.LocalMember.GetAttribute <RequiredAttribute>();
                if (required != null)
                {
                    map.NotNullable(true);
                }

                var name = prop.LocalMember.GetAttribute <ColumnAttribute>();
                if (name != null)
                {
                    map.Column(name.Name);
                }
            };

            // 2. Antes de mapear as many-to-one e one-to-many, verificar Ids;
            mapper.BeforeMapManyToOne += (insp, prop, map) =>
            {
                map.Column(prop.LocalMember.GetPropertyOrFieldType().Name + "Id");
                map.Cascade(Cascade.Persist);
            };

            mapper.BeforeMapBag += (insp, prop, map) =>
            {
                map.Key(km => km.Column(prop.GetContainerEntity(insp).Name + "Id"));
                map.Cascade(Cascade.All.Include(Cascade.DeleteOrphans));
                map.BatchSize(10);
            };

            // 3. Match
            Func <Type, bool, bool> matchRootEntity =
                (type, wasDeclared) => typeof(EntityWithTypedId <int>).Equals(type.BaseType) ||
                typeof(EntityWithTypedId <string>).Equals(type.BaseType);

            mapper.IsEntity((type, wasDeclared) => entities.Contains(type));
            mapper.IsRootEntity(matchRootEntity);
            mapper.IsComponent((type, declared) => entities.Contains(type));

            mapper.MapAllEnumsToStrings();

            List <Type> mappings =
                Assembly.GetAssembly(typeof(AlunoMap))
                .GetExportedTypes()
                .Where(t => t.BaseType.IsGenericType &&
                       (t.BaseType.GetGenericTypeDefinition().Equals(typeof(ClassMapping <>)) ||
                        t.BaseType.GetGenericTypeDefinition().Equals(typeof(SubclassMapping <>)) ||
                        t.BaseType.GetGenericTypeDefinition().Equals(typeof(JoinedSubclassMapping <>)) ||
                        t.BaseType.GetGenericTypeDefinition().Equals(typeof(UnionSubclassMapping <>))))
                .ToList();

            mapper.AddMappings(mappings);

            return(mapper.CompileMappingFor(entities));
        }