more refoctoring work
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ConfigurationInitializer<TConfiguration>(DefaultConfiguration<TConfiguration> defaults,
|
||||
IConfigurationReader<TConfiguration> reader,
|
||||
IConfigurationWriter<TConfiguration> writer) :
|
||||
IInitializer
|
||||
where TConfiguration :
|
||||
@@ -9,9 +10,12 @@ public class ConfigurationInitializer<TConfiguration>(DefaultConfiguration<TConf
|
||||
{
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
if (defaults.Configuration is not null)
|
||||
if (!reader.TryRead(out TConfiguration? _))
|
||||
{
|
||||
writer.Write(defaults.Configuration);
|
||||
if (defaults.Configuration is not null)
|
||||
{
|
||||
writer.Write(defaults.Configuration);
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ConfigurationMonitor<TConfiguration>(IConfigurationSource<TConfiguration> source,
|
||||
public class ConfigurationMonitor<TConfiguration>(IConfigurationFile<TConfiguration> configurationFile,
|
||||
IConfigurationReader<TConfiguration> reader,
|
||||
IMediator mediator) : IInitializer
|
||||
where TConfiguration :
|
||||
@@ -19,16 +19,21 @@ public class ConfigurationMonitor<TConfiguration>(IConfigurationSource<TConfigur
|
||||
}
|
||||
}
|
||||
|
||||
string fileName = Path.GetFileName(source.Path);
|
||||
|
||||
watcher = new FileSystemWatcher
|
||||
if (configurationFile.FileInfo.PhysicalPath is { } path)
|
||||
{
|
||||
NotifyFilter = NotifyFilters.LastWrite,
|
||||
Path = source.Path.Replace(fileName, ""),
|
||||
Filter = fileName,
|
||||
EnableRaisingEvents = true
|
||||
};
|
||||
watcher.Changed += ChangedHandler;
|
||||
string fileName = Path.GetFileName(path);
|
||||
|
||||
watcher = new FileSystemWatcher
|
||||
{
|
||||
NotifyFilter = NotifyFilters.LastWrite,
|
||||
Path = path.Replace(fileName, ""),
|
||||
Filter = fileName,
|
||||
EnableRaisingEvents = true
|
||||
};
|
||||
|
||||
watcher.Changed += ChangedHandler;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,14 @@
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
namespace Hyperbar;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ConfigurationReader<TConfiguration>(IConfigurationSource<TConfiguration> source,
|
||||
JsonSerializerOptions? serializerOptions = null) :
|
||||
public class ConfigurationReader<TConfiguration>(IConfigurationSource<TConfiguration> source) :
|
||||
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)
|
||||
if ((source.TryGet(out TConfiguration? value) ? value :
|
||||
new TConfiguration()) is TConfiguration configuration)
|
||||
{
|
||||
return configuration;
|
||||
}
|
||||
@@ -32,21 +16,15 @@ public class ConfigurationReader<TConfiguration>(IConfigurationSource<TConfigura
|
||||
return new TConfiguration();
|
||||
}
|
||||
|
||||
private bool TryGet(out TConfiguration? value)
|
||||
public bool TryRead(out TConfiguration? configuration)
|
||||
{
|
||||
if (File.Exists(source.Path))
|
||||
if (source.TryGet(out TConfiguration? value) && value is not null)
|
||||
{
|
||||
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;
|
||||
}
|
||||
configuration = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
value = default;
|
||||
configuration = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,129 @@
|
||||
namespace Hyperbar;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public class ConfigurationSource<TConfiguration>(string path,
|
||||
string section) :
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ConfigurationSource<TConfiguration>(IConfigurationFile<TConfiguration> configurationFile,
|
||||
string section,
|
||||
JsonSerializerOptions? serializerOptions = null) :
|
||||
IConfigurationSource<TConfiguration>
|
||||
where TConfiguration :
|
||||
class
|
||||
{
|
||||
public string Path => path;
|
||||
private readonly object lockingObject = new();
|
||||
|
||||
public string Section => section;
|
||||
private static readonly Func<JsonSerializerOptions> defaultSerializerOptions = new(() =>
|
||||
{
|
||||
return new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
Converters =
|
||||
{
|
||||
new JsonStringEnumConverter()
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
public void Set(TConfiguration value)
|
||||
{
|
||||
lock (lockingObject)
|
||||
{
|
||||
IFileInfo fileInfo = configurationFile.FileInfo;
|
||||
|
||||
if (!File.Exists(fileInfo.PhysicalPath))
|
||||
{
|
||||
string? fileDirectoryPath = Path.GetDirectoryName(fileInfo.PhysicalPath);
|
||||
if (!string.IsNullOrEmpty(fileDirectoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(fileDirectoryPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(fileInfo.PhysicalPath!, "{}");
|
||||
}
|
||||
|
||||
static Stream OpenReadWrite(IFileInfo fileInfo)
|
||||
{
|
||||
return fileInfo.PhysicalPath is not null
|
||||
? new FileStream(fileInfo.PhysicalPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
|
||||
: fileInfo.CreateReadStream();
|
||||
}
|
||||
|
||||
using Stream stream = OpenReadWrite(fileInfo);
|
||||
using StreamReader? reader = new(stream);
|
||||
|
||||
string? content = reader.ReadToEnd();
|
||||
using JsonDocument jsonDocument = JsonDocument.Parse(content);
|
||||
|
||||
using Stream stream2 = OpenReadWrite(fileInfo);
|
||||
Utf8JsonWriter writer = new(stream2, new JsonWriterOptions()
|
||||
{
|
||||
Indented = true,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
});
|
||||
|
||||
writer.WriteStartObject();
|
||||
bool isWritten = false;
|
||||
|
||||
JsonDocument optionsElement = JsonDocument.Parse(JsonSerializer.SerializeToUtf8Bytes(value,
|
||||
serializerOptions ?? defaultSerializerOptions()));
|
||||
|
||||
foreach (JsonProperty element in jsonDocument.RootElement.EnumerateObject())
|
||||
{
|
||||
if (element.Name != section)
|
||||
{
|
||||
element.WriteTo(writer);
|
||||
continue;
|
||||
}
|
||||
writer.WritePropertyName(element.Name);
|
||||
optionsElement.WriteTo(writer);
|
||||
isWritten = true;
|
||||
}
|
||||
|
||||
if (!isWritten)
|
||||
{
|
||||
writer.WritePropertyName(section);
|
||||
optionsElement.WriteTo(writer);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
writer.Flush();
|
||||
stream2.SetLength(stream2.Position);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGet(out TConfiguration? value)
|
||||
{
|
||||
lock (lockingObject)
|
||||
{
|
||||
IFileInfo fileInfo = configurationFile.FileInfo;
|
||||
|
||||
if (File.Exists(fileInfo.PhysicalPath))
|
||||
{
|
||||
static Stream OpenRead(IFileInfo fileInfo)
|
||||
{
|
||||
return fileInfo.PhysicalPath is not null
|
||||
? new FileStream(fileInfo.PhysicalPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
|
||||
: fileInfo.CreateReadStream();
|
||||
}
|
||||
|
||||
using Stream stream = OpenRead(fileInfo);
|
||||
using StreamReader? reader = new(stream);
|
||||
|
||||
string? content = reader.ReadToEnd();
|
||||
|
||||
using JsonDocument jsonDocument = JsonDocument.Parse(content);
|
||||
if (jsonDocument.RootElement.TryGetProperty(section, out JsonElement sectionValue))
|
||||
{
|
||||
value = JsonSerializer.Deserialize<TConfiguration>(sectionValue.ToString(), serializerOptions ?? defaultSerializerOptions());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,13 @@
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
namespace Hyperbar;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ConfigurationWriter<TConfiguration>(IConfigurationSource<TConfiguration> source,
|
||||
JsonSerializerOptions? serializerOptions = null) :
|
||||
public class ConfigurationWriter<TConfiguration>(IConfigurationSource<TConfiguration> source) :
|
||||
IConfigurationWriter<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
private static readonly Func<JsonSerializerOptions> defaultSerializerOptions = new(() =>
|
||||
{
|
||||
return new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
Converters = { new JsonStringEnumConverter() }
|
||||
};
|
||||
});
|
||||
|
||||
public void Write(Action<TConfiguration> updateDelegate)
|
||||
{
|
||||
if ((TryGet(out TConfiguration? value) ? value : new TConfiguration()) is TConfiguration updatedValue)
|
||||
if ((source.TryGet(out TConfiguration? value) ? value : new TConfiguration()) is TConfiguration updatedValue)
|
||||
{
|
||||
updateDelegate?.Invoke(updatedValue);
|
||||
Write(updatedValue);
|
||||
@@ -31,69 +16,6 @@ public class ConfigurationWriter<TConfiguration>(IConfigurationSource<TConfigura
|
||||
|
||||
public void Write(TConfiguration value)
|
||||
{
|
||||
if (!File.Exists(source.Path))
|
||||
{
|
||||
string? fileDirectoryPath = Path.GetDirectoryName(source.Path);
|
||||
if (!string.IsNullOrEmpty(fileDirectoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(fileDirectoryPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(source.Path, "{}");
|
||||
}
|
||||
|
||||
byte[] jsonContent = File.ReadAllBytes(source.Path);
|
||||
|
||||
using JsonDocument jsonDocument = JsonDocument.Parse(jsonContent);
|
||||
using FileStream stream = File.OpenWrite(source.Path);
|
||||
Utf8JsonWriter writer = new(stream, new JsonWriterOptions()
|
||||
{
|
||||
Indented = true,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
});
|
||||
|
||||
writer.WriteStartObject();
|
||||
bool isWritten = false;
|
||||
JsonDocument optionsElement = JsonDocument.Parse(JsonSerializer.SerializeToUtf8Bytes(value, serializerOptions ?? defaultSerializerOptions()));
|
||||
|
||||
foreach (JsonProperty element in jsonDocument.RootElement.EnumerateObject())
|
||||
{
|
||||
if (element.Name != source.Section)
|
||||
{
|
||||
element.WriteTo(writer);
|
||||
continue;
|
||||
}
|
||||
writer.WritePropertyName(element.Name);
|
||||
optionsElement.WriteTo(writer);
|
||||
isWritten = true;
|
||||
}
|
||||
|
||||
if (!isWritten)
|
||||
{
|
||||
writer.WritePropertyName(source.Section);
|
||||
optionsElement.WriteTo(writer);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
writer.Flush();
|
||||
stream.SetLength(stream.Position);
|
||||
}
|
||||
|
||||
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;
|
||||
source.Set(value);
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,7 @@ public interface IConfigurationReader<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
bool TryRead(out TConfiguration? configuration);
|
||||
|
||||
TConfiguration Read();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
namespace Hyperbar;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IConfigurationSource<TConfiguration>
|
||||
where TConfiguration :
|
||||
class
|
||||
{
|
||||
string Path { get; }
|
||||
bool TryGet(out TConfiguration? value);
|
||||
|
||||
string Section { get; }
|
||||
void Set(TConfiguration value);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public static class IHostBuilderExtensions
|
||||
{
|
||||
public static IHostBuilder UseContentRoot(this IHostBuilder hostBuilder,
|
||||
string contentRoot,
|
||||
bool createDirectory)
|
||||
{
|
||||
if (createDirectory)
|
||||
{
|
||||
Directory.CreateDirectory(contentRoot);
|
||||
}
|
||||
|
||||
hostBuilder.UseContentRoot(contentRoot);
|
||||
return hostBuilder;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,27 @@
|
||||
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 System.Text.Json;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IConfigurationFile<TConfiguration>
|
||||
where TConfiguration :
|
||||
class
|
||||
{
|
||||
IFileInfo FileInfo { get; }
|
||||
}
|
||||
|
||||
public class ConfigurationFile<TConfiguration>(IFileInfo fileInfo) :
|
||||
IConfigurationFile<TConfiguration>
|
||||
where TConfiguration :
|
||||
class
|
||||
{
|
||||
public IFileInfo FileInfo => fileInfo;
|
||||
}
|
||||
|
||||
public static class IServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddCache<TKey, TValue>(this IServiceCollection services)
|
||||
@@ -36,7 +51,7 @@ public static class IServiceCollectionExtensions
|
||||
return services.AddConfiguration<TConfiguration>(typeof(TConfiguration).Name, "Settings.json", null);
|
||||
}
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
Action<TConfiguration> configurationDelegate)
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
@@ -66,47 +81,34 @@ public static class IServiceCollectionExtensions
|
||||
{
|
||||
services.AddSingleton<IConfigurationSource<TConfiguration>>(provider =>
|
||||
{
|
||||
string? jsonFilePath = null;
|
||||
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.AddSingleton<IConfigurationFile<TConfiguration>>(provider =>
|
||||
{
|
||||
IFileInfo? fileInfo = null;
|
||||
if (provider.GetService<IHostEnvironment>() is IHostEnvironment hostEnvironment)
|
||||
{
|
||||
IFileProvider fileProvider = hostEnvironment.ContentRootFileProvider;
|
||||
IFileInfo fileInfo = fileProvider.GetFileInfo(path);
|
||||
|
||||
jsonFilePath = fileInfo.PhysicalPath;
|
||||
fileInfo = fileProvider.GetFileInfo(path);
|
||||
}
|
||||
|
||||
jsonFilePath ??= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
|
||||
return new ConfigurationSource<TConfiguration>(jsonFilePath, section);
|
||||
});
|
||||
|
||||
services.AddSingleton<IConfigurationReader<TConfiguration>>(provider =>
|
||||
{
|
||||
JsonSerializerOptions? defaultSerializer = null;
|
||||
if (serializerDelegate is not null)
|
||||
{
|
||||
defaultSerializer = new JsonSerializerOptions();
|
||||
serializerDelegate.Invoke(defaultSerializer);
|
||||
}
|
||||
|
||||
return new ConfigurationReader<TConfiguration>(provider.GetRequiredService<IConfigurationSource<TConfiguration>>(),
|
||||
defaultSerializer);
|
||||
});
|
||||
|
||||
services.AddSingleton<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);
|
||||
fileInfo ??= new PhysicalFileInfo(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path)));
|
||||
return new ConfigurationFile<TConfiguration>(fileInfo);
|
||||
});
|
||||
|
||||
services.AddSingleton<IInitializer, ConfigurationMonitor<TConfiguration>>();
|
||||
|
||||
services.AddSingleton<IConfigurationReader<TConfiguration>, ConfigurationReader<TConfiguration>>();
|
||||
services.AddSingleton<IConfigurationWriter<TConfiguration>, ConfigurationWriter<TConfiguration>>();
|
||||
|
||||
services.AddTransient(provider => new DefaultConfiguration<TConfiguration>(defaults));
|
||||
services.AddTransient<IInitializer, ConfigurationInitializer<TConfiguration>>();
|
||||
|
||||
@@ -151,8 +153,6 @@ public static class IServiceCollectionExtensions
|
||||
if (contract.GetGenericArguments() is { Length: 1 } arguments)
|
||||
{
|
||||
Type notificationType = arguments[0];
|
||||
|
||||
//services.TryAdd(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime));
|
||||
services.Add(new ServiceDescriptor(typeof(INotificationHandler<>).MakeGenericType(notificationType), typeof(THandler), lifetime));
|
||||
}
|
||||
}
|
||||
@@ -182,14 +182,16 @@ public static class IServiceCollectionExtensions
|
||||
return services;
|
||||
}
|
||||
|
||||
//public static IServiceCollection AddNotificationPipeline<TFromNotification, TToNotification>(this IServiceCollection services)
|
||||
// where TFromNotification :
|
||||
// INotification
|
||||
// where TToNotification :
|
||||
// INotification, new()
|
||||
//{
|
||||
// return services.AddHandler<NotficationPipelineHandler<TFromNotification, TToNotification>>();
|
||||
//}
|
||||
public static IServiceCollection AddRange(this IServiceCollection services,
|
||||
IServiceCollection fromServices)
|
||||
{
|
||||
foreach (ServiceDescriptor service in fromServices)
|
||||
{
|
||||
services.Add(service);
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddWidgetTemplate<TWidgetContent>(this IServiceCollection services)
|
||||
where TWidgetContent :
|
||||
|
||||
@@ -21,7 +21,7 @@ public class Cache<TValue>(IDisposer disposer) :
|
||||
|
||||
public void Clear() => cache.Clear();
|
||||
|
||||
public IEnumerator<TValue> GetEnumerator() => cache.GetEnumerator();
|
||||
public System.Collections.Generic.IEnumerator<TValue> GetEnumerator() => cache.GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
@@ -57,7 +57,7 @@ public class Cache<TKey, TValue>(IDisposer disposer) :
|
||||
|
||||
public bool ContainsKey(TKey key) => cache.ContainsKey(key);
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => cache.GetEnumerator();
|
||||
public System.Collections.Generic.IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => cache.GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IViewModelEnumerator<TItem>
|
||||
public interface IEnumerator<TItem>
|
||||
{
|
||||
IEnumerable<TItem?> Next();
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IViewModelInitialization
|
||||
public interface IInitialization
|
||||
{
|
||||
ICommand Initialize { get; }
|
||||
|
||||
@@ -18,7 +18,7 @@ public partial class ObservableCollectionViewModel<TItem> :
|
||||
IDisposable
|
||||
{
|
||||
public ObservableCollection<TItem> collection = [];
|
||||
private readonly IViewModelEnumerator<TItem>? enumerator;
|
||||
private readonly IEnumerator<TItem>? enumerator;
|
||||
|
||||
public ObservableCollectionViewModel(IServiceFactory serviceFactory,
|
||||
IMediator mediator,
|
||||
@@ -36,7 +36,7 @@ public partial class ObservableCollectionViewModel<TItem> :
|
||||
public ObservableCollectionViewModel(IServiceFactory serviceFactory,
|
||||
IMediator mediator,
|
||||
IDisposer disposer,
|
||||
IViewModelEnumerator<TItem> enumerator)
|
||||
IEnumerator<TItem> enumerator)
|
||||
{
|
||||
ServiceFactory = serviceFactory;
|
||||
Mediator = mediator;
|
||||
@@ -201,7 +201,7 @@ public partial class ObservableCollectionViewModel<TItem> :
|
||||
|
||||
public void Dispose() => Disposer.Dispose(this);
|
||||
|
||||
public IEnumerator<TItem> GetEnumerator() =>
|
||||
public System.Collections.Generic.IEnumerator<TItem> GetEnumerator() =>
|
||||
collection.GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() =>
|
||||
|
||||
@@ -2,9 +2,5 @@
|
||||
|
||||
public interface IWidget
|
||||
{
|
||||
Guid Id { get; set; }
|
||||
|
||||
string? Name { get; set; }
|
||||
|
||||
string? Description { get; set; }
|
||||
IWidgetBuilder Create();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Hyperbar
|
||||
{
|
||||
public interface IWidgetAssemblyLoader
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,7 @@ namespace Hyperbar;
|
||||
|
||||
public interface IWidgetBuilder
|
||||
{
|
||||
void Create(IServiceCollection services);
|
||||
WidgetConfiguration Configuration { get; }
|
||||
|
||||
IServiceCollection Services { get; }
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class AssemblyProvider
|
||||
{
|
||||
public static void Load()
|
||||
{
|
||||
string extensionsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Extensions");
|
||||
Directory.CreateDirectory(extensionsPath);
|
||||
}
|
||||
|
||||
public static IEnumerable<Assembly> Get(string path)
|
||||
{
|
||||
string extensionsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Extensions");
|
||||
|
||||
Directory.CreateDirectory(extensionsPath);
|
||||
foreach (string assemblyPath in Directory.GetFiles(extensionsPath, "*.dll", SearchOption.AllDirectories))
|
||||
{
|
||||
yield return AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class IWidgetBuilderExtensions
|
||||
{
|
||||
public static IWidgetBuilder ConfigureServices(this IWidgetBuilder builder,
|
||||
Action<IServiceCollection> servicesDelegate)
|
||||
{
|
||||
servicesDelegate(builder.Services);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public class Widget :
|
||||
IWidget
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
public string? Name { get; set; }
|
||||
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class WidgetBuilder(WidgetConfiguration configuration) :
|
||||
IWidgetBuilder
|
||||
{
|
||||
public IServiceCollection Services => new ServiceCollection();
|
||||
|
||||
public WidgetConfiguration Configuration => configuration;
|
||||
|
||||
public static IWidgetBuilder Configure<TWidgetConfiguration>(Action<TWidgetConfiguration> configurationDelegate)
|
||||
where TWidgetConfiguration :
|
||||
WidgetConfiguration,
|
||||
new()
|
||||
{
|
||||
TWidgetConfiguration configuration = new();
|
||||
configurationDelegate(configuration);
|
||||
|
||||
return new WidgetBuilder(configuration);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,10 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
namespace Hyperbar;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class WidgetBuilder
|
||||
public abstract class WidgetConfiguration
|
||||
{
|
||||
public static void Config(IServiceCollection services,
|
||||
Action<IWidget> widgetDelegate)
|
||||
{
|
||||
Widget widget = new();
|
||||
widgetDelegate(widget);
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
services.AddSingleton(widget);
|
||||
}
|
||||
}
|
||||
public string? Name { get; set; }
|
||||
|
||||
public string? Description { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,11 +1,28 @@
|
||||
|
||||
namespace Hyperbar;
|
||||
namespace Hyperbar;
|
||||
|
||||
public class WidgetContext(IServiceProvider serviceProvider) :
|
||||
IInitializer
|
||||
{
|
||||
public IServiceProvider ServiceProvider => serviceProvider;
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class WidgetMonitor :
|
||||
IInitializer
|
||||
{
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public class WidgetManager :
|
||||
IInitializer
|
||||
{
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class WidgetEnumerator() :
|
||||
IEnumerator<IWidgetComponentViewModel>
|
||||
{
|
||||
public IEnumerable<IWidgetComponentViewModel?> Next()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user