开发者

How do I wait for the results to be updated in RavenDB after DELETE

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 impleme开发者_JAVA百科ntation 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!


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