2024-06-27 16:02:41 +02:00

25 lines
1.1 KiB
C#

using DigitalData.Core.Abstractions.Client;
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.Core.Client
{
public static class ServiceFactory
{
private static readonly IServiceCollection _services = new ServiceCollection();
private static readonly Lazy<IServiceProvider> _lazyProvider = new(Build);
public static IServiceCollection Services => !_lazyProvider.IsValueCreated
? _services
: throw new InvalidOperationException(
"Service provider has already been created. " +
"Further modifications to the service collection are not allowed. " +
"This is to ensure that the dependency injection container remains in a consistent state.");
private static IServiceProvider Build() => _services.BuildServiceProvider();
public static T Provide<T>() where T : notnull => _lazyProvider.Value.GetRequiredService<T>();
public static IHttpClientService<TOptions> ProvideHttpClientService<TOptions>() where TOptions : notnull
=> _lazyProvider.Value.GetRequiredService<IHttpClientService<TOptions>>();
}
}