イベントソーシングでCQRSパターンを実装しています.NServiceBus、NEventStore、およびNES(NSBとNEventStoreを接続します)を使用しています。
私のアプリケーションは、Web サービスを定期的にチェックして、ダウンロードして処理するファイルがないか確認します。ファイルが見つかると、コマンド (DownloadFile) がバスに送信され、新しい集約ルート (File) を作成してメッセージを処理する FileCommandHandler によって受信されます。
(ファイル集約ルート)内で、ファイルのコンテンツが他のファイルコンテンツと一致しないことを確認する必要があります(Webサービスはファイル名のみが一意であることを保証し、コンテンツは別の名前で複製される可能性があるため) 、それをハッシュし、ハッシュされたコンテンツのリストと比較します。
問題は、ハッシュ コードのリストをどこに保存する必要があるかということです。読み取りモデルを照会することは許可されていますか?
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));
}
}