Compile-time DI Container with ASP.Net Core apps

126 Views Asked by At

I'm the author of a compile-time Dependency Injection (DI) container based on Roslyn Source Generator (MrMeeseeks.DIE).

The container is easily applicable to console projects, libraries and UI apps like WPF, because they usually have natural root types which can be determined at compile-time (e.g. Program or App) and they are completely agnostic to DI containers (or let's say "service providers").

However, this doesn't seem to be the case for ASP.Net Core. It looks like it heavily relies on the IServiceProvider interface with its single function public object? GetService (Type serviceType); (link). The issue with that while considering compile-time DI containers is that the function depends on a Type parameter which technically can only be known in runtime. Runtime comes of course after compile-time and therefore is too late for the container to adjust appropriately.

A possible solution that I have in mind is to generate an implementation of IServiceProvider which uses the compile-time DI container. However, for it to work the the types which the GetService function will be called with need to be collectable on compile-time.

Long story short: Given an arbitrary ASP.Net Core app, how can the list of all types, which can be possibly passed to GetService in an arbitrary app run, be determined comprehensively and automatically at compile-time?

Part of the answer probably are:

  • Types deriving from ControllerBase (link)
  • Types of parameters and properties marked with the FromServicesAttributes (link)

I have only little experience with ASP.Net Core yet, so I don't feel confident to compose a complete list of such types. So I'm asking for help here.

0

There are 0 best solutions below