Command Validations - CQRS

Asked

Viewed 67 times

1

I’m learning about CQRS and in a Hangout promoted by a Micrososft MVP he presented an implementation of Command where the model data are validated within the validation of the Command. Date validations or if there are blank mandatory fields, for example, were not in the model making the model entity almost anemic. Then came the doubts, that despite the MVP’s explanation that for me was not very convincing.

It is really good practice to implement model validation within the validation of the Command ?

Example of the approach used in Hungout:

public abstract class UserCommand : Command
{
    public Guid Id { get; protected set; }

    public string Name { get; protected set; }

    public string Email { get; protected set; }

    public DateTime BirthDate { get; protected set; }
}

public class RegisterNewUserCommand : UserCommand
{
    public RegisterNewUserCommand(string name, string email, DateTime birthDate)
    {
        Name = name;
        Email = email;
        BirthDate = birthDate;
    }

    public override bool IsValid()
    {
        ValidationResult = new RegisterNewUserCommandValidation().Validate(this);
        return ValidationResult.IsValid;
    }
}


public class RegisterNewUserCommand : UserCommand
{

 public class RegisterNewUserCommandValidation : UserValidation<RegisterNewUserCommand>
{
    public RegisterNewUserCommandValidation()
    {
        ValidateName();
        ValidateBirthDate();
        ValidateEmail();
    }
}

What is actually done within the validation of a COMMAND?

Permission for the execution of Command, existence of duplicity would be an example of validation ?

  • You can give more details?

  • @Maniero add snippets of code that exemplify the issue...

1 answer

0

Validations vary according to the application and the data to be validated.

If, for example, you have a customer registration form in an e-commerce, you would need, for example, to validate the person’s CPF (document), if the dates are in the correct format, if the mandatory fields have been filled in. You could still validate if the registered address exists with an integration with some API (Post Office, Google Maps, etc). Also, when making a purchase, it is necessary to validate credit card, discount coupon, etc...

These validations extend - you could validate permissions to access certain data, user credentials, encryption, etc.

In summary - are validations to see if the data contained in the object is correct to proceed with the command...

In the given code example, the validation is whether the data is correct for creating a user. They could be seeing if there are no duplicates, if the fields are blank, etc...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.