using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Umbraco.Cms.Core.Composing;
namespace Umbraco.Extensions;
public static class ServiceCollectionExtensions
{
///
/// Adds a service of type with an implementation type of
/// to the specified .
///
///
/// Removes all previous registrations for the type .
///
///
/// A reference to this instance after the operation has completed.
///
public static IServiceCollection AddUnique(
this IServiceCollection services)
where TService : class
where TImplementing : class, TService =>
AddUnique(services, ServiceLifetime.Singleton);
///
/// Adds a service of type with an implementation type of
/// to the specified .
///
///
/// Removes all previous registrations for the type .
///
///
/// A reference to this instance after the operation has completed.
///
public static IServiceCollection AddUnique(
this IServiceCollection services,
ServiceLifetime lifetime)
where TService : class
where TImplementing : class, TService
{
services.RemoveAll();
services.Add(ServiceDescriptor.Describe(typeof(TService), typeof(TImplementing), lifetime));
return services;
}
///
/// Adds services of types & with a shared
/// implementation type of to the specified .
///
///
/// Removes all previous registrations for the types &
/// .
///
///
/// A reference to this instance after the operation has completed.
///
public static IServiceCollection AddMultipleUnique(
this IServiceCollection services)
where TService1 : class
where TService2 : class
where TImplementing : class, TService1, TService2
=> services.AddMultipleUnique(ServiceLifetime.Singleton);
///
/// Adds services of types & with a shared implementation type of to the specified .
///
///
/// Removes all previous registrations for the types & .
///
///
/// A reference to this instance after the operation has completed.
///
public static IServiceCollection AddMultipleUnique(
this IServiceCollection services,
ServiceLifetime lifetime)
where TService1 : class
where TService2 : class
where TImplementing : class, TService1, TService2
{
services.AddUnique(lifetime);
services.AddUnique(factory => (TImplementing)factory.GetRequiredService(), lifetime);
return services;
}
///
/// Adds a service of type with an implementation factory method to the specified
/// .
///
///
/// Removes all previous registrations for the type .
///
///
/// A reference to this instance after the operation has completed.
///
public static IServiceCollection AddUnique(
this IServiceCollection services,
Func factory)
where TService : class
=> services.AddUnique(factory, ServiceLifetime.Singleton);
///
/// Adds a service of type with an implementation factory method to the specified .
///
///
/// Removes all previous registrations for the type .
///
///
/// A reference to this instance after the operation has completed.
///
public static IServiceCollection AddUnique(
this IServiceCollection services,
Func factory,
ServiceLifetime lifetime)
where TService : class
{
services.RemoveAll();
services.Add(ServiceDescriptor.Describe(typeof(TService), factory, lifetime));
return services;
}
///
/// Adds a singleton service of the type specified by to the specified
/// .
///
///
/// Removes all previous registrations for the type specified by .
///
///
/// A reference to this instance after the operation has completed.
///
public static IServiceCollection AddUnique(this IServiceCollection services, Type serviceType, object instance)
{
services.RemoveAll(serviceType);
services.AddSingleton(serviceType, instance);
return services;
}
///
/// Adds a singleton service of type to the specified
/// .
///
///
/// Removes all previous registrations for the type type .
///
///
/// A reference to this instance after the operation has completed.
///
public static IServiceCollection AddUnique(this IServiceCollection services, TService instance)
where TService : class
{
services.RemoveAll();
services.AddSingleton(instance);
return services;
}
internal static IServiceCollection AddLazySupport(this IServiceCollection services)
{
services.Replace(ServiceDescriptor.Transient(typeof(Lazy<>), typeof(LazyResolve<>)));
return services;
}
}