This is a long question, so I will go straight to the point. This is a pseudo code to better illustrate the problem.
DB structure
User (UserID, Name, LastName)
Address (AddressID, UserID, Street, City, State, ZipCode) => Lots to one user relationship
Phone (PhoneID, UserID, Number, IsPrimary) => Multi-to-One Relationship
Domain Classes
class User:IEntity { public string Name {get;set;} public string LastName {get;set;} public ContactInfo{get;set;} } class Phone: IValueObject or IEntity? will see later. { public int id;
So, so far we have a very basic view of this domain and its models.
My question is the following. Let's say that I want to update one of the addreses or fix the area code for one of the numbers due to the incorrect spelling wnen, on which it was originally entered.
If I follow Evan’s Bible on DDD, Value objects must be immutable. Meaning, no changes to its properties or fields after its creation. If this is the case, then, I think, none of my classes is a ValueObject, since I cannot just recreate the entire ContactInfo class just because one part of the line in the phone number is incorrect. So, I think, what does all my Entities classes do?
Keep in mind that I have a “persistence identifier” for each of these classes, as they are stored in the database.
Let's say that I decided to make Phone an object of value, since it is easily recreated in the constructor
public Phone(string newNumber)
so it would be like adding a method to the user (agg root) AND contactinfo? (Demeter Act)
as...
User.... public void UpdatePrimaryPhoneNumber(string number) { this.ContactInfo.UpdatePrimaryPhoneNumber(number); } ContactInfo.... public void UpdatePrimaryPhoneNumber(string number) { var oldPhone = Phones.Where(p=>p.IsPrimary).Single(); var newPhone = new Phone(number, oldPhone.persistenceid???-> this is not part of the domain) oldPhone = newPhone; }
but I still have to deal with the persistence id ... grrrrr. what a headache.
Sometimes I feel when I read those blogs that most of the "ddd experts" who evaluate objects are abused or, I would say, misused.
What would be the best solution for this scenario? Thanks you