Exemplo n.º 1
0
        public void Configure(Configuration configuration)
        {
            var autoPersistenceModel = new AutoPersistenceModel(this.autoMappingConfiguration);

            foreach (var conventionType in this.conventionTypes)
            {
                autoPersistenceModel.Conventions.Add(conventionType);
            }

            foreach (var assembly in this.autoMapAssemblies)
            {
                autoPersistenceModel.AddEntityAssembly(assembly);
            }

            foreach (var assembly in this.overrideAssemblies)
            {
                autoPersistenceModel.UseOverridesFromAssembly(assembly);
            }

            foreach (var @override in this.overrides)
            {
                @override(autoPersistenceModel);
            }

            autoPersistenceModel.Configure(configuration);
        }
Exemplo n.º 2
0
 public static AutoPersistenceModel UseOverridesFromAssemblies(this AutoPersistenceModel model, IEnumerable <Assembly> assemblies)
 {
     foreach (var assembly in assemblies)
     {
         model.UseOverridesFromAssembly(assembly);
     }
     return(model);
 }
Exemplo n.º 3
0
 public static AutoPersistenceModel UseOverridesFromAssemblies(this AutoPersistenceModel model,
                                                               params Assembly[] assemblies)
 {
     foreach (
         Assembly assembly in assemblies.Where(assembly => !assembly.IsDynamic && !assembly.GlobalAssemblyCache))
     {
         model.UseOverridesFromAssembly(assembly);
     }
     return(model);
 }
Exemplo n.º 4
0
        private AutoPersistenceModel Generate()
        {
            AutoPersistenceModel mappings = AutoMap.Assemblies(
                new AutoMapConfiguration(), new Assembly[] { this.GetType().Assembly });

            mappings.UseOverridesFromAssembly(this.GetType().Assembly);


            return(mappings);
        }
Exemplo n.º 5
0
        private AutoPersistenceModel Generate()
        {
            AutoPersistenceModel mappings = AutoMap.Assembly(this.GetType().Assembly, new AutoMapConfiguration());

            mappings.Conventions.Setup(finder =>
            {
                finder.Add <EnumConvention>();
            });
            mappings.UseOverridesFromAssembly(GetType().Assembly);
            return(mappings);
        }
        private static AutoPersistenceModel GetAutoMappingSettings( Assembly entitiesAssembly, Assembly mappingsOverridesAssembly )
        {
            var persistenceModel = new AutoPersistenceModel();
            persistenceModel.AddEntityAssembly( entitiesAssembly ).Where( x => x.BaseType.Name == "BaseEntity" );
            persistenceModel.UseOverridesFromAssembly( mappingsOverridesAssembly );
            persistenceModel.Conventions.Add(
                PrimaryKey.Name.Is( x => "Id" ),
                ConventionBuilder.Id.Always( x => x.GeneratedBy.Identity() ),
                ConventionBuilder.Property.Always( x => x.Not.Nullable() ),
                ConventionBuilder.Property.When( expectation =>
                    expectation.Expect( propertyInspector => propertyInspector.Property.PropertyType == typeof( string ) ),
                    instance => instance.Length( 256 ) ),
                ConventionBuilder.Reference.Always( x => x.Not.Nullable() ),
                ForeignKey.EndsWith( "Id" )
            );

            return persistenceModel;
        }
