ServiceScan.SourceGenerator icon indicating copy to clipboard operation
ServiceScan.SourceGenerator copied to clipboard

Generate implementationFactory

Open Dreamescaper opened this issue 1 year ago • 1 comments

We could generate registrations with implementationFactory parameter:

services.AddTransient<IService>(s => new MyService());

or

services.AddTransient<IService>(s => new MyService(s.GetRequieredService<ParameterType>()));

It should be more performant, as it doesn't involve reflection calls. It has downsides too, though.

  1. Implementation is harder, and we'd have to mirror M.E.DI behavior exactly, which is not straightforward
  2. ServiceProvider validation would be disabled, i.e. it will not check that all the required services are registered on container build.

Maybe it makes sense to make it configurable.

Dreamescaper avatar Jun 13 '24 11:06 Dreamescaper

I've made a small benchmark, and I can see no performance benefits to use services.AddTransient<IService>(s => new MyService()); VS services.AddTransient<IService, MyService>();

Maybe for other platforms, like wasm? Anyway, doesn't make a point to implement it until there is evidence for it to be better.

Dreamescaper avatar Aug 21 '24 09:08 Dreamescaper