1

I know that there are many articles on this subject and that entity has identity and equality is provided through this identity, while valueObject is provided through its values.

But I'm having a really hard time making a decision in practice. I plan to keep an employee's past salaries in the employeeSalaries table, which will be displayed as a list somewhere on the site. I can't decide whether employeeSalaries should be a value object or an entity.

How should I decide?

1 Answer 1

1

But I'm having a really hard time making a decision in practice. I plan to keep an employee's past salaries in the employeeSalaries table, which will be displayed as a list somewhere on the site. I can't decide whether employeeSalaries should be a value object or an entity.

The starting reference to review is Chapter 5 of Domain Driven Design (Eric Evans, 2003).

VALUE OBJECT is primarily about modeling information; we're taking some domain concept ("color") and representing it in code an a data structure (typically immutable) and a collection of methods that have high cohesion with that data structure.

They are somewhat analogous to numbers; most programming languages come with some general purpose concept of 7. 7s are immutable (it always means 7) and fungible (we don't really care which 7 we are talking about). A VALUE OBJECT is a generalization of that idea applied to more complicated data structures (Color, Money....,).

ENTITIES (as used in Evans's pattern language) are primarily about modeling relationships, and in the common case relationships that vary with time. Yesterday, Bob's house was RED; he had it painted, and now it is BLUE. We didn't change RED, we didn't change BLUE, but the answer to the question "what color is Bob's house" changed, which suggests that Bob's house is an ENTITY.

In Chapter 5 you'll find a discussion of Address -- should that be a value or an entity? And the answer is... that depends on what kind of model you are creating; you'll get different answers depending on which trade offs matter to you.

Recommended viewing: Stuart Halloway Perception and Action, especially the bits about Clojure's Epochal Time Model.

I understand this definition, but it's still hard to decide. employee_salaries will only hold salary and reason. It seems immutable, so I think it should be treated as a value object. Am I right?

In the common case, you're probably right. It is often convenient to model Facts as values. So an Salary that is some combination of a rate of compensation and an effective date, would be a perfectly reasonable thing to model as a value.

So when Bob gets a raise, you end up expressing that in your model by replacing one Salary object with another.

It gets trickier when you have something like a "salary history" - essentially a time ordered list of salaries. Should that be a value or an entity? In the general case, you're going to end up balancing trade offs: which approach is better aligned with the language that the business uses to discuss the concept? which approach gives you better performance? which gives you better correctness guarantees? which is easier to change/maintain? And so on.

A review of what Gary Bernhardt had to say about paradigms might be useful.

Sign up to request clarification or add additional context in comments.

3 Comments

I understand this definition, but it's still hard to decide. employee_salaries will only hold salary and reason. It seems immutable, so I think it should be treated as a value object. Am I right?
I extended my answer above, let me know if that helps.
I think it's confusing to have a salary list. If it were a single salary, or if it were a previous salary, it would have the properties of amount and reason, and it would be clear that it's immutable. More accurately, when the amount changes, Bob's salary would change, not the number 7. so that's a value object. even if the reason change thats mean bob's salary reason changed. Am i right? In a scenario with a list, it's more of a matter of preference than right or wrong, as far as I understand. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.