Example #1
0
        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;
                }
            }
        }
Example #2
0
        public async Task <IEnumerable <ShipDto> > GetShipsByNameAsync(string shipName)
        {
            var getShipByNameQuery = new GetShipsByNameQuery {
                ShipName = shipName
            };

            return(await _queryExecutor.ExecuteAsync <GetShipsByNameQuery, ShipDto>(getShipByNameQuery));
        }
        public async Task QueryShipsByName()
        {
            var ioCContainer = new WindsorContainer();

            _RegisterComponents(ioCContainer);

            var unitOfWork     = ioCContainer.Resolve <NhibernateUnitOfWork>();
            var shipRepository = new NhibernateRepository <Ship>(unitOfWork);

            try
            {
                unitOfWork.BeginTransaction();

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

                    unitOfWork.Flush();

                    var queryExecutor      = ioCContainer.Resolve <IQueryExecutor>();
                    var getShipByNameQuery = new GetShipsByNameQuery {
                        ShipName = "lady"
                    };
                    var shipDtos = await queryExecutor.ExecuteAsync <GetShipsByNameQuery, ShipDto>(getShipByNameQuery);

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

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

                    throw;
                }
            }
            finally
            {
                ioCContainer.Release(unitOfWork);
            }

            ioCContainer.Dispose();
        }
Example #4
0
        public async Task <IEnumerable <ShipDto> > GetShipsByMultipleNames(
            string shipNameOne,
            string shipNameTwo
            )
        {
            var getShipByNameOneQuery = new GetShipsByNameQuery {
                ShipName = shipNameOne
            };
            var shipsByNameOne = _queryExecutor.Execute <GetShipsByNameQuery, ShipDto>(getShipByNameOneQuery);

            // At this point, getShipByNameOneQuery have not been sent to the DB.
            // The query would be sent to the DB only when the result (shipsByNameOne) is enumerated.

            var getShipByNameTwoQuery = new GetShipsByNameQuery {
                ShipName = shipNameTwo
            };
            var shipsByNameTwo = await _queryExecutor.ExecuteAsync <GetShipsByNameQuery, ShipDto>(getShipByNameTwoQuery);

            // getShipByNameTwoQuery async execution was awaited, which sent both queries to the DB in the single round trip.

            return(shipsByNameOne.Union(shipsByNameTwo));
        }