1

I want to create an adapter for Microsoft.Extensions.DependencyInjection to move a Type based registrations to compile-time if possible. For instance, in Asp .Net Core it means that I need to get somehow service collection of ImplementationType based descriptors at compile-time.

I don't care about run-time descriptors of ImplementationFactory/Instance, it may be handled by run-time part of container. But Types are known at compile-time, e.g. assemblies are in place and not going to change. Then why not to build/resolve the object-graph for the types at compile-time to speed-up bootstraping time and get a vision on how types are composed.

Btw, the IoC to achieve this is not really important, but I will use DryIocZero/DryIoc

dadhi
  • 4,807
  • 19
  • 25
  • Compile-time? You mean like using Roslyn to generate object graphs that get compiled? – Steven Jun 02 '17 at 05:56
  • Yes, something like that. Tool is not important. For now I am using T4. – dadhi Jun 02 '17 at 05:57
  • I don't think this is possible, since the whole idea of the MS.Ext.DI system is to do things _at runtime_. Building object graphs at compile time means you need to know _at compile time_ what types are registered, but the framework and 3rd party libraries only 'tell' this at runtime. – Steven Jun 02 '17 at 06:02
  • You can do this with your application object graphs of course, as long as you don't send them through the MS.Ext.DI container. But if you do this, I would probably go with Pure DI instead of trying to do this with a 'compile-time container'. – Steven Jun 02 '17 at 06:04
  • @Steven, you are probably right and I kind of predicted this. But wanted to get a confirmation.. or I would say more info on the topic. – dadhi Jun 02 '17 at 06:05
  • You could of course use the runtime information to generate code that you could use the next time you compile, but that seems kind of weird. – Steven Jun 02 '17 at 06:10
  • I think this is not an option. The thing is the Asp .NET Core is discovering Type info, e.g. collecting the descriptors. Imagine, to run this discovery at compile-time, and Off it at run-time. Yes, this is part of Asp framework, and not MS.DI, but anyway. – dadhi Jun 02 '17 at 06:17
  • 1
    > You could of course use the runtime information to generate code that you could use the next time you compile, but that seems kind of weird. ASP.NET actually does this crazy thing all over the place. It's how EF migrations work, MVC pre-compilation etc. You basically need to run the application to get enough information to produce an artifact. – davidfowl Aug 02 '17 at 08:50

0 Answers0