Пример #1
0
        /// <summary>
        /// Called only be the main Program. The public static getter for <see cref="Instance"/>
        /// requires that this method has been called before.
        /// </summary>
        /// <param name="configuration"></param>
        public static void Configure(DataFactoryConfiguration configuration)
        {
            DataFactory.logger = configuration.Logger;
            lazySessionFactory = new Lazy <ISessionFactory>(() =>
            {
                var factory = FluentNHibernate.Cfg.Fluently.Configure()
                              .Database(DatabaseTypeWithConnectionStringToConfigurer(configuration))
                              .ExposeConfiguration(config =>
                {
                    config.SetInterceptor(new SqlStatementInterceptor());

                    var update = new NHibernate.Tool.hbm2ddl.SchemaUpdate(config);
                    update.Execute(scripts =>
                    {
                        logger.LogTrace(scripts);
                    }, doUpdate: true);

                    if (update.Exceptions.Count > 0)
                    {
                        foreach (var ex in update.Exceptions)
                        {
                            logger.LogError(ex, $"{ex.Message}\nStacktrace:\n{ex.StackTrace}");
                        }
                        throw new AggregateException("Cannot create/update the DB schema.", update.Exceptions);
                    }
                })
                              .Mappings(mappings =>
                {
                    mappings.FluentMappings
                    .AddFromAssemblyOf <DataFactory>()
                    .Conventions.Add(typeof(IndexedConvention))
                    .Conventions.Add(typeof(ForeignKeyConvention));
                })
                              .BuildSessionFactory();

                return(factory);
            });
        }
Пример #2
0
        /// <summary>
        /// Returns the correct type of <see cref="IPersistenceConfigurer"/> for the selected
        /// database-type.
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="tempDirectory"></param>
        /// <returns></returns>
        private static IPersistenceConfigurer DatabaseTypeWithConnectionStringToConfigurer(DataFactoryConfiguration configuration)
        {
            if (configuration.DatabaseType == DatabaseType.SQLiteTemp)
            {
                var pathToFile = $"{Path.Combine(configuration.TempDirectory ?? Path.GetTempPath(), Path.GetRandomFileName())}.sqlite";
                logger.LogWarning("Using temporary SQLite file: {0}", pathToFile);
                return(SQLiteConfiguration.Standard.UsingFile(pathToFile));
            }

            var connString = configuration.DatabaseConnectionString;

            if (String.IsNullOrEmpty(connString) || String.IsNullOrWhiteSpace(connString))
            {
                throw new ArgumentException("Configuration's database connection string not valid.");
            }

            switch (configuration.DatabaseType)
            {
            case DatabaseType.MsSQL2000:
                return(MsSqlConfiguration.MsSql2000.ConnectionString(connString));

            case DatabaseType.MsSQL2005:
                return(MsSqlConfiguration.MsSql2005.ConnectionString(connString));

            case DatabaseType.MsSQL2008:
                return(MsSqlConfiguration.MsSql2008.ConnectionString(connString));

            case DatabaseType.MsSQL2012:
                return(MsSqlConfiguration.MsSql2012.ConnectionString(connString));

            case DatabaseType.MySQL:
                return(MySQLConfiguration.Standard.ConnectionString(connString));

            case DatabaseType.Oracle9:
                return(OracleDataClientConfiguration.Oracle9.ConnectionString(connString));

            case DatabaseType.Oracle10:
                return(OracleDataClientConfiguration.Oracle10.ConnectionString(connString));

            case DatabaseType.PgSQL81:
                return(PostgreSQLConfiguration.PostgreSQL81.ConnectionString(connString));

            case DatabaseType.PgSQL82:
                return(PostgreSQLConfiguration.PostgreSQL82.ConnectionString(connString));

            case DatabaseType.SQLite:
                return(SQLiteConfiguration.Standard.ConnectionString(connString));

            default:
                throw new NotSupportedException(
                          String.Format("Database-type {0} is not currently supported.", configuration.DatabaseType.ToString()));
            }
        }