Add project files.
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public class EventBuilder : IEventBuilder
|
||||
{
|
||||
private readonly List<IEventBuilderConfiguration> configurations = new();
|
||||
|
||||
public IReadOnlyCollection<IEventBuilderConfiguration> Configurations => new ReadOnlyCollection<IEventBuilderConfiguration>(configurations);
|
||||
|
||||
public IEventBuilderConfiguration<TEvent> Add<TEvent>() where TEvent : class
|
||||
{
|
||||
EventConfiguration<TEvent>? configuration = new();
|
||||
configurations.Add(configuration);
|
||||
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public class EventConfiguration<TEvent> : IEventBuilderConfiguration<TEvent> where TEvent : class
|
||||
{
|
||||
private readonly List<IEventDescriptor> descriptors = new();
|
||||
|
||||
public EventConfiguration()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<IEventDescriptor> Descriptors => new ReadOnlyCollection<IEventDescriptor>(descriptors);
|
||||
|
||||
public Action<IServiceProvider, TEvent>? Factory { get; }
|
||||
|
||||
public Action<TEvent>? Next { get; }
|
||||
|
||||
public IEventBuilderConfiguration<TEvent> WithHandler<THandlerEvent>() where THandlerEvent : class
|
||||
{
|
||||
descriptors.Add(new EventDescriptor<TEvent, THandlerEvent>());
|
||||
return this;
|
||||
}
|
||||
|
||||
public IEventBuilderConfiguration<TEvent> WithHandler<THandlerEvent>(Func<TEvent, THandlerEvent> factoryDelegate) where THandlerEvent : class
|
||||
{
|
||||
descriptors.Add(new EventDescriptor<TEvent, THandlerEvent>(factoryDelegate));
|
||||
return this;
|
||||
}
|
||||
|
||||
public IEventBuilderConfiguration<TEvent> WithHandler<THandlerEvent>(Func<IServiceProvider, TEvent, THandlerEvent> factoryDelegate) where THandlerEvent : class
|
||||
{
|
||||
descriptors.Add(new EventDescriptor<TEvent, THandlerEvent>(factoryDelegate));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public record EventDescriptor<TEvent, THandlerEvent> : IEventDescriptor
|
||||
{
|
||||
public EventDescriptor()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public EventDescriptor(Func<IServiceProvider, TEvent, THandlerEvent>? factoryDelegate)
|
||||
{
|
||||
FactoryDelegate = factoryDelegate;
|
||||
}
|
||||
|
||||
public EventDescriptor(Func<TEvent, THandlerEvent>? nextDelegate)
|
||||
{
|
||||
NextDelegate = nextDelegate;
|
||||
}
|
||||
|
||||
public Func<IServiceProvider, TEvent, THandlerEvent>? FactoryDelegate { get; }
|
||||
|
||||
public Func<TEvent, THandlerEvent>? NextDelegate { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public class EventHandler<TTEvent> : IEventHandler<TTEvent> where TTEvent : class
|
||||
{
|
||||
private readonly IEventBuilderConfiguration<TTEvent> configuration;
|
||||
private readonly IDisposer disposer;
|
||||
private readonly IEventAggregator eventAggregator;
|
||||
private readonly IMediator mediator;
|
||||
private readonly IServiceFactory serviceFactory;
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
public EventHandler(IEventBuilderConfiguration<TTEvent> configuration,
|
||||
IServiceProvider serviceProvider,
|
||||
IServiceFactory serviceFactory,
|
||||
IEventAggregator eventAggregator,
|
||||
IMediator mediator,
|
||||
IDisposer disposer)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
this.serviceProvider = serviceProvider;
|
||||
this.serviceFactory = serviceFactory;
|
||||
this.eventAggregator = eventAggregator;
|
||||
this.mediator = mediator;
|
||||
this.disposer = disposer;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
disposer.Dispose(this);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
disposer.Add(this, eventAggregator.SubscribeUI<TTEvent>(OnEvent));
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void OnEvent(TTEvent args)
|
||||
{
|
||||
foreach (IEventDescriptor? descriptor in configuration.Descriptors)
|
||||
{
|
||||
mediator.Handle((descriptor as dynamic).NextDelegate?.Invoke(args) ?? (descriptor as dynamic).FactoryDelegate?.Invoke(serviceProvider, args));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace TheXamlGuy.Framework.Core;
|
||||
|
||||
public interface IEventBuilder
|
||||
{
|
||||
IReadOnlyCollection<IEventBuilderConfiguration> Configurations { get; }
|
||||
|
||||
IEventBuilderConfiguration<TEvent> Add<TEvent>() where TEvent : class;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public interface IEventBuilderConfiguration
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface IEventBuilderConfiguration<TEvent> : IEventBuilderConfiguration where TEvent : class
|
||||
{
|
||||
IReadOnlyCollection<IEventDescriptor> Descriptors { get; }
|
||||
|
||||
Action<IServiceProvider, TEvent>? Factory { get; }
|
||||
|
||||
Action<TEvent>? Next { get; }
|
||||
|
||||
IEventBuilderConfiguration<TEvent> WithHandler<THandlerEvent>() where THandlerEvent : class;
|
||||
|
||||
IEventBuilderConfiguration<TEvent> WithHandler<THandlerEvent>(Func<TEvent, THandlerEvent> factoryDelegate) where THandlerEvent : class;
|
||||
|
||||
IEventBuilderConfiguration<TEvent> WithHandler<THandlerEvent>(Func<IServiceProvider, TEvent, THandlerEvent> factoryDelegate) where THandlerEvent : class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public interface IEventDescriptor
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace TheXamlGuy.Framework.Core
|
||||
{
|
||||
public interface IEventHandler<TTEvent> : IInitializer, IDisposable where TTEvent : class
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user