我使用RavenDB作为从域事件填充的非规范化读取模型。我发现问题,当两个事件(让我们称它们为“已创建”和“已更新”)在同一时间被非规范化时,在保存Created事件所做的更改之前,会发生由更新事件更新的加载文档。我已经与解决方案基于API的变化上来,以等待创建文件:RavenDB - 等待文档创建
public static T WaitAndLoad<T>(this IDocumentSession @this, ValueType id)
where T : class
{
var fullId = @this.Advanced.DocumentStore.Conventions.FindFullDocumentKeyFromNonStringIdentifier(id, typeof(T), false);
var ev = new ManualResetEvent(false);
var cancelation = new CancellationTokenSource();
@this.Advanced.DocumentStore
.Changes()
.ForDocument(fullId)
.Subscribe(change =>
{
if (change.Type == DocumentChangeTypes.Put)
{
ev.Set();
}
}, cancelation.Token);
try
{
var existing = @this.Load<T>(id);
if (existing != null)
{
return existing;
}
ev.WaitOne();
return @this.Load<T>(id);
}
finally
{
cancelation.Cancel();
}
}
不幸的是第二次调用加载返回null,原因的文件ID已在knownMissingIds领域InMemoryDocumentSessionOperations和服务器发出任何请求。
有没有其他方式可以等到创建文档?