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 · 4 comments
Open

Register services with ImplementationFactory #211

LeonxPRO opened this issue Aug 31, 2023 · 4 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 :)

@kdrcetintas
Copy link

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

@pfeigl
Copy link

pfeigl commented Sep 24, 2024

We also would have loved to register a bunch of classes with an ImplementationFactory. The usecase for us is pretty straight forward: The classes are from a external library and do not really support DI as we need to call a method on each of the classes right after initialization.

Find some sample code attached and how we use it currently:

    private static IServiceCollection AddReportPlugin<TPlugin>(this IServiceCollection serviceCollection) where TPlugin : ReportPluginBase, IReportPlugin, new()
    {
        serviceCollection.AddScoped<IReportPlugin, TPlugin>(sp =>
        {
            var plugin = new TPlugin();
            var pluginParameter = sp.GetRequiredService<IPluginParameters>();
            plugin.Initialize(pluginParameter);
            return plugin;
        });

        return serviceCollection;
    }

// ---

serviceCollection.AddReportPlugin<ExternalPlugin>()

Beeing able to simple search for all IReportPlugin implementations and calling AddReportPlugin as factory for each of them would help alot.

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

5 participants