Skip to content

Add hook to allow actions on the context after it is leased/returned from/to the pool #17086

@MorenoGentili

Description

@MorenoGentili

Hello,
this issue is somewhat related to #14625 but I'd like to suggest something slightly different.

Basically, I need global filter values to be resolved on a per-request basis, even if the DbContext instance comes from the pool. Please, let's not discuss performance gains here, like in #14625, and just assume my app DOES benefit from using the pool.

Here's my suggestion for a new overload of the HasQueryFilter API.
First, I'd register a global filter in the OnModelCreating like this. Please note no actual value is provided here, but just a link between the TenantId property of an entity class and a string identifier such as "TenantId".

modelBuilder.Entity<Product>().HasQueryFilter(p => p.TenantId, "TenantId");

Then, each time a DbContext is needed (e.g. user sending a request), the identifier "TenantId" has to be resolved to an actual value. So, I would need to add a method to the DbContext to do just that. Maybe marked by an atttribute such as GlobalFilterResolverAttribute. Please note this method must be dependency injection enabled so I can get a reference to scoped services like the ITenantProvider here.

[GlobalFilterResolver("TenantId")]
protected object ResolveTenantId(ITenantProvider provider)
{
  return provider.GetTenantId();
}

What do you think?
Thanks for your attention.
Moreno

Metadata

Metadata

Assignees

No fields configured for Feature.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions