DELETE后如何等待RavenDB中的结果更新
我正在将存储库模式与 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 命名空间中找不到它。
总结 如何等待数据库更新以及如何读取新数据。我做错了什么吗?感谢您的帮助!
I am using repository pattern with Raven DB. My repository interface is
public interface IRepository<T> where T : Entity
{
IEnumerable<T> Find(Func<T, bool> exp);
void Delete(T entity);
void Save();
...
}
And implementation is
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();
}
...
}
I have a test that marks all entities for deletion, saves changes and query database to see if the result count is zero
[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);
}
The test fails with sample output
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
The problem is if I run this test several time, the items actually are being removed (2-3 items at a time). I saw that there is an IDocumentQuery that has WaitForNonStaleResults method.
IDocumentQuery<T> WaitForNonStaleResults();
But I cannot find it in Raven.Client.Lightweight namespace that was installed by NuGet.
To sum up How do I wait until the database is updated and how do I read fresh data. Am I doing something awfully wrong? Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,不建议使用此功能。
至少,使用 WaitForNonStaleResultsAsOfNow
根据 ayende 的响应:
http://groups.google.com/group/ravendb/browse_thread/thread/32c8e7e2453efed6/090b2e0a9c722e9f?#090b2e0a9c722e9f
Please note that it isn't recommended to use this.
At a minimum, use WaitForNonStaleResultsAsOfNow
As per ayende's response in:
http://groups.google.com/group/ravendb/browse_thread/thread/32c8e7e2453efed6/090b2e0a9c722e9f?#090b2e0a9c722e9f