コード例 #1
0
        public async Task CreateNewShip(CoreDddSampleNhibernateConfigurator nhibernateConfigurator)
        {
            using (var unitOfWork = new NhibernateUnitOfWork(nhibernateConfigurator))
            {
                unitOfWork.BeginTransaction();

                var shipRepository = new NhibernateRepository <Ship>(unitOfWork);

                try
                {
                    var createNewShipCommand = new CreateNewShipCommand {
                        ShipName = "lady", Tonnage = 10m
                    };
                    var createNewShipCommandHandler = new CreateNewShipCommandHandler(shipRepository);

                    var generatedShipId = 0;
                    createNewShipCommandHandler.CommandExecuted += args => generatedShipId = (int)args.Args;

                    await createNewShipCommandHandler.ExecuteAsync(createNewShipCommand);

                    Console.WriteLine($"Create new ship command was executed. Generated ship id: {generatedShipId}");

                    await unitOfWork.CommitAsync();
                }
                catch
                {
                    await unitOfWork.RollbackAsync();

                    throw;
                }
            }
        }
コード例 #2
0
        public async Task Context()
        {
            var domainEventHandlerFactory = new FakeDomainEventHandlerFactory(domainEvent => _raisedDomainEvent = domainEvent as IDomainEvent);

            DomainEvents.Initialize(domainEventHandlerFactory);

            _unitOfWork = new NhibernateUnitOfWork(new CoreDddSharedNhibernateConfigurator());
            _unitOfWork.BeginTransaction();

            var createNewShipCommand = new CreateNewShipCommand
            {
                ShipName  = "ship name",
                Tonnage   = 23.45678m,
                ImoNumber = "IMO 12345"
            };
            var createNewShipCommandHandler = new CreateNewShipCommandHandler(new NhibernateRepository <Ship>(_unitOfWork));

            createNewShipCommandHandler.CommandExecuted += args => _createdShipId = (int)args.Args;
            await createNewShipCommandHandler.ExecuteAsync(createNewShipCommand);

            _unitOfWork.Flush();
            _unitOfWork.Clear();

            _persistedShip = _unitOfWork.Get <Ship>(_createdShipId);
        }
コード例 #3
0
        public void Context()
        {
            _unitOfWork = new NhibernateUnitOfWork(new CoreDddSharedNhibernateConfigurator());
            _unitOfWork.BeginTransaction();

            var createNewShipCommand = new CreateNewShipCommand
            {
                ShipName  = "ship name",
                Tonnage   = 23.45678m,
                ImoNumber = "IMO 765432"
            };

            var internationalMaritimeOrganizationVerifier = A.Fake <IInternationalMaritimeOrganizationVerifier>();

            A.CallTo(() => internationalMaritimeOrganizationVerifier.IsImoNumberValid("IMO 765432")).Returns(true);

            var createNewShipCommandHandler = new CreateNewShipCommandHandler(
                new NhibernateRepository <Ship>(_unitOfWork),
                internationalMaritimeOrganizationVerifier
                );

            createNewShipCommandHandler.CommandExecuted += args => _createdShipId = (int)args.Args;
            createNewShipCommandHandler.Execute(createNewShipCommand);

            _unitOfWork.Flush();
            _unitOfWork.Clear();

            _persistedShip = _unitOfWork.Get <Ship>(_createdShipId);
        }
コード例 #4
0
ファイル: QuerySample.cs プロジェクト: xhafan/coreddd-sample
        public async Task QueryShipsByName(CoreDddSampleNhibernateConfigurator nhibernateConfigurator)
        {
            using (var unitOfWork = new NhibernateUnitOfWork(nhibernateConfigurator))
            {
                unitOfWork.BeginTransaction();

                var shipRepository = new NhibernateRepository <Ship>(unitOfWork);

                try
                {
                    var ship = new Ship("lady starlight", tonnage: 10m);
                    await shipRepository.SaveAsync(ship);

                    unitOfWork.Flush();

                    var getShipByNameQuery = new GetShipsByNameQuery {
                        ShipName = "lady"
                    };
                    var getShipByNameQueryHandler = new GetShipsByNameQueryHandler(unitOfWork);

                    var shipDtos = await getShipByNameQueryHandler.ExecuteAsync <ShipDto>(getShipByNameQuery);

                    Console.WriteLine($"Ship by name query was executed. Number of ships queried: {shipDtos.Count()}");

                    await unitOfWork.CommitAsync();
                }
                catch
                {
                    await unitOfWork.RollbackAsync();

                    throw;
                }
            }
        }
