New Posts New Posts RSS Feed: Not every query is an async query
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Not every query is an async query

 Post Reply Post Reply
Author
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Topic: Not every query is an async query
    Posted: 25-Sep-2009 at 2:51pm
A customer asks:
 
"I have a question about retrieving entities from cache using DevForce Silverlight.  I was wondering why you have to write an async query in order to retrieve cached entities?  I figured since the cached data is already on the client, I can just access it using client side LINQ."
 

Excellent question. The answer is: you can query the cache synchronously J

 

Here is a little experiment using PrismExplorer.

 

·         Launch it

·         Run the query “Get Customers with Orders in Oregon” query (this populates the cache with some customers and some orders)

·         Put breakpoint on line in “Infrastructure.Repository” class that reads:
EntityManager.DefaultQueryStrategy = QueryStrategy.Normal;
That’s line 31 in the version on my desktop.

·         Back in the UI, select “Get Orders in Cache”

·         It will break here

·         Open the Immediate Window

·         Enter “query”; you should see something like this:


query

{value(IdeaBlade.EntityModel.EntityQueryProxy`1[DomainModel.Order])}

    [IdeaBlade.EntityModel.EntityQuery<DomainModel.Order>]: {value(IdeaBlade.EntityModel.EntityQueryProxy`1[DomainModel.Order])}

    CommandTimeout: 0

    ElementType: {DomainModel.Order}

    EntityManager: {DomainModel.DomainModelEntityManager}

    QueryableType: {DomainModel.Order}

    QueryStrategy: {IdeaBlade.EntityModel.QueryStrategy}

    Tag: null

 

If you dig in, you will see that the QueryStrategy is CacheOnly

 

·         Enter “EntityManager.ExecuteQuery(query)”; you should see something like this:

Count = 28

    [0]: {DomainModel.Order}

    [1]: {DomainModel.Order}

    [2]: {DomainModel.Order}

    [3]: {DomainModel.Order}

    [4]: {DomainModel.Order}

    ...

    [25]: {DomainModel.Order}

    [26]: {DomainModel.Order}

    [27]: {DomainModel.Order}

 

Yup. The query is executing immediately and synchronously.

 
I’m taking advantage of the fact that I’ve pinned this query to CacheOnly. Had I not, and it tried to satisfy the query from the database, I would have received an exception.

 

One common tactic is to set the EntityManager.DefaultQueryStrategy to a CacheOnly strategy. Then DevForce will attempt to satisfy all queries without an explicit strategy from the cache. You can always take a query and change its strategy by saying “q.With(QueryStrategy)” .

 

Just make sure the EntityManager can do what you ask of it.



Edited by WardBell - 25-Sep-2009 at 2:52pm
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 25-Sep-2009 at 4:38pm
I just wanted to add that if you do use an async query and the query can be satisfied from cache, that the call will never "go async" and will return results to your callback synchronously.   It's usually better though, as Ward states, to control query execution and strategy through your code.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down