Simple question: Should model/domain objects only include attributes that are meant to be persisted in a database or serialized to any other specific format?
My understanding of a domain/model object is that it should reflect the data store (the model) as tightly as possible, rather than carrying "off-topic" attributes.
Am I on the right track?
Best Answer
It depends. Starting with persistent-only attributes is the most typical approach, and adding non-persistent attributes should typically be an exception. But such a decision is always a trade-off, and where you draw the line depends on the responsibilities you assign to that object. Some typical reasons for adding non-persistent attributes:
you need them for an operation which clearly belongs to the object, and holding them within the object turn out to be much simpler than using a second object (note that a second object needs its lifetime to be manage separately)
you see an opportunity to keep the API of your domain model as simple as possible
you need an option for storing this additional, temporary information without changing too much code in an existing program, and you are sure using a different class or objects will have a too big impact
performance reasons (assumed you have a real, measurable performance bottleneck, which can be most easily solved by caching some data in your object)