mirror of https://github.com/abpframework/abp.git
committed by
GitHub
44 changed files with 259 additions and 113 deletions
@ -0,0 +1,31 @@ |
|||
using System; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection; |
|||
|
|||
/// <summary>
|
|||
/// Extensions for working with <see cref="ServiceDescriptor"/>.
|
|||
/// </summary>
|
|||
public static class ServiceDescriptorExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Normalizes the implementation instance data between keyed and not keyed services.
|
|||
/// </summary>
|
|||
/// <param name="descriptor">
|
|||
/// The <see cref="ServiceDescriptor"/> to normalize.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The appropriate implementation instance from the service descriptor.
|
|||
/// </returns>
|
|||
public static object? NormalizedImplementationInstance(this ServiceDescriptor descriptor) => descriptor.IsKeyedService ? descriptor.KeyedImplementationInstance : descriptor.ImplementationInstance; |
|||
|
|||
/// <summary>
|
|||
/// Normalizes the implementation type data between keyed and not keyed services.
|
|||
/// </summary>
|
|||
/// <param name="descriptor">
|
|||
/// The <see cref="ServiceDescriptor"/> to normalize.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The appropriate implementation type from the service descriptor.
|
|||
/// </returns>
|
|||
public static Type? NormalizedImplementationType(this ServiceDescriptor descriptor) => descriptor.IsKeyedService ? descriptor.KeyedImplementationType : descriptor.ImplementationType; |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using System; |
|||
using Volo.Abp; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection; |
|||
|
|||
public static class ServiceProviderKeyedServiceExtensions |
|||
{ |
|||
public static object? GetKeyedService(this IServiceProvider provider, Type serviceType, object? serviceKey) |
|||
{ |
|||
Check.NotNull(provider, nameof(provider)); |
|||
|
|||
if (provider is IKeyedServiceProvider keyedServiceProvider) |
|||
{ |
|||
return keyedServiceProvider.GetKeyedService(serviceType, serviceKey); |
|||
} |
|||
|
|||
throw new InvalidOperationException("This service provider doesn't support keyed services."); |
|||
} |
|||
} |
|||
@ -1,14 +1,15 @@ |
|||
using System; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.DependencyInjection; |
|||
|
|||
public interface ICachedServiceProviderBase : IServiceProvider |
|||
public interface ICachedServiceProviderBase : IKeyedServiceProvider |
|||
{ |
|||
T GetService<T>(T defaultValue); |
|||
|
|||
|
|||
object GetService(Type serviceType, object defaultValue); |
|||
|
|||
T GetService<T>(Func<IServiceProvider, object> factory); |
|||
|
|||
object GetService(Type serviceType, Func<IServiceProvider, object> factory); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,54 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.DependencyInjection; |
|||
|
|||
/// <summary>
|
|||
/// https://github.com/dotnet/runtime/blob/release/8.0/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceIdentifier.cs#L9
|
|||
/// </summary>
|
|||
public readonly struct ServiceIdentifier : IEquatable<ServiceIdentifier> |
|||
{ |
|||
public object? ServiceKey { get; } |
|||
|
|||
public Type ServiceType { get; } |
|||
|
|||
public ServiceIdentifier(Type serviceType) |
|||
{ |
|||
ServiceType = serviceType; |
|||
} |
|||
|
|||
public ServiceIdentifier(object? serviceKey, Type serviceType) |
|||
{ |
|||
ServiceKey = serviceKey; |
|||
ServiceType = serviceType; |
|||
} |
|||
|
|||
public bool Equals(ServiceIdentifier other) |
|||
{ |
|||
if (ServiceKey == null && other.ServiceKey == null) |
|||
{ |
|||
return ServiceType == other.ServiceType; |
|||
} |
|||
else if (ServiceKey != null && other.ServiceKey != null) |
|||
{ |
|||
return ServiceType == other.ServiceType && ServiceKey.Equals(other.ServiceKey); |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
public override bool Equals(object? obj) |
|||
{ |
|||
return obj is ServiceIdentifier && Equals((ServiceIdentifier)obj); |
|||
} |
|||
|
|||
public override int GetHashCode() |
|||
{ |
|||
if (ServiceKey == null) |
|||
{ |
|||
return ServiceType.GetHashCode(); |
|||
} |
|||
unchecked |
|||
{ |
|||
return (ServiceType.GetHashCode() * 397) ^ ServiceKey.GetHashCode(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue