Add project files.

This commit is contained in:
Daniel Clark
2022-11-01 15:26:08 +00:00
parent daa7b59f22
commit 7e4f880821
408 changed files with 16863 additions and 0 deletions
+19
View File
@@ -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;
}
}
}
+24
View File
@@ -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; }
}
}
+47
View File
@@ -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));
}
}
}
}
+8
View File
@@ -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
{
}
}
+7
View File
@@ -0,0 +1,7 @@
namespace TheXamlGuy.Framework.Core
{
public interface IEventHandler<TTEvent> : IInitializer, IDisposable where TTEvent : class
{
}
}