6 Commits

Author SHA1 Message Date
Developer 02
246184165f feat(Core.Abstractions): Upgrade auf 3.6 2025-04-28 16:11:26 +02:00
Developer 02
9d36ced82f feat: Hinzufügen von Erweiterungsmethoden zum Abrufen von Konfigurationsoptionen von IServiceProvider
- Implementiert GetOptions<TOptions> zum Abrufen von Optionen oder null.
- Implementiert GetRequiredOptions<TOptions> für den Abruf von Optionen oder das Auslösen einer Ausnahme.
2025-04-28 16:10:31 +02:00
Developer 02
c81ff2c628 feat(ConfigurationExtension): Hinzufügen der Klasse ConfigurationExtension mit der Methode GetOrDefault zum sichereren Abrufen von Konfigurationen
- Einführung einer neuen statischen Klasse ConfigurationExtension im Namespace DigitalData.Core.Abstractions.
- Hinzufügen der GetOrDefault-Erweiterungsmethode zu IConfiguration, die eine einfachere Abfrage von Konfigurationswerten mit Standardverhalten ermöglicht, wenn diese nicht gefunden werden.
- Aktualisierung der Versionsnummern auf 3.5.0 in den Projektdateien.
2025-04-28 15:44:01 +02:00
Developer 02
653665bb25 refactor(LazyServiceProvider.cs): rename DeferredServiceProvider.cs 2025-04-28 14:58:30 +02:00
Developer 02
17edf605e7 chore(Abstractions): Aktualisiert auf 3.4.4 2025-04-28 14:57:29 +02:00
Developer 02
b41373339e feat(core): DeferredServiceProvider für verzögerte IServiceProvider-Initialisierung hinzugefügt 2025-04-28 14:56:25 +02:00
4 changed files with 104 additions and 3 deletions

View File

@@ -0,0 +1,30 @@
using Microsoft.Extensions.Configuration;
namespace DigitalData.Core.Abstractions
{
/// <summary>
/// Extension methods for the <see cref="IConfiguration"/> interface, providing
/// additional functionality for retrieving configuration values with default behavior.
/// </summary>
public static class ConfigurationExtension
{
/// <summary>
/// Retrieves a configuration value for the specified key, or returns a default value
/// of type <typeparamref name="T"/> if the configuration is not found or the key is null.
/// </summary>
/// <typeparam name="T">The type of the object to retrieve from the configuration.</typeparam>
/// <param name="configuration">The <see cref="IConfiguration"/> instance.</param>
/// <param name="key">The optional key to look for in the configuration. If null, the method
/// retrieves the root configuration.</param>
/// <returns>
/// An instance of <typeparamref name="T"/> populated from the configuration values, or
/// a new instance of <typeparamref name="T"/> if no matching configuration is found.
/// </returns>
public static T GetOrDefault<T>(this IConfiguration configuration, string? key = null)
where T : new()
=> (key is null
? configuration.Get<T>()
: configuration.GetSection(key).Get<T>())
?? new T();
}
}

View File

@@ -0,0 +1,40 @@
namespace DigitalData.Core.Abstractions;
/// <summary>
/// A deferred implementation of <see cref="IServiceProvider"/> that allows the <see cref="IServiceProvider"/> instance
/// to be provided at a later time via a factory callback.
/// </summary>
/// <remarks>
/// This is particularly useful when service registration requires an <see cref="IServiceProvider"/> instance,
/// but the service provider is not yet available at registration time.
/// By using <see cref="DeferredServiceProvider"/>, a service can safely resolve dependencies once
/// the application's service provider has been built, ensuring a single consistent scope.
/// </remarks>
public class DeferredServiceProvider : IServiceProvider
{
private Lazy<IServiceProvider>? _serviceProvider;
/// <summary>
/// Sets the factory that will be used to lazily initialize the <see cref="IServiceProvider"/> instance.
/// </summary>
public Func<IServiceProvider> Factory
{
set => _serviceProvider = new(value);
}
/// <summary>
/// Retrieves the requested service object from the deferred <see cref="IServiceProvider"/>.
/// </summary>
/// <param name="serviceType">The type of service object to retrieve.</param>
/// <returns>The requested service object, or <c>null</c> if the service is not available.</returns>
/// <exception cref="InvalidOperationException">
/// Thrown if the service provider factory has not been set before calling <see cref="GetService"/>.
/// </exception>
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);
}
}

View File

@@ -17,9 +17,9 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackAsTool>False</PackAsTool>
<PackageIcon>core_icon.png</PackageIcon>
<Version>3.4.3</Version>
<AssemblyVersion>3.4.3</AssemblyVersion>
<FileVersion>3.4.3</FileVersion>
<Version>3.6.0</Version>
<AssemblyVersion>3.6.0</AssemblyVersion>
<FileVersion>3.6.0</FileVersion>
</PropertyGroup>
<ItemGroup>

View File

@@ -0,0 +1,31 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace DigitalData.Core.Abstractions;
/// <summary>
/// Extension methods for <see cref="IServiceProvider"/> to retrieve configuration options.
/// </summary>
public static class ServiceProviderExtensions
{
/// <summary>
/// Retrieves an instance of <typeparamref name="TOptions"/> from the <see cref="IServiceProvider"/>.
/// If the options are not registered, returns null.
/// </summary>
/// <typeparam name="TOptions">The type of the options to retrieve.</typeparam>
/// <param name="service">The service provider instance.</param>
/// <returns>An instance of <typeparamref name="TOptions"/> or null if not found.</returns>
public static TOptions? GetOptions<TOptions>(this IServiceProvider service) where TOptions : class
=> service.GetService<IOptions<TOptions>>()?.Value;
/// <summary>
/// Retrieves an instance of <typeparamref name="TOptions"/> from the <see cref="IServiceProvider"/>.
/// Throws an exception if the options are not registered.
/// </summary>
/// <typeparam name="TOptions">The type of the options to retrieve.</typeparam>
/// <param name="service">The service provider instance.</param>
/// <returns>An instance of <typeparamref name="TOptions"/>.</returns>
/// <exception cref="InvalidOperationException">Thrown when the options are not registered.</exception>
public static TOptions GetRequiredOptions<TOptions>(this IServiceProvider service) where TOptions : class
=> service.GetRequiredService<IOptions<TOptions>>().Value;
}