Print Page | Close Window

QueryInterceptor: Adding a Filter based on Interface

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4181
Printed Date: 29-Mar-2025 at 9:41am


Topic: QueryInterceptor: Adding a Filter based on Interface
Posted By: gregweb
Subject: QueryInterceptor: Adding a Filter based on Interface
Date 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



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



Posted By: gregweb
Date Posted: 18-Jun-2013 at 4:29am
Great, thanks very much.


Posted By: gregweb
Date 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


Posted By: kimj
Date 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));


Posted By: gregweb
Date Posted: 18-Jun-2013 at 1:36pm
Works perfectly. Thanks again.

Greg



Print Page | Close Window