Compression
This commit is contained in:
@@ -4,7 +4,7 @@ namespace FileTime.App.Core.Services;
|
||||
|
||||
public interface IIdentifiableUserCommandService
|
||||
{
|
||||
void AddIdentifiableUserCommandFactory(string identifier, IIdentifiableUserCommand commandFactory);
|
||||
void AddIdentifiableUserCommand(IIdentifiableUserCommand command);
|
||||
IIdentifiableUserCommand? GetCommand(string identifier);
|
||||
IReadOnlyCollection<string> GetCommandIdentifiers();
|
||||
IReadOnlyDictionary<string, IIdentifiableUserCommand> IdentifiableUserCommands { get; }
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace FileTime.App.Core.Services.UserCommandHandler;
|
||||
|
||||
public abstract class AggregatedUserCommandHandler : IUserCommandHandler
|
||||
{
|
||||
private readonly List<IUserCommandHandler> _userCommandHandlers = new();
|
||||
|
||||
public bool CanHandleCommand(UserCommand.IUserCommand command) => _userCommandHandlers.Any(h => h.CanHandleCommand(command));
|
||||
|
||||
public async Task HandleCommandAsync(UserCommand.IUserCommand command)
|
||||
{
|
||||
var handler = _userCommandHandlers.Find(h => h.CanHandleCommand(command));
|
||||
|
||||
if (handler is null) return;
|
||||
await handler.HandleCommandAsync(command);
|
||||
}
|
||||
|
||||
protected void AddCommandHandler(IUserCommandHandler userCommandHandler) => _userCommandHandlers.Add(userCommandHandler);
|
||||
|
||||
protected void AddCommandHandler(IEnumerable<IUserCommandHandler> commandHandlers)
|
||||
{
|
||||
foreach (var userCommandHandler in commandHandlers)
|
||||
{
|
||||
AddCommandHandler(userCommandHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using FileTime.App.Core.UserCommand;
|
||||
|
||||
namespace FileTime.App.Core.Services.UserCommandHandler;
|
||||
|
||||
public class TypeUserCommandHandler<T> : IUserCommandHandler where T : IUserCommand
|
||||
{
|
||||
private readonly Func<T, Task> _handler;
|
||||
|
||||
public TypeUserCommandHandler(Func<T, Task> handler)
|
||||
{
|
||||
_handler = handler;
|
||||
}
|
||||
|
||||
public TypeUserCommandHandler(Func<Task> handler)
|
||||
{
|
||||
_handler = async (_) => await handler();
|
||||
}
|
||||
|
||||
public bool CanHandleCommand(IUserCommand command) => command is T;
|
||||
|
||||
public Task HandleCommandAsync(IUserCommand command)
|
||||
{
|
||||
if (command is not T typedCommand) throw new ArgumentException($"Parameter '{nameof(command)}' is not of type '{typeof(T).Name}'");
|
||||
return _handler(typedCommand);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user