inject the widget assembly into the IoC as we are going to need it further in

This commit is contained in:
TheXamlGuy
2024-01-27 16:21:53 +00:00
parent 48925b89ff
commit 640b3292b2
16 changed files with 105 additions and 64 deletions
@@ -7,12 +7,12 @@ public static class IServiceCollectionExtensions
{
public static IServiceCollection AddWidget(this IServiceCollection services)
{
services.AddTransient<IInitializer, WidgetInitializer>();
services.AddTransient<IInitializer, WidgetExtensionInitializer>();
services.AddTransient<IFactory<Type, IWidget>, WidgetFactory>();
services.AddHandler<WidgetEnumerator>();
services.AddHandler<WidgetAssemblyHandler>();
services.AddHandler<WidgetHandler>();
services.AddHandler<WidgetExtensionEnumerator>();
services.AddHandler<WidgetExtensionHandler>();
services.AddHandler<WidgetExtensionHandler>();
services.AddHandler<WidgetHostHandler>();
return services;
+8
View File
@@ -0,0 +1,8 @@
using System.Reflection;
namespace Hyperbar.Widget;
public interface IWidgetAssembly
{
public Assembly Assembly { get; }
}
@@ -1,7 +0,0 @@
namespace Hyperbar.Widget;
public interface IWidgetResourceInitialization :
IInitializer
{
}
+1 -2
View File
@@ -2,5 +2,4 @@
namespace Hyperbar.Widget;
public record WidgetAssembly(Assembly? Assembly = default) :
INotification;
public record WidgetAssembly(Assembly Assembly) : IWidgetAssembly;
-23
View File
@@ -1,23 +0,0 @@
using System.Reflection;
namespace Hyperbar.Widget;
public class WidgetAssemblyHandler(IMediator mediator,
IFactory<Type, IWidget> factory) :
INotificationHandler<Created<Assembly>>
{
public Task Handle(Created<Assembly> notification,
CancellationToken cancellationToken)
{
if (notification.Value?.GetTypes().FirstOrDefault(x => typeof(IWidget).IsAssignableFrom(x)) is Type widgetType)
{
if (factory.Create(widgetType) is IWidget widget)
{
mediator.PublishAsync(new Created<IWidget>(widget),
cancellationToken);
}
}
return Task.CompletedTask;
}
}
+4
View File
@@ -0,0 +1,4 @@
using System.Reflection;
namespace Hyperbar.Widget;
public record WidgetExtension(IWidget Widget, IWidgetAssembly Assembly);
@@ -4,12 +4,12 @@ using System.Runtime.Loader;
namespace Hyperbar.Widget;
public class WidgetEnumerator(IFactory<Type, IWidget> factory,
public class WidgetExtensionEnumerator(IFactory<Type, IWidget> factory,
IHostEnvironment hostEnvironment,
IMediator mediator) :
INotificationHandler<Enumerate<IWidget>>
INotificationHandler<Enumerate<WidgetExtension>>
{
public Task Handle(Enumerate<IWidget> notification,
public Task Handle(Enumerate<WidgetExtension> notification,
CancellationToken cancellationToken)
{
string extensionsDirectory = Path.Combine(hostEnvironment.ContentRootPath, "Extensions");
@@ -25,12 +25,12 @@ public class WidgetEnumerator(IFactory<Type, IWidget> factory,
Parallel.ForEach(assemblyPaths, async (string assemblyPath) =>
{
Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
if (assembly.GetTypes().FirstOrDefault(x => typeof(IWidget).IsAssignableFrom(x)) is Type widgetType)
foreach (Type widgetType in assembly.GetTypes().Where(x => typeof(IWidget).IsAssignableFrom(x)))
{
if (factory.Create(widgetType) is IWidget widget)
{
await mediator.PublishAsync(new Created<IWidget>(widget),
cancellationToken);
await mediator.PublishAsync(new Created<WidgetExtension>(new WidgetExtension(widget,
new WidgetAssembly(assembly))), cancellationToken);
}
}
});
@@ -2,20 +2,21 @@
namespace Hyperbar.Widget;
public class WidgetHandler(IProxyServiceCollection<IWidgetBuilder> typedServices,
public class WidgetExtensionHandler(IProxyServiceCollection<IWidgetBuilder> typedServices,
IServiceProvider provider,
IMediator mediator) :
INotificationHandler<Created<IWidget>>
INotificationHandler<Created<WidgetExtension>>
{
public async Task Handle(Created<IWidget> notification,
public async Task Handle(Created<WidgetExtension> notification,
CancellationToken cancellationToken)
{
if(notification.Value is IWidget widget)
if(notification.Value is WidgetExtension widgetExtension)
{
IWidgetBuilder builder = widget.Create();
IWidgetBuilder builder = widgetExtension.Widget.Create();
builder.ConfigureServices(args =>
{
args.AddSingleton(widgetExtension.Assembly);
args.AddTransient(_ => provider.GetRequiredService<IProxyService<IMediator>>());
args.AddRange(typedServices.Services);
});
@@ -0,0 +1,8 @@
namespace Hyperbar.Widget;
public class WidgetExtensionInitializer(IMediator mediator) :
IInitializer
{
public async Task InitializeAsync() =>
await mediator.PublishAsync<Enumerate<WidgetExtension>>();
}
-11
View File
@@ -1,11 +0,0 @@
namespace Hyperbar.Widget;
public class WidgetInitializer(IMediator mediator) :
IInitializer
{
public Task InitializeAsync()
{
mediator.PublishAsync<Enumerate<IWidget>>();
return Task.CompletedTask;
}
}