This Commands library provides generic ICommand interfaces.
It also includes an implementation of a CompositeCommand which executes a list of ICommands in order.
The IResultCommand may be used in cases when the command has a status result which needs to be returned. For actual queries, please use an IQuery interface.
Available on nuget
PM> Install-Package ReneWiersma.Commands
public class ExampleCommand : ICommand
{
public bool IsExecuted { get; private set; }
public void Execute()
{
IsExecuted = true;
}
}The ICommand supports only one input parameter. If a command needs multiple input parameters, bundle them in a Parameter object.
public class ExampleStringCommand : ICommand<string>
{
public string Message { get; private set; }
public void Execute(string input)
{
Message = input;
}
}public class ExampleResultCommand : IResultCommand<Result>
{
public bool IsExecuted { get; private set; }
public Result Execute()
{
IsExecuted = true;
return Result.Ok();
}
}The IResultCommand supports only one input parameter. If a command needs multiple input parameters, bundle them in a Parameter object.
public class ExampleStringCommand : ICommand<string, Result>
{
public string Message { get; private set; }
public Result Execute(string input)
{
Message = input;
return Result.Ok();
}
}CompositeCommand accepts a list of ICommand parameters in the constructor, either as an IList, or as a parameter array. The latter approach is shown in this example.
public void MultipleCommandsAreExecuted()
{
var testCommand1 = new TestCommand();
var testCommand2 = new TestCommand();
var sut = new CompositeCommand(testCommand1, testCommand2);
sut.Execute();
Assert.IsTrue(testCommand1.IsExecuted);
Assert.IsTrue(testCommand2.IsExecuted);
}