Exemplo n.º 1
0
        private ISessionFactory GenerateNHibernateSessionFactory()
        {
            var sw = Stopwatch.StartNew();

            ForceLoadObjectModel(); // This is needed for "new Configuration()".
            var configuration = new global::NHibernate.Cfg.Configuration();
            configuration.SetProperty("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
            configuration.SetProperty("connection.connection_string", _connectionString);

            // Set factory-level property:
            configuration.SetProperty("command_timeout", SqlUtility.SqlCommandTimeout.ToString());
            // Set system-level property:
            // Note: Using NHibernate.Cfg.Environment.Properties does not allow setting properties becase the public property returns a copy of the dictionary.
            var globalPropertiesField = typeof(global::NHibernate.Cfg.Environment).GetField("GlobalProperties", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
            var globalProperties = (Dictionary<string, string>)globalPropertiesField.GetValue(null);
            globalProperties.Add("command_timeout", SqlUtility.SqlCommandTimeout.ToString());

            if (SqlUtility.DatabaseLanguage == "MsSql")
            {
                configuration.SetProperty("dialect", "NHibernate.Dialect.MsSql2005Dialect");
                configuration.SetProperty("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
            }
            else if (SqlUtility.DatabaseLanguage == "Oracle")
            {
                configuration.SetProperty("dialect", "NHibernate.Dialect.Oracle10gDialect");
                configuration.SetProperty("connection.driver_class", "NHibernate.Driver.OracleDataClientDriver");
            }
            else
                throw new FrameworkException(DatabaseLanguageError);

            ResolveEventHandler resolveAssembly = (s, args) => _domainObjectModel.Assembly;
            try
            {
                AppDomain.CurrentDomain.AssemblyResolve += resolveAssembly;
                configuration.AddXmlString(_nHibernateMapping.GetMapping());
            }
            finally
            {
                AppDomain.CurrentDomain.AssemblyResolve -= resolveAssembly;
            }

            foreach (var configurationExtension in _nHibernateConfigurationExtensions)
                configurationExtension.ExtendConfiguration(configuration);

            SchemaMetadataUpdater.QuoteTableAndColumns(configuration);
            var sessionFactory = configuration.BuildSessionFactory();

            _performanceLogger.Write(sw, "NHibernatePersistenceEngine.PrepareNHSessionFactory");
            return sessionFactory;
        }