Added the abilty to load configuration sections using * pattern
This commit is contained in:
@@ -32,8 +32,8 @@ public class FrameHandler(INavigationContext navigationContext) :
|
|||||||
|
|
||||||
if (control.DataContext is object content)
|
if (control.DataContext is object content)
|
||||||
{
|
{
|
||||||
if (content is IPrimaryConfirmation confirmNavigation &&
|
if (content is IConfirmation confirmation &&
|
||||||
!await confirmNavigation.Confirm())
|
!await confirmation.Confirm())
|
||||||
{
|
{
|
||||||
args.Cancel = true;
|
args.Cancel = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,7 @@ namespace Toolkit.Avalonia;
|
|||||||
public class NavigationPageFactory :
|
public class NavigationPageFactory :
|
||||||
INavigationPageFactory
|
INavigationPageFactory
|
||||||
{
|
{
|
||||||
public Control? GetPage(Type srcType)
|
public Control? GetPage(Type srcType) => default;
|
||||||
{
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Control GetPageFromObject(object target)
|
public Control GetPageFromObject(object target) => (Control)target;
|
||||||
{
|
|
||||||
return (Control)target;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,4 +2,11 @@
|
|||||||
namespace Toolkit.Foundation;
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
public record Create<TValue>(TValue Value) :
|
public record Create<TValue>(TValue Value) :
|
||||||
INotification;
|
INotification;
|
||||||
|
|
||||||
|
public record Create
|
||||||
|
{
|
||||||
|
public static Create<TValue> As<TValue>(TValue value) => new(value);
|
||||||
|
|
||||||
|
public static Create<TValue> As<TValue>() where TValue : new() => new(new TValue());
|
||||||
|
}
|
||||||
@@ -1,9 +1,155 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
using Microsoft.Extensions.FileProviders.Physical;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace Toolkit.Foundation;
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public static class Test
|
||||||
|
{
|
||||||
|
public static IHostBuilder AddConfiguration<TConfiguration>(this IHostBuilder builder,
|
||||||
|
string section)
|
||||||
|
where TConfiguration : class, new() =>
|
||||||
|
builder.AddConfiguration<TConfiguration>(section, "Settings.json", null);
|
||||||
|
|
||||||
|
public static IHostBuilder AddConfiguration<TConfiguration>(this IHostBuilder services)
|
||||||
|
where TConfiguration : class, new() =>
|
||||||
|
services.AddConfiguration<TConfiguration>(typeof(TConfiguration).Name, "Settings.json", null);
|
||||||
|
|
||||||
|
public static IHostBuilder AddConfiguration<TConfiguration>(this IHostBuilder builder,
|
||||||
|
Action<TConfiguration> configurationDelegate)
|
||||||
|
where TConfiguration : class, new()
|
||||||
|
{
|
||||||
|
TConfiguration configuration = new();
|
||||||
|
configurationDelegate.Invoke(configuration);
|
||||||
|
|
||||||
|
return builder.AddConfiguration(typeof(TConfiguration).Name, "Settings.json", configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IHostBuilder AddConfiguration<TConfiguration>(this IHostBuilder builder,
|
||||||
|
Action<TConfiguration> configurationDelegate,
|
||||||
|
string section)
|
||||||
|
where TConfiguration : class, new()
|
||||||
|
{
|
||||||
|
TConfiguration configuration = new();
|
||||||
|
configurationDelegate.Invoke(configuration);
|
||||||
|
|
||||||
|
return builder.AddConfiguration(section, "Settings.json", configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IHostBuilder AddConfiguration<TConfiguration>(this IHostBuilder builder,
|
||||||
|
TConfiguration configuration)
|
||||||
|
where TConfiguration : class, new() =>
|
||||||
|
builder.AddConfiguration(configuration.GetType().Name, "Settings.json", configuration);
|
||||||
|
|
||||||
|
public static IHostBuilder AddConfiguration<TConfiguration>(this IHostBuilder builder,
|
||||||
|
object configuration)
|
||||||
|
where TConfiguration : class, new() =>
|
||||||
|
builder.AddConfiguration(configuration.GetType().Name,
|
||||||
|
"Settings.json", (TConfiguration?)configuration);
|
||||||
|
|
||||||
|
public static IHostBuilder AddConfiguration<TConfiguration>(this IHostBuilder builder, string section,
|
||||||
|
string path = "Settings.json",
|
||||||
|
TConfiguration? defaultConfiguration = null,
|
||||||
|
Action<JsonSerializerOptions>? serializerDelegate = null)
|
||||||
|
where TConfiguration : class, new()
|
||||||
|
{
|
||||||
|
builder.ConfigureServices((context, services) =>
|
||||||
|
{
|
||||||
|
HashSet<string> sections = [];
|
||||||
|
|
||||||
|
if (section.EndsWith(":*"))
|
||||||
|
{
|
||||||
|
section = section[..^1];
|
||||||
|
if (context.Configuration is ConfigurationRoot root)
|
||||||
|
{
|
||||||
|
foreach (KeyValuePair<string, string?> configuration in root.AsEnumerable())
|
||||||
|
{
|
||||||
|
string[] segments = configuration.Key.Split(':');
|
||||||
|
if (segments.Length > 2)
|
||||||
|
{
|
||||||
|
string keyPrefix = string.Join(':', segments.Take(2));
|
||||||
|
if (!keyPrefix.EndsWith(":*"))
|
||||||
|
{
|
||||||
|
sections.Add(keyPrefix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sections.Add(section);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (string section in sections)
|
||||||
|
{
|
||||||
|
services.TryAddSingleton<IConfigurationFile<TConfiguration>>(provider =>
|
||||||
|
{
|
||||||
|
IFileInfo? fileInfo = null;
|
||||||
|
if (provider.GetService<IHostEnvironment>() is IHostEnvironment hostEnvironment)
|
||||||
|
{
|
||||||
|
IFileProvider fileProvider = hostEnvironment.ContentRootFileProvider;
|
||||||
|
fileInfo = fileProvider.GetFileInfo(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
fileInfo ??= new PhysicalFileInfo(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path)));
|
||||||
|
return new ConfigurationFile<TConfiguration>(fileInfo);
|
||||||
|
});
|
||||||
|
|
||||||
|
services.TryAddKeyedSingleton<IConfigurationSource<TConfiguration>>(section, (provider, KeyAccelerator) =>
|
||||||
|
{
|
||||||
|
JsonSerializerOptions? defaultSerializer = null;
|
||||||
|
if (serializerDelegate is not null)
|
||||||
|
{
|
||||||
|
defaultSerializer = new JsonSerializerOptions();
|
||||||
|
serializerDelegate.Invoke(defaultSerializer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ConfigurationSource<TConfiguration>(provider.GetRequiredService<IConfigurationFile<TConfiguration>>(),
|
||||||
|
section, defaultSerializer);
|
||||||
|
});
|
||||||
|
|
||||||
|
//services.AddHostedService<ConfigurationMonitor<TConfiguration>>();
|
||||||
|
services.TryAddKeyedTransient<IConfigurationReader<TConfiguration>>(section, (provider, key) =>
|
||||||
|
new ConfigurationReader<TConfiguration>(provider.GetRequiredKeyedService<IConfigurationSource<TConfiguration>>(key),
|
||||||
|
provider.GetRequiredKeyedService<IConfigurationFactory<TConfiguration>>(key)));
|
||||||
|
|
||||||
|
services.TryAddKeyedTransient<IConfigurationWriter<TConfiguration>>(section, (provider, key) =>
|
||||||
|
new ConfigurationWriter<TConfiguration>(provider.GetRequiredKeyedService<IConfigurationSource<TConfiguration>>(key)));
|
||||||
|
|
||||||
|
services.TryAddKeyedTransient<IConfigurationFactory<TConfiguration>>(section, (provider, key) =>
|
||||||
|
new ConfigurationFactory<TConfiguration>(() => defaultConfiguration ?? new TConfiguration()));
|
||||||
|
|
||||||
|
services.AddTransient<IInitializer, ConfigurationInitializer<TConfiguration>>(provider =>
|
||||||
|
new ConfigurationInitializer<TConfiguration>(provider.GetRequiredKeyedService<IConfigurationReader<TConfiguration>>(section),
|
||||||
|
provider.GetRequiredKeyedService<IConfigurationWriter<TConfiguration>>(section),
|
||||||
|
provider.GetRequiredKeyedService<IConfigurationFactory<TConfiguration>>(section),
|
||||||
|
provider.GetRequiredService<IPublisher>()));
|
||||||
|
|
||||||
|
services.AddTransient<IConfigurationInitializer<TConfiguration>, ConfigurationInitializer<TConfiguration>>(provider =>
|
||||||
|
provider.GetRequiredService<IServiceFactory>().Create<ConfigurationInitializer<TConfiguration>>(section));
|
||||||
|
|
||||||
|
services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
|
||||||
|
|
||||||
|
services.TryAddKeyedTransient<IConfigurationDescriptor<TConfiguration>>(section, (provider, key) =>
|
||||||
|
new ConfigurationDescriptor<TConfiguration>(section, provider.GetRequiredKeyedService<IConfigurationReader<TConfiguration>>(key)));
|
||||||
|
|
||||||
|
services.AddTransient(provider =>
|
||||||
|
provider.GetRequiredKeyedService<IConfigurationDescriptor<TConfiguration>>(section));
|
||||||
|
|
||||||
|
services.AddTransient(provider =>
|
||||||
|
provider.GetRequiredKeyedService<IConfigurationDescriptor<TConfiguration>>(section).Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
}
|
||||||
public class DefaultBuilder :
|
public class DefaultBuilder :
|
||||||
HostBuilder
|
HostBuilder
|
||||||
{
|
{
|
||||||
@@ -11,7 +157,7 @@ public class DefaultBuilder :
|
|||||||
{
|
{
|
||||||
return new HostBuilder()
|
return new HostBuilder()
|
||||||
.UseContentRoot("Local", true)
|
.UseContentRoot("Local", true)
|
||||||
.ConfigureAppConfiguration(config =>
|
.ConfigureAppConfiguration((context, config) =>
|
||||||
{
|
{
|
||||||
config.AddJsonFile("Settings.json", true, true);
|
config.AddJsonFile("Settings.json", true, true);
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.Configuration.Json;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
using Microsoft.Extensions.FileProviders.Physical;
|
using Microsoft.Extensions.FileProviders.Physical;
|
||||||
@@ -146,8 +147,11 @@ public static class IServiceCollectionExtensions
|
|||||||
services.TryAddKeyedTransient<IConfigurationDescriptor<TConfiguration>>(section, (provider, key) =>
|
services.TryAddKeyedTransient<IConfigurationDescriptor<TConfiguration>>(section, (provider, key) =>
|
||||||
new ConfigurationDescriptor<TConfiguration>(section, provider.GetRequiredKeyedService<IConfigurationReader<TConfiguration>>(key)));
|
new ConfigurationDescriptor<TConfiguration>(section, provider.GetRequiredKeyedService<IConfigurationReader<TConfiguration>>(key)));
|
||||||
|
|
||||||
services.AddTransient(provider => provider.GetRequiredKeyedService<IConfigurationDescriptor<TConfiguration>>(section));
|
services.AddTransient(provider =>
|
||||||
services.AddTransient(provider => provider.GetRequiredKeyedService<IConfigurationDescriptor<TConfiguration>>(section).Value);
|
provider.GetRequiredKeyedService<IConfigurationDescriptor<TConfiguration>>(section));
|
||||||
|
|
||||||
|
services.AddTransient(provider =>
|
||||||
|
provider.GetRequiredKeyedService<IConfigurationDescriptor<TConfiguration>>(section).Value);
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user