我正在使用Raven DB的存储库模式。我的存储库界面是
public interface IRepository<T> where T : Entity
{
IEnumerable<T> Find(Func<T, bool> exp);
void Delete(T entity);
void Save();
...
}
实施是
public class Repository<T> : IRepository<T> where T : Entity
{
public IEnumerable<T> Find(Func<T, bool> exp)
{
return session.Query<T>().Where(exp);
}
public void Delete(T entity)
{
session.Delete(entity);
}
public void Save()
{
session.SaveChanges();
}
...
}
我有一个测试,标记所有要删除的实体,保存更改并查询数据库以查看结果计数是否为零
[Test]
public void DeleteValueTest()
{
//resolve repository
var repository = ContainerService.Instance.Resolve<IRepository<DummyType>>();
Func<DummyType, bool> dummyTypeExpr = item => item.GetType() == typeof(DummyType);
//find all entries of dummy types and mark for deletion
var items = repository.Find(dummyTypeExpr);
foreach (var item in items)
{
repository.Delete(item);
}
//commit changes
repository.Save();
//populate all dummy types, shall be nothing in there
items = repository.Find(dummyTypeExpr);
int actualCount = items.Count();
int expectedCount = 0;
Assert.AreEqual(expectedCount, actualCount);
}
测试失败并带有样本输出
RepositoryTest.DeleteValueTest : FailedExecuting query '' on index 'dynamic/DummyTypes' in 'http://localhost:8080'
Query returned 5/5 results
Saving 1 changes to http://localhost:8080
Executing query '' on index 'dynamic/DummyTypes' in 'http://localhost:8080'
Query returned 4/5 results
Expected: 0
But was: 4
问题是如果我多次运行此测试,实际上正在删除项目(一次2-3项)。我看到有一个IDocumentQuery有 WaitForNonStaleResults 方法。
IDocumentQuery<T> WaitForNonStaleResults();
但是我无法在NuGet安装的 Raven.Client.Lightweight 命名空间中找到它。
总结如何等待数据库更新以及如何阅读新数据。我做错了什么吗?谢谢你的帮助!
答案 0 :(得分:13)
Session.Query<Foo>().Customize(x=>x.WaitForNonStaleResults())
请注意,建议不要使用此功能。 至少使用WaitForNonStaleResultsAsOfNow
根据ayende的回应: