autofac/Autofac

Integration with C#12 "interceptors"

开放

#1,385 创建于 2023年7月19日

 (2 条评论) (0 个反应) (0 位负责人)C# (827 个派生)batch import
enhancementhelp wanted

仓库指标

星标
 (4,327 个星标)
PR 合并指标
 (平均合并 3小时 24分钟) (30 天内合并 10 个 PR)

描述

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:

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:

builder.RegisterType<MyService>()

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

builder.Register<MyService>(c => new MyService(c.Resolve<IFoo>()))

(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 :-) 👋

贡献者指南