The journeylism of @yreynhout

On CQRS, DDD(D), ES, …

Command Design Survey

A while back, I did a command design survey to get a feeling as to how people were designing their commands in a CQRS environment. A total of 28 people participated in the survey (excluding myself, as I didn’t want to bias the results in any way). I want to thank each one of you for taking the time to do so. Here are the results:

To the question “What else is special about your commands?” I got this compiled list:

  • Nothing
  • For now I use what ncqrs requires
  • They contain correlation id, user, role, time stamp, context reference
  • Use data annotations for validations. Commands are treated as value objects.
  • They are DTOs, no behaviour
  • Nothing, they are just standard nCQRS CommandBase implementations at the moment.
  • Simple, no behavior in commands, handlers may throw.
  • Validates them using a nsb message mutator on the sending side and a msg handler on the receiving side
  • Immutable, self describing, explicit
  • They are dead simple 🙂
  • The base class/interface contains an Id property of type Guid.
  • Getters/Setters to use the command as the ASP.NET MVC view model;nCQRS base class has a marker interface, so both really.;No constructor, so no exception from the constructor. Validation is done by ASP.NET MVC so the user can see the validation errors. I validate again in a command service interceptor, to catch base commands from other sources.
  • I have a ICommand.Validate method that gets called by the dispatcher (returns IEnumerable)

Come to your own conclusions.


2 responses to “Command Design Survey

  1. roundcrisis July 29, 2011 at 18:11

    I m reviewing command creation at the moment. and I m considering throwing when the command is not valid (for example a date is null ) and use a Dto for the command going over the network.
    It seems to me like there would be a lot of code to write to enable this and the only advantage i get is that I can share a validator; I would need to create this to validate the dto converted into a command in a valid way.
    To be honest I m not 100% sold on it, what are you doing at the moment?

  2. yreynhout July 30, 2011 at 11:03

    I define commands as POCOs (although sprinkled with any attributes to satisfy serialization needs). I don’t throw upon their construction (so, no constructor validation). I use separate command validators. This allows the construction of commands to be considered separate from the validation. Especially useful when reusing datastructures that might need to be validated differently depending on the command they are reused by.
    On a related note, I’m gradually moving away from getters and setters for command datastructures to a more builder-centric approach akin to what I’m doing for events.

    Below a brief sample of where I’m currently at:

    public class RolloutTemplateCommand : ICommand {
      public TemplateId Id { get; set; }
      public RolloutPeriod RolloutPeriod { get; set; }
    public class RolloutPeriod {
      public DateTime StartDate { get; set; }
      public DateTime? EndDate { get; set; }
    public class TemplateId {
      public Guid Id { get; set; }
    public class RolloutTemplateCommandValidator : FluentValidation.AbstractValidator<RolloutTemplateCommand> {
      public RolloutTemplateCommandValidator() {
        RuleFor(c => c.Id).NotNull().SetValidator(new TemplateIdValidator());
        RuleFor(c => c.RolloutPeriod).NotNull().SetValidator(new RolloutPeriodValidator());
    //TemplateIdValidator could enforce that you don't inadvertently pass in Guid.Empty
    //RolloutPeriodValidator could enforce that EndDate (if provided) must be greater than StartDate.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: