Example #1
0
        /// <summary>
        /// Configures the NHibernate session.
        /// </summary>
        /// <param name="configurationProvider">The configuration provider.</param>
        /// <param name="container">The container.</param>
        protected override void ConfigureNHibernateSession(IConfigurationProvider configurationProvider, IContainer container)
        {
            var configuration = configurationProvider.Configure().GetEnumerator().Current;
            configuration.SetProperty(Environment.CurrentSessionContextClass, typeof(WebSessionContext).AssemblyQualifiedName);
            var sessionFactoryProvider = new SessionFactoryProvider(configurationProvider);
            sessionFactoryProvider.Initialize();

            container.Configure(x => x.For<ISessionFactoryProvider>().Singleton().Use(sessionFactoryProvider));
        }
Example #2
0
        public override void Configure(IWindsorContainer container)
        {
            container.AddFacility<PersistenceConversationFacility>();
            container.AddFacility<FactorySupportFacility>();

            NHibernate.Cfg.Environment.BytecodeProvider = new EnhancedBytecode(container);
            var nhConfigurator = new DefaultSessionFactoryConfigurationProvider();

            //sessionFactory part
            var sessionFactoryProvider = new SessionFactoryProvider(nhConfigurator);
            container.Register(Component.For<ISessionFactory>().UsingFactoryMethod(() => sessionFactoryProvider.GetFactory(null)));
            container.Register(Component.For<ISessionFactoryImplementor>().UsingFactoryMethod(() => (ISessionFactoryImplementor)sessionFactoryProvider.GetFactory(null)));

            //uNhAddIns part
            container.Register(Component.For<ISessionFactoryProvider>().Instance(sessionFactoryProvider));
            container.Register(Component.For<ISessionWrapper>().ImplementedBy<SessionWrapper>());
            container.Register(Component.For<IConversationFactory>().ImplementedBy<DefaultConversationFactory>());
            container.Register(Component.For<IConversationsContainerAccessor>().ImplementedBy<NhConversationsContainerAccessor>());

            nhConfigurator.BeforeConfigure += (sender, e) =>
            {
                var nhCfg = e.Configuration;
                nhCfg.RegisterEntityNameResolver();
                RegisterProperties(nhCfg);
                RegisterMappings(nhCfg);
                RegisterListeners(nhCfg);
                RegisterEntityCaches(nhCfg);
                SchemaMetadataUpdater.QuoteTableAndColumns(nhCfg);
                e.Configured = true; // prevent read from Nhibernate.cfg
            };


            nhConfigurator.AfterConfigure += (sender, e) =>
            {
                var nhCfg = e.Configuration;

                //TryUpdateSchema
                var value = ConfigurationManager.AppSettings["SchemaUpdate"];
                if (value.IsNotNullOrEmpty() && value.Equals(bool.TrueString, StringComparison.OrdinalIgnoreCase))
                    new SchemaUpdate(nhCfg).Execute(false, true);
            };

           

            base.Configure(container);
        }
        public void Configure(IWindsorContainer container)
        {
            container.AddFacility<PersistenceConversationFacility>();

            Environment.BytecodeProvider = new EnhancedBytecode(container);

            var nhConfigurator = new DefaultSessionFactoryConfigurationProvider();
            nhConfigurator.BeforeConfigure += (sender, e) =>
                {
                    ValidatorInitializer.Initialize(e.Configuration);
                    e.Configuration.RegisterEntityNameResolver();
                    e.Configuration.Properties[Environment.CollectionTypeFactoryClass]
                        = typeof (WpfCollectionTypeFactory).AssemblyQualifiedName;

                };

            var sfp = new SessionFactoryProvider(nhConfigurator);

            container.Register(Component.For<ISessionFactoryProvider>()
                               	.Instance(sfp));
            container.Register(
                Component.For<ISessionFactory>().UsingFactoryMethod(
                    () =>

                            container.Resolve<ISessionFactoryProvider>().GetFactory(null))
                        );

            container.Register(
                Component.For<ISessionFactoryImplementor>().UsingFactoryMethod(
                    () => (ISessionFactoryImplementor) container.Resolve<ISessionFactoryProvider>().GetFactory(null)));

            container.Register(Component.For<ISessionWrapper>().ImplementedBy<SessionWrapper>());
            container.Register(Component.For<IConversationFactory>().ImplementedBy<DefaultConversationFactory>());
            container.Register(
                Component.For<IConversationsContainerAccessor>().ImplementedBy<NhConversationsContainerAccessor>());
        }
