Hello,
I need to implement a data filter for our Event entity. The filtering should be done by Event Id, retrieved from an IEventSession we defined, and should be overriden with the UnitOfWork SetFilterParameter method.
This is what I have done so far, according to this document: <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Data-Filters">https://aspnetboilerplate.com/Pages/Doc ... ta-Filters</a>
Defined the interfaces IMayHavEvent and IMustHaveEvent Defined filter names in a static class Registered the filters at the Core project Module using Configuration.UnitOfWork.RegisterFilter
So what I need to do now, is to do the actual filtering. The docs say that EntityFramework.DynamicFilters do not apply for EF Core, so what is the most convenient way to implement the filter? If I do it at repository level, what should I override at the RepositoryBase abstract class?
Thank you!
11 Answer(s)
-
0
Override ShouldFilterEntity and CreateFilterExpression in your DbContext.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
<cite>aaron: </cite> Override ShouldFilterEntity and CreateFilterExpression in your DbContext.
Ok, I see I can get it done easily that way.
In order to get the filter parameter defined with IUnitOfWork.SetFilterParameter, I have to use IUnitOfWork.GetFilter and check in the DataFilterConfiguration.FilterParameters collection, right?
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Yes. Something like this in your DbContext:
var eventId = CurrentUnitOfWorkProvider.Current? .Filters.FirstOrDefault(f => f.FilterName == MyDataFilters.MayHaveEvent)? .FilterParameters[MyDataFilters.Parameters.EventId] as long?;Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
I have aspzero, can you please provide us with example to set custom filler ?
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
see the implementation of ISoftDelete and imitate the same <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Domain/Entities/ISoftDelete.cs">https://github.com/aspnetboilerplate/as ... tDelete.cs</a>
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Sorry I set the below implementation 1)
public interface IHasCustomer { string InsCustID { get; set; } }- override ShouldFilterEntity and CreateFilterExpression as below
protected override bool ShouldFilterEntity<TEntity>(IMutableEntityType entityType) { return base.ShouldFilterEntity<TEntity>(entityType); } protected override Expression<Func<TEntity, bool>> CreateFilterExpression<TEntity>() { Expression<Func<TEntity, bool>> expression = null; if (typeof(IHasCustomer).IsAssignableFrom(typeof(TEntity))) { Expression<Func<TEntity, bool>> mustHaveTenantFilter = e => ((IHasCustomer)e).InsCustID == GetCurrentCustomerIdOrNull() || (((IHasCustomer)e).InsCustID == GetCurrentCustomerIdOrNull()) == true; expression = expression == null ? mustHaveTenantFilter : CombineExpressions(expression, mustHaveTenantFilter); } return base.CreateFilterExpression<TEntity>(); }my question what the next to run and apply the filter
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Tip: Wrap your code in the following for formatting and readability:
[code][/code:2iuqdo9h] A shortcut is to highlight your code and click on the </> button in the formatting toolbar.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
<cite>aaron: </cite> Tip: Wrap your code in the following for formatting and readability:
[code][/code:39lrdbwm] A shortcut is to highlight your code and click on the </> button in the formatting toolbar.
I did that wrap my code in <code> tage can tell me what the next step to apply the filter on the all query
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Could you show your GetCurrentCustomerIdOrNull method ?
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Can you please support me with sample of code working on the ASPZero version 3.5
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
@jehadk,
Unfortunately we don't have a sample for this. What is your latest problem, we can try to help.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image)