New Posts New Posts RSS Feed: OnFetching
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

OnFetching

 Post Reply Post Reply
Author
ouelletf View Drop Down
Newbie
Newbie
Avatar

Joined: 15-May-2009
Location: Québec, Canada
Posts: 9
Post Options Post Options   Quote ouelletf Quote  Post ReplyReply Direct Link To This Post Topic: OnFetching
    Posted: 21-May-2009 at 1:13pm
I am trying to filter out some entities with OnFetching in the EntityManager

I use the Filter method on EntityQuery to add a filter.

But I can't set the new query.

args.Query is read-only.

Filter() return a new one if your query is a simple EntityQuery
And it modify the query, if it is a ServerQuery.

Back to Top
eileenv View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 15-Jun-2007
Location: United States
Posts: 68
Post Options Post Options   Quote eileenv Quote  Post ReplyReply Direct Link To This Post Posted: 27-May-2009 at 10:11am

You should be able to modify the query passed into your Fetching handler as long as you cast it first to an EntityQuery. For example:

    void OnFetching(object sender, EntityFetchingEventArgs e) {
      EntityQuery q = e.Query as EntityQuery;
      q.Filter(...);
    }
Back to Top
ouelletf View Drop Down
Newbie
Newbie
Avatar

Joined: 15-May-2009
Location: Québec, Canada
Posts: 9
Post Options Post Options   Quote ouelletf Quote  Post ReplyReply Direct Link To This Post Posted: 27-May-2009 at 10:57am
Yes, no problem on this part.

But if you check the code executed by Filter()
You will see that if the Query isnt a ServerEntityQuery it return a Clone of it instead of modifying the "this" query and returning it.  I find this behavior strange and "hard" to predict. I think it should alwasy return a clone, or always this, not sometimes one, and other times the other (my two cents).

And there is no way to change the args.Query for the new one returned by Filter()

Ive done it implementing IEntityServerFetching, but I prefer to override it in my manager.
The EventArgs provided by IEntityServerFetching.OnFetching allow write on the Query property.
Maybe I fail to see the purpose of doing in another class and being able to change it by configuration file.

Back to Top
eileenv View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 15-Jun-2007
Location: United States
Posts: 68
Post Options Post Options   Quote eileenv Quote  Post ReplyReply Direct Link To This Post Posted: 28-May-2009 at 11:56am
We will address the issue about not being able to set the Query object involved in the EntityManager.Fetching event in our upcoming release (v5.1.1). This means you will be able to set the Query object in the client-side handler just as you would in the server-side handler.


Edited by eileenv - 29-May-2009 at 9:49am
Back to Top
ouelletf View Drop Down
Newbie
Newbie
Avatar

Joined: 15-May-2009
Location: Québec, Canada
Posts: 9
Post Options Post Options   Quote ouelletf Quote  Post ReplyReply Direct Link To This Post Posted: 28-May-2009 at 1:05pm
OK, cool

Thanks
Back to Top
eileenv View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 15-Jun-2007
Location: United States
Posts: 68
Post Options Post Options   Quote eileenv Quote  Post ReplyReply Direct Link To This Post Posted: 01-Jun-2009 at 1:27pm
Upon further review of the code, there is actually a good reason why the Query object is not modifiable in the Fetching event on the client-side. The reason is that the original query you submitted on the client may not be the same query you get in the Fetching event, because the query in the latter may be a "reduced" version of the query.
 
Say for example your original query was "Get me all the Customers who reside in the USA". And let's say there are a total of 100 customers that satisfy the query. If there are 20 customers already residing in the cache, then the query in the Fetching event will be reduced to fetch just the remaining 80 customers. In this case, the original query is not the same as the query in the Fetching event, and modifying this query would be a bad idea.
 
In our upcoming release (v5.1.1), we will provide a new event for modifying the query: EntityManager.Querying. The Querying event will fire before the EntityManager queries data from an EntityServer. Note that cache-only queries or optimized queries that are handled by the client will not go thru this handler.
 
Here's an example of how it might be used:
 
    public void QueryModifyQuery() {
      _em1.Querying += (s, e) => {
        var query = e.Query as EntityQuery;
        if (query != null) {
          var newQuery = query.Filter((IQueryable<Customer> q) => q.Where(c => c.Country == "USA"));
          e.Query = newQuery;
        }
      };
      var custs = _em1.Customers.Take(7).ToList();
      Assert.IsTrue(custs.All(c => c.Country == "USA"));
    }
 
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down