New Posts New Posts RSS Feed: QueryInterceptor: Adding a Filter based on Interface
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

QueryInterceptor: Adding a Filter based on Interface

 Post Reply Post Reply
Author
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Topic: QueryInterceptor: Adding a Filter based on Interface
    Posted: 15-Jun-2013 at 8:53am
In a QueryInterceptor, I have this:

QueryFilters.AddFilter<IDeletable>(q => q.Where(c => c.IsDeleted == false));

IDeletable is an interface which just specifies there is a IsDeleted field in the entity.

This compiles, but it does not seem to actually filter the query. Is this type of filter supported?

The intent is to add a filter to any entity that has an IsDeleted field so it doesn't have to be added to every query.

Greg
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: 17-Jun-2013 at 11:23am
Filtering by interface is not currently supported. We have a feature request to support this, but it hasn't yet been implemented.

As a workaround, albeit heavy-handed, you can do something like this:

foreach (var t in ReflectionFns.GetTypesImplementing(typeof(IDeletable), CompositionHost.Instance.ProbeAssemblies))
   this.QueryFilters.AddFilter(new PredicateDescription(t, "IsDeleted", FilterOperator.IsEqualTo, false));

Or this:

if (typeof(IDeletable).IsAssignableFrom(this.Query.QueryableType))
   this.QueryFilters.AddFilter(new PredicateDescription(this.Query.QueryableType, "IsDeleted", FilterOperator.IsEqualTo, false));

Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Posted: 18-Jun-2013 at 4:29am
Great, thanks very much.
Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Posted: 18-Jun-2013 at 9:12am
Kim, I have been using the above and it works great. But I haven't figured out how to make this one work:

     QueryFilters.AddFilter<IAuditEntityBase>(q => q.Where(c => principal.Authorizations.Contains(c.OwnedBy)));

I tried writing it similar to the above, but an error message is returned saying that it only works with a string. Principal.Authorizations is a List<int> of authorizations.

Greg
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: 18-Jun-2013 at 1:22pm
FilterOperator.Contains only works with strings, but you can use InList instead. Something like this should work:

   QueryFilters.AddFilter(new PredicateDescription(t, "OwnedBy", FilterOperator.InList, principal.Authorizations));
Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Posted: 18-Jun-2013 at 1:36pm
Works perfectly. Thanks again.

Greg
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down