Peter Morlion logo


CQRS/ES In .NET: The Command Object

Let’s start with the C in CQRS. As you know by now, the C is for “Command.” In our implementation of CQRS/ES, a command will be a basic DTO object. Later, we’ll see that CommandHandlers will take such a command and perform the necessary calls to domain objects. But let’s start simple.

As stated, a command will be a simple DTO object, without any special logic. In my case, without any logic at all, except for a constructor. The name of the command should be something that makes sense from a business language point of view. In DDD terms, you should use the ubiquitous language.

Here is an example:

public class AddChatMessage : ICommand
    public Guid Id { get; }
    public Guid ChatId { get; }
    public int UserId { get; }
    public string Message { get; }
    public DateTime DateTime { get; }

    public AddChatMessage(Guid chatId, int userId, string message, DateTime dateTime) {
        Id = Guid.NewGuid();
        ChatId = chatId;
        UserId = userId;
        Message = message;
        DateTime = dateTime;

It should be clear what this command is for. It’s for adding a message to a chat.

You can also see there is a constructor that takes some parameters. This is the information that is necessary for the handling of the command, which we’ll see later. This information could come from a HTTP POST request, handled by an ASP.NET controller, for example.

Notice how the command implements the ICommand interface. We’ll use this in our CommandHandlers later, but that’s for the next post in this series.

For now, just remember that in our CQRS/ES implementation, a command is a DTO objects that contains no logic. It contains all the information that is necessary to handle the command. This information is often provided by an end-user.

Leave a Reply

Your email address will not be published. Required fields are marked *