Exemplo n.º 7
0
        private ISessionFactory CreateSessionFactory()
        {
            Configuration initialConfiguration = this.GetConfiguration()
                                                 .OrFail(nameof(this.GetConfiguration) + "()");

            FluentConfiguration fluentConfiguration = Fluently.Configure(initialConfiguration)
                                                      //.CurrentSessionContext<CurrentSessionContext>()
                                                      //.Mappings(
                                                      //    m => m.FluentMappings
                                                      //          .Add<GusContractorSnapshot.Map>()
                                                      //          .Add<GusContractorLog.Map>()
                                                      //)
            ;

            Library[] libraries = this.Librarian.OrFail(nameof(this.Librarian))
                                  .GetLibraries();

            IConvention[] conventions = this.GetConventions()
                                        .OrFail(nameof(this.Conventions));

            Type[] entities = this.GetEntities()
                              .ToArray();

            var automappingConfiguration = new AutomappingConfiguration(entities);

            foreach (Library library in libraries)
            {
                Assembly             assembly         = library.GetAssembly();
                AutoPersistenceModel assemblyMappings = AutoMap.Assembly(assembly, automappingConfiguration);
                assemblyMappings.Conventions.Add(conventions);
                assemblyMappings.UseOverridesFromAssembly(assembly);
                assemblyMappings.IgnoreBase <Entity>();

                fluentConfiguration.Mappings(m =>
                {
                    m.FluentMappings.Conventions.Add(conventions);
                    m.AutoMappings.Add(assemblyMappings);
                });
            }

            this.configuration = fluentConfiguration.BuildConfiguration();
            return(fluentConfiguration.BuildSessionFactory());
        }
        //public static IList<Type> IgnoredBaseTypes = new List<Type>
        //                                                 {

        //                                                     typeof(Person),typeof(Entity)

        //                                                 };

        //public static IList<Type> IncludeBaseTypes = new List<Type>
        //                                                 {
        //                                                     typeof (Employee)
        //                                                 };

        public static AutoPersistenceModel Generate(string[] domainAssemblies, string[] dalAssemblies)
        {
            AutoPersistenceModel mappings = AutoMap.Assemblies(
                new AutoMapConfiguration(), domainAssemblies.Select(Assembly.LoadFrom).ToArray());

            //foreach (Type ignoredBaseType in IgnoredBaseTypes)
            //{
            //    mappings.IgnoreBase(ignoredBaseType);
            //}
            //foreach (Type includeBaseType in IncludeBaseTypes)
            //{
            //    mappings.IncludeBase(includeBaseType);
            //}

            mappings.Conventions.Setup(GetConventions());


            foreach (string dalAssembly in dalAssemblies)
            {
                mappings.UseOverridesFromAssembly(Assembly.LoadFrom(dalAssembly));
            }

            return(mappings);
        }
Exemplo n.º 9
0
        private static AutoPersistenceModel AddMappingAssembliesTo(string instCode, Configuration cfg)
        {
            var hc = System.Configuration.ConfigurationManager.GetSection(CfgXmlHelper.CfgSectionName) as HibernateConfiguration;

            if (hc == null)
            {
                throw new HibernateConfigException("Cannot process NHibernate Section in config file.");
            }

            var mappingAssemblies = new HashSet <string>();

            mappingAssemblies.Add(ThisAssembly); // Add yourself

            // Check for automap overrides, ClassMap<> or .hbm type mapping files
            if (hc.SessionFactory != null)
            {
                foreach (var file in hc.SessionFactory.Mappings.Select(x => x.Assembly))
                {
                    mappingAssemblies.Add(file);
                }
            }

            //Doing this ensures that any other specified libraries are captured
            if (MappingAssemblies.Any())
            {
                foreach (var file in MappingAssemblies)
                {
                    mappingAssemblies.Add(file);
                }
            }

            var mappingAssembliesLoaded = new List <Assembly>(mappingAssemblies.Count);

            foreach (var mappingAssemblyCfg in mappingAssemblies)
            {
                var assembly = Assembly.Load(mappingAssemblyCfg);
                mappingAssembliesLoaded.Add(assembly);
            }

            var autoMapConfig = new AutomappingConfiguration();
            //autoMapConfig.ScanForPotentialClassMaps(mappingAssembliesLoaded);
            AutoPersistenceModel autoPersistenceModel = new AutoPersistenceModel(autoMapConfig);

            foreach (var assembly in mappingAssembliesLoaded)
            {
                // Looks for any HBMs
                cfg.AddAssembly(assembly);

                // Looks for fluent mappings
                autoPersistenceModel.AddMappingsFromAssembly(assembly);

                // Looks for auto-mapping overrides
                autoPersistenceModel.UseOverridesFromAssembly(assembly);
            }

            #region Auto-mapping
            // Check entity assemblies for possible automapping in case the mapping file is not written
            // Hey, don't forget this assembly and the 'Core' - check them too

            // 'Core' first
            var entityAssembly = typeof(Entity).Assembly;
            autoPersistenceModel.AddEntityAssembly(entityAssembly);

            // The rest
            var entityAssemblyName = entityAssembly.GetName().Name;
            foreach (var assemblyName in EntityAssemblies)
            {
                if (assemblyName.Equals(entityAssemblyName, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                // Looks for automap
                var assembly = Assembly.Load(assemblyName);
                autoPersistenceModel.AddEntityAssembly(assembly);
            }

            // The below code does not work because we (may) have user-defined configurations
            //bool notCoreInst = !instCode.Equals(Utilities.INST_DEFAULT_CODE, StringComparison.OrdinalIgnoreCase);
            //if (notCoreInst) // => Tenant, so do not map those entities marked as Hosted Centrally
            //{
            //    autoPersistenceModel.Where(x => !typeof(IAmHostedCentrally).IsAssignableFrom(x));
            //}
            #endregion

            cfg.BeforeBindMapping += (sender, args) => args.Mapping.autoimport = false;

            return(autoPersistenceModel);
        }