diff --git a/DigitalData.Core.Abstractions/ServiceProviderExtensions.cs b/DigitalData.Core.Abstractions/ServiceProviderExtensions.cs new file mode 100644 index 0000000..a12f5ca --- /dev/null +++ b/DigitalData.Core.Abstractions/ServiceProviderExtensions.cs @@ -0,0 +1,31 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + +namespace DigitalData.Core.Abstractions; + +/// +/// Extension methods for to retrieve configuration options. +/// +public static class ServiceProviderExtensions +{ + /// + /// Retrieves an instance of from the . + /// If the options are not registered, returns null. + /// + /// The type of the options to retrieve. + /// The service provider instance. + /// An instance of or null if not found. + public static TOptions? GetOptions(this IServiceProvider service) where TOptions : class + => service.GetService>()?.Value; + + /// + /// Retrieves an instance of from the . + /// Throws an exception if the options are not registered. + /// + /// The type of the options to retrieve. + /// The service provider instance. + /// An instance of . + /// Thrown when the options are not registered. + public static TOptions GetRequiredOptions(this IServiceProvider service) where TOptions : class + => service.GetRequiredService>().Value; +}