#if NET
namespace DigitalData.Core.Abstractions;
///
/// A deferred implementation of that allows the instance
/// to be provided at a later time via a factory callback.
///
///
/// This is particularly useful when service registration requires an instance,
/// but the service provider is not yet available at registration time.
/// By using , a service can safely resolve dependencies once
/// the application's service provider has been built, ensuring a single consistent scope.
///
public class DeferredServiceProvider : IServiceProvider
{
private Lazy? _serviceProvider;
///
/// Sets the factory that will be used to lazily initialize the instance.
///
public Func Factory
{
set => _serviceProvider = new(value);
}
///
/// Retrieves the requested service object from the deferred .
///
/// The type of service object to retrieve.
/// The requested service object, or null if the service is not available.
///
/// Thrown if the service provider factory has not been set before calling .
///
public object? GetService(Type serviceType)
{
if (_serviceProvider is null)
throw new InvalidOperationException("The service provider has not been initialized. Make sure 'Factory' is set before calling 'GetService'.");
return _serviceProvider.Value.GetService(serviceType);
}
}
#endif