9

我正在使用 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 个项目)。我看到有一个具有WaitForNonStaleResults方法的 IDocumentQuery。

IDocumentQuery<T> WaitForNonStaleResults();

但我在NuGet 安装的Raven.Client.Lightweight命名空间中找不到它。

总结一下如何等到数据库更新以及如何读取新数据。我做错了什么吗?谢谢你的帮助!

4

1 回答 1

13
Session.Query<Foo>().Customize(x=>x.WaitForNonStaleResults()) 

请注意,不建议使用它。至少使用 WaitForNonStaleResultsAsOfNow

根据ayende的回复:

http://groups.google.com/group/ravendb/browse_thread/thread/32c8e7e2453efed6/090b2e0a9c722e9f?#090b2e0a9c722e9f

于 2011-05-03T20:55:08.300 回答