コード例 #1
0
        public async Task <Unit> Handle(RemoveCustomerCommand request, CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                NotifyValidationErrors(request);//激活验证错误通知
                return(Unit.Value);
            }
            try
            {
                _customerRepository.DeleteById(request.Id);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                //发布异常通知
                await Bus.RaiseEvent(new DomainNotification(e.Source, e.Message));
            }


            if (Commit())
            {
                //提交成功,发布领域事件
                await Bus.RaiseEvent(new CustomerDeletedEvent(request.Id));
            }
            return(Unit.Value);
        }
コード例 #2
0
        public Task <bool> Handle(RemoveCustomerCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Task.FromResult(false));
            }

            Customer customer = _customerRepository.GetById(message.Id);

            if (customer == null)
            {
                // notificar o dominio
                Bus.RaiseEvent(new DomainNotification(message.MessageType, "Registro não encontrado"));

                return(Task.FromResult(false));
            }

            _customerRepository.Remove(message.Id);

            if (Commit())
            {
                Bus.RaiseEvent(new CustomerRemovedEvent(message.Id));
            }

            return(Task.FromResult(true));
        }
コード例 #3
0
        /// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public Task Handle(RemoveCustomerCommand request, CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                foreach (var error in request.ValidationResult.Errors)
                {
                    _eventDispatcher.RaiseEvent(new DomainNotification(request.MessageType, error.ErrorMessage));
                }
                return(Task.CompletedTask);
            }

            var existingCustomer = _customerRepository.FindOne(request.Id);

            if (existingCustomer == null)
            {
                _eventDispatcher.RaiseEvent(new DomainNotification(request.MessageType, @"The customer does not exit in system"));
                return(Task.CompletedTask);
            }

            //remove customer
            _customerRepository.Delete(existingCustomer);
            _unitOfWork.Commit();

            return(Task.CompletedTask);
        }
コード例 #4
0
 public void Handle(RemoveCustomerCommand message)
 {
     if (!message.IsValid())
     {
         NotifyValidationErrors(message);
         return;
     }
     _customerRepository.Remove(message.Id);
     if (Commit())
     {
         _bus.RaiseEvent(new CustomerRemovedEvent(message.Id));
     }
 }
コード例 #5
0
        public Task Handle(RemoveCustomerCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Task.CompletedTask);
            }

            _customerRepository.Remove(message.Id);

            if (Commit())
            {
                Bus.RaiseEvent(new CustomerRemovedEvent(message.Id));
            }

            return(Task.CompletedTask);
        }
コード例 #6
0
        public Task <bool> Handle(RemoveCustomerCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Task.FromResult(false));
            }

            _customerRepository.Remove(message.Id);

            if (Commit())
            {
                Bus.RaiseEvent(new CustomerRemovedEvent(message.Id));
            }

            return(Task.FromResult(true));
        }
コード例 #7
0
        public async Task <ValidationResult> Handle(RemoveCustomerCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                return(message.ValidationResult);
            }

            var customer = await _customerRepository.GetById(message.Id);

            if (customer is null)
            {
                AddError("The customer doesn't exists.");
                return(ValidationResult);
            }

            customer.AddDomainEvent(new CustomerRemovedEvent(message.Id));

            _customerRepository.Remove(customer);

            return(await Commit(_customerRepository.UnitOfWork));
        }
コード例 #8
0
        /// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task <bool> Handle(RemoveCustomerCommand request, CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                foreach (var error in request.ValidationResult.Errors)
                {
                    throw new CustomerDomainException(error.ErrorMessage);
                }
                return(false);
            }

            var existingCustomer = _customerRepository.FindOne(request.Id);

            if (existingCustomer == null)
            {
                throw new CustomerDomainException(@"The customer does not exit in system");
            }

            //remove customer
            _customerRepository.Delete(existingCustomer);
            return(await _unitOfWork.CommitAsync(cancellationToken));
        }