コード例 #1
0
        public NHibernateInMemoryRepository(IFrameworkContext fakeFrameworkContext, ISessionFactory sessionFactory = null, ISession sessionForTest = null)
        {
            using (DisposableTimer.TraceDuration <NHibernateInMemoryRepository>("Start setup", "End setup"))
            {
                if (sessionFactory == null && sessionForTest == null)
                {
                    var builder = new NHibernateConfigBuilder("data source=:memory:", "unit-tester",
                                                              SupportedNHDrivers.SqlLite, "thread_static", false);
                    var config = builder.BuildConfiguration();
                    _sessionFactory = config.BuildSessionFactory();
                    SessionForTest  = _sessionFactory.OpenSession();

                    // See http://stackoverflow.com/questions/4325800/testing-nhibernate-with-sqlite-no-such-table-schema-is-generated
                    // and also http://nhforge.org/doc/nh/en/index.html#architecture-current-session
                    // regarding contextual sessions and GetCurrentSession()

                    // We pass in our own TextWriter because a bug in VS's testing framework means directly passing in Console.Out causes an ObjectDisposedException
                    new SchemaExport(config).Execute(false, true, false, SessionForTest.Connection, _schemaWriter);
                }
                else
                {
                    _sessionFactory = sessionFactory;
                    SessionForTest  = sessionForTest;
                }

                _dataContextFactory = new DataContextFactory(fakeFrameworkContext, SessionForTest, true);

                // Create reader
                ReadOnlyUnitOfWorkFactory = new ReadOnlyUnitOfWorkFactory();
                _hiveReadProvider         = new HiveReadProvider(new HiveProviderSetup(fakeFrameworkContext, "r-unit-tester", new FakeHiveProviderBootstrapper(), ReadOnlyUnitOfWorkFactory, null, _dataContextFactory));

                // Create writer
                ReadWriteUnitOfWorkFactory = new ReadWriteUnitOfWorkFactory();
                _writeProvider             = new HiveReadWriteProvider(new HiveProviderSetup(fakeFrameworkContext, "rw-unit-tester", new FakeHiveProviderBootstrapper(), ReadOnlyUnitOfWorkFactory, ReadWriteUnitOfWorkFactory, _dataContextFactory));

                //setup nhibernate mappers
                var manualMapper = new ManualMapper(new NhLookupHelper(_dataContextFactory), _writeProvider);
                fakeFrameworkContext.TypeMappers.Add(new Lazy <AbstractTypeMapper, TypeMapperMetadata>(() => manualMapper, new TypeMapperMetadata(true)));


                // Create hive wrappers for the readers and writers
                var governorRUowFactory = new ReadOnlyUnitOfWorkFactoryWrapper(new[] { _hiveReadProvider });

                var governorRWUowFactory = new ReadWriteUnitOfWorkFactoryWrapper(new[] { _writeProvider });

                _hiveReadProviderViaGovernor      = new Framework.Hive.PersistenceGovernor.HiveReadProvider(new HiveProviderSetup(fakeFrameworkContext, "r-unit-wrapper", new FakeHiveProviderBootstrapper(), governorRUowFactory, null, null), new[] { _hiveReadProvider });
                _hiveReadWriteProviderViaGovernor = new Framework.Hive.PersistenceGovernor.HiveReadWriteProvider(new HiveProviderSetup(fakeFrameworkContext, "rw-unit-wrapper", new FakeHiveProviderBootstrapper(), governorRUowFactory, governorRWUowFactory, null), new[] { _writeProvider });
            }
        }
コード例 #2
0
        public NHibernateInMemoryRepository(IFrameworkContext fakeFrameworkContext, ISessionFactory sessionFactory = null, ISession sessionForTest = null)
        {
            using (DisposableTimer.TraceDuration<NHibernateInMemoryRepository>("Start setup", "End setup"))
            {
                if (sessionFactory == null && sessionForTest == null)
                {
                    var builder = new NHibernateConfigBuilder("data source=:memory:", "unit-tester",
                                                              SupportedNHDrivers.SqlLite, "thread_static", false);
                    var config = builder.BuildConfiguration();
                    _sessionFactory = config.BuildSessionFactory();
                    SessionForTest = _sessionFactory.OpenSession();

                    // See http://stackoverflow.com/questions/4325800/testing-nhibernate-with-sqlite-no-such-table-schema-is-generated
                    // and also http://nhforge.org/doc/nh/en/index.html#architecture-current-session
                    // regarding contextual sessions and GetCurrentSession()

                    // We pass in our own TextWriter because a bug in VS's testing framework means directly passing in Console.Out causes an ObjectDisposedException
                    new SchemaExport(config).Execute(false, true, false, SessionForTest.Connection, _schemaWriter);
                }
                else
                {
                    _sessionFactory = sessionFactory;
                    SessionForTest = sessionForTest;
                }

                _dataContextFactory = new DataContextFactory(fakeFrameworkContext, SessionForTest, true);

                // Create reader
                ReadOnlyUnitOfWorkFactory = new ReadOnlyUnitOfWorkFactory();
                _hiveReadProvider = new HiveReadProvider(new HiveProviderSetup(fakeFrameworkContext, "r-unit-tester", new FakeHiveProviderBootstrapper(), ReadOnlyUnitOfWorkFactory, null, _dataContextFactory));

                // Create writer
                ReadWriteUnitOfWorkFactory = new ReadWriteUnitOfWorkFactory();
                _writeProvider = new HiveReadWriteProvider(new HiveProviderSetup(fakeFrameworkContext, "rw-unit-tester", new FakeHiveProviderBootstrapper(), ReadOnlyUnitOfWorkFactory, ReadWriteUnitOfWorkFactory, _dataContextFactory));

                //setup nhibernate mappers
                var manualMapper = new ManualMapper(new NhLookupHelper(_dataContextFactory), _writeProvider);
                fakeFrameworkContext.TypeMappers.Add(new Lazy<AbstractTypeMapper, TypeMapperMetadata>(() => manualMapper, new TypeMapperMetadata(true)));


                // Create hive wrappers for the readers and writers
                var governorRUowFactory = new ReadOnlyUnitOfWorkFactoryWrapper(new[] { _hiveReadProvider });

                var governorRWUowFactory = new ReadWriteUnitOfWorkFactoryWrapper(new[] { _writeProvider });

                _hiveReadProviderViaGovernor = new Framework.Hive.PersistenceGovernor.HiveReadProvider(new HiveProviderSetup(fakeFrameworkContext, "r-unit-wrapper", new FakeHiveProviderBootstrapper(), governorRUowFactory, null, null), new[] { _hiveReadProvider });
                _hiveReadWriteProviderViaGovernor = new Framework.Hive.PersistenceGovernor.HiveReadWriteProvider(new HiveProviderSetup(fakeFrameworkContext, "rw-unit-wrapper", new FakeHiveProviderBootstrapper(), governorRUowFactory, governorRWUowFactory, null), new[] { _writeProvider });
            }
        }