-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add Keyed Services Support to Dependency Injection #87183
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
Changes from 40 commits
02b1c4d
c26941c
e165c2e
6fc500f
bdc6bfb
b0152ac
57c62f7
e741b3e
3c228f1
f84c8a1
ddc7459
18f4879
3fa39b0
8b58809
0d17ace
68c3d71
38574c6
24e1e72
8111510
b4eb5f2
7cd4378
c8e397c
840d3af
8ccac9e
2558c99
aea27cb
afeaed1
f2beeb2
115c3ef
840d285
8f4e6f3
845d914
a5705dc
460ecb4
72b2b5d
17b9312
ccbedc6
db15ad7
ec9b7ef
3a0a306
44e2fea
f7680e4
d9a0270
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection | ||
| { | ||
| [AttributeUsage(AttributeTargets.Parameter)] | ||
| public class FromKeyedServicesAttribute : Attribute | ||
|
||
| { | ||
| public FromKeyedServicesAttribute(object key) => Key = key; | ||
|
|
||
| public object Key { get; } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection | ||
| { | ||
| public interface IKeyedServiceProvider : IServiceProvider | ||
| { | ||
| /// <summary> | ||
| /// Gets the service object of the specified type. | ||
| /// </summary> | ||
| /// <param name="serviceType">An object that specifies the type of service object to get.</param> | ||
| /// <param name="serviceKey">An object that specifies the key of service object to get.</param> | ||
| /// <returns> A service object of type serviceType. -or- null if there is no service object of type serviceType.</returns> | ||
| object? GetKeyedService(Type serviceType, object? serviceKey); | ||
|
|
||
| /// <summary> | ||
| /// Gets service of type <paramref name="serviceType"/> from the <see cref="IServiceProvider"/> implementing | ||
| /// this interface. | ||
| /// </summary> | ||
| /// <param name="serviceType">An object that specifies the type of service object to get.</param> | ||
| /// <param name="serviceKey">The <see cref="ServiceDescriptor.ServiceKey"/> of the service.</param> | ||
| /// <returns>A service object of type <paramref name="serviceType"/>. | ||
| /// Throws an exception if the <see cref="IServiceProvider"/> cannot create the object.</returns> | ||
| object GetRequiredKeyedService(Type serviceType, object? serviceKey); | ||
| } | ||
|
|
||
| public static class KeyedService | ||
| { | ||
| public static object AnyKey { get; } = new AnyKeyObj(); | ||
|
|
||
| private sealed class AnyKeyObj | ||
| { | ||
| public override string? ToString() => "*"; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection | ||
| { | ||
| public interface IServiceProviderIsKeyedService : IServiceProviderIsService | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for making the API changes I suggested. As much as I like them, we should at least send an API review email to make sure any API adjustments are approved before merging. Or better yet discuss them in tomorrow's meeting. |
||
| { | ||
| /// <summary> | ||
| /// Determines if the specified service type is available from the <see cref="IServiceProvider"/>. | ||
| /// </summary> | ||
| /// <param name="serviceType">An object that specifies the type of service object to test.</param> | ||
| /// <param name="serviceKey">The <see cref="ServiceDescriptor.ServiceKey"/> of the service.</param> | ||
| /// <returns>true if the specified service is a available, false if it is not.</returns> | ||
| bool IsKeyedService(Type serviceType, object? serviceKey); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.