Library featuring a command handler pattern.
$ dotnet add package ResultCommanderLibrary featuring a command handler pattern for both synchronous and asynchronous operations.
Uses a functional Result approach for failure-prone operations.
To utilize all features using Autofac is required.
There are two command types - one that only returns a Result and one that returns an additional entity contained within the Result.
Every handler must return a Result struct which determines whether the operation succedeed or not, handlers may or may not return additional results contained within the Result struct - this is defined by the handled comand.
To register handlers with the DI container use the ContainerBuilder or IServiceCollection extension methods provided by the library:
builder.AddResultCommander();
To register decorators or adapters use the methods available on ResultCommanderConfiguration like so:
builder.AddResultCommander(options =>
{
options.AddDecorator<FancyDecorator, ISyncCommandHandler<SimpleCommand>();
});
You can register multiple decorators and they'll be applied in the order that you register them - read more at Autofac's docs regarding decorators and adapters.
<b> You should never throw exceptions from within handlers, they should be exception free - instead return appropriate error results (and catch possible exceptions).</b> Library offers a simple error catching, logging and exception to result error decorators for uses where writing try-catch blocks becomes a pain - but remember that these results will never be as informative as manually returned proper result error types.
A command without a concrete result:
public SimpleCommand : ICommand
{
public bool IsSuccess { get; }
public SimpleCommand(bool isSuccess = true)
=> IsSuccess = isSuccess;
}And a synchronous handler that handles it:
public SimpleSyncCommandHandler : ISyncCommandHandler<SimpleCommand>
{
Result Handle(SimpleCommand command)
{
if (command.IsSuccess)
return Result.FromSuccess();
return new InvalidOperationError();
}
}A command with a concrete result:
public SimpleCommandWithConcreteResult : ICommand<int>
{
public bool IsSuccess { get; }
public SimpleCommand(bool isSuccess = true)
=> IsSuccess = isSuccess;
}And a synchronous handler that handles it:
public SimpleSyncCommandHandlerWithConcreteResult : ISyncCommandHandler<SimpleCommand, int>
{
Result<int> Handle(SimpleCommand command)
{
if (command.IsSuccess)
return 1;
return new InvalidOperationError();
}
}