Skip to content

Commit dd7e64d

Browse files
committed
Add FakeCommandHandler for testing ICommandHandler implementations
1 parent ac19693 commit dd7e64d

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace Odin.Patterns.CommandHandler;
2+
3+
/// <summary>
4+
/// Generic treatment of ICommandHandler for use in testing and other scenarios.
5+
/// </summary>
6+
/// <typeparam name="TCommand"></typeparam>
7+
public class FakeCommandHandler<TCommand> : ICommandHandler<TCommand>
8+
where TCommand : ICommand
9+
{
10+
/// <summary>
11+
/// Default constructor.
12+
/// </summary>
13+
public FakeCommandHandler()
14+
{
15+
16+
}
17+
18+
/// <summary>
19+
/// Does nothing.
20+
/// </summary>
21+
/// <param name="command"></param>
22+
/// <param name="ct"></param>
23+
/// <exception cref="NotImplementedException"></exception>
24+
public virtual async Task HandleAsync(TCommand command, CancellationToken ct = default)
25+
{
26+
await Task.CompletedTask;
27+
}
28+
}
29+
30+
/// <summary>
31+
/// Does not do anything.
32+
/// </summary>
33+
/// <typeparam name="TCommand"></typeparam>
34+
/// <typeparam name="TResult"></typeparam>
35+
public class FakeCommandHandler<TCommand, TResult> : ICommandHandler<TCommand, TResult>
36+
where TCommand : ICommand<TResult>
37+
{
38+
private readonly TResult _resultToReturn;
39+
40+
/// <summary>
41+
/// Initialise to return 'result' on HandleAsync.
42+
/// </summary>
43+
/// <param name="result"></param>
44+
public FakeCommandHandler(TResult result)
45+
{
46+
_resultToReturn = result;
47+
}
48+
49+
/// <summary>
50+
/// Does nothing.
51+
/// </summary>
52+
/// <param name="command"></param>
53+
/// <param name="ct"></param>
54+
/// <exception cref="NotImplementedException"></exception>
55+
public async Task<TResult> HandleAsync(TCommand command, CancellationToken ct = default)
56+
{
57+
return await Task.FromResult(_resultToReturn);
58+
}
59+
}

0 commit comments

Comments
 (0)