Compare commits
4 Commits
feat/Infra
...
c81ff2c628
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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.5.0</Version>
|
||||||
<AssemblyVersion>3.4.3</AssemblyVersion>
|
<AssemblyVersion>3.5.0</AssemblyVersion>
|
||||||
<FileVersion>3.4.3</FileVersion>
|
<FileVersion>3.5.0</FileVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user