Beispiel #1
0
        /// <summary>
        /// 更新操作
        /// </summary>
        /// <param name="message"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task <Unit> Handle(UpdateCustomerCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Unit.Task);
            }

            var customer         = new Entitys.Customer(message.Id, message.Name, message.Email, message.BirthDate);
            var existingCustomer = _customerRepository.GetByEmail(customer.Email);

            ////如果按邮箱查出来的customer的id和传过来的不相等
            if (existingCustomer != null && existingCustomer.Id != customer.Id)
            {
                //和查出来的对象不相等
                if (!existingCustomer.Equals(customer))
                {
                    //发布通知,这个邮箱被占用了
                    Bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken."));
                    return(Unit.Task);
                }
            }
            //否则更新
            _customerRepository.Update(customer);


            if (Commit())
            {
                //提交成功,发布领域事件
                Bus.RaiseEvent(new CustomerUpdatedEvent(customer.Id, customer.Name, customer.Email, customer.BirthDate));
            }

            return(Unit.Task);
        }
Beispiel #2
0
        /// <summary>
        /// 新增操作
        /// </summary>
        /// <param name="message"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task <Unit> Handle(RegisterNewCustomerCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Unit.Task);
            }

            var customer = new Entitys.Customer(message.Name, message.Email, message.BirthDate);

            if (_customerRepository.GetByEmail(customer.Email) != null)
            {
                Bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken."));
                return(Unit.Task);
            }

            _customerRepository.Insert(customer);

            if (Commit())
            {
                //提交成功,发布领域事件
                Bus.RaiseEvent(new CustomerRegisteredEvent(customer.Id, customer.Name, customer.Email, customer.BirthDate));
            }

            return(Unit.Task);
        }