コード例 #5
0
        public async Task Context()
        {
            _serviceProvider = new ServiceProviderHelper().BuildServiceProvider();
            DomainEvents.Initialize(_serviceProvider.GetService <IDomainEventHandlerFactory>());

            _serviceScope = _serviceProvider.CreateScope();

            _unitOfWork = _serviceProvider.GetService <NhibernateUnitOfWork>();
            _unitOfWork.BeginTransaction();

            _shipCountBefore = _GetShipCount();

            var manageShipsController = new ManageShipsControllerBuilder(_serviceProvider).Build();

            var createNewShipCommand = new CreateNewShipCommand
            {
                ShipName  = "ship name",
                Tonnage   = 23.4m,
                ImoNumber = "IMO 12345"
            };

            _actionResult = await manageShipsController.CreateNewShip(createNewShipCommand);

            _unitOfWork.Flush();
            _unitOfWork.Clear();
        }
コード例 #6
0
        public void Context()
        {
            _unitOfWork = new NhibernateUnitOfWork(new CoreDddSharedNhibernateConfigurator());
            _unitOfWork.BeginTransaction();

            var ship = new ShipBuilder().Build();

            _unitOfWork.Save(ship);

            var updateShipCommand = new UpdateShipCommand
            {
                ShipId   = ship.Id,
                ShipName = "updated ship name",
                Tonnage  = 34.5m
            };
            var updateShipCommandHandler = new UpdateShipCommandHandler(new NhibernateRepository <Ship>(_unitOfWork));

#if NET40
            updateShipCommandHandler.Execute(updateShipCommand);
#endif
#if NETCOREAPP
            updateShipCommandHandler.ExecuteAsync(updateShipCommand).Wait();
#endif

            _unitOfWork.Flush();
            _unitOfWork.Clear();

            _updatedShip = _unitOfWork.Get <Ship>(ship.Id);
        }
コード例 #7
0
        public async Task PersistNewEntity(CoreDddSampleNhibernateConfigurator nhibernateConfigurator)
        {
            using (var unitOfWork = new NhibernateUnitOfWork(nhibernateConfigurator))
            {
                unitOfWork.BeginTransaction();

                var shipRepository = new NhibernateRepository <Ship>(unitOfWork);

                try
                {
                    var ship = new Ship("ship name", tonnage: 10m);
                    await shipRepository.SaveAsync(ship);

                    await unitOfWork.CommitAsync();

                    Console.WriteLine("Ship entity was persisted.");
                }
                catch
                {
                    await unitOfWork.RollbackAsync();

                    throw;
                }
            }
        }
コード例 #8
0
        public async Task Context()
        {
            _serviceProvider = new ServiceProviderHelper().BuildServiceProvider();
            _serviceScope    = _serviceProvider.CreateScope();

            _unitOfWork = _serviceProvider.GetService <NhibernateUnitOfWork>();
            _unitOfWork.BeginTransaction();

            _newShip = new ShipBuilder().Build();
            _unitOfWork.Save(_newShip);

            var manageShipsController = new ManageShipsControllerBuilder(_serviceProvider).Build();

            var updateShipCommand = new UpdateShipCommand
            {
                ShipId   = _newShip.Id,
                ShipName = "updated ship name",
                Tonnage  = 34.5m
            };

            _actionResult = await manageShipsController.UpdateShip(updateShipCommand);

            _unitOfWork.Flush();
            _unitOfWork.Clear();
        }
コード例 #9
0
        public void entities_are_persisted_and_rollback_is_handled_gracefully()
        {
            _unitOfWork.BeginTransaction();
            _testEntity = _testEntityRepository.Get(_testEntity.Id);

            _testEntity.ShouldNotBeNull();

            _unitOfWork.Rollback();
        }
コード例 #10
0
        public void entities_stays_persisted()
        {
            _unitOfWork.BeginTransaction();
            _testEntity = _testEntityRepository.Get(_testEntity.Id);

            _testEntity.ShouldNotBeNull();

            _unitOfWork.Rollback();
        }
コード例 #11
0
        public void Context()
        {
            _unitOfWork = new NhibernateUnitOfWork(new CoreDddSharedNhibernateConfigurator());
            _unitOfWork.BeginTransaction();

            _newShip = new ShipBuilder().Build();

            _unitOfWork.Save(_newShip); // save entity into DB -> send INSERT SQL statement into DB
            _unitOfWork.Clear();        // clear NHibernate session so the following SQL SELECT would not load the cached entity version from the session, but would query the database

            _persistedShip = _unitOfWork.Get <Ship>(_newShip.Id);
        }
コード例 #12
0
        public void Context()
        {
            _serviceProvider = new ServiceProviderHelper().BuildServiceProvider();
            _serviceScope    = _serviceProvider.CreateScope();

            _unitOfWork = _serviceProvider.GetService <NhibernateUnitOfWork>();
            _unitOfWork.BeginTransaction();

            var manageShipsController = new ManageShipsControllerBuilder(_serviceProvider).Build();

            _actionResult = manageShipsController.CreateNewShip();
        }
        public void Context()
        {
            _unitOfWork = IoC.Resolve <NhibernateUnitOfWork>();
            _unitOfWork.BeginTransaction(IsolationLevel.Serializable);

            _testEntityRepository = new NhibernateRepository <TestEntity>(_unitOfWork);

            _testEntity = new TestEntity();
            _testEntityRepository.Save(_testEntity);

            _unitOfWork.Commit();
        }
コード例 #14
0
        public async Task Context()
        {
            _unitOfWork = IoC.Resolve <NhibernateUnitOfWork>();
            _unitOfWork.BeginTransaction();

            _testEntityRepository = new NhibernateRepository <TestEntity>(_unitOfWork);

            _testEntity = new TestEntity();
            _testEntityRepository.Save(_testEntity);

            await _unitOfWork.RollbackAsync();
        }
コード例 #15
0
        public void Context()
        {
            _unitOfWork = new NhibernateUnitOfWork(new CoreDddSharedNhibernateConfigurator());
            _unitOfWork.BeginTransaction();

            _newShip = new ShipBuilder().Build();
            _unitOfWork.Save(_newShip);
            _unitOfWork.Clear();

            var queryHandler = new GetAllShipsQueryHandler(_unitOfWork);

            _shipDtos = queryHandler.Execute <ShipDto>(new GetAllShipsQuery());
        }
コード例 #16
0
        public void Context()
        {
            _unitOfWork = new NhibernateUnitOfWork(new CoreDddSharedNhibernateConfigurator());
            _unitOfWork.BeginTransaction();

            _newShip = new ShipBuilder().Build();

            _unitOfWork.Save(_newShip);

            _unitOfWork.Clear();

            _persistedShip        = _unitOfWork.Get <Ship>(_newShip.Id);
            _persistedShipHistory = _persistedShip.ShipHistories.SingleOrDefault();
        }
コード例 #17
0
        public void Context()
        {
            var specification = new TCommittingUnitOfWorkSpecification();

            _unitOfWork = IoC.Resolve <NhibernateUnitOfWork>();
            _unitOfWork.BeginTransaction();

            _testEntityRepository = new NhibernateRepository <TestEntity>(_unitOfWork);

            _testEntity = new TestEntity();
            _testEntityRepository.Save(_testEntity);

            specification.CommitAct(_unitOfWork);
        }
コード例 #18
0
        public async Task Context()
        {
            _serviceProvider = new ServiceProviderHelper().BuildServiceProvider();
            _serviceScope    = _serviceProvider.CreateScope();

            _unitOfWork = _serviceProvider.GetService <NhibernateUnitOfWork>();
            _unitOfWork.BeginTransaction();

            _newShip = new ShipBuilder().Build();
            _unitOfWork.Save(_newShip);

            var manageShipsController = new ManageShipsControllerBuilder(_serviceProvider).Build();

            _actionResult = await manageShipsController.Index();
        }
        public void Context()
        {
            _unitOfWork = IoC.Resolve <NhibernateUnitOfWork>();
            _unitOfWork.BeginTransaction();

            _testEntityRepository = new NhibernateRepository <TestEntity>(_unitOfWork);

            _testEntity = new TestEntity();
            _testEntityRepository.Save(_testEntity);

            _makeTransactionDisconnected();

            void _makeTransactionDisconnected()
            {
                _unitOfWork.Session.Transaction.Dispose();
            }
        }
        public void Context()
        {
            var specification = new TRollingBackUnitOfWorkInTransactionScopeSpecification();

            using (var transactionScope = new TransactionScope(TransactionScopeOption.Required,
                                                               new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            }))
            {
                _unitOfWork = IoC.Resolve <NhibernateUnitOfWork>();
                _unitOfWork.BeginTransaction();

                _testEntityRepository = new NhibernateRepository <TestEntity>(_unitOfWork);
                _testEntity           = new TestEntity();
                _testEntityRepository.Save(_testEntity);

                specification.RollbackAct(_unitOfWork);
            }
        }
コード例 #21
0
        public void Context()
        {
            using (var transactionScope = new TransactionScope(TransactionScopeOption.Required,
                                                               new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            }))
            {
                _unitOfWork = IoC.Resolve <NhibernateUnitOfWork>();
                _unitOfWork.BeginTransaction();

                _testEntityRepository = new NhibernateRepository <TestEntity>(_unitOfWork);
                _testEntity           = new TestEntity();
                _testEntityRepository.Save(_testEntity);

                _unitOfWork.Commit();

                transactionScope.Complete();
            }

            _unitOfWork.Rollback();
        }
        public void entities_are_not_persisted()
        {
            if (_shouldNotBeTestedDueToOldNhibernateVersionAndSqliteDriver())
            {
                return;
            }

            _unitOfWork.BeginTransaction();
            _testEntity = _testEntityRepository.Get(_testEntity.Id);

            _testEntity.ShouldBeNull();

            _unitOfWork.Rollback();

            bool _shouldNotBeTestedDueToOldNhibernateVersionAndSqliteDriver()
            {
                // Nhibernate 4.1.1 does not rollback properly within a transaction scope for sqllite (latest stable 1.0.108). This is working fine for Nhibernate 5.0.3
                // strangely, if there is a TestEntity sql select just before the TestEntity sql insert, it all works fine. Anyway, marking it as not working for
                // Nhibernate 4.1.1 so it's visible that there might be some issues with transaction scope, nhibernate 4.1.1 and sqlite combination.

                var configuration         = IoC.Resolve <INhibernateConfigurator>().GetConfiguration();
                var connectionDriverClass = configuration.Properties["connection.driver_class"];
                var isSqlite = connectionDriverClass.Contains("SQLite");

                if (isSqlite)
                {
#if NET40 //Nhibernate 4.1.1
                    return(true);
#endif
#if NET45 //Nhibernate 4.1.1
                    return(true);
#endif
#if NET461
                    return(false);
#endif
                }

                return(false);
            }
        }
コード例 #23
0
        public async Task Context()
        {
            _serviceProvider = new ServiceProviderHelper().BuildServiceProvider();
            DomainEvents.Initialize(_serviceProvider.GetService <IDomainEventHandlerFactory>());

            _unitOfWork = _serviceProvider.GetService <NhibernateUnitOfWork>();
            _unitOfWork.BeginTransaction();

            var createNewShipCommand = new CreateNewShipCommand
            {
                ShipName  = "ship name",
                Tonnage   = 23.45678m,
                ImoNumber = "IMO 12345"
            };

            _bus = A.Fake <IBus>();
            var createNewShipCommandMessageHandler = new CreateNewShipCommandMessageHandler(_serviceProvider.GetService <ICommandExecutor>(), _bus);

            await createNewShipCommandMessageHandler.Handle(createNewShipCommand);

            _unitOfWork.Flush();
            _unitOfWork.Clear();
        }
コード例 #24
0
        public async Task Context()
        {
            var specification = new TSearchingForFilmsSpecification();

            _serviceProvider = new ServiceProviderHelper().BuildServiceProvider();

            _serviceScope = _serviceProvider.CreateScope();

            _unitOfWork = _serviceProvider.GetService <NhibernateUnitOfWork>();
            _unitOfWork.BeginTransaction();

            _BuildAndSaveCountries();
            _BuildAndSaveCinamas();
            _BuildAndSaveFilms();
            _BuildAndSaveLocations();

            _unitOfWork.Clear();

            var controller = new FilmsWithGoodRatingNotificationControllerBuilder(_serviceProvider).Build();

            _searchFilmsArgs = specification.GetSearchFilmsArgs();

            _actionResult = await controller.SearchForFilms(_searchFilmsArgs);
        }
        public async Task Context()
        {
            _unitOfWork = new NhibernateUnitOfWork(new CoreDddSharedNhibernateConfigurator());
            _unitOfWork.BeginTransaction();

            var imoNumber = "IMO 7564321";

            _newShip = new ShipBuilder().WithImoNumber(imoNumber).Build();
            _unitOfWork.Save(_newShip);

            var internationalMaritimeOrganizationVerifier = A.Fake <IInternationalMaritimeOrganizationVerifier>();

            A.CallTo(() => internationalMaritimeOrganizationVerifier.IsImoNumberValid(imoNumber)).Returns(true);

            var shipCreatedDomainEventMessageHandler = new VerifyImoNumberShipCreatedDomainEventMessageHandler(
                new NhibernateRepository <Ship>(_unitOfWork),
                internationalMaritimeOrganizationVerifier
                );

            await shipCreatedDomainEventMessageHandler.Handle(new ShipCreatedDomainEventMessage { ShipId = _newShip.Id });

            _unitOfWork.Flush();
            _unitOfWork.Clear();
        }
コード例 #26
0
 public void BaseSetUp()
 {
     UnitOfWork = new NhibernateUnitOfWork(NhibernateConfigurator);
     UnitOfWork.BeginTransaction();
 }
コード例 #27
0
 public void TestFixtureSetUp()
 {
     UnitOfWork.BeginTransaction();
 }
コード例 #28
0
 public void TestFixtureSetUp()
 {
     UnitOfWork = IoC.Resolve <NhibernateUnitOfWork>();
     UnitOfWork.BeginTransaction();
 }