Add conditional interception via type predicate (#42)#64
Merged
Conversation
Add a shouldIntercept predicate (Func<Type, bool>) to both EnableClassInterceptors and EnableInterfaceInterceptors so interception can be applied selectively based on the implementation type. This is particularly useful with assembly scanning, where only some scanned types should be intercepted. For interface interception the predicate is evaluated at resolve time against the resolved implementation type; when it returns false the instance is returned without a proxy. For class interception the proxy type is generated at registration time, so the predicate is evaluated per type at registration; when it returns false the type is registered without interception. All new overloads are additive; existing signatures are unchanged to preserve binary compatibility.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #64 +/- ##
===========================================
+ Coverage 89.34% 90.71% +1.37%
===========================================
Files 2 2
Lines 122 140 +18
Branches 21 23 +2
===========================================
+ Hits 109 127 +18
Misses 6 6
Partials 7 7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #42.
Problem
There was no way to apply interception selectively. When using assembly scanning,
EnableClassInterceptors/EnableInterfaceInterceptorsapplied interception to every scanned type, with no hook to intercept only some of them based on the type.Solution
Add a
shouldInterceptpredicate (Func<Type, bool>) to bothEnableClassInterceptorsandEnableInterfaceInterceptors, evaluated against the implementation type. This matches the shape suggested on the issue:All new overloads are additive — every existing signature is unchanged, preserving binary compatibility (this is a strong-named assembly).
EnableInterfaceInterceptors(Func<Type,bool>)and(ProxyGenerationOptions?, Func<Type,bool>?)EnableClassInterceptors(concrete & scanning)(Func<Type,bool>)and(ProxyGenerationOptions, Func<Type,bool>?, params Type[])Semantics
The predicate is evaluated at different times for the two styles, because they work differently:
ctx.Instance.GetType()). When it returnsfalsethe instance is returned without a proxy (and the interface-only guard is skipped, since no proxy is attempted).falsethe registration is left untouched (real type, no proxy). Per-instance conditionality is not possible for class interception by construction.Both behaviors are covered by XML docs and code comments.
Tests
New fixture
ConditionalInterceptionFixture(7 tests):Full suite green on net8.0 and net10.0; clean Release build (warnings-as-errors + analyzers) and
dotnet formatclean.