Example #4
0
        private static void Main()
        {
            var appConfig = new AppSettingsConfiguration ();
            var connectionStringBuilder = new SqlConnectionStringBuilder ();

            connectionStringBuilder.DataSource = appConfig.GetProperty ( "DatabaseServerName" );
            connectionStringBuilder.IntegratedSecurity = true;

            var dbCreated = false;
            var databaseName = appConfig.GetProperty ( "DatabaseCatalogName" );
            var sqlDataPath = String.Empty; // appConfig.GetProperty("SqlDataPath");

            using ( var sqlConnnection = new SqlConnection ( connectionStringBuilder.ConnectionString ) )
            {
                sqlConnnection.Open ();

                sqlDataPath = GetDbFilePath ( sqlConnnection, "master" );

                if ( !CheckDbExists ( sqlConnnection, databaseName ) )
                {
                    CreateDatabase ( sqlConnnection, databaseName, sqlDataPath );
                    dbCreated = true;
                }
            }
            var appSettingsConfiguration = new AppSettingsConfiguration ();
            Console.WriteLine ( "Creating schema script...." );
            var msSql2008PersistenceConfigurerProvider = new MsSql2008PersistenceConfigurerProvider ( appSettingsConfiguration );
            #if DEBUG
            FluentConfigurationSerializer.IsEnabled = false;
            #endif
            IConfigurationProvider fluentConfigurationProvider = new FluentConfigurationProvider ( msSql2008PersistenceConfigurerProvider, new AssemblyLocator() );
            var configuration = fluentConfigurationProvider.Configure ().GetEnumerator ().Current;
            ISessionFactoryProvider sessionFactoryProvider = new SessionFactoryProvider ( fluentConfigurationProvider );
            var sessionFactory = sessionFactoryProvider.GetFactory ( null );

            var exportFileName = appConfig.GetProperty ( ExportOutputFilePropertyName );

            var sb = new StringBuilder ();

            var headerComment = string.Format (
                "{0}{0}{1}{0}{2}{0}{3}{0}",
                Environment.NewLine,
                "/***********************************************************",
                "Begin: SQL Scripts to create RemGenDatabase database objects",
                "***********************************************************/"
                );
            sb.Append ( headerComment );

            var sql = GenerateSchemaCreationSql ();

            using ( var session = sessionFactory.OpenSession () )
            {
                TextWriter textWriter = new StringWriter ();

                new SchemaExport ( configuration ).Execute (
                    true,
                    false,
                    false,
                    session.Connection,
                    textWriter );
                sql += textWriter.ToString ();
            }

            sb.Append ( sql );
            sb.Append ( Environment.NewLine );
            sb.Append ( "/***********************************************************" );
            sb.Append ( Environment.NewLine );
            sb.Append ( "End: SQL Scripts to create RemGenDatabase database objects" );
            sb.Append ( Environment.NewLine );
            sb.Append ( "***********************************************************/" );
            sb.Append ( Environment.NewLine );
            sb.Append ( Environment.NewLine );

            Debug.Write ( sb.ToString () );
            Console.WriteLine ( sb.ToString () );

            using ( var fs = new FileStream ( exportFileName, FileMode.OpenOrCreate, FileAccess.Write ) )
            {
                using ( var streamWriter = new StreamWriter ( fs ) )
                {
                    streamWriter.Write ( sb.ToString () );
                }
            }
            Console.WriteLine ( "Deploy to Database (Y/N)?" );
            var answer = Console.ReadLine ();
            if ( string.Compare ( answer, "yes", true ) == 0 || string.Compare ( answer, "y", true ) == 0 )
            {
                if ( !dbCreated )
                {
                    Console.WriteLine ( "Dropping Database..." );
                    using ( var sqlConnection = new SqlConnection ( connectionStringBuilder.ConnectionString ) )
                    {
                        sqlConnection.Open ();
                        using (
                            var sqlDropDatabaseCommand =
                                new SqlCommand (
                                    string.Format (
                                        "USE {0} ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE USE master DROP DATABASE {0}",
                                        databaseName ),
                                    sqlConnection ) )
                        {
                            sqlDropDatabaseCommand.ExecuteNonQuery ();
                        }
                    }
                }
                using ( var sqlConnection = new SqlConnection ( connectionStringBuilder.ConnectionString ) )
                {
                    sqlConnection.Open ();
                    if ( !dbCreated )
                    {
                        CreateDatabase ( sqlConnection, databaseName, sqlDataPath );
                    }
                }
                //connectionStringBuilder.InitialCatalog = databaseName;
                using ( var sqlConnection = new SqlConnection ( connectionStringBuilder.ConnectionString ) )
                {
                    sqlConnection.Open ();
                    sqlConnection.ChangeDatabase ( databaseName );
                    Console.WriteLine ( "Running schema script..." );
                    var fullScript = sb.ToString ();
                    var goSplit = fullScript.Split (
                        new[] { string.Format ( "go {0}", Environment.NewLine ) }, StringSplitOptions.RemoveEmptyEntries );
                    foreach ( var sqlstring in goSplit )
                    {
                        var sqlToRun = sqlstring;
                        if ( sqlstring == goSplit[0] )
                        {
                            sqlToRun = sqlstring.Replace ( headerComment, "" );
                        }
                        using ( var createSchemaCommand = new SqlCommand ( sqlToRun, sqlConnection ) )
                        {
                            createSchemaCommand.ExecuteNonQuery ();
                        }
                    }
                }
            }
        }
Example #5
0
        private static void InitializeAndRegisterPersistenceStuff( IContainer container )
        {
            var configurationProvider = container.GetInstance<IConfigurationProvider> ();
            var configuration = configurationProvider.Configure ().GetEnumerator ().Current;
            configuration.SetProperty (
                Environment.CurrentSessionContextClass,
                "uNhAddIns.SessionEasier.Contexts.ThreadLocalSessionContext, uNhAddIns" );
            var sessionFactoryProvider = new SessionFactoryProvider ( configurationProvider );
            sessionFactoryProvider.Initialize ();

            container.Configure ( x => x.For<ISessionFactoryProvider> ().Singleton ().Use ( sessionFactoryProvider ) );
            container.Configure ( x => x.For<ISessionProvider> ().Use<SessionProvider> () );
        }