Where to raise persistence-dependent domain events - service, repository, or UI?

前端 未结 6 1910
暖寄归人
暖寄归人 2021-01-31 09:03

My ASP.NET MVC3 / NHibernate application has a requirement to fire off and handle a variety of events related to my domain objects. For example, an Order object mig

6条回答
  •  你的背包
    2021-01-31 09:24

    Domain Events should be raised in... the Domain. That's why they are Domain Events.

    public void ExecuteCommand(MakeCustomerGoldCommand command)
    {
        if (ValidateCommand(command) == ValidationResult.OK)
        {
            Customer item = CustomerRepository.GetById(command.CustomerId);
            item.Status = CustomerStatus.Gold;
            CustomerRepository.Update(item);
        }
    }
    

    (and then in the Customer class, the following):

    public CustomerStatus Status
    {
        ....
        set
        {
            if (_status != value)
            {
                _status = value;
                switch(_status)
                {
                    case CustomerStatus.Gold:
                        DomainEvents.Raise(CustomerIsMadeGold(this));
                        break;
                    ...
                }
            }
        }
    

    The Raise method will store the event in the Event Store. It may also execute locally-registered event handlers.

提交回复
热议问题