New Posts New Posts RSS Feed: Changing query before execution
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Changing query before execution

 Post Reply Post Reply
Author
mrdaniel76 View Drop Down
Newbie
Newbie


Joined: 13-Mar-2009
Posts: 2
Post Options Post Options   Quote mrdaniel76 Quote  Post ReplyReply Direct Link To This Post Topic: Changing query before execution
    Posted: 13-Mar-2009 at 6:42am
We are currently evaluating DevForce EF and we have a need to append an extra condition to most queries before it runs in order to filter the data, so the user will only see the rows they are authorized to see.
 
So we need the following before any query runs:
* Determine if the table has a certain column on it (for example userid column)
* If it does, add an extra criteria to the query so the result is filtered based on that query (for example userid=current user)
 
I found something on the Developer's guide but that information seems to be outdated and for the classic version of devforce because it does not work:
static void mManager_Fetching(object sender, EntityFetchingEventArgs e)
{
     IdeaBlade.Persistence.Rdb.RdbQuery q = e.Query;
     if ( q.EntityType is ActiveEntity )
     {
         q.AddClause("ActiveFlag", EntityQueryOp.EQ, true); // '1' == true 
     }
}
 
What's the equivalent of this code so I can fulfill my requirement?
 
I know I can create a partial class for the EntityManager and override the OnFetching method but do not know what to do from there.
public partial class DomainModelEntityManager
{
     protected override bool OnFetching(IdeaBlade.EntityModel.v4.EntityFetchingEventArgs args)
     {
     }
}
 
Thanks a lot!
 
Daniel
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Posted: 13-Mar-2009 at 8:47am
     public class BOSFetch : IEntityServerFetching
     {
       
         public void OnFetching(IdeaBlade.EntityModel.v4.EntityServerFetchingEventArgs args)
         {
             if (args.Query.QueryableType is ActiveEntity) {
                //In here you can cast the args.Query to an EntityQuery<ActiveEntity>
                //and then you are able to do .Where and all your other query functions.
             }
         }
     }
Back to Top
mrdaniel76 View Drop Down
Newbie
Newbie


Joined: 13-Mar-2009
Posts: 2
Post Options Post Options   Quote mrdaniel76 Quote  Post ReplyReply Direct Link To This Post Posted: 13-Mar-2009 at 9:21am
Thanks for the reply. But I was not able to follow it completely. What is ActiveEntity? It does not seem to resolve to anything.
 
I tried adding a new .Where but it didn't work. Would you mind posting a more complete example?
 
Also, how do I know if the entity being searched has a certain column so I can add the extra criteria? Not all entities in my solution will have the column in question.
 
You seem to be creating a new class and implementing IEntityServerFetching, but I don't see anywhere in the developer's guide that references it. Is that a better way to my way of creating a partial class for the EntityManager and overriding the OnFetching method?
 
Sorry for the newbie questions but I just started evaluating DevForce and this is one of the first requirements that I need to verify.
 
 
Thanks a lot!
 
Daniel
 
 
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Posted: 13-Mar-2009 at 9:37am
I took 'ActiveEntity' from your code snippet. It is any class you have, be it a single class, or a base class for multiple other entity objects. IdeaBlade looks for any classes implement IEntityServerFetching and calls that method.

This is what you could do,

1. Create an injected base type called 'MyBaseEntity' (Or a more meaningful name) using the object mapper and assign the tables that have that column to inherit from that base entity
2. In the onfetching instead of 'ActiveEntity' you can use 'MyBaseEntity' and do whatever processing logic you need
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: 13-Mar-2009 at 10:23am
Thanks for the excellent response, Mark.
 
Daniel, the documentation you reference is in fact outdated and we're getting it fixed.  The EntityManager is not a partial class, and there's no reason you should have to override OnFetching here.  As Mark stated, implementing IEntityServerFetching is the best way to go.   There's also an EntityManager.Fetching event you can handle, but that would place your security logic within your client application.
Back to Top
jsobell View Drop Down
Groupie
Groupie
Avatar

Joined: 02-Apr-2009
Location: Australia
Posts: 80
Post Options Post Options   Quote jsobell Quote  Post ReplyReply Direct Link To This Post Posted: 02-Apr-2009 at 7:12am
Originally posted by smi-mark

     public class BOSFetch : IEntityServerFetching
     {
       
         public void OnFetching(IdeaBlade.EntityModel.v4.EntityServerFetchingEventArgs args)
         {
             if (args.Query.QueryableType is ActiveEntity) {
                //In here you can cast the args.Query to an EntityQuery<ActiveEntity>
                //and then you are able to do .Where and all your other query functions.
             }
         }
     }


Brilliant!  Now where on earth do I put this?
I assume it's added to my Thingy.Web project, but it appears to have no effect there. How would it every get picked up? Does it go in an entity's partial class?

I'm interested in intercepting all client requests and filtering them based on our own per-entity permission set.
In Linq to SQL or Ria we would simply add a '.Where' to the .GetClients, but what would be the correct manner in DF?
I only installed this yesterday, so please explain in simple terms, and assume that I have not read the manual (which I have scanned through several times but not found a solution).
Also, how on earth does the Login feature work? How do I verify the login info?
If you could point me to any relevant doco that would be great, but the videos on the site are all covering pretty fundamental stuff, so the more technical customers could probably use a few more advanced ones :)
I'm using the SL version, just in case that makes a difference, and it looks excellent.

Cheers,
 Jason
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down