2020-12-24 09:50:05 +11:00
using System ;
2020-10-30 11:16:17 +00:00
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.DependencyInjection.Extensions ;
2021-02-18 11:06:02 +01:00
using Umbraco.Cms.Core.Composing ;
2020-10-30 11:16:17 +00:00
2021-02-18 11:06:02 +01:00
namespace Umbraco.Extensions
2020-10-30 11:16:17 +00:00
{
public static class ServiceCollectionExtensions
{
2022-01-13 08:48:38 +00:00
/// <summary>
/// Adds a service of type <typeparamref name="TService"/> with an implementation type of <typeparamref name="TImplementing"/> to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService"/>.
/// </remarks>
public static void AddUnique < TService , TImplementing > (
this IServiceCollection services ,
ServiceLifetime lifetime = ServiceLifetime . Singleton )
2020-10-30 11:16:17 +00:00
where TService : class
where TImplementing : class , TService
2022-01-13 08:48:38 +00:00
{
services . RemoveAll < TService > ( ) ;
services . Add ( ServiceDescriptor . Describe ( typeof ( TService ) , typeof ( TImplementing ) , lifetime ) ) ;
}
2020-10-30 11:16:17 +00:00
2020-10-30 19:56:26 +01:00
/// <summary>
2022-01-13 08:48:38 +00:00
/// Adds services of types <typeparamref name="TService1"/> & <typeparamref name="TService2"/> with a shared implementation type of <typeparamref name="TImplementing"/> to the specified <see cref="IServiceCollection"/>.
2020-10-30 19:56:26 +01:00
/// </summary>
2022-01-13 08:48:38 +00:00
/// <remarks>
/// Removes all previous registrations for the types <typeparamref name="TService1"/> & <typeparamref name="TService2"/>.
/// </remarks>
public static void AddMultipleUnique < TService1 , TService2 , TImplementing > (
this IServiceCollection services ,
ServiceLifetime lifetime = ServiceLifetime . Singleton )
2020-10-30 19:56:26 +01:00
where TService1 : class
where TService2 : class
where TImplementing : class , TService1 , TService2
{
2022-01-13 08:48:38 +00:00
services . AddUnique < TService1 , TImplementing > ( lifetime ) ;
services . AddUnique < TService2 > ( factory = > ( TImplementing ) factory . GetRequiredService < TService1 > ( ) , lifetime ) ;
2020-10-30 19:56:26 +01:00
}
2022-01-10 16:03:33 +00:00
// TODO(V11): Remove this function.
[Obsolete("This method is functionally equivalent to AddSingleton<TImplementing>() please use that instead.")]
2020-10-30 11:16:17 +00:00
public static void AddUnique < TImplementing > ( this IServiceCollection services )
where TImplementing : class
2022-01-13 08:48:38 +00:00
{
services . RemoveAll < TImplementing > ( ) ;
services . AddSingleton < TImplementing > ( ) ;
}
2020-10-30 11:16:17 +00:00
/// <summary>
2022-01-13 08:48:38 +00:00
/// Adds a service of type <typeparamref name="TService"/> with an implementation factory method to the specified <see cref="IServiceCollection"/>.
2020-10-30 11:16:17 +00:00
/// </summary>
2022-01-13 08:48:38 +00:00
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService"/>.
/// </remarks>
public static void AddUnique < TService > (
this IServiceCollection services ,
Func < IServiceProvider , TService > factory ,
ServiceLifetime lifetime = ServiceLifetime . Singleton )
2020-10-30 11:16:17 +00:00
where TService : class
2022-01-13 08:48:38 +00:00
{
services . RemoveAll < TService > ( ) ;
services . Add ( ServiceDescriptor . Describe ( typeof ( TService ) , factory , lifetime ) ) ;
}
2020-10-30 11:16:17 +00:00
/// <summary>
2022-01-13 08:48:38 +00:00
/// Adds a singleton service of the type specified by <paramref name="serviceType"/> to the specified <see cref="IServiceCollection"/>.
2020-10-30 11:16:17 +00:00
/// </summary>
2022-01-13 08:48:38 +00:00
/// <remarks>
/// Removes all previous registrations for the type specified by <paramref name="serviceType"/>.
/// </remarks>
2020-10-30 11:16:17 +00:00
public static void AddUnique ( this IServiceCollection services , Type serviceType , object instance )
2022-01-13 08:48:38 +00:00
{
services . RemoveAll ( serviceType ) ;
services . AddSingleton ( serviceType , instance ) ;
}
2020-10-30 11:16:17 +00:00
/// <summary>
2022-01-13 08:48:38 +00:00
/// Adds a singleton service of type <typeparamref name="TService"/> to the specified <see cref="IServiceCollection"/>.
2020-10-30 11:16:17 +00:00
/// </summary>
2022-01-13 08:48:38 +00:00
/// <remarks>
/// Removes all previous registrations for the type type <typeparamref name="TService"/>.
/// </remarks>
2020-10-30 11:16:17 +00:00
public static void AddUnique < TService > ( this IServiceCollection services , TService instance )
where TService : class
2022-01-13 08:48:38 +00:00
{
services . RemoveAll < TService > ( ) ;
services . AddSingleton ( instance ) ;
}
2020-10-30 11:16:17 +00:00
2020-12-24 11:46:17 +11:00
internal static IServiceCollection AddLazySupport ( this IServiceCollection services )
2020-10-30 11:16:17 +00:00
{
services . Replace ( ServiceDescriptor . Transient ( typeof ( Lazy < > ) , typeof ( LazyResolve < > ) ) ) ;
return services ;
}
}
}