我使用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和服務器發出任何請求。
有沒有其他方式可以等到創建文檔?