private static SessionFactoryInfo GetSessionFactoryInfo <T>(FluentNHibernatePersistenceBuilderOptions options,
                                                                    KeyInfo keyInfo,
                                                                    ProviderTypeEnum providerType, Func <ConfigurationInfo> configFunc)
        {
            var key = CalculateSHA512Hash(new JavaScriptSerializer().Serialize(keyInfo));

            lock (Mutex)
            {
                if (SessionFactoryInfos.ContainsKey(key))
                {
                    return(SessionFactoryInfos[key]);
                }
                var configurationInfo = configFunc();
                var configurationInfoPersistenceConfigurer = configurationInfo.PersistenceConfigurer;
                var fluentConfiguration = Fluently.Configure()
                                          .Mappings(x => x.FluentMappings.AddFromAssemblyOf <T>())
                                          .Database(configurationInfoPersistenceConfigurer);
                fluentConfiguration.ExposeConfiguration(cfg =>
                {
                    if (options.UpdateSchema)
                    {
                        var schemaUpdate = new SchemaUpdate(cfg);
                        using (var stringWriter = new StringWriter())
                        {
                            try
                            {
                                schemaUpdate.Execute(i => stringWriter.WriteLine(i), true);
                            }
                            catch (Exception ex)
                            {
                                throw;
                            }
                            var d = stringWriter.ToString();
                        }
                    }
                });
                fluentConfiguration.BuildConfiguration();
                SessionFactoryInfos[key] = new SessionFactoryInfo(key, fluentConfiguration.BuildSessionFactory(),
                                                                  providerType, options);
                return(SessionFactoryInfos[key]);
            }
        }
Пример #2
0
        private static SessionFactoryInfo GetSessionFactoryInfo(List <Assembly> sourceAssemblies,
                                                                FluentNHibernatePersistenceBuilderOptions options,
                                                                KeyInfo keyInfo,
                                                                ProviderTypeEnum providerType, Func <ConfigurationInfo> configFunc)
        {
            var key = CalculateSHA512Hash(JsonConvert.SerializeObject(keyInfo));

            lock (Mutex)
            {
                if (SessionFactoryInfos.ContainsKey(key))
                {
                    return(SessionFactoryInfos[key]);
                }
                var configurationInfo = configFunc();
                var configurationInfoPersistenceConfigurer = configurationInfo.PersistenceConfigurer;
                var configuration = Fluently.Configure().Database(configurationInfoPersistenceConfigurer);
                foreach (var assembly in sourceAssemblies)
                {
                    configuration = configuration.Mappings(x => x.FluentMappings.AddFromAssembly(assembly));
                }

                using (var objectNameStore = new ObjectRenameManager())
                {
                    //make sure we only execute ExposeConfiguration once.  It will try to execute again when we build the session factory
                    var exposed = false;
                    configuration.ExposeConfiguration(cfg =>
                    {
                        if (exposed)
                        {
                            return;
                        }
                        exposed = true;

                        objectNameStore.RenameObjects(options, cfg);


                        if (options.UpdateSchema)
                        {
                            var schemaUpdate = new SchemaUpdate(cfg);
                            using (LogProvider.OpenNestedContext("Schema update"))
                            {
                                Logger.Debug("Starting schema update");
                                schemaUpdate.Execute(i => Logger.Debug(i), true);
                                Logger.Debug("Done with schema update");
                            }


                            if (schemaUpdate.Exceptions != null && schemaUpdate.Exceptions.Any())
                            {
                                throw new SchemaException(
                                    string.Format("Schema update failed with {1} exceptions.  See {0} property",
                                                  nameof(SchemaException.Exceptions), schemaUpdate.Exceptions.Count),
                                    schemaUpdate.Exceptions);
                            }
                        }
                    });

                    SessionFactoryInfos[key] = new SessionFactoryInfo(key, configuration.BuildSessionFactory(),
                                                                      providerType, options);
                }

                return(SessionFactoryInfos[key]);
            }
        }