我在社交网络网站的设计中使用 DDD(包括域事件)和 CQRS(没有事件溯源)。
我有聚合根,如User
, FriendRequest
, Friendship
。我也有像UserAddressChanged
,之类的领域事件FriendRequestAccepted
。其中一些事件需要通知相关用户。所以我正在考虑Notification
上课,比如:
public enum NotificationReason
{
IncomingFriendRequest = 1,
OutgoingFriendRequestAccepted = 2,
// and many more ...
}
public class Notification
{
public long Id { get; set; }
public int UserId { get; set; }
public string UserName { get; set; }
public string AvatarUrl { get; set; }
public DateTime Timestamp { get; set; }
public NotificationReason Reason { get; set; }
public bool Read { get; set; } //if user has read this notification.
}
但是我应该将Notification
类建模为聚合根吗?如果是,则当User
聚合更改地址并引发UserAddressChanged
域事件时,在相应的事件处理程序中,Notification
将创建一个新聚合并随后通过NotificationRepository
. 但是在事件处理程序中创建新的聚合对我来说听起来很可疑。
同时,我也觉得对于一个简单的类来说太重了Notification
。我无法确定通知是域问题还是基础设施问题。