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

Extend ILifetimeSelector with a new WithLifetime method that acepts a Func<Type, ServiceLifetime> parameter to map implementation type to service lifetime #240

Merged
merged 1 commit into from
Dec 23, 2024

Conversation

ripebananas
Copy link

Hello,

Thanks for the great library. One limitation currently stops be from fully adopting it in the project currently I work on (or maybe I am missing something and don't know how to do it).

Would you consider extending the ILifetimeSelector with a new WithLifetime method that accepts a Func<Type, ServiceLifetime> parameter, i.e. a function that maps an implementation type to a service lifetime?
This will allow registering services marked with a marker interface IAppService, but that have different lifetimes, something like this:

services.Scan(typeSourceSelector =>
{
    var assemblies = typeof(IAppService).Assembly;

    var serviceTypeSelector = typeSourceSelector
        .FromAssemblies(assemblies)
        .AddClasses(x => x.AssignableTo(typeof(IAppService)))
        .AsSelfWithInterfaces(x => typeof(IAppService).IsAssignableFrom(x) && typeof(IAppService) != x)
        .WithLifetime(implementationType => GetServiceLifeTime(implementationType));
});

Thanks

…cepts a `Func<Type, ServiceLifetime>` parameter, i.e. a function that maps the implementation type to the service lifetime
@khellang khellang merged commit e77e0f2 into khellang:master Dec 23, 2024
@khellang
Copy link
Owner

Thank you @ripebananas! This is a nice feature! 🙏🏻

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

Successfully merging this pull request may close these issues.

2 participants