【发布时间】:2016-06-20 15:20:19
【问题描述】:
我目前正在尝试实现没有事件溯源的简单 DDD/CQRS 架构。
目前我需要编写一些代码来向文档实体添加通知(文档可以有多个通知)。
我已经创建了一个命令 NotificationAddCommand、ICommandService 和 IRepository。
在通过 IRepository 插入新通知之前,我必须使用 NotificationAddCommand.User_name 属性从 db 查询当前 user_id。
我不知道该怎么做,因为我可以
- 使用读取流中的
IQuery。 - 将 user_name 传递给域实体并在存储库中解析 user_id。
代码:
public class DocumentsCommandService : ICommandService<NotificationAddCommand>
{
private readonly IRepository<Notification, long> _notificationsRepository;
public DocumentsCommandService(
IRepository<Notification, long> notifsRepo)
{
_notificationsRepository = notifsRepo;
}
public void Handle(NotificationAddCommand command)
{
// command.user_id = Resolve(command.user_name) ??
// command.source_secret_id = Resolve(command.source_id, command.source_type) ??
foreach (var receiverId in command.Receivers)
{
var notificationEntity = _notificationsRepository.Get(0);
notificationEntity.TargetId = receiverId;
notificationEntity.Body = command.Text;
_notificationsRepository.Add(notificationEntity);
}
}
}
如果我在插入之前需要更复杂的逻辑怎么办?可以使用 IQuery 还是应该创建其他服务?
【问题讨论】:
-
命令处理程序应该注入另一个专门的服务。顺便说一句,你的代码很奇怪。我敢肯定,当他们描述“添加通知”用例时,它完全没有反映领域专家的语言。无论如何,即使“添加”通知也没有任何意义……通知可以让您了解发生的事情,例如事件。你想改写历史吗?
-
通知就像一条关于某事的消息,它包含notificationBody和notificationTargetUserId属性
标签: asp.net entity-framework domain-driven-design repository-pattern cqrs