dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.8k stars 3.2k forks source link

Intercept resolution of entity type #34765

Open kjkrum opened 1 month ago

kjkrum commented 1 month ago

IMaterializationInterceptor allows the interception of individual entity materialization. Conceptually there must be a preceding step where EF resolves the entity type that a database row will be materialized into. It may be useful to intercept that type resolution.

My use case for this feature would be to examine authorization attributes on the entity type and throw an exception if the current user is not authorized to access that entity type. I'm currently doing this experimentally with an IMaterializationInterceptor. This feature request is based on the possibly incorrect assumption that type resolution only occurs once for a result set, as opposed to materialization which occurs once for every row. My entire use case may prove to be a stupid idea, but there may be more legitimate use cases for a type resolution interceptor.

AndriySvyryd commented 1 month ago

Type resolution does occur once for every row (roughly). Currently this logic is hardcoded but might be possible to do something like this after https://github.com/dotnet/efcore/issues/32923 is implemented.

roji commented 1 month ago

@kjkrum I'd be interested in more context on exactly what you're trying to achieve, i.e. how exactly you would vary the entity type being materialized, and why - there may be simpler ways to achieve the same overall goal.

kjkrum commented 1 month ago

@roji I'm not interested in changing the entity type, only examining its attributes to see if the current user is authorized to load entities of that type. In other words, roughly speaking, table-level permissions. A materialization interceptor works for this, and performance isn't an issue yet, but maybe it could be more efficient.