autofac / autofac/Autofac

Integration with C#12 "interceptors"

Open
#1,385 2 comments 0 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
C#
Stars
4.7k
Forks
846
Avg merge
1h 29m
Merged PRs (30d)
1

Description

First, I should say that this is intended as a "heads-up", and not a feature request per se (nor something I am prepared to try implementing), although someone might have fun investigating or implementing it. Please feel free to close without comment :-)

Anyway - interesting thing spotted in [New C# 12 preview features](https://devblogs.microsoft.com/dotnet/new-csharp-12-preview-features/):

> Interceptors
>
> Static dependency graph resolution for dependency injection, where provider.Register() can be intercepted

Since the term "interceptor" is so overloaded, and already has precedent when it comes to IoC, this one's easy to overlook so I thought it'd be worth mentioning here.

An interceptor, in this context, is a compile-time substitution of one method for another at the call site. The mention of static dependency graph resolution is referring to:

```csharp
builder.RegisterType()
```

at compile-time with a call to an equivalent method:

```csharp
builder.Register(c => new MyService(c.Resolve()))
```

(It's not possible to directly substitute `RegisterType` for the real `Register` as I'm doing here - the substitute method needs to be defined elsewhere using the new language features.).

By making these substitutions, it's no longer necessary for reflection metadata to be preserved for `MyService`, so the code will work in AoT scenarios where metadata is trimmed/discarded, and potentially run faster.

Seems like a very interesting feature to explore, from Autofac's point of view :-) 👋

Contributor guide

Open the contributing guide

Research direction

Start with the linked C# 12 preview-features announcement and the issue's Autofac examples for RegisterType and Register. Investigate whether compile-time interceptors could support static dependency-graph resolution and trimmed AoT scenarios; no repository file, test, or concrete acceptance criteria is identified in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.