Scrutor icon indicating copy to clipboard operation
Scrutor copied to clipboard

Register services with ImplementationFactory

Open LeonxPRO opened this issue 2 years ago • 4 comments

Hello! First, thanks for a Scrutor) Is it any way for register services with ImplementationFactory, like in native IServiceCollectionmethod ? AddSingleton<TService>(Func<IServiceProvider, TService> implementationFactory) I imagine that like:

IServiceCollection collection = _serviceCollection.Scan(scan => scan.FromAssemblies(ass)
.AddClasses(classes => classes.AssignableTo<TService>(), false)
.AsImplementedInterfaces()
.WithSingletonLifetime(Func<IServiceProvider, TService> implementationFactory))

instead WithSingletonLifetime()

LeonxPRO avatar Aug 31 '23 13:08 LeonxPRO

Hello @LeonxPRO! 👋🏻

The issue with providing a factory like that is that it needs to work for all subtypes of TService, so you it would also have to know the specific type that it's trying to register in the callback.

I'd love to know a bit more about what you're trying to do. Do you have a concrete example?

khellang avatar Sep 01 '23 07:09 khellang

Sample is pretty straighforward, just have a bunch of classes that have the same interface for wich you want to have a property filled during creation.

.WithSingletonLifetime(Func<IServiceProvider, TService> implementationFactory)) would be awesome :)

carlhoogstoel avatar Oct 18 '23 06:10 carlhoogstoel

Any progress on this one? Basically it would be nice to get a callback for instance creation; (IServiceProvider, Type concreteType)

kdrcetintas avatar Jul 27 '24 15:07 kdrcetintas