Skip to content

[API Proposal]: Use Source Generators in default DependencyInjector #82679

Description

@Rabadash8820

Background and motivation

Source generators are a pretty handy replacement for lots of traditionally reflection-based systems, like dependency injection and IoC containers. Rather than looking up constructors and invoking them via reflection, source generator-based libraries can generate classes at build time that call specific constructors explicitly, dramatically improving the speed of service resolution at runtime. Several community libraries are already pursuing this approach for DI, including:

Unfortunately, all of these libraries are still pretty immature, with little exposure in the community, meaning there are few tutorials/articles written about them, spotty support, etc. Some of them may become more popular and outcompete the mainstays like Autofac, Ninject, Zenject, etc., but for now, their adoption is a hard sell, despite the performance benefits.

I've tried looking through the source code of the Microsoft.Extensions.DependencyInjection namespace, but it's still pretty over my head. It's unclear to me whether those implementations are already using source generators, but I'm pretty sure I read elsewhere that they're not. Seems to me that the default DI system provided by .NET should be as performant as possible, and source generators are an ideal way to achieve this.

API Proposal

This less of an API Proposal, and more of an "implementation suggestion". If the IoC containers in Microsoft.Extensions.DependencyInjection are not already using source generators, then I think that they should be!

API Usage

APIs will probably look/feel the same, but just generate a type with explicit constructor calls at build time.

Alternative Designs

The existing design. :P

Risks

Would probably involve a pretty significant rewrite of types in Microsoft.Extensions.DependencyInjection, which could take up a lot of time/resources.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions