Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

enable/disable a custom data filter #6143


User avatar
0
kasem created

Hi,

I don't know how to locate a custom data filter to manually enable/disable it as needed. I need this because my custom filter uses a user session variable in DbContext and it's inaccessible in background job. I'm trying to manually disable it so I can add the filter to my query or just change its value through the background job. My custom filter has been added to DbContext overriding CreateFilterExpression method:

Expression<Func<TEntity, bool>> mustHaveYearFilter = e => ((IMustHaveYear)e).YearId == MyYear || (((IMustHaveYear)e).YearId == MyYear) == true;
                    expression = expression == null ? mustHaveYearFilter : CombineExpressions(expression, mustHaveYearFilter);

I tried to register the filter in WebHostModule > PreInitialize Configuration.UnitOfWork.RegisterFilter("MustHaveYear", true);

But it doesn't change the filter or disable it.

Change filter code:

_unitOfWorkManager.Current.EnableFilter("MustHaveYearFilter");
_unitOfWorkManager.Current.SetFilterParameter("MustHaveYearFilter", "YearId", args.YearId);

Disable filter code:

_unitOfWorkManager.Current.DisableFilter("MustHaveYearFilter");

Regards

UPDATE I was able to enable and disable the filter successfuly. Somehow the parameter name wasn't injected to the query. I disabled the filter and added it to my WHERE statements as a workaround.


2 Answer(s)
  • User Avatar
    0
    ryancyq created
    Support Team

    Hi, have you tried passing in YearId as background job arguments as an alternative?

    Since job arugments are serialized and persisted, you can use it in background job.

  • User Avatar
    0
    kasem created

    Hi,

    Yes, this is what I've mentioned, I'm using this as a workaround now. I'm fine with this for now - I mean you can close my ticket. I'll reopen it if I still find a serious added value for auto filter.

    Thank you