我正在研究DDD方法的模块化单片应用程序。我已经分离了购物篮和订单模块。我已经决定,我想使用事件在模块之间进行通信,保持它们之间的松散耦合,并尽可能地独立。我正在使用中介发布事件。
在basket模块中,我有CheckoutCommand,它最终发布事件BasketCheckedOutEvent。
public async Task<Unit> Handle(CheckoutBasketCommand command, CancellationToken cancellationToken)
{
var customerId = _currentUserService.UserId;
var basket = await _basketRepository.GetBasketByCustomerId(customerId)
?? throw new NotFoundException("Basket not found.");
await _unitOfWork.CommitAndDispatchDomainEventsAsync(basket);
await _eventDispatcher.PublishAsync(new BasketCheckedOutEvent(basket,
command.CouponCode,
command.ShippingMethod,
command.PaymentMethod));
}
事件
public record BasketCheckedOutEvent(Basket Basket,
string CouponCode,
int ShippingMethod,
int PaymentMethod) : IEvent
{
}
我希望其他模块处理此事件,但存在问题。另一个模块无法访问Basket对象(因为它是不同模块中的域对象-合理)。所以我不能创建一个篮子通过事件的订单。
我正在努力寻找解决方案。我唯一想到的是将BasketCheckedOutEvent放在Shared.Application模块中,然后创建一个BasketDto-然后Dto和Event将在Order模块中可用,我可以进一步处理它。在我看来,这看起来不是一个好的做法。它为生活中真正简单的东西创造了另一层抽象。经典的巨石。
只传球篮下。Id是可以的(以最小化事件大小),但我必须以某种方式从另一个模块的存储库中读取我无法访问的篮子。
谢谢