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
2022-06-07 15:28:38 +02:00
namespace Umbraco.Extensions ;
public static class ServiceCollectionExtensions
2020-10-30 11:16:17 +00:00
{
2022-06-07 15:28:38 +02: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 )
where TService : class
where TImplementing : class , TService = >
AddUnique < TService , TImplementing > ( services , ServiceLifetime . Singleton ) ;
2022-02-21 12:49:17 +01:00
2022-06-07 15:28:38 +02: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 )
where TService : class
where TImplementing : class , TService
2020-10-30 11:16:17 +00:00
{
2022-06-07 15:28:38 +02:00
services . RemoveAll < TService > ( ) ;
services . Add ( ServiceDescriptor . Describe ( typeof ( TService ) , typeof ( TImplementing ) , lifetime ) ) ;
}
2022-02-21 12:49:17 +01:00
2022-06-07 15:28:38 +02:00
/// <summary>
/// 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" />.
/// </summary>
/// <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 )
where TService1 : class
2022-07-11 14:07:08 +02:00
where TService2 : class
where TImplementing : class , TService1 , TService2
= > services . AddMultipleUnique < TService1 , TService2 , TImplementing > ( ServiceLifetime . Singleton ) ;
2020-10-30 11:16:17 +00:00
2022-07-11 14:07:08 +02:00
/// <summary>
/// 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"/>.
/// </summary>
/// <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 )
2022-06-07 15:28:38 +02:00
where TService1 : class
where TService2 : class
where TImplementing : class , TService1 , TService2
{
services . AddUnique < TService1 , TImplementing > ( lifetime ) ;
services . AddUnique < TService2 > ( factory = > ( TImplementing ) factory . GetRequiredService < TService1 > ( ) , lifetime ) ;
}
2020-10-30 19:56:26 +01:00
2022-09-19 16:37:24 +02:00
/// <summary>
2022-06-07 15:28:38 +02:00
/// Adds a service of type <typeparamref name="TService" /> with an implementation factory method to the specified
/// <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type <typeparamref name="TService" />.
/// </remarks>
public static void AddUnique < TService > (
this IServiceCollection services ,
Func < IServiceProvider , TService > factory )
where TService : class
2022-07-11 14:07:08 +02:00
= > services . AddUnique ( factory , ServiceLifetime . Singleton ) ;
2020-10-30 11:16:17 +00:00
2022-07-11 14:07:08 +02:00
/// <summary>
/// Adds a service of type <typeparamref name="TService"/> with an implementation factory method to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <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 )
2022-06-07 15:28:38 +02:00
where TService : class
{
services . RemoveAll < TService > ( ) ;
services . Add ( ServiceDescriptor . Describe ( typeof ( TService ) , factory , lifetime ) ) ;
}
2020-10-30 11:16:17 +00:00
2022-06-07 15:28:38 +02:00
/// <summary>
/// Adds a singleton service of the type specified by <paramref name="serviceType" /> to the specified
/// <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type specified by <paramref name="serviceType" />.
/// </remarks>
public static void AddUnique ( this IServiceCollection services , Type serviceType , object instance )
{
services . RemoveAll ( serviceType ) ;
services . AddSingleton ( serviceType , instance ) ;
}
2020-10-30 11:16:17 +00:00
2022-06-07 15:28:38 +02:00
/// <summary>
/// Adds a singleton service of type <typeparamref name="TService" /> to the specified
/// <see cref="IServiceCollection" />.
/// </summary>
/// <remarks>
/// Removes all previous registrations for the type type <typeparamref name="TService" />.
/// </remarks>
public static void AddUnique < TService > ( this IServiceCollection services , TService instance )
where TService : class
{
services . RemoveAll < TService > ( ) ;
services . AddSingleton ( instance ) ;
}
2020-10-30 11:16:17 +00:00
2022-06-07 15:28:38 +02:00
internal static IServiceCollection AddLazySupport ( this IServiceCollection services )
{
services . Replace ( ServiceDescriptor . Transient ( typeof ( Lazy < > ) , typeof ( LazyResolve < > ) ) ) ;
return services ;
2020-10-30 11:16:17 +00:00
}
}