Пример #1
0
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            var schemaCreator = new SchemaCreator();

            schemaCreator.CreateSchema();

            var container = new WindsorContainer()
                            .AddFacility <WcfFacility>()
                            .AddFacility <FactorySupportFacility>()
                            .Register(SessionEndPoint)
                            .Register(Component.For <IProductRepository>().ImplementedBy <ProductRepository>())
                            .Register(Service <IProductModel, ProductModel>(9595));

            var nhConfigurator = new DefaultSessionFactoryConfigurationProvider();
            var sfp            = new SessionFactoryProvider(nhConfigurator);

            container.Register(Component.For <ISessionFactoryProvider>().Instance(sfp));
            container.Register(Component.For <ISessionWrapper>().ImplementedBy <SessionWrapper>());
            container.Register(Component.For <ISessionFactory>().Instance(sfp.GetFactory(null)));

            IoC.RegisterResolver(new WindsorDependencyResolver(container));

            CurrentSessionContext.Wrapper = container.Resolve <ISessionWrapper>();

            Console.WriteLine("Server started");
            Console.ReadLine();
            schemaCreator.CreateSchema();
        }
        public void ShouldInitializeOnlyOneTime()
        {
            var sfp = new SessionFactoryProvider();

            Assert.That(sfp.GetFactory(null), Is.Not.Null);

            Assert.That(Spying.Logger <SessionFactoryProvider>().Execute(sfp.Initialize).WholeMessage,
                        Text.DoesNotContain("Initialize a new session factory"));
        }
        public void ShouldHasOnlyOneInstanceOfFactory()
        {
            var             sfp = new SessionFactoryProvider();
            ISessionFactory sf1 = sfp.GetFactory(null);
            ISessionFactory sf2 = sfp.GetFactory(null);

            Assert.That(sf1, Is.Not.Null);
            Assert.That(ReferenceEquals(sf1, sf2));
            Assert.That(sfp.Count(), Is.EqualTo(1));
            IEnumerator en = ((IEnumerable)sfp).GetEnumerator();
            int         i  = 0;

            while (en.MoveNext())
            {
                i++;
            }
            Assert.That(i, Is.EqualTo(1));
        }
Пример #4
0
        static void InitializeNHSessionManagement()
        {
            container.AddFacility <PersistenceConversationFacility>();
            var nhConfigurator = new DefaultSessionFactoryConfigurationProvider();

            nhConfigurator.AfterConfigure += ((sender, e) => new SchemaExport(e.Configuration).Create(false, true));
            var sfp = new SessionFactoryProvider(nhConfigurator);

            container.Register(Component.For <ISessionFactoryProvider>().Instance(sfp));
            // I prefer to use uNhAddIns.LinFuAdapters.SessionWrapper , the discussion: http://groups.google.com/group/unhaddins/browse_thread/thread/e548a6f6a8a444fd
            container.Register(Component.For <ISessionWrapper>().ImplementedBy <SessionWrapper>());
            container.Register(Component.For <ISessionFactory>().Instance(sfp.GetFactory(null)));
            container.Register(Component.For <IConversationFactory>().ImplementedBy <DefaultConversationFactory>());
            container.Register(Component.For <IConversationsContainerAccessor>().ImplementedBy <NhConversationsContainerAccessor>());
        }
        public void ShouldExecuteDisposeOlnyOne()
        {
            SessionFactoryProvider sfp;
            ISessionFactory        sf1;
            bool disposed = false;

            using (sfp = new SessionFactoryProvider())
            {
                sfp.BeforeCloseSessionFactory += ((sender, e) => disposed = true);
                sf1 = sfp.GetFactory(null);
            }
            Assert.That(disposed);
            Assert.That(sf1.IsClosed, "The session-factory should be closed.");
            disposed = false;
            sfp.Dispose();
            Assert.That(!disposed);
        }
Пример #6
0
        public IServiceLocator GetServiceLocator()
        {
            var sl = new HomeMadeServiceLocator();

            var nhConfigurator = new DefaultSessionFactoryConfigurationProvider();

            nhConfigurator.AfterConfigure += ((sender, e) => new SchemaExport(e.Configuration).Create(false, true));
            var sfp = new SessionFactoryProvider(nhConfigurator);

            sl.LoadSingletonService <ISessionFactoryProvider>(sfp);

            sl.LoadSingletonService <IConversationFactory>(new DefaultConversationFactory(sfp, new FakeSessionWrapper()));

            sl.LoadSingletonService <IConversationsContainerAccessor>(new NhConversationsContainerAccessor(sfp));

            sl.LoadSingletonService <IDaoFactory>(new DaoFactory(sl));
            sl.LoadDelegatedService(() => sfp.GetFactory(null));
            LoadNaturalnessDaos <Reptile>(sl);
            LoadNaturalnessDaos <Human>(sl);
            LoadNaturalnessModels <Reptile>(sl);
            LoadNaturalnessModels <Human>(sl);
            return(sl);
        }
Пример #7
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();
                        }
                    }
                }
            }
        }
Пример #8
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);
        }