1

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.

2

CQRS solves one problem for sure - it separates domain models from read models. So with properly implemented CQRS you would not run into this kind of problems and your domain models would be focused about business logic, not about reading outcomes of it.

how would you reconstruct an Aggregate Root from the Read-Model?

You don't; you just use id's from your read side to get domain model with same id from your command side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.