Dealing with Complex Value Objects
https://softwareengineering.stackexchange.com/questions/370803
سؤال
I am working with Martin Fowler's definition as a working base.
I have a class called Condition
that I am having trouble properly classifying.
What makes me think this might be a value object:
A given
Condition
is only valid within a context of a single entity;conditions
are never shared, nor addressed independently.Its equality is based on its values; ie, a single entity should not have multiple of the same
condition
They are never modified, only added or removed from a given entity.
However, they have some characteristics not typical of value objects as well:
Very complex logic and behavior.
Potentially, references to first-class entities make up its values.
Should I be treating my Condition
class as an entity or a value?
If I do treat it as a value object, is it justified to place its data-access code in with its parent object's data-access code (ParentEntityRepository
)? How should I deal with this class differently if it is a value object?
المحلول
Based on the description and the answer you gave in the comments, it sounds to me like condition is not a value object itself but that one of it's components is a value object.
I would only consider something a proper value object if all of it's state is evaluated in determining whether two instances are equal. I would even go so far as to say that (e.g. in Java) you should not define an equals
or hashcode
for an object unless it is immutable and every piece of state is used as part of the implementation. Under this rule, any object that has equals implemented is a value object.
If this works for your situation, it opens up the option of using flyweights for these value objects within your condition
objects.