1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 22:55:42 +00:00

feat: add service/impl register for AddHostedSingleton

This commit is contained in:
Oliver Booth 2023-08-21 17:21:58 +01:00
parent b977b7a4ec
commit 9b995524dd
Signed by: oliverbooth
GPG Key ID: B89D139977693FED
2 changed files with 31 additions and 0 deletions

View File

@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- X10D: Added `TextWriter.WriteLineNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`. - X10D: Added `TextWriter.WriteLineNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`.
- X10D: Added `TextWriter.WriteLineNoAlloc(ulong[, ReadOnlySpan<char>[, IFormatProvider]])`. - X10D: Added `TextWriter.WriteLineNoAlloc(ulong[, ReadOnlySpan<char>[, IFormatProvider]])`.
- X10D: Added `string.ConcatIf`. - X10D: Added `string.ConcatIf`.
- X10D.Hosting: Added support for service/implementation registration with `AddHostedSingleton`.
- X10D.Unity: Added `RaycastHit.GetComponent` and `RaycastHit.TryGetComponent`. - X10D.Unity: Added `RaycastHit.GetComponent` and `RaycastHit.TryGetComponent`.
- X10D.Unity: Added `DebugUtility.DrawFunction`, and `DebugUtility.DrawUnjoinedPolyhedron` on which it relies. - X10D.Unity: Added `DebugUtility.DrawFunction`, and `DebugUtility.DrawUnjoinedPolyhedron` on which it relies.

View File

@ -21,6 +21,21 @@ public static class ServiceCollectionExtensions
return services.AddSingleton<IHostedService, TService>(provider => provider.GetRequiredService<TService>()); return services.AddSingleton<IHostedService, TService>(provider => provider.GetRequiredService<TService>());
} }
/// <summary>
/// Adds an <see cref="IHostedService" /> registration for the given type, while simultaneously adding it as a singleton.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddHostedSingleton<TService, TImplementation>(this IServiceCollection services)
where TService : class, IHostedService
where TImplementation : class, TService
{
services.AddSingleton<TService, TImplementation>();
return services.AddSingleton<IHostedService, TService>(provider => provider.GetRequiredService<TService>());
}
/// <summary> /// <summary>
/// Adds an <see cref="IHostedService" /> registration for the given type, while simultaneously adding it as a singleton. /// Adds an <see cref="IHostedService" /> registration for the given type, while simultaneously adding it as a singleton.
/// </summary> /// </summary>
@ -32,4 +47,19 @@ public static class ServiceCollectionExtensions
services.AddSingleton(type); services.AddSingleton(type);
return services.AddSingleton(provider => (IHostedService)provider.GetRequiredService(type)); return services.AddSingleton(provider => (IHostedService)provider.GetRequiredService(type));
} }
/// <summary>
/// Adds an <see cref="IHostedService" /> registration for the given type, while simultaneously adding it as a singleton.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="serviceType">The type of the service to register.</param>
/// <param name="implementationType">The type of the implementation to use.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddHostedSingleton(this IServiceCollection services,
Type serviceType,
Type implementationType)
{
services.AddSingleton(serviceType, implementationType);
return services.AddSingleton(provider => (IHostedService)provider.GetRequiredService(serviceType));
}
} }