-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Make parameterization of global query filters more obvious #18417
Comments
Your assumption is wrong. There are various different ways in which you can define Query filter outside of DbContext and still use it to change value of parameters. |
But the fields are defined on DbContext, the fields need to be defined on the custom class for true seperation. For examaple. I want todo something like this public class AgreementAuthorization : IEntityAuthorization
{
private readonly string _legalEntityNumber;
public AgreementAuthorization(IAuthScope scope) //Scoped through scoped service provider
{
_legalEntityNumber = scope.LegalEntityNumber;
}
public void Build(ModelBuilder builder)
{
builder
.Entity<Agreement>()
.HasQueryFilter(a => _legalEntityNumber == null || a.LegalEntity.Number == _legalEntityNumber);
}
}
public class MyDbContext : DbContext
{
private IEnumerable<IEntityAuthorization> _entityAuthorization;
MyDbContext(IEnumerable<IEntityAuthorization> entityAuthorization)
{
_entityAuthorization = entityAuthorization;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
builder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly);
_entityAuthorization.ForEach(a => a.Build(builder));
}
} |
First you are defining QueryFilter for same EntityType repeatedly so that will be overwritten. Even if I ignore that, can you describe how do you want us to figure out exact value to use at the runtime in the filter when executing query? Currently property/field/methods are defined on DbContext because we know how to inject DbContext which is executing query to get actual value when running the query. |
No I am not, they are injected using IoC service.AddTransient<IEntityAuthorization, AgreementAuthorization>();
service.AddTransient<IEntityAuthorization, OtherAuth>();
service.AddTransient<IEntityAuthorization, SomeAuth>(); |
Why not have hooks that trigger before queries. |
This thing calls HasQueryFilter on |
No it calls |
Internally called when doing |
Notes from triage: the current design of query filters uses the
Based on this, it might be worth designing and implementing query filters where the filter parameter is obtained in a different way--such as through a defined callback. This would be easier for many to understand and would not have a strong coupling to the context. |
We have already abstracted the Dbcontext away from the domain it uses a custom interface with a custom Set method so I might just write my own mechanic that hooks in our custom Set method. Only downside is that it will return a IQueryable instead of DbSet. But I can work around that by making sure everybody uses AddRange and similar on the abstracted dbcobtext rather than the DbSet |
@AndersMalmgren this won't apply the query filters to any |
No, If any entity has a relation to another entity that is under restriction that entity also needs to have filters. Thats ok in our case. Child and parents always have same restrictions. We dont use Include but we project all queries to DTOs that might include children |
Also consider adding an overload to make the context parameter more obvious:
|
From below:
Notes from triage: the current design of query filters uses the DbContext as the anchor from which filter parameters are read. This works well in many cases, but has a couple of limitations:
Based on this, it might be worth designing and implementing query filters where the filter parameter is obtained in a different way--such as through a defined callback. This would be easier for many to understand and would not have a strong coupling to the context.
Original issue:
For a enterprise level application you cant define an entire data model directly on the DbContext, you use
IEntityTypeConfiguration<TEntity>
to seperate configuration away fromOnModelCreating
method.When it comes to
HasQueryFilter
that uses expressions with dependencies to scoped members there is no such mechanics. It needs to be defined directly on theDbContext
. This just wont cut it for a large Enterprise level application.The text was updated successfully, but these errors were encountered: