Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register services with ImplementationFactory #211

Open
LeonxPRO opened this issue Aug 31, 2023 · 2 comments
Open

Register services with ImplementationFactory #211

LeonxPRO opened this issue Aug 31, 2023 · 2 comments

Comments

@LeonxPRO
Copy link

LeonxPRO commented Aug 31, 2023

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 LeonxPRO changed the title Collect and register services with ImplementationFactory Register services with ImplementationFactory Aug 31, 2023
@khellang
Copy link
Owner

khellang commented Sep 1, 2023

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?

@carlhoogstoel
Copy link

carlhoogstoel commented Oct 18, 2023

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants