【发布时间】:2014-04-22 09:47:14
【问题描述】:
我正在使用事件源实现 CQRS 模式,我正在使用 NServiceBus、NEventStore 和 NES(NSB 和 NEventStore 之间的连接)。
我的应用程序会定期检查 Web 服务是否有任何要下载和处理的文件。当找到一个文件时,一个命令(DownloadFile)被发送到总线,并由 FileCommandHandler 接收,它创建一个新的聚合根(File)并处理消息。
现在在(文件聚合根目录)中,我必须检查文件的内容是否与任何其他文件内容不匹配(因为网络服务保证只有文件名是唯一的,并且内容可能与不同的名称),通过对其进行散列并与散列内容列表进行比较。
问题是我必须在哪里保存哈希码列表?是否允许查询读取模型?
public class File : AggregateBase
{
public File(DownloadFile cmd, IFileService fileDownloadService, IClaimSerializerService serializerService, IBus bus)
: this()
{
// code to download the file content, deserialize it, and publish an event.
}
}
public class FileCommandHandler : IHandleMessages<DownloadFile>, IHandleMessages<ExtractFile>
{
public void Handle(DownloadFile command)
{
//for example, is it possible to do this (honestly, I feel it is not, since read model should always considered stale !)
var file = readModelContext.GetFileByHashCode (Hash(command.FileContent));
if (file != null)
throw new Exception ("File content matched with another already downloaded file");
// Since there is no way to query the event source for file content like:
// eventSourceRepository.Find<File>(c=>c.HashCode == Hash(command.FileContent));
}
}
【问题讨论】:
-
我相信原则是“命令”和“查询”分离(例如可能是不同的数据源)。这并不意味着该命令不能使用查询。我倾向于在命令执行中使用查询。
-
重复的内容文件(即无法处理的DownloadFile命令)应该怎么办?
标签: c# nservicebus cqrs event-sourcing neventstore