Where is IAsyncDocumentSession.QueryAsync()?

980 views
Skip to first unread message

Matt Johnson

unread,
Oct 22, 2012, 11:04:46 AM10/22/12
to ravendb
Using ASP.Net WebApi, and I can use LoadAsync to write an async
controller such as:

public async Task<Foo> Get(string id)
{
return await _asyncSession.LoadAsync<Foo>(id);
}

I was thinking the query for would look like

public async Task<IQueryable<Foo>> Get()
{
return await _asyncSession.QueryAsync<Foo>();
}

but QueryAsync doesn't exist, and you can't await the results of a
Query because it isn't a Task.


Or am I missing something fundamental with this approach?

Fitzchak Yitzchaki

unread,
Oct 22, 2012, 11:10:49 AM10/22/12
to rav...@googlegroups.com
You have session.Query<Foo>().ToListAsync()

Chris Marisic

unread,
Oct 22, 2012, 11:13:06 AM10/22/12
to rav...@googlegroups.com
Session.Advanced.AsyncDatabaseCommands.QueryAsync()

Matt Johnson

unread,
Oct 22, 2012, 12:12:36 PM10/22/12
to ravendb
Yeah, I saw both of those. But
DocumentStore.AsyncDatabaseCommands.QueryAsync() seems more akin to
DocumentStore.DatabaseCommand.Query(). It returns a QueryResult, not
an IQueryable.

ToListAsync() might be a good approach, but am I gaining anything by
awaiting on it? Wouldn't just returning IQueryable be almost the same
thing?

My understanding of async webapi controllers is that they free up the
thread during long running tasks. So should I even consider this if
the load or query doesn't take much time?

I have no issues with the synchronous approach, I just don't want to
pass up an opportunity for increased perf or scalability. Is there
one here?

-Matt

Oren Eini (Ayende Rahien)

unread,
Oct 22, 2012, 12:16:45 PM10/22/12
to rav...@googlegroups.com
Matt,
Query is just used to build the IQueryable, that means that is has very little cost.
ToListAsync() actually execute the query against the server.

Matt Johnson

unread,
Oct 22, 2012, 2:48:13 PM10/22/12
to ravendb
So would something like the following:

public async Task<IEnumerable<Foo>> Get()
{
return await _asyncSession.Query<Foo>().ToListAsync();
}

Work any better than

public async Task<IEnumerable<Foo>> Get()
{
return _session.Query<Foo>().ToList();
}

What if I want to expose the IQueryable to enable odata filters like
WebApi allows? This works, but is synchronous:

public IQueryable<Foo> Get()
{
return _session.Query<Foo>();
}

Would something like the following have the same effect and be
asynchronous?

public async Task<IQueryable<Foo>> Get()
{
return await
_asyncSession.Query<Foo>().ToListAsync().AsQueryable();
}

I don't think that last one will work. How do I stay async and still
expose IQueryable?

-Matt


On Oct 22, 9:17 am, "Oren Eini (Ayende Rahien)" <aye...@ayende.com>
wrote:

Oren Eini (Ayende Rahien)

unread,
Oct 22, 2012, 3:14:12 PM10/22/12
to rav...@googlegroups.com
This is probably best:

public Task<IEnumerable<Foo>> Get()
{
  return _asyncSession.Query<Foo>().ToListAsync();
}

Or:


public IQueryable<Foo> Get()
{
  return _asyncSession.Query<Foo>();
}


Which will use the sync API.
Reply all
Reply to author
Forward
0 new messages