Persistence and Domain Events with persistence ignorant objects

后端 未结 2 1374
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-03 13:01

I\'ve been studying on domain driven design in conjunction with domain events. I really like the separations of concerns those events provide. I ran into an issue with the order

2条回答
  •  攒了一身酷
    2021-02-03 13:38

    Disclaimer: I don't know what I am talking about. ⚠️

    As an alternative to raising events in the ShoppingCartService you could raise events in the OrderRepository.

    As an alternative to queuing domain events in the ShoppingCartService you could queue them in the Order aggregate by inheriting from a base class that provides a AddDomainEvent method.

    public class Order : Aggregate
    {
        public Order(IEnumerable cart, Customer customer)
        {
            AddDomainEvent(new OrderCreated(cart, customer))
        }
    }
    
    public abstract class Aggregate
    {
        private List _domainEvents = new List();
    
        public IReadOnlyCollection DomainEvents => _domainEvents.AsReadOnly();
    
        public void AddDomainEvent(IDomainEvent domainEvent)
        {
            _domainEvents.Add(domainEvent);
        }
    }
    

提交回复
热议问题