-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Keyed Services Support to Dependency Injection (#87183)
- Loading branch information
1 parent
33e0669
commit 138cb59
Showing
35 changed files
with
3,353 additions
and
184 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...ndencyInjection.Abstractions/ref/Microsoft.Extensions.DependencyInjection.Abstractions.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
413 changes: 413 additions & 0 deletions
413
...dencyInjection.Abstractions/src/Extensions/ServiceCollectionDescriptorExtensions.Keyed.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...s/Microsoft.Extensions.DependencyInjection.Abstractions/src/FromKeyedServicesAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...raries/Microsoft.Extensions.DependencyInjection.Abstractions/src/IKeyedServiceProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() => "*"; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...crosoft.Extensions.DependencyInjection.Abstractions/src/IServiceProviderIsKeyedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
/// <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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.