Example #1
0
        public void Commit()
        {
            var batch = new StructuredDataBatchRequest
            {
                ReturnBox = _inbox.Name,
                Request   = Commands
            };

            _server.Enqueue(batch);
            _server.Send();

            var reply = _inbox.WaitForMessages <PutResult>(TimeSpan.FromMinutes(2.0));

            if (!reply.Any())
            {
                throw new TimeoutException();
            }

            _commands.Clear();

            if (reply.First().Code == PutResultCode.Ok)
            {
                return;
            }

            var innerExceptions = new List <Exception>();

            foreach (var result in reply)
            {
                var info = string.Format("For {0}: {1}", result.Key, result.Message);
                switch (result.Code)
                {
                case PutResultCode.Concurrency:
                    innerExceptions.Add(new DBConcurrencyException(info));
                    break;

                case PutResultCode.StorageCapacity:
                    innerExceptions.Add(new EndOfStreamException(info));
                    break;

                default:
                    innerExceptions.Add(new ApplicationException(info));
                    break;
                }
            }
            var badResult = new AggregateException(innerExceptions);

            throw badResult;
        }
        public async Task Handle(OrderStartedDomainEvent orderStartedEvent, CancellationToken cancellationToken)
        {
            var cardTypeId = (orderStartedEvent.CardTypeId != 0) ? orderStartedEvent.CardTypeId : 1;
            var buyer      = await _buyerRepository.FindAsync(orderStartedEvent.UserId);

            bool buyerOriginallyExisted = (buyer == null) ? false : true;

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(orderStartedEvent.UserId, orderStartedEvent.UserName);
            }

            buyer.VerifyOrAddPaymentMethod(cardTypeId,
                                           $"Payment Method on {DateTime.UtcNow}",
                                           orderStartedEvent.CardNumber,
                                           orderStartedEvent.CardSecurityNumber,
                                           orderStartedEvent.CardHolderName,
                                           orderStartedEvent.CardExpiration,
                                           orderStartedEvent.Order.Id);

            var buyerUpdated = buyerOriginallyExisted ?
                               _buyerRepository.Update(buyer) :
                               _buyerRepository.Add(buyer);

            await _buyerRepository.UnitOfWork
            .SaveEntitiesAsync(cancellationToken);

            var orderStatusChangedTosubmittedIntegrationEvent = new OrderStatusChangedToSubmittedIntegrationEvent(orderStartedEvent.Order.Id, orderStartedEvent.Order.OrderStatus.Name, buyer.Name);
            await _messageOutbox.Send(orderStatusChangedTosubmittedIntegrationEvent);
        }
        public async Task Handle(OrderShippedDomainEvent orderShippedDomainEvent, CancellationToken cancellationToken)
        {
            var order = await _orderRepository.GetAsync(orderShippedDomainEvent.Order.Id);

            var buyer = await _buyerRepository.FindByIdAsync(order.GetBuyerId.Value.ToString());

            var orderStatusChangedToShippedIntegrationEvent = new OrderStatusChangedToShippedIntegrationEvent(order.Id, order.OrderStatus.Name, buyer.Name);
            await _messageOutbox.Send(orderStatusChangedToShippedIntegrationEvent);
        }
        public async Task Handle(OrderStatusChangedToAwaitingValidationDomainEvent orderStatusChangedToAwaitingValidationDomainEvent, CancellationToken cancellationToken)
        {
            var order = await _orderRepository.GetAsync(orderStatusChangedToAwaitingValidationDomainEvent.OrderId);

            var buyer = await _buyerRepository.FindByIdAsync(order.GetBuyerId.Value.ToString());

            var orderStockList = orderStatusChangedToAwaitingValidationDomainEvent.OrderItems
                                 .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits()));
            var orderStatusChangedToAwaitingValidationIntegrationEvent = new OrderStatusChangedToAwaitingValidationIntegrationEvent(
                order.Id, order.OrderStatus.Name, buyer.Name, orderStockList);
            await _messageOutbox.Send(orderStatusChangedToAwaitingValidationIntegrationEvent);
        }
        public async Task <TestResponseDto> Handle(TestCommand request, CancellationToken cancellationToken)
        {
            //Dispatcher içerisnde save işleminden hemen önce yapılacak IDomainEventsDispatcher ile birlikte kullanıcak dışarıda çağrılmayacak.
            //Unit of work ile beraber kullanılacak.
            //await _domainEventSender.PublishDomainEvent(new TestDomainNotification(new TestDomainEvent(request.Id)), cancellationToken);

            //Gerçek eent publish edilecek ve consumer yakalıyor mu bakılacqk.
            if (_outbox.Enabled)
            {
                await _outbox.Send(new TestIntegrationEvent(request.TestId));
            }

            //await _busPublisher.PublishAsync(new TestIntegrationEvent(request.TestId));



            return(await Task.FromResult(new TestResponseDto { Id = Guid.NewGuid() }));
        }
        public async Task <bool> Handle(CreateOrderCommand message, CancellationToken cancellationToken)
        {
            // Add Integration event to clean the basket Add integration event add mongo db integration events
            var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(message.UserId);
            await _messageOutbox.Send(orderStartedIntegrationEvent);


            // Add/Update the Buyer AggregateRoot
            // DDD patterns comment: Add child entities and value-objects through the Order Aggregate-Root
            // methods and constructor so validations, invariants and business logic
            // make sure that consistency is preserved across the whole aggregate
            var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode);
            var order   = new Order(message.UserId, message.UserName, address, message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration);

            foreach (var item in message.OrderItems)
            {
                order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
            }

            _orderRepository.Add(order);

            return(await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken));
        }