Print Page | Close Window

Modifying query results in EntityServerQueryInterceptor

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2541
Printed Date: 28-Mar-2025 at 8:48am


Topic: Modifying query results in EntityServerQueryInterceptor
Posted By: mikewishart
Subject: Modifying query results in EntityServerQueryInterceptor
Date Posted: 03-Mar-2011 at 11:10am
We have some cases where we'd like to improve performance.  To achieve this what I'd like to do is add a couple commonly queried data to the Entity and flag them as [DataMember].

Using a Northwind analogy, pretend there's no FK between Orders and Customers, or Customers is in a separate database and consider the queries:

var order = Orders.First(o => o.OrderID == 10248)
Customers.Where(c => c.CustomerId == order.CustomerId)

We could add a CustomerName property to the Order entity partial class to avoid the second query, if it could be updated in the EntityServerQueryInterceptor.

    protected override bool ExecuteQuery()
    {
      var result = base.ExecuteQuery();
      if (result)
      {
        //Get QueryResults
        //find all Orders being returned
        //Query all Customers for Orders.CustomerId
        //Update the Order.CustomerName on each Order for the matching Customer
      }
      return result;
    }

Am I looking in the right place to do this?  Any examples of how to do this?

Thanks!



Replies:
Posted By: smi-mark
Date Posted: 03-Mar-2011 at 12:31pm
I think perhaps a better approach would be to look at doing it from the edmx.

This link may help: http://msdn.microsoft.com/en-us/library/cc716698.aspx - http://msdn.microsoft.com/en-us/library/cc716698.aspx




Posted By: mikewishart
Date Posted: 03-Mar-2011 at 12:39pm
Thanks smi-mark, that's an interesting idea.  It doesn't help with the entities in two dbs.  This also seems to only work if the entities share a common key (such as sub-typing).


Posted By: smi-mark
Date Posted: 03-Mar-2011 at 3:57pm
Using Projection may be a better approach:

http://drc.ideablade.com/xwiki/bin/view/Documentation/CookbookSimpleProjection


Posted By: mikewishart
Date Posted: 13-Mar-2011 at 10:06pm
Following up.  I found what I was looking for in the 6.0.3 release notes.

Added QueriedEntities collection to EntityServerQueryInterceptor. [1374]

Now you can examine queried entities on the server before they are sent to the client. You could record what was queried for audit purposes (“What returns has a tax preparer tried to view?”). You can write fine-grained authorization logic to prevent transmission to client of forbidden entities … in case they slip past a protective query filter you added before the query was processed.

You cannot add or remove entities to the collection (yet). You can modify the entities (e.g., obscure the Social Security Number) before they continue on their way to the client. But be careful, especially if you expect the user to be able to update these entities and save them later!

This worked great!  I hope to see adding/removing entities from the result set sometime in the near future.  This would be helpful as well.

PS, in my earlier post I mistakenly wrote [DataContract] rather than [DataMember].  This has been fixed.



Print Page | Close Window