Files
xCommons/Extensions/IConfigurationExtensions.cs
T

179 lines
5.4 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using xCommons.Configurations;
using xCommons.Constants;
namespace xCommons.Extensions
{
public static partial class IConfigurationExtensions
{
/// <summary>
/// Retrieve AppConfiguration from Configurations
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XAppConfiguration GetXAppConfiguration(this IConfiguration source)
{
//
var xMessageResourceTitlesConfigSection = source
.GetSection(ConfigurationNodeNames.MESSAGE_RESOURCE_TITLES_NODE);
//
var result = new XAppConfiguration
{
Name = source[nameof(XAppConfiguration.Name)],
Version = source[nameof(XAppConfiguration.Version)],
XPoweredValue = source[nameof(XAppConfiguration.XPoweredValue)],
WelcomeMessage = source[nameof(XAppConfiguration.WelcomeMessage)],
AllowedOrigins = source
.GetSection(nameof(XAppConfiguration.AllowedOrigins))
.Get<IEnumerable<string>>(),
DefaultLanguage = source[nameof(XAppConfiguration.DefaultLanguage)],
MessageResourceTitles = xMessageResourceTitlesConfigSection
.Get<XMessageResourceTitles>()
};
//
return result;
}
/// <summary>
/// Retrieve Swagger Configurations
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XSwaggerConfiguration GetXSwaggerConfiguration(this IConfiguration source)
{
//
var xSwaggerConfigSection = source
.GetSection(ConfigurationNodeNames.SWAGGER_NODE);
return xSwaggerConfigSection.Get<XSwaggerConfiguration>();
}
/// <summary>
/// Converts a ConfigurationSection to object Representation ...
/// </summary>
/// <param name="section"></param>
/// <returns></returns>
public static object ToObject(this IConfigurationSection section)
{
//
// an Inner Function for Create Recursive Parsing ...
object ConvertSection(IConfigurationSection sec)
{
//
var children = sec
.GetChildren()
.ToList();
if (!children.Any())
{
return sec.Value ?? string.Empty;
}
//
// Detect Array Like Sections ...
bool isArray = children.All(ch => int.TryParse(ch.Key, out _));
if (isArray)
{
//
return children
.OrderBy(c => int.Parse(c.Key))
.Select(ConvertSection)
.ToList();
}
//
// Otherwise ...
return children
.ToDictionary(
c => c.Key,
c => ConvertSection(c)
);
}
//
// Try to Parse using Inner Function ...
var result = ConvertSection(section);
//
return result;
}
/// <summary>
/// Converts a ConfigurationSection to Json Representation ...
/// </summary>
/// <param name="section"></param>
/// <returns></returns>
public static string ToJSON(this IConfigurationSection section)
{
//
// Try to Parse using Inner Function ...
var obj = section.ToObject();
//
// Convert it to Json ...
var result = obj.ToJSON();
//
return result;
}
/// <summary>
/// Get a Configuration Section as Object ...
/// </summary>
/// <param name="source"></param>
/// <param name="section"></param>
/// <returns></returns>
public static object GetSectionAsObject(
this IConfiguration source,
string section
)
{
//
object result = null;
//
if (!section.IsNullOrEmpty())
{
//
var sec = source.GetSection(section);
if (!sec.IsNull())
{
result = sec.ToObject();
}
}
//
return result;
}
/// <summary>
/// Retrieve a Configuration Section as Json String ...
/// </summary>
/// <param name="source"></param>
/// <param name="section"></param>
/// <returns></returns>
public static string GetSectionAsJson(
this IConfiguration source,
string section
)
{
//
var result = string.Empty;
//
// Retrieve Secion as Object ...
var obj = source.GetSectionAsObject(section);
if (!obj.IsNull())
{
//
// Converts Section Object to Json ...
result = obj.ToJSON();
}
//
return result;
}
}
}