Here is a similar question Where to validate domain model rules that depend on database content?
I am asking this new question because I have more descriptions and I don't want to change the previous question mentioned above
Problem
I have Form aggregate root that has Fields collection. On my form I have the SetCustomField(fieldname, value) method. I also have default properties on the form e.g. DateOfBirth, FirstName, and LastName.
public DateTime DateOfBirth
{
get { return _dob; }
set {
if(value > 2000)
throw new ArgumentException(null, "You must be 15 yrs old or more");
_dob = value;
}
}
This works when creating the form. But when you want to edit or view the form, the validation kicks in too. This throws exception for fields that are empty or have invalid values - perhaps changed in the db or loaded from Excel. The expected behavior is to allow the form to show on updating so the Admin can correct those fields.
CQRS
I have been reading about CQRS but so far I don't think it would solve this problem. If I want to edit the form, Form aggregate root still needs to be loaded, modified and updated:
//repository needs to set the DateOfBirth here and throws exception
var form = repo.GetFormById(1);
form.DateOfBirth = "Some value";
repo.Save(form);
Since I need to reconstruct the model, CQRS Read-Model might not help. If I have to use CQRS, how would you reconstruct an Aggregate Root from the Read-Model?
Thanks for your contribution.