Initial Commit ...
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using xApi.Models;
|
||||
using xApi.Constants;
|
||||
|
||||
namespace xApi.Extensions
|
||||
{
|
||||
public static class DIHelperExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extract Application Configuration based on appSetting file ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static ApiConfiguration? GetApiConfiguration(
|
||||
this IConfiguration source
|
||||
)
|
||||
{
|
||||
//
|
||||
var section = source
|
||||
.GetSection(ConfigurationNodeNames.ApiConfigurationNodeName);
|
||||
var result = section.Get<ApiConfiguration>();
|
||||
if (result is null)
|
||||
{
|
||||
//
|
||||
result = new ApiConfiguration
|
||||
{
|
||||
Name = source[nameof(ApiConfiguration.Name)],
|
||||
Version = source[nameof(ApiConfiguration.Version)],
|
||||
Description = source[nameof(ApiConfiguration.Description)],
|
||||
Contact = source.GetSection(nameof(ApiConfiguration.Contact)).Get<ApiConfigurationContact>()
|
||||
};
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inject Specific Registered Service from IServiceCollection
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T? GetRegisteredService<T>(this IServiceCollection source)
|
||||
{
|
||||
//
|
||||
T? result = default;
|
||||
var serviceProvider = source.BuildServiceProvider();
|
||||
if (serviceProvider is not null)
|
||||
{
|
||||
result = serviceProvider.GetService<T>();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using xApi.Models;
|
||||
|
||||
namespace xApi.Extensions
|
||||
{
|
||||
public static class ModelExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Validate Configuration Model ...
|
||||
/// </summary>
|
||||
/// <param name="configuration"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsValid(this ApiConfiguration? configuration)
|
||||
{
|
||||
//
|
||||
var result = configuration != null
|
||||
&& !string.IsNullOrWhiteSpace(configuration.Name)
|
||||
&& !string.IsNullOrWhiteSpace(configuration.Version)
|
||||
&& !string.IsNullOrWhiteSpace(configuration.Description);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user