-
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 IQueryExpressionInterceptor an ISingletonService #29573
Comments
@GianvitoDifilippo As you have found, queries are cached, so this won't work.
This is a good point. We will discuss. |
No problem, but it would be good to know exactly what limitations of query filters made you look at the new query experssion interceptor (as we may want to improve query filters in the future). |
First of all, thank you all for your answers. The main reason I don't think I can use
I actually did manage to achieve (2) using this solution, as we can see in this log, Generated query execution expression
This is totally fine, but since this will generate something like this The other reason why I wanted to use it is I could inject expressions that are not filters in the query - which I think is its original purpose - but using data provided by scoped services and providing points (1) and (2). Point (2) is actually more important than point (1), so I will evaluate both solutions (the other being overriding EntityQueryProvider). |
Hello everyone,
I implemented an
IQueryExpressionInterceptor
for multitenancy, which modifies the expression by adding a Where filter like so:The expression
entity => entity.TenantId == myTenantId
is constructed dynamically by the interceptor kind of like this (simplified):Obviously, the value of 'myTenantId' comes from a scoped service which reads the current user's claims.
The problem is that this doesn't work, since the
QueryCompiler
service caches the compiled query using the original query expression instead of the new one. This means all subsequent calls toFirstOrDefault(entity => entity.Id == myId)
will use the tenant id used in the first interception.Is this intended behavior? If yes, I would expect
IQueryExpressionInterceptor
to implementISingletonInterceptor
instead ofIInterceptor
- if my understanding of these interfaces is correct - since the interception result will be permanently cached.Anyway, is there a way to achieve the desired behavior with this new interceptor? I intended to override
EntityQueryProvider
only as a last resort :)Note: I am aware of the
HasQueryFilter
method, I'm not using it because of more complex logic that was omitted in this example.Tested with SqlServer and Postgres, attaching example code using InMemory.
QueryExpressionInterceptorIssue.zip
The text was updated successfully, but these errors were encountered: