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 SagaPersistenceModel(IEnumerable<Type> typesToScan)
        {
            var sagaEntites = typesToScan.Where(t => typeof(ISagaEntity).IsAssignableFrom(t) && !t.IsInterface);

            var model = new AutoPersistenceModel();

            model.Conventions.AddFromAssemblyOf<IdShouldBeAssignedConvention>();

            var entityTypes = GetTypesThatShouldBeAutoMapped(sagaEntites,typesToScan);

            var assembliesContainingSagas = sagaEntites.Select(t => t.Assembly).Distinct();

            foreach (var assembly in assembliesContainingSagas)
                model.AddEntityAssembly(assembly)
                    .Where(t => entityTypes.Contains(t));

            var componentTypes = GetTypesThatShouldBeMappedAsComponents(sagaEntites);

            model.Setup(s =>
                          {
                              s.IsComponentType =
                                  type => componentTypes.Contains(type);
                          });
            return model;
        }
Exemplo n.º 3
0
        public static AutoPersistenceModel SagaPersistenceModel(IEnumerable <Type> typesToScan)
        {
            var sagaEntites = typesToScan.Where(t => typeof(ISagaEntity).IsAssignableFrom(t) && !t.IsInterface);


            var model = new AutoPersistenceModel();

            model.Conventions.AddFromAssemblyOf <IdShouldBeAssignedConvention>();

            var entityTypes = GetTypesThatShouldBeAutoMapped(sagaEntites, typesToScan);

            var assembliesContainingSagas = sagaEntites.Select(t => t.Assembly).Distinct();

            foreach (var assembly in assembliesContainingSagas)
            {
                model.AddEntityAssembly(assembly)
                .Where(t => entityTypes.Contains(t));
            }

            var componentTypes = GetTypesThatShouldBeMappedAsComponents(sagaEntites);

            model.Setup(s =>
            {
                s.IsComponentType =
                    type => componentTypes.Contains(type);
            });
            return(model);
        }
 public AutoPersistenceModel Generate()
 {
     var mappings = new AutoPersistenceModel();
     mappings.AddEntityAssembly(typeof (Class1).Assembly).Where(GetAutoMappingFilter);
     mappings.Conventions.Setup(GetConventions());
     mappings.Setup(GetSetup());
     mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
     return mappings;
 }
        public AutoPersistenceModel Generate()
        {
            var mappings = new AutoPersistenceModel();
            mappings.AddEntityAssembly(typeof(Soul).Assembly).Where(GetAutoMappingFilter);
            mappings.Conventions.Setup(GetConventions());
            mappings.Setup(GetSetup());
            mappings.IgnoreBase<Entity>();
            mappings.IgnoreBase(typeof(EntityWithTypedId<>));
            mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

            // Membershipprovider Automappings
            mappings.AddEntityAssembly(typeof(Shaml.Membership.Core.User).Assembly).Where(GetAutoMappingFilter);
            mappings.Override<Shaml.Membership.Core.User>(map => Shaml.Membership.Data.Overrides.UserOverride(map));
            mappings.Override<Shaml.Membership.Core.Role>(map => Shaml.Membership.Data.Overrides.RoleOverride(map));
            mappings.Override<Shaml.Membership.Core.ProfileData>(map => Shaml.Membership.Data.Overrides.ProfileDataOverride(map));
            mappings.Override<Shaml.Membership.Core.Session>(map => Shaml.Membership.Data.Overrides.SessionOverride(map));

            return mappings;
        }
Exemplo n.º 6
0
 public AutoPersistenceModel Generate()
 {
     var mappings = new AutoPersistenceModel();
     mappings.AddEntityAssembly(typeof(Site).Assembly).Where(GetAutoMappingFilter);
     //    mappings.Setup(GetSetup());
     mappings.IgnoreBase<Entity>();
     mappings.IgnoreBase(typeof(EntityWithTypedId<>));
     mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
     return mappings;
 }
Exemplo n.º 7
0
        public static AutoPersistenceModel Generate()
        {
            AutoPersistenceModel automap = new AutoPersistenceModel(new AutoMappingConfig());

            automap.Conventions.AddFromAssemblyOf <Entity>();//.IncludeBase<Dictionary>();;
            automap.UseOverridesFromAssemblyOf <Entity>();
            automap.AddEntityAssembly(Assembly.GetAssembly(typeof(Entity)));
            automap.AddMappingsFromAssembly(Assembly.GetAssembly(typeof(Entity)));
            return(automap);
        }
        public AutoPersistenceModel Generate()
        {
            var mappings = new AutoPersistenceModel();

            mappings.AddEntityAssembly(typeof(TestEntity).Assembly).Where(this.GetAutoMappingFilter);
            mappings.Conventions.Setup(this.GetConventions());
            mappings.Setup(this.GetSetup());
            mappings.IgnoreBase <Entity>();
            mappings.IgnoreBase(typeof(EntityWithTypedId <>));
            mappings.UseOverridesFromAssemblyOf <AutoPersistenceModelGenerator>();
            return(mappings);
        }
        public AutoPersistenceModel Generate()
        {
            var mappings = new AutoPersistenceModel();

            mappings.AddEntityAssembly(typeof (Class1).Assembly).Where(GetAutoMappingFilter);
            mappings.Conventions.Setup(GetConventions());
            mappings.Setup(GetSetup());
            mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

            mappings.Override<Idea>(i => i.Map(m => m.Text).CustomSqlType("TEXT"));
            mappings.Override<Comment>(c => c.Map(m => m.Text).CustomSqlType("TEXT"));

            return mappings;
        }
        public AutoPersistenceModel Generate()
        {
            var mappings = new AutoPersistenceModel();

            mappings
                .AddEntityAssembly(typeof(Player).Assembly)
                .UseOverridesFromAssembly(typeof(AutoPersistenceModelGenerator).Assembly)
                .Where(GetAutoMappingFilter);
                //.Setup(GetSetup())
                //.Conventions.Setup(GetConventions);

            MapEnums(mappings);

            return mappings;
        }
        public AutoPersistenceModel Generate()
        {
            var mappings = new AutoPersistenceModel();

            mappings.AddEntityAssembly(typeof(Layout).Assembly).Where(GetAutoMappingFilter);
            mappings.Conventions.Setup(GetConventions());
            mappings.Setup(GetSetup());
            mappings.IgnoreBase <Entity>();
            mappings.IgnoreBase <BasicLocation>();
            mappings.IncludeBase <BasicUser>();
            //mappings.IncludeBase<RegisteredUser>();
            //mappings.IncludeBase<Contact>();
            mappings.IgnoreBase(typeof(EntityWithTypedId <>));
            mappings.UseOverridesFromAssemblyOf <AutoPersistenceModelGenerator>();

            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.º 13
0
        ISessionFactory GetSessionFactory()
        {
            var autoPersistenceModel = new AutoPersistenceModel();

            autoPersistenceModel
                .AddEntityAssembly(GetType().Assembly)
                .Where(t => t.Namespace.StartsWith("NHibernateWebPersistenceExample.Models"));

            ISessionFactory sessionFactory = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                              .ConnectionString(c =>
                                                c.Is(
                                                    @"Data Source=.\SQLEXPRESS;Initial Catalog=Example;Integrated Security=True;Pooling=False"))
                              .ShowSql())
                .Mappings(m => m.AutoMappings.Add(autoPersistenceModel))
                .ExposeConfiguration(innerConfiguration =>
                                     innerConfiguration.SetProperty("current_session_context_class", "web"))
                .BuildSessionFactory();

            return sessionFactory;
        }
Exemplo n.º 14
0
 /// <summary>
 /// Configures FluentNHibernate to map the domain types used in PerfTest.
 /// </summary>
 public static AutoPersistenceModel AutoMap(AutoPersistenceModel model)
 {
     model
         .AddEntityAssembly(typeof(TestSuiteResult).Assembly)
         .Override<TestSuiteResult>(map =>
         {
             map.HasMany(x => x.SystemInfoList).AsSet().Inverse().Cascade.All().Not.LazyLoad();
             map.HasMany(x => x.TestResultList).AsSet().Inverse().Cascade.All().Not.LazyLoad();
         })
         .Override<SystemInfo>(map =>
         {
             map.References(x => x.TestSuiteResult, "TestSuiteResultId").Cascade.None().Not.Nullable();
         })
         .Override<TestResult>(map =>
         {
             map.HasMany(x => x.TimingDataList).AsSet().Inverse().Cascade.All().Not.LazyLoad();
             map.References(x => x.TestSuiteResult, "TestSuiteResultID").Cascade.None().Not.Nullable();
         })
         .Override<TimingData>(map =>
         {
             map.References(x => x.TestResult, "TestResultID").Cascade.None().Not.Nullable();
         });
     return model;
 }
        private void RegisterEntitiesAssembly(Configuration configuration, Assembly assembly)
        {
            var model = new AutoPersistenceModel()
                .WithSetup(s => s.IsBaseType = (type => type == typeof (BaseEntity) || type == typeof (BaseOrderedEntity)
                                                        || type == typeof (BaseVersionedEntity) || type == typeof (BaseVersionedAndOrderedEntity)))
                .ConventionDiscovery.Setup(c =>
                                           	{
                                           		c.Add<FluentNHibernate.Conventions.Defaults.PrimaryKeyConvention>();
                                           		c.Add<ForeignKeyConvention>();
                                           		c.Add<TableNameConvention>();
                                           		c.Add<ManyToManyTableConvention>();
                                           	}
                );

            model
                .AddEntityAssembly(assembly)
                .Where(entity => entity.IsAbstract == false &&
                                 entity.GetInterface("IEntity") != null)
                .Configure(configuration);
        }
Exemplo n.º 16
0
        public static void SetupAutoMapperForEntities(IDependencyResolver dependencyResolver, params Assembly[] assemblies)
        {
            var database = dependencyResolver.Resolve<IPersistenceConfigurer>();
            var config = Fluently.Configure().Database(database);
            var stringPropertyconvention = ConventionBuilder.Property.When(x => x.Expect(p => p.Property.PropertyType == typeof (string)), a => a.Length(255));

            var cacheConvention = ConventionBuilder.Class.Always(c => c.Cache.ReadWrite());

            var collectionsConventionMany = ConventionBuilder.HasMany.When(
                x => x.Expect(p => !(p.Member is DummyPropertyInfo)),
                instance => { instance.Cascade.SaveUpdate(); instance.Cache.ReadWrite(); });

            var collectionsConventionManyToMany = ConventionBuilder.HasManyToMany.When(
                x => x.Expect(p => !(p.Member is DummyPropertyInfo)),
                instance => { instance.Cascade.SaveUpdate(); instance.Cache.ReadWrite(); });
            var lazyConvention = ConventionBuilder.Reference.Always(c => c.LazyLoad());

            IEnumerable<Assembly> ass;
            if (assemblies == null || assemblies.Length == 0)
            {
                ass = AppDomain.CurrentDomain.GetAssemblies().Where(
                    a => !a.FullName.StartsWith("System.") &&
                         !a.FullName.StartsWith("Microsoft.") &&
                         !a.FullName.Contains("mscorlib") &&
                         a != typeof(ISession).Assembly &&
                         a != typeof(AutoMap).Assembly
               );
            }
            else
            {
                ass = assemblies;
            }

            var autoPersistenceModel = new AutoPersistenceModel()
                .Conventions.Add(cacheConvention, collectionsConventionMany, collectionsConventionManyToMany, stringPropertyconvention, lazyConvention)
                .IgnoreBase(typeof (BaseEntity<>))
                .IgnoreBase(typeof (IBaseEntity));

            foreach (var assembly in ass)
            {
                try
                {
                    assembly.GetExportedTypes();
                }
                catch (NotSupportedException)
                {
                    continue;//cannot do dynamic assemblies
                }
                if (assembly != null)
                {
                    Assembly automapper = assembly;

                    config.Mappings(m => m.HbmMappings.AddFromAssembly(automapper));
                    autoPersistenceModel
                        .AddEntityAssembly(automapper)
                        .Conventions.AddAssembly(automapper)
                        .Alterations(alterations => alterations.AddFromAssembly(automapper))
                        .Alterations(collection => collection.Add(new AutoMappingOverrideAlteration(automapper))) //same as: UseOverridesFromAssemblyOf<Tentity>()
                        .Where(t => typeof (IBaseEntity).IsAssignableFrom(t));

                    // MORE Evil hack, since adding to the Alterations does NOT work.
                    //new AutoMappingOverrideAlteration(automapper).Alter(autoPersistenceModel);
                }
            }

            // Evil hack, since adding to the Alterations does NOT work.
            //foreach (var overrideAssembly in ass)
            //{
            //    new AutoMappingOverrideAlteration(overrideAssembly).Alter(autoPersistenceModel);
            //}

            config.Mappings(m => m.AutoMappings.Add(autoPersistenceModel)
            #if DEBUG
            .ExportTo(@"C:\temp\")
            #endif
            );

            var nhconfig = config.BuildConfiguration();
            var sessionFactory = config.BuildSessionFactory();

            dependencyResolver.RegisterInstance<Configuration>(nhconfig);
            dependencyResolver.RegisterInstance<ISessionFactory>(sessionFactory);

            new SchemaUpdate(nhconfig).Execute(true, true);
        }
Exemplo n.º 17
0
        public static Configuration SetupAutoMapperForEntities(IPersistenceConfigurer database, IDependencyResolver dependencyResolver, params Assembly[] assemblies)
        {
            var config = Fluently.Configure().Database(database)
                         .Cache(cache => cache.UseQueryCache().ProviderClass <SysCacheProvider>().QueryCacheFactory <ProjectionEnabledQueryCacheFactory>())
                         .ProxyFactoryFactory(typeof(DefaultProxyFactoryFactory))
                         .CurrentSessionContext(CurrentSessionContextClass);

            var stringPropertyconvention = ConventionBuilder.Property.When(x => x.Expect(p => p.Property.PropertyType == typeof(string)), a => a.Length(255));

            var cacheConvention = ConventionBuilder.Class.Always(c => c.Cache.ReadWrite());

            var collectionsConventionMany = ConventionBuilder.HasMany.When(
                x => x.Expect(p => !(p.Member is DummyPropertyInfo)),
                instance => { instance.Cascade.SaveUpdate(); instance.Cache.ReadWrite(); });

            var collectionsConventionManyToMany = ConventionBuilder.HasManyToMany.When(
                x => x.Expect(p => !(p.Member is DummyPropertyInfo)),
                instance => { instance.Cascade.SaveUpdate(); instance.Cache.ReadWrite(); });
            var lazyConvention = ConventionBuilder.Reference.Always(c => c.LazyLoad());

            IEnumerable <Assembly> ass;

            if (assemblies == null || assemblies.Length == 0)
            {
                ass = AppDomain.CurrentDomain.GetAssemblies().Where(
                    a => !a.FullName.StartsWith("System.") &&
                    !a.FullName.StartsWith("Microsoft.") &&
                    !a.FullName.Contains("mscorlib") &&
                    a != typeof(ISession).Assembly &&
                    a != typeof(AutoMap).Assembly
                    );
            }
            else
            {
                ass = assemblies;
            }

            var autoPersistenceModel = new AutoPersistenceModel()
                                       .Conventions.Add(cacheConvention, collectionsConventionMany, collectionsConventionManyToMany, stringPropertyconvention, lazyConvention)
                                       .IgnoreBase(typeof(BaseEntity <>))
                                       .IgnoreBase(typeof(IBaseEntity));

            foreach (var assembly in ass)
            {
                try
                {
                    assembly.GetExportedTypes();
                }
                catch (NotSupportedException)
                {
                    continue;//cannot do dynamic assemblies
                }
                if (assembly != null)
                {
                    Assembly automapper = assembly;

                    config.Mappings(m => m.HbmMappings.AddFromAssembly(automapper));
                    autoPersistenceModel
                    .AddEntityAssembly(automapper)
                    .Conventions.AddAssembly(automapper)
                    .Alterations(alterations => alterations.AddFromAssembly(automapper))
                    .Alterations(collection => collection.Add(new AutoMappingOverrideAlteration(automapper)))     //same as: UseOverridesFromAssemblyOf<Tentity>()
                    .Where(t => typeof(IBaseEntity).IsAssignableFrom(t));

                    // MORE Evil hack, since adding to the Alterations does NOT work.
                    //new AutoMappingOverrideAlteration(automapper).Alter(autoPersistenceModel);
                }
            }

            // Evil hack, since adding to the Alterations does NOT work.
            //foreach (var overrideAssembly in ass)
            //{
            //    new AutoMappingOverrideAlteration(overrideAssembly).Alter(autoPersistenceModel);
            //}

            config.Mappings(m => m.AutoMappings.Add(autoPersistenceModel)
#if DEBUG
                            .ExportTo(@"C:\temp\")
#endif
                            );

            return(config.BuildConfiguration());
        }
Exemplo n.º 18
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);
        }