diff --git a/Toolkit.Foundation/ComponentConfiguration.cs b/Toolkit.Foundation/ComponentConfiguration.cs index 72feb26..f16b093 100644 --- a/Toolkit.Foundation/ComponentConfiguration.cs +++ b/Toolkit.Foundation/ComponentConfiguration.cs @@ -1,13 +1,6 @@ -using System.Text.Json.Serialization; - -namespace Toolkit.Foundation; +namespace Toolkit.Foundation; public record ComponentConfiguration { - public string? Description { get; set; } - public string? Name { get; set; } - - [JsonInclude] - internal Guid Id { get; set; } = Guid.NewGuid(); } \ No newline at end of file diff --git a/Toolkit.Foundation/ConfigurationInitializer.cs b/Toolkit.Foundation/ConfigurationInitializer.cs index 29977cb..537f9a5 100644 --- a/Toolkit.Foundation/ConfigurationInitializer.cs +++ b/Toolkit.Foundation/ConfigurationInitializer.cs @@ -1,9 +1,10 @@ namespace Toolkit.Foundation; -public class ConfigurationInitializer(IPublisher publisher, +public class ConfigurationInitializer(string section, IConfigurationReader reader, IConfigurationWriter writer, - IConfigurationFactory factory) : + IConfigurationFactory factory, + IPublisher publisher) : IConfigurationInitializer, IInitializer where TConfiguration : @@ -11,6 +12,7 @@ public class ConfigurationInitializer(IPublisher publisher, { public async Task Initialize() { + var d = section; if (!reader.TryRead(out TConfiguration? configuration)) { if (factory.Create() is object defaultConfiguration) diff --git a/Toolkit.Foundation/ConfigurationSource.cs b/Toolkit.Foundation/ConfigurationSource.cs index 8484f2b..2cc7e35 100644 --- a/Toolkit.Foundation/ConfigurationSource.cs +++ b/Toolkit.Foundation/ConfigurationSource.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.FileProviders; +using System; using System.Text.Encodings.Web; using System.Text.Json; +using System.Text.Json.Nodes; using System.Text.Json.Serialization; namespace Toolkit.Foundation; @@ -35,7 +37,6 @@ public class ConfigurationSource(IConfigurationFile(IConfigurationFile(this IServiceCollection services, + string section) + where TConfiguration : class => + services.AddConfiguration(section, "Settings.json", null); + public static IServiceCollection AddConfiguration(this IServiceCollection services) where TConfiguration : class => services.AddConfiguration(typeof(TConfiguration).Name, "Settings.json", null); @@ -63,6 +68,17 @@ public static class IServiceCollectionExtensions return services.AddConfiguration(typeof(TConfiguration).Name, "Settings.json", configuration); } + public static IServiceCollection AddConfiguration(this IServiceCollection services, + Action configurationDelegate, + string section) + where TConfiguration : class, new() + { + TConfiguration configuration = new(); + configurationDelegate.Invoke(configuration); + + return services.AddConfiguration(section, "Settings.json", configuration); + } + public static IServiceCollection AddConfiguration(this IServiceCollection services, TConfiguration configuration) where TConfiguration : class => @@ -81,20 +97,7 @@ public static class IServiceCollectionExtensions Action? serializerDelegate = null) where TConfiguration : class { - services.AddSingleton>(provider => - { - JsonSerializerOptions? defaultSerializer = null; - if (serializerDelegate is not null) - { - defaultSerializer = new JsonSerializerOptions(); - serializerDelegate.Invoke(defaultSerializer); - } - - return new ConfigurationSource(provider.GetRequiredService>(), - section, defaultSerializer); - }); - - services.AddSingleton>(provider => + services.TryAddSingleton>(provider => { IFileInfo? fileInfo = null; if (provider.GetService() is IHostEnvironment hostEnvironment) @@ -107,15 +110,39 @@ public static class IServiceCollectionExtensions return new ConfigurationFile(fileInfo); }); - services.AddHostedService>(); - services.AddSingleton, ConfigurationReader>(); - services.AddSingleton, ConfigurationWriter>(); + services.TryAddKeyedSingleton>(section, (provider, KeyAccelerator) => + { + JsonSerializerOptions? defaultSerializer = null; + if (serializerDelegate is not null) + { + defaultSerializer = new JsonSerializerOptions(); + serializerDelegate.Invoke(defaultSerializer); + } - services.AddTransient>(provider => new ConfigurationFactory(() => - configuration ?? provider.GetRequiredService())); + return new ConfigurationSource(provider.GetRequiredService>(), + section, defaultSerializer); + }); - services.AddTransient>(); - services.AddTransient, ConfigurationInitializer>(); + //services.AddHostedService>(); + services.TryAddKeyedTransient>(section, (provider, key) => + new ConfigurationReader(provider.GetRequiredKeyedService>(key), + provider.GetRequiredKeyedService>(key))); + + services.TryAddKeyedTransient>(section, (provider, key) => + new ConfigurationWriter(provider.GetRequiredKeyedService>(key))); + + services.TryAddKeyedTransient>(section, (provider, key) => + new ConfigurationFactory(() => configuration ?? provider.GetRequiredService())); + + services.AddTransient>(provider => + new ConfigurationInitializer(section, + provider.GetRequiredKeyedService>(section), + provider.GetRequiredKeyedService>(section), + provider.GetRequiredKeyedService>(section), + provider.GetRequiredService())); + + services.AddTransient, ConfigurationInitializer>(provider => + provider.GetRequiredService().Create>(section)); services.AddTransient, WritableConfiguration>(); @@ -125,31 +152,6 @@ public static class IServiceCollectionExtensions return services; } - public static IServiceCollection AddTemplate(this IServiceCollection services, - object? key = null, - params object[]? parameters) - { - Type viewModelType = typeof(TViewModel); - Type viewType = typeof(TView); - - key ??= viewModelType.Name.Replace("ViewModel", ""); - - services.AddTransient(viewModelType, provider => - provider.GetRequiredService().Create(parameters)!); - - services.AddTransient(viewType); - - services.AddKeyedTransient(viewModelType, key, (provider, key) => - provider.GetRequiredService().Create(parameters)!); - - services.AddKeyedTransient(viewType, key); - - services.AddTransient(provider => - new ContentTemplateDescriptor(key, viewModelType, viewType, parameters)); - - return services; - } - public static IServiceCollection AddHandler(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Transient) where THandler : IHandler @@ -169,7 +171,7 @@ public static class IServiceCollectionExtensions services.TryAdd(new ServiceDescriptor(typeof(INotificationHandler<>) .MakeGenericType(notificationType), typeof(THandler), lifetime)); - services.Add(new ServiceDescriptor(wrapperType, provider => + services.Add(new ServiceDescriptor(wrapperType, provider => provider.GetService()?.Create(wrapperType, provider.GetRequiredService(typeof(INotificationHandler<>).MakeGenericType(notificationType)), provider.GetServices(typeof(IPipelineBehavior<>) @@ -185,10 +187,10 @@ public static class IServiceCollectionExtensions Type wrapperType = typeof(HandlerWrapper<,>) .MakeGenericType(requestType, responseType); - services.TryAdd(new ServiceDescriptor(typeof(THandler), + services.TryAdd(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime)); - services.Add(new ServiceDescriptor(wrapperType, provider => + services.Add(new ServiceDescriptor(wrapperType, provider => provider.GetService()?.Create(wrapperType, provider.GetRequiredService(), provider.GetServices(typeof(IPipelineBehavior<,>) @@ -203,7 +205,7 @@ public static class IServiceCollectionExtensions } public static IServiceCollection AddInitializer(this IServiceCollection services) - where TInitializer : class, + where TInitializer : class, IInitializer { services.AddTransient(); @@ -211,7 +213,7 @@ public static class IServiceCollectionExtensions } public static IServiceCollection AddNavigateHandler(this IServiceCollection services) - where THandler : INavigateHandler, + where THandler : INavigateHandler, IHandler { IEnumerable contracts = typeof(THandler).GetInterfaces() @@ -231,6 +233,7 @@ public static class IServiceCollectionExtensions services.AddHandler(); return services; } + public static IServiceCollection AddRange(this IServiceCollection services, IServiceCollection fromServices) { @@ -241,4 +244,29 @@ public static class IServiceCollectionExtensions return services; } + + public static IServiceCollection AddTemplate(this IServiceCollection services, + object? key = null, + params object[]? parameters) + { + Type viewModelType = typeof(TViewModel); + Type viewType = typeof(TView); + + key ??= viewModelType.Name.Replace("ViewModel", ""); + + services.AddTransient(viewModelType, provider => + provider.GetRequiredService().Create(parameters)!); + + services.AddTransient(viewType); + + services.AddKeyedTransient(viewModelType, key, (provider, key) => + provider.GetRequiredService().Create(parameters)!); + + services.AddKeyedTransient(viewType, key); + + services.AddTransient(provider => + new ContentTemplateDescriptor(key, viewModelType, viewType, parameters)); + + return services; + } } \ No newline at end of file diff --git a/Toolkit.Foundation/NavigationViewModel.cs b/Toolkit.Foundation/NavigationViewModel.cs deleted file mode 100644 index f9052e7..0000000 --- a/Toolkit.Foundation/NavigationViewModel.cs +++ /dev/null @@ -1,31 +0,0 @@ -using CommunityToolkit.Mvvm.ComponentModel; - -namespace Toolkit.Foundation; - -public partial class NavigationViewModel(IServiceProvider serviceProvider, - IServiceFactory serviceFactory, - IPublisher publisher, - ISubscriber subscriber, - IDisposer disposer, - string text) : - ObservableViewModel(serviceProvider, serviceFactory, publisher, subscriber, disposer), - INavigationViewModel -{ - [ObservableProperty] - private string? text = text; -} - -public partial class NavigationViewModel(IServiceProvider serviceProvider, - IServiceFactory serviceFactory, - IPublisher publisher, - ISubscriber subscriber, - IDisposer disposer, - string text) : - ObservableViewModel(serviceProvider, serviceFactory, publisher, subscriber, disposer), - INavigationViewModel - where TNavigationViewModel : - INavigationViewModel -{ - [ObservableProperty] - private string? text = text; -}