I am using CQRS. Everywhere I read tells me to put validation logic in the command objects. For example, see this link: https://lostechies.com/jimmybogard/2016/04/29/validation-inside-or-outside-entities/
Please see the command below (taken from the link):
public class ChangeNameCommand {
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
}
and the Business Object below (also taken from the link - note that I have changed the the parameter passed to the Customer constructor from a class to an interface):
public class Customer
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public void ChangeName(IChangeNameCommand command) {
FirstName = command.FirstName;
LastName = command.LastName;
}
}
In my case the commands are stored in one class library and the business objects in others (because the commands are shared by multiple microservice type projects). If I follow the guidance (and put the validation in the commands) then I believe there is nothing to stop a developer doing this:
public class ChangeNameCommandWithoutValidation : IChangeNameCommand {
public string FirstName { get; set; }
public string LastName { get; set; }
}
and then passing the command (without the validation) to the domain object. In this case I believe the Domain Object has no control what is passed to it?
Therefore should I be going against all of the guidance I can find and do the validation in the domain object? I believe I should do this because the commands are in a separate class library to the domain objects. Have I understood this correctly?
I believe this question is also relevant when passing an event to the customer domain object (when using event sourcing).