Skip to content

Add conditional interception via type predicate (#42)#64

Merged
tillig merged 2 commits into
developfrom
feature/issue-42
Jun 24, 2026
Merged

Add conditional interception via type predicate (#42)#64
tillig merged 2 commits into
developfrom
feature/issue-42

Conversation

@tillig

@tillig tillig commented Jun 24, 2026

Copy link
Copy Markdown
Member

Fixes #42.

Problem

There was no way to apply interception selectively. When using assembly scanning, EnableClassInterceptors/EnableInterfaceInterceptors applied interception to every scanned type, with no hook to intercept only some of them based on the type.

Solution

Add a shouldIntercept predicate (Func<Type, bool>) to both EnableClassInterceptors and EnableInterfaceInterceptors, evaluated against the implementation type. This matches the shape suggested on the issue:

builder.RegisterAssemblyTypes(asm)
       .AsClosedTypesOf(typeof(ICommandHandler<,>))
       .EnableInterfaceInterceptors(t => t.IsDefined(typeof(AuditAttribute)))
       .InterceptedBy(typeof(LoggingInterceptor));

All new overloads are additive — every existing signature is unchanged, preserving binary compatibility (this is a strong-named assembly).

Method New overloads
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:

  • Interface interception is resolve-time middleware, so the predicate is evaluated against the resolved implementation type (ctx.Instance.GetType()). When it returns false the instance is returned without a proxy (and the interface-only guard is skipped, since no proxy is attempted).
  • Class interception rewrites the implementation type to a proxy subclass at registration time, so the predicate is evaluated per type at registration. When it returns false the 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):

  • Applies / skips the proxy for both interface and class interception based on the predicate.
  • Predicate applies per-type under assembly scanning for both styles.
  • Null-registration guard for the new overloads.

Full suite green on net8.0 and net10.0; clean Release build (warnings-as-errors + analyzers) and dotnet format clean.

tillig added 2 commits June 23, 2026 17:06
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

codecov Bot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.71%. Comparing base (e63ef5c) to head (a6fd591).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tillig tillig merged commit f79fa27 into develop Jun 24, 2026
12 checks passed
@tillig tillig deleted the feature/issue-42 branch June 24, 2026 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conditional registration for interceptors

1 participant