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); }
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; } } }
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(); }
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); }
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; } } }
public UserStore() { var _unitOfWork = new NhibernateUnitOfWork(); var _repository = new NHibernateRepository <User>(_unitOfWork); _manager = new Domain.Manager.UserManager(_repository, _unitOfWork); }
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; } } }
/// <summary> /// Loads an aggregate root domain entity proxy using NhibernateRepository. /// </summary> /// <typeparam name="TAggregateRoot">An aggregate root domain entity type</typeparam> /// <typeparam name="TId">Entity Id type</typeparam> /// <param name="unitOfWork">NHibernate unit of work</param> /// <param name="id">An aggregate root domain entity id</param> /// <returns>An aggregate root domain entity proxy</returns> public static TAggregateRoot Load <TAggregateRoot, TId>(this NhibernateUnitOfWork unitOfWork, TId id) where TAggregateRoot : Entity <TId>, IAggregateRoot { var repository = new NhibernateRepository <TAggregateRoot, TId>(unitOfWork); return(repository.Load(id)); }
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(); }
/// <summary> /// Fetches an aggregate root domain entity from the database using NhibernateRepository. /// </summary> /// <typeparam name="TAggregateRoot">An aggregate root domain entity type</typeparam> /// <param name="unitOfWork">NHibernate unit of work</param> /// <param name="id">An aggregate root domain entity id</param> /// <returns>An aggregate root domain entity, or null when not found</returns> public static TAggregateRoot Get <TAggregateRoot>(this NhibernateUnitOfWork unitOfWork, int id) where TAggregateRoot : Entity, IAggregateRoot { var repository = new NhibernateRepository <TAggregateRoot>(unitOfWork); return(repository.Get(id)); }
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); }
/// <summary> /// Saves an aggregate root domain entity using NhibernateRepository. /// </summary> /// <typeparam name="TAggregateRoot">An aggregate root domain entity type</typeparam> /// <typeparam name="TId">Entity Id type</typeparam> /// <param name="unitOfWork">NHibernate unit of work</param> /// <param name="aggregateRoot">An instance of aggregate root domain entity</param> public static void Save <TAggregateRoot, TId>(this NhibernateUnitOfWork unitOfWork, TAggregateRoot aggregateRoot) where TAggregateRoot : Entity <TId>, IAggregateRoot { var repository = new NhibernateRepository <TAggregateRoot, TId>(unitOfWork); repository.Save(aggregateRoot); unitOfWork.Flush(); }
/// <summary> /// Deletes an aggregate root domain entity using NhibernateRepository. /// </summary> /// <typeparam name="TAggregateRoot">An aggregate root domain entity type</typeparam> /// <param name="unitOfWork">NHibernate unit of work</param> /// <param name="aggregateRoot">An instance of aggregate root domain entity</param> public static void Delete <TAggregateRoot>(this NhibernateUnitOfWork unitOfWork, TAggregateRoot aggregateRoot) where TAggregateRoot : Entity, IAggregateRoot { var repository = new NhibernateRepository <TAggregateRoot>(unitOfWork); repository.Delete(aggregateRoot); unitOfWork.Flush(); }
public void Execute(IJobExecutionContext context) { var unitOfWork = new NhibernateUnitOfWork(); var userManager = new UserManager(new NHibernateRepository <User>(unitOfWork), unitOfWork); var questionManager = new QuestionManager(new NHibernateRepository <Question>(unitOfWork), unitOfWork); var coursemanager = new CourseManager(new NHibernateRepository <Course>(unitOfWork), unitOfWork); var subscriptionManager = new SubscribeManager(new NHibernateRepository <Subscription>(unitOfWork), unitOfWork); var sendQuestionManager = new SendQuestionManager(new NHibernateRepository <UserQuestion>(unitOfWork), unitOfWork); sendQuestionManager.SendQuestions(userManager, questionManager, coursemanager, subscriptionManager); }
static void Run(Action <NhibernateUnitOfWork, Mock <ISession>, Mock <ITransaction> > action, bool isFlush = true) { var transaction = Pleasure.Mock <ITransaction>(); var session = Pleasure.Mock <ISession>(mock => mock.Setup(r => r.BeginTransaction(IsolationLevel.RepeatableRead)).Returns(transaction.Object)); var nhibernateUnit = new NhibernateUnitOfWork(session.Object, IsolationLevel.RepeatableRead, isFlush); session.Verify(r => r.BeginTransaction(IsolationLevel.RepeatableRead), Times.Once()); action(nhibernateUnit, session, transaction); }
public HomeController() { _manager = new HomeManager(); var unitOfWork = new NhibernateUnitOfWork(); _courseManager = new CourseManager(new NHibernateRepository <Course>(unitOfWork), unitOfWork); _subscribeManager = new SubscribeManager(new NHibernateRepository <Subscription>(unitOfWork), unitOfWork); _answerManager = new AnswerManager(new NHibernateRepository <Answer>(unitOfWork), unitOfWork); _questionManager = new QuestionManager(new NHibernateRepository <Question>(unitOfWork), unitOfWork); _userManager = new UserManager(new NHibernateRepository <User>(unitOfWork), unitOfWork); }
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); }
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 async Task Context() { _unitOfWork = IoC.Resolve <NhibernateUnitOfWork>(); _unitOfWork.BeginTransaction(); _testEntityRepository = new NhibernateRepository <TestEntity>(_unitOfWork); _testEntity = new TestEntity(); _testEntityRepository.Save(_testEntity); await _unitOfWork.RollbackAsync(); }
public void Context() { _unitOfWork = IoC.Resolve <NhibernateUnitOfWork>(); _unitOfWork.BeginTransaction(IsolationLevel.Serializable); _testEntityRepository = new NhibernateRepository <TestEntity>(_unitOfWork); _testEntity = new TestEntity(); _testEntityRepository.Save(_testEntity); _unitOfWork.Commit(); }
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()); }
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(); }
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); }
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); } }
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(); }
protected Context_nhibernate_unit_of_work() { session = Pleasure.Mock <ISession>(); var sessionFactoryImplementor = Pleasure.MockAsObject <ISessionFactoryImplementor>(mock => mock .SetupGet(r => r.CurrentSessionContext) .Returns(Pleasure.MockAsObject <CurrentSessionContext>())); session .Setup(r => r.SessionFactory) .Returns(sessionFactoryImplementor); const IsolationLevel isolationLevel = IsolationLevel.ReadCommitted; transaction = Pleasure.Mock <ITransaction>(); session .Setup(r => r.BeginTransaction(isolationLevel)) .Returns(transaction.Object); nhibernateUnit = new NhibernateUnitOfWork(session.Object, isolationLevel); }
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(); }
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); }