예제 #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);
            });
        }