-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBaseCommandHandler.cs
50 lines (46 loc) · 2.09 KB
/
BaseCommandHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
#if !NET40
using System.Threading.Tasks;
#endif
namespace CoreDdd.Commands
{
/// <summary>
/// The base class to derive your command handlers from. Override method Execute to implement the command handling logic,
/// or override ExecuteAsync to implement the command handling logic asynchronously.
/// </summary>
/// <typeparam name="TCommand">The command type which implements <see cref="ICommand"/></typeparam>
public abstract class BaseCommandHandler<TCommand> : ICommandHandler<TCommand> where TCommand : ICommand
{
/// <summary>
/// Override this method to implement the command handling logic when handling a command in a non-async way.
/// </summary>
/// <param name="command">The command instance with data</param>
public virtual void Execute(TCommand command)
{
}
/// <summary>
/// Command executed event. Can be used for instance to raise an event with generated id for an aggregate root domain entity.
/// Commands should not return any values - to return values, use queries instead of commands.
/// </summary>
public event Action<CommandExecutedArgs> CommandExecuted;
/// <summary>
/// Raises a command executed event.
/// </summary>
/// <param name="eventArgs">Holds data generated during command execution</param>
protected void RaiseCommandExecutedEvent(CommandExecutedArgs eventArgs)
{
CommandExecuted?.Invoke(eventArgs);
}
#if !NET40
/// <summary>
/// Override this method to implement the command handling logic when handling a command in an async way.
/// </summary>
/// <param name="command">The command instance with data</param>
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public virtual async Task ExecuteAsync(TCommand command)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
}
#endif
}
}