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

Adding support for multiple and named query filters #35104

Closed
wants to merge 3 commits into from

Conversation

bittola
Copy link

@bittola bittola commented Nov 14, 2024

  • I've read the guidelines for contributing and seen the walkthrough

  • The code builds and tests pass locally (also verified by our automated build checks)

  • Tests for the changes have been added (for bug fixes / features)

  • Code follows the same patterns and style as existing code in this repo

Closes #8576

This PR adds support for defining multiple named global query filters on an entity.
It follows pattern used for keyed services in ASP.NET Core dependency injection.
It's backward compatible with the existing implementation where only single unnamed filter was possible.

Examples:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    => modelBuilder.Entity<MyEntity>()
        .HasQueryFilter(x => !_ids.Contains(x.Id))
        .HasQueryFilter("NameFilter", x => x.Name.StartsWith("Name"))
        .HasQueryFilter(GlobalFilters.Active, x => !x.IsDeleted)
        .HasQueryFilter(GlobalFilters.Published, x => !x.IsDraft);

public enum GlobalFilters
{
    Active,
    Published
}

The example above shows the basic usage of the feature. As you can see we can use a value of any type as a filter key. In our example it was string and enum values. If the filter key is not defined as it is in HasQueryFilter(x => !_ids.Contains(x.Id)), string.Empty is used under the hood. It can be later used to disable that anonymous filter.

var result = context.Entities
    .IgnoreQueryFilters(GlobalFilters.Active, "NameFilter")
    .IgnoreQueryFilters(string.Empty)
    .ToList();

This example shows how to ignore specific query filters by using their keys. To simplify the use we can use an overload allowing us to specify filters keys as params array. Sequential use of the method adds new keys to the ignored list. Note, that the string.Empty key disables the anonymous filter from the previous example.

If we want to ignore all filters at once we can do it be calling IgnoreQueryFilters overload with no parameters:

var result = context.Entities
    .IgnoreQueryFilters()
    .ToList();

@bittola bittola requested a review from a team as a code owner November 14, 2024 07:46
@bittola
Copy link
Author

bittola commented Nov 14, 2024

@dotnet-policy-service agree

@bittola
Copy link
Author

bittola commented Nov 25, 2024

will anybody review this?
@roji , @ajcvickers

@roji
Copy link
Member

roji commented Nov 25, 2024

@bittola apologies, but the team is currently still very busy dealing with the 9.0 release that came out recently - we'll hopefully be able to get some time and review soon.

@cincuranet
Copy link
Contributor

cincuranet commented Jan 24, 2025

Thanks for your PR @bittola. I'm not saying this is wrong, but first we'd like to have solid and approved API and behavior proposal in place (there's multiple proposals in #8576 together with this PR). After we have that ironed out, we'd be happy to take some PR(s).

The team does not have too much of available bandwidth right now, so the better the preparation/proposal, the faster the PR will get through.

If you feel like driving the proposal, feel free to do it in #8576.

I'll close this PR now, but would the final approved design be the same as what you have here, feel free to submit it again.

@cincuranet cincuranet closed this Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Named query filters
4 participants