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

DI resolve parameter using same ServiceKey #99084

Open
dferretti opened this issue Feb 28, 2024 · 7 comments · May be fixed by #102160
Open

DI resolve parameter using same ServiceKey #99084

dferretti opened this issue Feb 28, 2024 · 7 comments · May be fixed by #102160
Assignees
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-DependencyInjection
Milestone

Comments

@dferretti
Copy link

I have a few classes that are set up using keyed services in DI. Currently I have to do one of the following:

// classes are nice and clean
class A() { }
class B(A a) { public A A { get; } = a; }

// but registration is less convenient
services.AddKeyedSingleton<A>("first");
services.AddKeyedSingleton<B>("first", (sp, key) => new B(sp.GetRequiredKeyedService<A>(key)));

or

// injecting IServiceProvider doesn't feel graet
class A() { }
class B([ServiceKey] string name, IServiceProvider sp) { public A A { get; } = sp.GetRequiredKeyedService<A>(name); }

// but registration is cleaner
services.AddKeyedSingleton<A>("first");
services.AddKeyedSingleton<B>("first");

I would like to be able to do something like this instead:

// classes are still fairly clean
class A() { }
class B([FromKeyedServices(something)] A a) { public A A { get; } = a; }

// registration is still clean
services.AddKeyedSingleton<A>("first");
services.AddKeyedSingleton<B>("first");

Not sure exactly what that would look like, a different attribute or something maybe. Or maybe some sentinel value to use in FromKeyedServices. But the idea is being able to say "from keyed services, but using the value that ServiceKeyAttribute would return"

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Feb 28, 2024
@ghost
Copy link

ghost commented Feb 28, 2024

Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection
See info in area-owners.md if you want to be subscribed.

Issue Details

I have a few classes that are set up using keyed services in DI. Currently I have to do one of the following:

// classes are nice and clean
class A() { }
class B(A a) { public A A { get; } = a; }

// but registration is less convenient
services.AddKeyedSingleton<A>("first");
services.AddKeyedSingleton<B>("first", (sp, key) => new B(sp.GetRequiredKeyedService<A>(key)));

or

// injecting IServiceProvider doesn't feel graet
class A() { }
class B([ServiceKey] string name, IServiceProvider sp) { public A A { get; } = sp.GetRequiredKeyedService<A>(name); }

// but registration is cleaner
services.AddKeyedSingleton<A>("first");
services.AddKeyedSingleton<B>("first");

I would like to be able to do something like this instead:

// classes are still fairly clean
class A() { }
class B([FromKeyedServices(something)] A a) { public A A { get; } = a; }

// registration is still clean
services.AddKeyedSingleton<A>("first");
services.AddKeyedSingleton<B>("first");

Not sure exactly what that would look like, a different attribute or something maybe. Or maybe some sentinel value to use in FromKeyedServices. But the idea is being able to say "from keyed services, but using the value that ServiceKeyAttribute would return"

Author: dferretti
Assignees: -
Labels:

untriaged, area-Extensions-DependencyInjection

Milestone: -

@Oskarsson
Copy link

Oskarsson commented Mar 5, 2024

I would see this as very helpful and I've seen that Aspire has the same "problem". They went with the first workaround

@steveharter steveharter self-assigned this Mar 7, 2024
@steveharter
Copy link
Member

cc @benjaminpetit

@drolevar
Copy link

Indeed a very useful feature.

I'd suggest adding a new attribute, something like InheritServiceKeyAttribute and modifying CallSiteFactory::CreateArgumentCallSites so that it follows the same logic as for FromKeyedServicesAttribute, only with serviceIdentifier.ServiceKey instead of keyed.Key.

@drolevar
Copy link

@dferretti I've provided a PR #102160 to address this issue. But apparently it has to go through API review. Could you please update the issue with a proposed API change as per referenced PR and mark it as api-suggestion? Or alternatively I can make a new issue and reference yours.

@pinkfloydx33
Copy link

Just ran into needing this as well. Was able to provide a factory to workaround the need, but it gets messy with multiple levels or multiple dependencies. Would be nice to have it built in

@steveharter
Copy link
Member

PTAL @benjaminpetit

@steveharter steveharter added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration api-suggestion Early API idea and discussion, it is NOT ready for implementation and removed untriaged New issue has not been triaged by the area owner needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration labels May 15, 2024
@steveharter steveharter added this to the 9.0.0 milestone May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Extensions-DependencyInjection
Projects
None yet
5 participants