Skip to content

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