I hope you can help settle a slight disagreement I am having with a colleague.
I always assumed it was a principle of object-oriented design that objects should protect their internal state and only expose publicly what the outside world needs to access or change. In the context of a rich domain model, this means that domain objects should enforce the validity of the model they represent, and that they should not allow their state to be made invalid by external callers, even if they are core domain objects and not directly exposed to the outside world.
Take as an example an object of type Thing, which has a collection of properties. My colleague has proposed the following.
public class Thing
{
public List<ThingProperty> Properties { get; set; }
}
I dislike this, because it allows external callers to set the Properties reference to null. His defence is that without this mutable reference, it is difficult to load data into the model from data access, or to map from the presentation layer to the core model.
My solution is as follows...
public class Thing
{
private readonly List<ThingProperty> properties;
public Thing() { properties = new List<ThingProperty>(); }
public ReadOnlyCollection<ThingProperty> { get { return properties.AsReadOnly(); } }
public void AddProperty(ThingProperty add) { (validate) properties.Add(add); }
}
This means that any validation that may be required when adding properties to a Thing can be validated inside the model (say if only 1 instance of a given property type may be added) and the model always ensures a valid state. The downside is that it's difficult to map from data access or the presentation model into this form, but I think that's a price worth paying.
Any thoughts? Thanks.