From 9d36ced82fd85c9c3161555f2bcdfbf933de5083 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 28 Apr 2025 16:10:31 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Hinzuf=C3=BCgen=20von=20Erweiterungsmet?= =?UTF-8?q?hoden=20zum=20Abrufen=20von=20Konfigurationsoptionen=20von=20IS?= =?UTF-8?q?erviceProvider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Implementiert GetOptions zum Abrufen von Optionen oder null. - Implementiert GetRequiredOptions für den Abruf von Optionen oder das Auslösen einer Ausnahme. --- .../ServiceProviderExtensions.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 DigitalData.Core.Abstractions/ServiceProviderExtensions.cs 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; +}