ServiceScan.SourceGenerator
ServiceScan.SourceGenerator copied to clipboard
Generate implementationFactory
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.
- Implementation is harder, and we'd have to mirror M.E.DI behavior exactly, which is not straightforward
- 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.
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.