I am new to DDD and I am designing a roommate matching system. One of my Aggregate Roots is a "Profile"
public class Profile
{
...
public Traits Traits { get; private set; }
public Traits Preferences { get; private set; }
...
}
As you can see a profile has a relationship to two entities of type Traits, "Traits" is the users own traits such as the max rent they can contribute, weather they smoke or not etc. and "Preferences" is the same type of info but it represents what they are looking for in a roomate.
I am struggling to understand how to best enforce an invariant like the following example "A profile cannot prefer a roommate who contributes more rent then they do themselves."
If I add a method to profile ChangeMaxRent() like such
public class Profile
{
...
public Traits Traits { get; private set; }
public Traits Preferences { get; private set; }
...
public void ChangeMaxRent(int newMaxRent)
{
//enforce variant here
Traits.SetMaxRent(newMaxRent);
}
}
Then a consumer could just circumvent this by accessing the Traits property directly like so
profile.traits.SetMaxRent(x)
The only way to avoid this is to make the getter for Traits and Preferences private so that a consumer cannot directly access it;
but this would cause a lot of code to be written in the Profile class since I would now need a getter for every property of Trait and Preference
I am struggling to see a clear path forward here, any help would be greatly appreciated.