Author |
Share Topic Topic Search Topic Options
|
gregweb
DevForce MVP
Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
|
Post Options
Quote Reply
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
|
|
kimj
IdeaBlade
Joined: 09-May-2007
Posts: 1391
|
Post Options
Quote Reply
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));
|
|
gregweb
DevForce MVP
Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
|
Post Options
Quote Reply
Posted: 18-Jun-2013 at 4:29am |
Great, thanks very much.
|
|
gregweb
DevForce MVP
Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
|
Post Options
Quote Reply
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
|
|
kimj
IdeaBlade
Joined: 09-May-2007
Posts: 1391
|
Post Options
Quote Reply
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));
|
|
gregweb
DevForce MVP
Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
|
Post Options
Quote Reply
Posted: 18-Jun-2013 at 1:36pm |
Works perfectly. Thanks again.
Greg
|
|