Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply/disable filter per query #21

Open
hikalkan opened this issue Mar 4, 2015 · 3 comments
Open

Apply/disable filter per query #21

hikalkan opened this issue Mar 4, 2015 · 3 comments

Comments

@hikalkan
Copy link

hikalkan commented Mar 4, 2015

Hi,

I want to use SoftDelete pattern. But, sometimes we may need to deleted entities. It should work per query. Example:

DbContext.Set<User>().AsQueryable().DisableFilter("SoftDeleteFilter").Where(u => u.DeletionTime > SOME_TIME).ToList();

Is this possible? This is my project: https://github.com/aspnetboilerplate/aspnetboilerplate

@jcachat
Copy link
Collaborator

jcachat commented Mar 5, 2015

This is something I was thinking about recently too. And also to be able to set parameter values on a per query basis. But, I'm not sure there is a way that I can store that information attached to the query and then retrieve it again during the query parameter substitution. But I'll give it some more thought and see if I can some up with something.

In the meantime, you can disable the filter (which is scoped to just that DbContext instance) before executing the query and then re-enable it after the query. It's not as clean as the syntax you're proposing, but it will get the job done.

@hikalkan
Copy link
Author

hikalkan commented Mar 5, 2015

Thanks for your answer. I want to hear if you can find a good solution.

@TehWardy
Copy link

TehWardy commented Mar 30, 2017

random thought @JacHat how about an extension method for an IQueryableT that takes the context in so you can do that behind the scenes, the resulting code to give you literally everything you needed might look something like ...

DbContext.Set<User>()
    .AsQueryable()
    .DisableFilter("SoftDeleteFilter", DbContext)
    .Where(u => u.DeletionTime > SOME_TIME)
    .ToList()
    .ReEnableFilters(DbContext);

its not perfect but it does allow the functionality and abstracts from the current querying code the extra calls to remove or add the filter as needed.

You may not need the ReEnable bit on the end if you "track" the next call actually executed on the context from when the disable filter extension is called somehow (but I admit I don't know enough about what you are seeing in terms of variables to hand in such a context)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants