Library featuring a command handler pattern.
$ dotnet add package ResultCommanderLibrary featuring a command handler pattern for both synchronous and asynchronous operations.
Utilizes Autofac thus Autofac is required and a functional result approach for failure-prone operations.
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.
Since the library utilizes Autofac, base Autofac configuration is required to use command handlers - Autofac's docs.
To register handlers with the DI container use the ContainerBuilder extension method provided by the library:
builder.AddCommandHandlers();
To register decorators or adapters use the methods available on CommanderConfiguration like so:
builder.AddCommandHandlers(options =>
{
options.AddDecorator<FancyDecorator, SimpleHandler>();
});
You can register multiple decorators and they'll be applied in the order that you register them - read more at .
You should never throw exceptions from within handlers, they should be exception free - instead return appropriate error results (and catch possible exceptions). 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();
}
}