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));
        }
Exemple #2
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);
        }
        /// <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);
        }
 public Task Handle(RemoveCustomerCommand message, CancellationToken cancellationToken)
 {
     return(Task.Run(() =>
     {
         Handle(message);
     }, cancellationToken));
 }
Exemple #5
0
        public void Handle(RemoveCustomerCommand cmd)
        {
            var customer = _db.Customers.FirstOrDefault(c => c.Id == cmd.Id);

            if (customer != null)
            {
                _db.Customers.Remove(customer);
            }
        }
Exemple #6
0
 /// <summary>
 /// 按id移除
 /// </summary>
 /// <param name="id"></param>
 public void Remove(int id)
 {
     try
     {
         var removeCommand = new RemoveCustomerCommand(id);
         Bus.SendCommand(removeCommand);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         throw;
     }
 }
 public void Handle(RemoveCustomerCommand message)
 {
     if (!message.IsValid())
     {
         NotifyValidationErrors(message);
         return;
     }
     _customerRepository.Remove(message.Id);
     if (Commit())
     {
         _bus.RaiseEvent(new CustomerRemovedEvent(message.Id));
     }
 }
Exemple #8
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));
        }
        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);
        }
Exemple #10
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));
        }
Exemple #11
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));
        }
Exemple #12
0
        public async Task <ValidationResult> Remove(Guid id)
        {
            var removeCommand = new RemoveCustomerCommand(id);

            return(await _mediator.SendCommand(removeCommand));
        }
        public void Remove(string id)
        {
            var removeCommand = new RemoveCustomerCommand(id);

            Bus.SendCommand(removeCommand);
        }
 Task <Unit> IRequestHandler <RemoveCustomerCommand, Unit> .Handle(RemoveCustomerCommand request, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
 public async Task <IActionResult> Remove([FromBody] RemoveCustomerCommand removeCustomerCommand)
 => base.Ok(await mediator.Send <bool>(removeCustomerCommand));
Exemple #16
0
        static void Main(string[] args)
        {
            string keypress;

            do
            {
                // 0)
                // Display welcome text
                Console.Clear();

                Console.WriteLine("CQRS Pattern -- Customer List Context");
                Console.WriteLine();

                // 1)
                Console.WriteLine("Select Action:");

                Console.WriteLine();
                Console.WriteLine("1) Add Customer");
                Console.WriteLine("2) Remove Customer");
                Console.WriteLine("3) View all Customers");
                Console.WriteLine("4) Find Customer");

                var selection = Console.ReadKey().KeyChar.ToString();
                int.TryParse(selection, out int actionSelection);
                Console.Clear();

                // 2)
                // Should be broken into individual methods / classes,
                // utilizing other patterns such as the strategy pattern
                // for handling the user's selection
                switch (actionSelection)
                {
                case 1:     // Add Customer
                {
                    Console.Write("Customer ID (number): ");
                    var id = Console.ReadKey().KeyChar.ToString();
                    int.TryParse(id, out int customerId);

                    Console.WriteLine();
                    Console.Write("Customer Name: ");
                    var name = Console.ReadLine();

                    var cmd = new AddCustomerCommand(customerId, name);
                    _commandHandler.Handle(cmd);

                    Console.WriteLine();
                    Console.WriteLine("Customer added");
                }
                break;

                case 2:     // Remove Customer
                {
                    Console.Write("Customer ID (number): ");
                    var id = Console.ReadKey().KeyChar.ToString();
                    int.TryParse(id, out int customerId);

                    var cmd = new RemoveCustomerCommand(customerId);
                    _commandHandler.Handle(cmd);

                    Console.WriteLine();
                    Console.WriteLine("Customer removed");
                }
                break;

                case 3:     // View all Customers
                    Console.WriteLine(_queryHandler.Handle(new GetAllCustomersQuery()));
                    break;

                case 4:     // Find Customer
                {
                    Console.Write("Customer ID (number): ");
                    var id = Console.ReadKey().KeyChar.ToString();
                    int.TryParse(id, out int customerId);

                    var qry = new FindCustomerQuery(customerId);

                    Console.WriteLine();
                    Console.WriteLine(_queryHandler.Handle(qry));
                }
                break;

                default:    // Invalid choice - skip and display start menu
                    break;
                }

                // 3)
                Console.WriteLine();
                Console.WriteLine("Menu ( M )");
                Console.WriteLine("Exit ( X )");

                keypress = Console.ReadKey().KeyChar.ToString();
            } while (keypress.ToLower() != "x");
        }
 public void Handle(RemoveCustomerCommand command)
 {
     throw new NotImplementedException("ToDo");
 }
Exemple #18
0
        public void Remove(Guid id)
        {
            var removeCommand = new RemoveCustomerCommand(id);

            _bus.SendCommand(removeCommand);
        }
Exemple #19
0
 public void Handle(RemoveCustomerCommand command)
 {
     customerRepository.Remove(command.Id);
 }