From 186d244268c54542cdee4cd3d655e0391d570143 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Mon, 22 Jan 2024 20:46:41 +0000 Subject: [PATCH] more widget host work --- Hyperbar.Windows/App.xaml.cs | 5 +- .../IServiceCollectionExtensions.cs | 6 ++- Hyperbar/Lifecycles/IProxyService.cs | 6 +++ .../IProxyServiceCollection.cs} | 4 +- Hyperbar/Lifecycles/ProxyService.cs | 7 +++ .../ProxyServiceCollection.cs} | 6 +-- Hyperbar/Widgets/IWidgetBuilder.cs | 4 +- Hyperbar/Widgets/IWidgetHost.cs | 4 ++ Hyperbar/Widgets/IWidgetServiceBuilder.cs | 6 --- Hyperbar/Widgets/Started.cs | 3 ++ Hyperbar/Widgets/Stopped.cs | 3 ++ Hyperbar/Widgets/WidgetBuilder.cs | 51 ++++++++----------- Hyperbar/Widgets/WidgetContext.cs | 21 -------- Hyperbar/Widgets/WidgetHandler.cs | 27 ++++++---- Hyperbar/Widgets/WidgetHost.cs | 25 +++++++++ Hyperbar/Widgets/WidgetHostHander.cs | 18 +++++++ 16 files changed, 118 insertions(+), 78 deletions(-) create mode 100644 Hyperbar/Lifecycles/IProxyService.cs rename Hyperbar/{Widgets/IWidgetServiceCollection.cs => Lifecycles/IProxyServiceCollection.cs} (72%) create mode 100644 Hyperbar/Lifecycles/ProxyService.cs rename Hyperbar/{Widgets/WidgetServiceCollection.cs => Lifecycles/ProxyServiceCollection.cs} (61%) delete mode 100644 Hyperbar/Widgets/IWidgetServiceBuilder.cs create mode 100644 Hyperbar/Widgets/Started.cs create mode 100644 Hyperbar/Widgets/Stopped.cs delete mode 100644 Hyperbar/Widgets/WidgetContext.cs create mode 100644 Hyperbar/Widgets/WidgetHost.cs create mode 100644 Hyperbar/Widgets/WidgetHostHander.cs diff --git a/Hyperbar.Windows/App.xaml.cs b/Hyperbar.Windows/App.xaml.cs index 44540d3..e8e8c85 100644 --- a/Hyperbar.Windows/App.xaml.cs +++ b/Hyperbar.Windows/App.xaml.cs @@ -47,12 +47,11 @@ public partial class App : services.AddSingleton(); services.AddContentTemplate(); - services.AddTransient(provider => - new WidgetServiceCollection(services => + services.AddTransient>(provider => + new ProxyServiceCollection(services => { services.AddSingleton(); services.AddTransient(); - services.AddScoped(); services.AddHandler(); diff --git a/Hyperbar/Extensions/IServiceCollectionExtensions.cs b/Hyperbar/Extensions/IServiceCollectionExtensions.cs index ec9f663..5b562b4 100644 --- a/Hyperbar/Extensions/IServiceCollectionExtensions.cs +++ b/Hyperbar/Extensions/IServiceCollectionExtensions.cs @@ -12,9 +12,12 @@ public static class IServiceCollectionExtensions public static IServiceCollection AddDefault(this IServiceCollection services) { services.AddSingleton(provider => - new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!))); + new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!))); services.AddSingleton(); + services.AddSingleton>(provider => + new ProxyService(provider.GetRequiredService())); + services.AddSingleton(); services.AddTransient(); @@ -23,6 +26,7 @@ public static class IServiceCollectionExtensions services.AddHandler(); services.AddHandler(); services.AddHandler(); + services.AddHandler(); return services; } diff --git a/Hyperbar/Lifecycles/IProxyService.cs b/Hyperbar/Lifecycles/IProxyService.cs new file mode 100644 index 0000000..fbd98d8 --- /dev/null +++ b/Hyperbar/Lifecycles/IProxyService.cs @@ -0,0 +1,6 @@ +namespace Hyperbar; + +public interface IProxyService +{ + TService Proxy { get; } +} diff --git a/Hyperbar/Widgets/IWidgetServiceCollection.cs b/Hyperbar/Lifecycles/IProxyServiceCollection.cs similarity index 72% rename from Hyperbar/Widgets/IWidgetServiceCollection.cs rename to Hyperbar/Lifecycles/IProxyServiceCollection.cs index ff538fc..5084a23 100644 --- a/Hyperbar/Widgets/IWidgetServiceCollection.cs +++ b/Hyperbar/Lifecycles/IProxyServiceCollection.cs @@ -2,7 +2,7 @@ namespace Hyperbar; -public interface IWidgetServiceCollection +public interface IProxyServiceCollection { IServiceCollection Services { get; } -} +} \ No newline at end of file diff --git a/Hyperbar/Lifecycles/ProxyService.cs b/Hyperbar/Lifecycles/ProxyService.cs new file mode 100644 index 0000000..c5e22a9 --- /dev/null +++ b/Hyperbar/Lifecycles/ProxyService.cs @@ -0,0 +1,7 @@ +namespace Hyperbar; + +public class ProxyService(TService proxy) : + IProxyService +{ + public TService Proxy { get; private set; } = proxy; +} \ No newline at end of file diff --git a/Hyperbar/Widgets/WidgetServiceCollection.cs b/Hyperbar/Lifecycles/ProxyServiceCollection.cs similarity index 61% rename from Hyperbar/Widgets/WidgetServiceCollection.cs rename to Hyperbar/Lifecycles/ProxyServiceCollection.cs index 79c7337..6dcf6c8 100644 --- a/Hyperbar/Widgets/WidgetServiceCollection.cs +++ b/Hyperbar/Lifecycles/ProxyServiceCollection.cs @@ -2,10 +2,10 @@ namespace Hyperbar; -public class WidgetServiceCollection : - IWidgetServiceCollection +public class ProxyServiceCollection : + IProxyServiceCollection { - public WidgetServiceCollection(Action configureDelegate) + public ProxyServiceCollection(Action configureDelegate) { Services = new ServiceCollection(); configureDelegate.Invoke(Services); diff --git a/Hyperbar/Widgets/IWidgetBuilder.cs b/Hyperbar/Widgets/IWidgetBuilder.cs index a017677..9c38ab0 100644 --- a/Hyperbar/Widgets/IWidgetBuilder.cs +++ b/Hyperbar/Widgets/IWidgetBuilder.cs @@ -4,9 +4,9 @@ namespace Hyperbar; public interface IWidgetBuilder { - IWidgetBuilder ConfigureServices(Action configureDelegate); - IWidgetHost Build(); + + IWidgetBuilder ConfigureServices(Action configureDelegate); } public interface IWidgetBuilder : diff --git a/Hyperbar/Widgets/IWidgetHost.cs b/Hyperbar/Widgets/IWidgetHost.cs index 8e65433..cb70baf 100644 --- a/Hyperbar/Widgets/IWidgetHost.cs +++ b/Hyperbar/Widgets/IWidgetHost.cs @@ -2,5 +2,9 @@ public interface IWidgetHost { + IServiceProvider Services { get; } + Task StartAsync(); + + Task StopAsync(); } diff --git a/Hyperbar/Widgets/IWidgetServiceBuilder.cs b/Hyperbar/Widgets/IWidgetServiceBuilder.cs deleted file mode 100644 index 94322de..0000000 --- a/Hyperbar/Widgets/IWidgetServiceBuilder.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Hyperbar; - -public interface IWidgetServiceBuilder -{ - void ConfigureWidgetServices(IWidgetServiceCollection widgetServices); -} diff --git a/Hyperbar/Widgets/Started.cs b/Hyperbar/Widgets/Started.cs new file mode 100644 index 0000000..899d61e --- /dev/null +++ b/Hyperbar/Widgets/Started.cs @@ -0,0 +1,3 @@ +namespace Hyperbar; + +public record Started(TValue Value) : INotification; diff --git a/Hyperbar/Widgets/Stopped.cs b/Hyperbar/Widgets/Stopped.cs new file mode 100644 index 0000000..6a01900 --- /dev/null +++ b/Hyperbar/Widgets/Stopped.cs @@ -0,0 +1,3 @@ +namespace Hyperbar; + +public record Stopped(TValue Value) : INotification; \ No newline at end of file diff --git a/Hyperbar/Widgets/WidgetBuilder.cs b/Hyperbar/Widgets/WidgetBuilder.cs index 3b9b8bc..2aa914b 100644 --- a/Hyperbar/Widgets/WidgetBuilder.cs +++ b/Hyperbar/Widgets/WidgetBuilder.cs @@ -5,37 +5,32 @@ using System.Reflection; namespace Hyperbar.Widgets; -public class WidgetBuilder : - IWidgetBuilder, - IWidgetServiceBuilder +public class WidgetBuilder(TConfiguration configuration) : + IWidgetBuilder where TConfiguration : WidgetConfiguration, new() { - private readonly IHostBuilder hostBuilder; - - public WidgetBuilder(TConfiguration configuration) - { - hostBuilder = new HostBuilder() - .UseContentRoot(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + private readonly IHostBuilder hostBuilder = new HostBuilder() + .UseContentRoot(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Assembly.GetEntryAssembly()?.GetName().Name!), true) - .ConfigureAppConfiguration(config => - { - config.AddJsonFile("Settings.json", true, true); - }) - .ConfigureServices((context, services) => - { - services.AddScoped(provider => - new ServiceFactory((type, parameters) => - ActivatorUtilities.CreateInstance(provider, type, parameters!))); + .ConfigureAppConfiguration(config => + { + config.AddJsonFile("Settings.json", true, true); + }) + .ConfigureServices((context, services) => + { + services.AddScoped(provider => + new ServiceFactory((type, parameters) => + ActivatorUtilities.CreateInstance(provider, type, parameters!))); - services.AddHostedService(); + services.AddHostedService(); - services.AddScoped(); - services.AddScoped(); - services.AddConfiguration(configuration); - }); - } + services.AddScoped(); + services.AddScoped(); + + services.AddConfiguration(configuration); + }); public static IWidgetBuilder Configure(Action configurationDelegate) { @@ -57,7 +52,8 @@ public class WidgetBuilder : .GetResult(); } - return new WidgetHost(host); + return (IWidgetHost)ActivatorUtilities.CreateInstance(host.Services, + typeof(WidgetHost), host); } public IWidgetBuilder ConfigureServices(Action configureDelegate) @@ -65,9 +61,4 @@ public class WidgetBuilder : hostBuilder.ConfigureServices(configureDelegate.Invoke); return this; } - - public void ConfigureWidgetServices(IWidgetServiceCollection widgetServices) - { - hostBuilder.ConfigureServices(services => services.AddRange(widgetServices.Services)); - } } \ No newline at end of file diff --git a/Hyperbar/Widgets/WidgetContext.cs b/Hyperbar/Widgets/WidgetContext.cs deleted file mode 100644 index 7b763d4..0000000 --- a/Hyperbar/Widgets/WidgetContext.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.Extensions.Hosting; - -namespace Hyperbar; -public class WidgetHost : - IWidgetHost -{ - public WidgetHost(IHost host) - { - - } - - public void Start() - { - - } - - public void Stop() - { - - } -} \ No newline at end of file diff --git a/Hyperbar/Widgets/WidgetHandler.cs b/Hyperbar/Widgets/WidgetHandler.cs index 66f9fa3..84b716e 100644 --- a/Hyperbar/Widgets/WidgetHandler.cs +++ b/Hyperbar/Widgets/WidgetHandler.cs @@ -1,23 +1,30 @@ -namespace Hyperbar; +using Microsoft.Extensions.DependencyInjection; -public class WidgetHandler(IWidgetServiceCollection serviceCollection, +namespace Hyperbar; + +public class WidgetHandler(IProxyServiceCollection typedServices, + IServiceProvider provider, IMediator mediator) : INotificationHandler> { - public Task Handle(Created notification, + public async Task Handle(Created notification, CancellationToken cancellationToken) { if(notification.Value is IWidget widget) { - IWidgetBuilder widgetBuilder = widget.Create(); - if (widgetBuilder is IWidgetServiceBuilder serviceBuilder) + IWidgetBuilder builder = widget.Create(); + + builder.ConfigureServices(args => { - serviceBuilder.ConfigureWidgetServices(serviceCollection); - } + args.AddTransient(_ => provider.GetRequiredService>()); + args.AddRange(typedServices.Services); + }); - widgetBuilder.Build(); + IWidgetHost host = builder.Build(); + + await host.StartAsync(); + await mediator.PublishAsync(new Created(host), + cancellationToken); } - - return Task.CompletedTask; } } diff --git a/Hyperbar/Widgets/WidgetHost.cs b/Hyperbar/Widgets/WidgetHost.cs new file mode 100644 index 0000000..8fc7e67 --- /dev/null +++ b/Hyperbar/Widgets/WidgetHost.cs @@ -0,0 +1,25 @@ +using Microsoft.Extensions.Hosting; + +namespace Hyperbar; +public class WidgetHost(IHost host, + IProxyService proxyMediator) : + IWidgetHost +{ + public IServiceProvider Services => host.Services; + + public async Task StartAsync() + { + if (proxyMediator.Proxy is IMediator mediator) + { + await mediator.PublishAsync(new Started(this)); + } + } + + public async Task StopAsync() + { + if (proxyMediator.Proxy is IMediator mediator) + { + await mediator.SendAsync(new Started(this)); + } + } +} \ No newline at end of file diff --git a/Hyperbar/Widgets/WidgetHostHander.cs b/Hyperbar/Widgets/WidgetHostHander.cs new file mode 100644 index 0000000..75796f8 --- /dev/null +++ b/Hyperbar/Widgets/WidgetHostHander.cs @@ -0,0 +1,18 @@ +namespace Hyperbar; + +public class WidgetHostHander : + INotificationHandler>, + INotificationHandler> +{ + public Task Handle(Started notification, + CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + + public Task Handle(Stopped notification, + CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } +} \ No newline at end of file