|
| 1 | +# Commands |
| 2 | + |
| 3 | +Commands are the basic value objects, or models, that represent write operations |
| 4 | +that you can perform in your domain. |
| 5 | + |
| 6 | +As an example, one might implement create this command for updating user |
| 7 | +passwords. |
| 8 | + |
| 9 | +```csharp |
| 10 | +public class UserUpdatePasswordCommand : Command<UserAggregate, UserId> |
| 11 | +{ |
| 12 | + public Password NewPassword { get; private set; } |
| 13 | + public Password OldPassword { get; private set; } |
| 14 | + |
| 15 | + public UserUpdatePasswordCommand( |
| 16 | + UserId id, |
| 17 | + Password newPassword, |
| 18 | + Password oldPassword) |
| 19 | + : base(id) |
| 20 | + { |
| 21 | + Username = username; |
| 22 | + Password = password; |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +Note that the `Password` class is merely a value object created to hold the |
| 28 | +password and do basic validation. Read the article regarding |
| 29 | +[value objects](./ValueObjects.md) for more information. Also, you don't |
| 30 | +have to use the default EventFlow `Command<,>` implementation, you can create |
| 31 | +your own, it merely have to implement the `ICommand<,>` interface. |
| 32 | + |
| 33 | +A command by itself doesn't do anything and will throw an exception if |
| 34 | +published. To make a command work, you need to implement one (and only one) |
| 35 | +command handler which is responsible for invoking the aggregate. |
| 36 | + |
| 37 | +```csharp |
| 38 | +public class UserUpdatePasswordCommandHandler : |
| 39 | + CommandHandler<UserAggregate, UserId, UserUpdatePasswordCommand> |
| 40 | +{ |
| 41 | + public override Task ExecuteAsync( |
| 42 | + UserAggregate aggregate, |
| 43 | + UserUpdatePasswordCommand command, |
| 44 | + CancellationToken cancellationToken) |
| 45 | + { |
| 46 | + aggregate.UpdatePassword( |
| 47 | + command.OldPassword, |
| 48 | + command.NewPassword); |
| 49 | + return Task.FromResult(0); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Ensure idempotency |
| 55 | + |
| 56 | +Detecting duplicate operations can be hard, especially if you have a |
| 57 | +distributed application, or simply a web application. Consider the following |
| 58 | +simplified scenario. |
| 59 | + |
| 60 | +1. The user wants to change his password |
| 61 | +1. The user fills in the "change password form" |
| 62 | +1. As user is impatient, or by accident, the user submits the for twice |
| 63 | +1. The first web request completes and the password is changed. However, as |
| 64 | + the browser is waiting on the first web request, this result is ignored |
| 65 | +1. The second web request throws a domain error as the "old password" doesn't |
| 66 | + match as the current password has already been changed |
| 67 | +1. The user is presented with a error on the web page |
| 68 | + |
| 69 | +Handling this is simple, merely ensure that the aggregate is idempotent |
| 70 | +is regards to password changes. But instead of implementing this yourself, |
| 71 | +EventFlow has support for it and its simple to utilize and is done per |
| 72 | +command. |
| 73 | + |
| 74 | +To use the functionality, merely ensure that commands that represent the |
| 75 | +same operation has the same `ISourceId` which implements `IIdentity` like |
| 76 | +the example blow. |
| 77 | + |
| 78 | +```csharp |
| 79 | +public class UserUpdatePasswordCommand : Command<UserAggregate, UserId> |
| 80 | +{ |
| 81 | + public Password NewPassword { get; private set; } |
| 82 | + public Password OldPassword { get; private set; } |
| 83 | + |
| 84 | + public UserCreateCommand( |
| 85 | + UserId id, |
| 86 | + ISourceId sourceId, |
| 87 | + Password newPassword, |
| 88 | + Password oldPassword) |
| 89 | + : base(id, sourceId) |
| 90 | + { |
| 91 | + Username = username; |
| 92 | + Password = password; |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +Note the use of the other `protected` constructor of `Command<,>` that |
| 98 | +takes a `ISourceId` in addition to the aggregate root identity. |
| 99 | + |
| 100 | +If a duplicate command is detected, a `DuplicateOperationException` is thrown. |
| 101 | +The application could then ignore the exception or report the problem to the |
| 102 | +end user. |
| 103 | + |
| 104 | +The default `ISourceId` history size of the aggregate root, is ten. But it can |
| 105 | +be configured using the `SetSourceIdHistory(...)` that must be called from |
| 106 | +within the aggregate root constructor. |
| 107 | + |
| 108 | + |
| 109 | +### Easier ISourceId calculation |
| 110 | + |
| 111 | +Ensuring the correct calculation of the command `ISourceId` can be somewhat |
| 112 | +cumbersome, which is why EventFlow provides another base command you can use, |
| 113 | +the `DistinctCommand<,>`. By using the `DistinctCommand<,>` you merely have |
| 114 | +to implement the `GetSourceIdComponents()` and providing the |
| 115 | +`IEnumerable<byte[]>` that makes the command unique. The bytes is used to |
| 116 | +create a deterministic GUID to be used as an `ISourceId`. |
| 117 | + |
| 118 | +```csharp |
| 119 | +public class UserUpdatePasswordCommand : |
| 120 | + DistinctCommand<UserAggregate, UserId> |
| 121 | +{ |
| 122 | + public Password NewPassword { get; private set; } |
| 123 | + public Password OldPassword { get; private set; } |
| 124 | + |
| 125 | + public UserUpdatePasswordCommand( |
| 126 | + UserId id, |
| 127 | + Password newPassword, |
| 128 | + Password oldPassword) |
| 129 | + : base(id) |
| 130 | + { |
| 131 | + Username = username; |
| 132 | + Password = password; |
| 133 | + } |
| 134 | + |
| 135 | + protected override IEnumerable<byte[]> GetSourceIdComponents() |
| 136 | + { |
| 137 | + yield return NewPassword.GetBytes(); |
| 138 | + yield return OldPassword.GetBytes(); |
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +The `GetBytes()` merely returns the `Encoding.UTF8.GetBytes(...)` of the |
| 144 | +password. |
| 145 | + |
| 146 | +Its important that you don't use the `GetHashCode()`, as the implementation |
| 147 | +is different for e.g. `string` on 32 bit and 64 bit .NET. |
0 commit comments