Print Page | Close Window

Not every query is an async query

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1497
Printed Date: 21-May-2025 at 4:38pm


Topic: Not every query is an async query
Posted By: WardBell
Subject: Not every query is an async query
Date 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.




Replies:
Posted By: kimj
Date 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.



Print Page | Close Window