Refactor...
This commit is contained in:
@@ -7,6 +7,5 @@ public class ContextualWidgetProvider :
|
||||
IWidgetProvider
|
||||
{
|
||||
public void Create(HostBuilderContext comtext, IServiceCollection services) => services
|
||||
.AddConfiguration<ContextualWidgetConfiguration>(comtext.Configuration.GetSection(nameof(ContextualWidgetConfiguration)))
|
||||
.AddWidgetTemplate<ContextualWidgetViewModel>();
|
||||
}
|
||||
@@ -46,7 +46,7 @@ public partial class App :
|
||||
services.AddTransient<DesktopFlyout>();
|
||||
services.AddContentTemplate<CommandViewModel, CommandView>();
|
||||
|
||||
services.AddWidgetProvider<ContextualWidgetProvider>();
|
||||
// services.AddWidgetProvider<ContextualWidgetProvider>();
|
||||
services.AddWidgetProvider<PrimaryWidgetProvider>();
|
||||
|
||||
services.AddTransient(provider =>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Hyperbar.Windows.Interop;
|
||||
using Hyperbar.Windows.Primary;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public class Configuration<TConfiguration>(IConfigurationReader<TConfiguration> reader) :
|
||||
IConfiguration<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
public TConfiguration Value => reader.Read();
|
||||
}
|
||||
@@ -1,21 +1,14 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Hyperbar;
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ConfigurationInitializer<TConfiguration>(DefaultConfiguration<TConfiguration> defaults,
|
||||
IConfigurationWriter<TConfiguration> writer,
|
||||
IOptionsMonitor<TConfiguration> options,
|
||||
IMediator mediator) : IInitializer
|
||||
IConfigurationWriter<TConfiguration> writer) :
|
||||
IInitializer
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
class,
|
||||
new()
|
||||
{
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
options.OnChange(async args =>
|
||||
{
|
||||
await mediator.PublishAsync(new ConfigurationChanged<TConfiguration>(args));
|
||||
});
|
||||
|
||||
if (defaults.Configuration is not null)
|
||||
{
|
||||
writer.Write(defaults.Configuration);
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ConfigurationMonitor<TConfiguration> : IInitializer
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
private readonly FileSystemWatcher watcher;
|
||||
|
||||
public ConfigurationMonitor(IConfigurationReader<TConfiguration> reader)
|
||||
{
|
||||
this.watcher = new FileSystemWatcher();
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
void ChangedHandler(object sender,
|
||||
FileSystemEventArgs args)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
watcher.Changed += ChangedHandler;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ConfigurationReader<TConfiguration>(IConfigurationSource<TConfiguration> source,
|
||||
JsonSerializerOptions? serializerOptions = null) :
|
||||
IConfigurationReader<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
private static readonly Func<JsonSerializerOptions> defaultSerializerOptions = new(() =>
|
||||
{
|
||||
return new JsonSerializerOptions
|
||||
{
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
Converters =
|
||||
{
|
||||
new JsonStringEnumConverter()
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
public TConfiguration Read()
|
||||
{
|
||||
if ((TryGet(out TConfiguration? value) ? value : new TConfiguration()) is TConfiguration configuration)
|
||||
{
|
||||
return configuration;
|
||||
}
|
||||
|
||||
return new TConfiguration();
|
||||
}
|
||||
|
||||
private bool TryGet(out TConfiguration? value)
|
||||
{
|
||||
if (File.Exists(source.Path))
|
||||
{
|
||||
byte[] jsonContent = File.ReadAllBytes(source.Path);
|
||||
|
||||
using JsonDocument jsonDocument = JsonDocument.Parse(jsonContent);
|
||||
if (jsonDocument.RootElement.TryGetProperty(source.Section, out JsonElement sectionValue))
|
||||
{
|
||||
value = JsonSerializer.Deserialize<TConfiguration>(sectionValue.ToString(), serializerOptions ?? defaultSerializerOptions());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ConfigurationSource<TConfiguration>(string path,
|
||||
string section) :
|
||||
IConfigurationSource<TConfiguration>
|
||||
where TConfiguration :
|
||||
class
|
||||
{
|
||||
public string Path => path;
|
||||
|
||||
public string Section => section;
|
||||
}
|
||||
@@ -4,63 +4,7 @@ using System.Text.Json.Serialization;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IConfigurationReader<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
TConfiguration Read();
|
||||
}
|
||||
|
||||
public class ConfigurationReader<TConfiguration>(string path,
|
||||
string section,
|
||||
JsonSerializerOptions? serializerOptions = null) :
|
||||
IConfigurationReader<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
private static readonly Func<JsonSerializerOptions> defaultSerializerOptions = new(() =>
|
||||
{
|
||||
return new JsonSerializerOptions
|
||||
{
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
Converters =
|
||||
{
|
||||
new JsonStringEnumConverter()
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
public TConfiguration Read()
|
||||
{
|
||||
if ((TryGet(out TConfiguration? value) ? value : new TConfiguration()) is TConfiguration configuration)
|
||||
{
|
||||
return configuration;
|
||||
}
|
||||
|
||||
return new TConfiguration();
|
||||
}
|
||||
|
||||
private bool TryGet(out TConfiguration? value)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
byte[] jsonContent = File.ReadAllBytes(path);
|
||||
|
||||
using JsonDocument jsonDocument = JsonDocument.Parse(jsonContent);
|
||||
if (jsonDocument.RootElement.TryGetProperty(section, out JsonElement sectionValue))
|
||||
{
|
||||
value = JsonSerializer.Deserialize<TConfiguration>(sectionValue.ToString(), serializerOptions ?? defaultSerializerOptions());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class ConfigurationWriter<TConfiguration>(string path,
|
||||
string section,
|
||||
public class ConfigurationWriter<TConfiguration>(IConfigurationSource<TConfiguration> source,
|
||||
JsonSerializerOptions? serializerOptions = null) :
|
||||
IConfigurationWriter<TConfiguration>
|
||||
where TConfiguration :
|
||||
@@ -87,21 +31,21 @@ public class ConfigurationReader<TConfiguration>(string path,
|
||||
|
||||
public void Write(TConfiguration value)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
if (!File.Exists(source.Path))
|
||||
{
|
||||
string? fileDirectoryPath = Path.GetDirectoryName(path);
|
||||
string? fileDirectoryPath = Path.GetDirectoryName(source.Path);
|
||||
if (!string.IsNullOrEmpty(fileDirectoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(fileDirectoryPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(path, "{}");
|
||||
File.WriteAllText(source.Path, "{}");
|
||||
}
|
||||
|
||||
byte[] jsonContent = File.ReadAllBytes(path);
|
||||
byte[] jsonContent = File.ReadAllBytes(source.Path);
|
||||
|
||||
using JsonDocument jsonDocument = JsonDocument.Parse(jsonContent);
|
||||
using FileStream stream = File.OpenWrite(path);
|
||||
using FileStream stream = File.OpenWrite(source.Path);
|
||||
Utf8JsonWriter writer = new(stream, new JsonWriterOptions()
|
||||
{
|
||||
Indented = true,
|
||||
@@ -114,7 +58,7 @@ public class ConfigurationReader<TConfiguration>(string path,
|
||||
|
||||
foreach (JsonProperty element in jsonDocument.RootElement.EnumerateObject())
|
||||
{
|
||||
if (element.Name != section)
|
||||
if (element.Name != source.Section)
|
||||
{
|
||||
element.WriteTo(writer);
|
||||
continue;
|
||||
@@ -126,7 +70,7 @@ public class ConfigurationReader<TConfiguration>(string path,
|
||||
|
||||
if (!isWritten)
|
||||
{
|
||||
writer.WritePropertyName(section);
|
||||
writer.WritePropertyName(source.Section);
|
||||
optionsElement.WriteTo(writer);
|
||||
}
|
||||
|
||||
@@ -137,12 +81,12 @@ public class ConfigurationReader<TConfiguration>(string path,
|
||||
|
||||
private bool TryGet(out TConfiguration? value)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
if (File.Exists(source.Path))
|
||||
{
|
||||
byte[] jsonContent = File.ReadAllBytes(path);
|
||||
byte[] jsonContent = File.ReadAllBytes(source.Path);
|
||||
|
||||
using JsonDocument jsonDocument = JsonDocument.Parse(jsonContent);
|
||||
if (jsonDocument.RootElement.TryGetProperty(section, out JsonElement sectionValue))
|
||||
if (jsonDocument.RootElement.TryGetProperty(source.Section, out JsonElement sectionValue))
|
||||
{
|
||||
value = JsonSerializer.Deserialize<TConfiguration>(sectionValue.ToString(), serializerOptions ?? defaultSerializerOptions());
|
||||
return true;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IConfiguration<out TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
TConfiguration Value { get; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IConfigurationReader<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
TConfiguration Read();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IConfigurationSource<TConfiguration>
|
||||
where TConfiguration :
|
||||
class
|
||||
{
|
||||
string Path { get; }
|
||||
|
||||
string Section { get; }
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
namespace Hyperbar;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IWritableConfiguration<out TConfiguration> :
|
||||
IOptionsSnapshot<TConfiguration>
|
||||
public interface IWritableConfiguration<out TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
void Write(Action<TConfiguration> updateAction,
|
||||
bool reload = true);
|
||||
void Write(Action<TConfiguration> updateDelegate);
|
||||
}
|
||||
@@ -1,26 +1,9 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
namespace Hyperbar;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class WritableConfiguration<TConfiguration>(IConfigurationWriter<TConfiguration> writer,
|
||||
IOptionsMonitor<TConfiguration> options,
|
||||
IConfiguration configuration) :
|
||||
public class WritableConfiguration<TConfiguration>(IConfigurationWriter<TConfiguration> writer) :
|
||||
IWritableConfiguration<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
public TConfiguration Value => options.CurrentValue;
|
||||
|
||||
public TConfiguration Get(string? name) => options.Get(name);
|
||||
|
||||
public void Write(Action<TConfiguration> updateDelegate,
|
||||
bool reload = true)
|
||||
{
|
||||
writer.Write(updateDelegate);
|
||||
if (reload && configuration is IConfigurationRoot configurationRoot)
|
||||
{
|
||||
configurationRoot.Reload();
|
||||
}
|
||||
}
|
||||
public void Write(Action<TConfiguration> updateDelegate) => writer.Write(updateDelegate);
|
||||
}
|
||||
@@ -33,9 +33,7 @@ public static class IServiceCollectionExtensions
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
services.AddTransient(provider => provider.GetRequiredService<IConfigurationReader<TConfiguration>>().Read());
|
||||
|
||||
services.AddSingleton<IConfigurationReader<TConfiguration>>(provider =>
|
||||
services.AddSingleton<IConfigurationSource<TConfiguration>>(provider =>
|
||||
{
|
||||
string? jsonFilePath = null;
|
||||
if (provider.GetService<IHostEnvironment>() is IHostEnvironment hostEnvironment)
|
||||
@@ -47,30 +45,11 @@ public static class IServiceCollectionExtensions
|
||||
}
|
||||
|
||||
jsonFilePath ??= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
|
||||
|
||||
JsonSerializerOptions? defaultSerializer = null;
|
||||
if (serializerDelegate is not null)
|
||||
{
|
||||
defaultSerializer = new JsonSerializerOptions();
|
||||
serializerDelegate.Invoke(defaultSerializer);
|
||||
}
|
||||
|
||||
return new ConfigurationReader<TConfiguration>(jsonFilePath, section, defaultSerializer);
|
||||
return new ConfigurationSource<TConfiguration>(jsonFilePath, section);
|
||||
});
|
||||
|
||||
services.AddSingleton<IConfigurationWriter<TConfiguration>>(provider =>
|
||||
services.AddTransient<IConfigurationReader<TConfiguration>>(provider =>
|
||||
{
|
||||
string? jsonFilePath = null;
|
||||
if (provider.GetService<IHostEnvironment>() is IHostEnvironment hostEnvironment)
|
||||
{
|
||||
IFileProvider fileProvider = hostEnvironment.ContentRootFileProvider;
|
||||
IFileInfo fileInfo = fileProvider.GetFileInfo(path);
|
||||
|
||||
jsonFilePath = fileInfo.PhysicalPath;
|
||||
}
|
||||
|
||||
jsonFilePath ??= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
|
||||
|
||||
JsonSerializerOptions? defaultSerializer = null;
|
||||
if (serializerDelegate is not null)
|
||||
{
|
||||
@@ -78,7 +57,21 @@ public static class IServiceCollectionExtensions
|
||||
serializerDelegate.Invoke(defaultSerializer);
|
||||
}
|
||||
|
||||
return new ConfigurationWriter<TConfiguration>(jsonFilePath, section, defaultSerializer);
|
||||
return new ConfigurationReader<TConfiguration>(provider.GetRequiredService<IConfigurationSource<TConfiguration>>(),
|
||||
defaultSerializer);
|
||||
});
|
||||
|
||||
services.AddTransient<IConfigurationWriter<TConfiguration>>(provider =>
|
||||
{
|
||||
JsonSerializerOptions? defaultSerializer = null;
|
||||
if (serializerDelegate is not null)
|
||||
{
|
||||
defaultSerializer = new JsonSerializerOptions();
|
||||
serializerDelegate.Invoke(defaultSerializer);
|
||||
}
|
||||
|
||||
return new ConfigurationWriter<TConfiguration>(provider.GetRequiredService<IConfigurationSource<TConfiguration>>(),
|
||||
defaultSerializer);
|
||||
});
|
||||
|
||||
services.AddTransient(provider => new DefaultConfiguration<TConfiguration>(defaults));
|
||||
@@ -86,6 +79,9 @@ public static class IServiceCollectionExtensions
|
||||
|
||||
services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
|
||||
|
||||
services.AddTransient<IConfiguration<TConfiguration>, Configuration<TConfiguration>>();
|
||||
services.AddTransient(provider => provider.GetRequiredService<IConfiguration<TConfiguration>>().Value);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,8 +13,5 @@ public class AppService(IEnumerable<IInitializer> initializers) :
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
Reference in New Issue
Block a user