Compare commits
6 Commits
feat/Infra
...
246184165f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
246184165f | ||
|
|
9d36ced82f | ||
|
|
c81ff2c628 | ||
|
|
653665bb25 | ||
|
|
17edf605e7 | ||
|
|
b41373339e |
30
DigitalData.Core.Abstractions/ConfigurationExtension.cs
Normal file
30
DigitalData.Core.Abstractions/ConfigurationExtension.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
40
DigitalData.Core.Abstractions/DeferredServiceProvider.cs
Normal file
40
DigitalData.Core.Abstractions/DeferredServiceProvider.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,9 +17,9 @@
|
|||||||
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
|
||||||
<PackAsTool>False</PackAsTool>
|
<PackAsTool>False</PackAsTool>
|
||||||
<PackageIcon>core_icon.png</PackageIcon>
|
<PackageIcon>core_icon.png</PackageIcon>
|
||||||
<Version>3.4.3</Version>
|
<Version>3.6.0</Version>
|
||||||
<AssemblyVersion>3.4.3</AssemblyVersion>
|
<AssemblyVersion>3.6.0</AssemblyVersion>
|
||||||
<FileVersion>3.4.3</FileVersion>
|
<FileVersion>3.6.0</FileVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
31
DigitalData.Core.Abstractions/ServiceProviderExtensions.cs
Normal file
31
DigitalData.Core.Abstractions/ServiceProviderExtensions.cs
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user