Skip to content

Entities, value objects, and services

Difficulty: Beginner

Key concepts

  • entity
  • value object
  • domain service
  • identity
  • immutability
  • validated value
  • manager class
  • How to tell an entity from a value object, with one question.
  • How to write a value object in C++: immutable, compared by value, valid from the moment it exists.
  • When an operation belongs in a domain service rather than on an entity.
  • Why a “manager” class that does everything is a warning sign.

The previous lesson placed responsibilities on objects. To decide how each object should behave, it also helps to know what kind of thing each concept is, because the kind decides how it is compared, copied, changed, and stored. Most concepts in a business domain fall into one of three kinds:

  • An entity has an identity that lasts while everything else about it changes. Ada is the same member after a change of name, e-mail address, and balance.
  • A value object is defined only by its values. $10 is $10; there is no “which $10”. If any part changes, it is a different value.
  • A domain service is an operation of the domain that does not belong to any one entity or value, such as “charge the late fee for a return”.

The question that separates the first two: if two of these have exactly the same attributes, are they the same thing? Two $10 amounts: yes, a value. Two members both named “Sam Lee”, living at the same address: no, they are two people, so a member is an entity.

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

money.hpp
// A value object: an amount of money in cents, in the library's one
// currency. It has no identity: two Money values with the same amount are
// interchangeable. It is immutable: operations return a new value.
class Money {
public:
// The only way to make a Money: Money::cents(150) is $1.50. A named
// `static` function, so the unit is written at every call. constexpr
// lets it run at compile time, which the tests use in static_assert.
[[nodiscard]] static constexpr Money cents(std::int64_t amount) noexcept {
return Money(amount);
}
[[nodiscard]] constexpr std::int64_t in_cents() const noexcept { return cents_; }
// Arithmetic returns a new Money and leaves both inputs unchanged
// (`const`). `other` is passed by value: a Money is one integer, as
// cheap to copy as an int. Nothing checks for overflow; amounts in this
// example stay far below the 64-bit limit.
[[nodiscard]] constexpr Money operator+(Money other) const noexcept {
return Money(cents_ + other.cents_);
}
[[nodiscard]] constexpr Money operator-(Money other) const noexcept {
return Money(cents_ - other.cents_);
}
// `count` copies of this amount, as in "25 cents a day, times 4 days".
[[nodiscard]] constexpr Money times(int count) const noexcept { return Money(cents_ * count); }
// Equal amounts are equal Money; amounts are ordered.
// `= default` asks the compiler to write the comparison by comparing
// cents_. A defaulted <=> also gives ==, so Money gets ==, !=, <, <=, >,
// and >=. `friend` lets this free function be declared inside the class.
friend constexpr auto operator<=>(Money, Money) noexcept = default;
[[nodiscard]] std::string text() const; // for example "$1.25" or "-$0.50"
private:
// Private, so code outside the class must write Money::cents(...) and
// cannot pass a bare number that might mean days or dollars.
constexpr explicit Money(std::int64_t amount) noexcept : cents_(amount) {}
// The only data member, and no function changes it after construction.
// (Assigning a whole new Money to a variable is still allowed.)
std::int64_t cents_;
};

Money shows the properties of a good value object:

  • Compared by value. The defaulted operator<=> compares the amounts, and also gives ==. Two Money objects with the same amount are equal, and either can be used in place of the other.
  • Immutable. There is no setter. price - Money::cents(50) returns a new value; price is unchanged. A value that cannot change can be copied, shared, and passed between threads without anyone worrying about who else holds it.
  • Behaviour with the data. Adding, subtracting, and formatting money live with the amount, not scattered across the code as arithmetic on raw integers.
  • A name for the unit. Money::cents(150) cannot be confused with a count of days, as a plain int could. The constructor is private, so every Money is created by a function whose name says what the number means.

Immutable does not mean a Money variable can never change. balance_ = balance_ + amount assigns a new value to the variable, just as with an int. What never happens is a Money object changing in place while other code looks at it.

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

email_address.hpp
// A value object with a rule: the text has one '@', with something before
// and after it. The only way to get an EmailAddress is parse(), so every
// EmailAddress that exists follows the rule. (Real e-mail validation is
// far more involved; this rule is deliberately simple.)
class EmailAddress {
public:
// Checks `text` against the rule. Returns an EmailAddress holding a copy
// of the text if it passes, or an empty std::optional (std::nullopt) if
// it does not. Bad input is an ordinary answer here, so nothing throws.
// std::string_view lets a caller pass a string literal or std::string
// without a copy; the copy is made only for a valid address.
[[nodiscard]] static std::optional<EmailAddress> parse(std::string_view text);
[[nodiscard]] const std::string& text() const noexcept { return text_; }
// Two addresses are equal when their text is equal: `= default` compares
// text_. The comparison is exact, so "Ada@x" and "ada@x" differ.
friend bool operator==(const EmailAddress&, const EmailAddress&) = default;
private:
// Private: only parse() can create one, after checking the rule. The
// string is taken by value and moved in, because the object keeps it.
explicit EmailAddress(std::string text) : text_(std::move(text)) {}
std::string text_;
};

A value object can have a rule. EmailAddress can only be created by parse, which returns an empty std::optional for text that breaks the rule. So every EmailAddress in the program is already checked. (One exception: an EmailAddress that has been moved from, with std::move, may hold empty text. Do not use an object after moving from it.) A function that takes an EmailAddress never has to ask “is this valid?”, which it would have to ask of every std::string. This “parse, then trust the type” idea returns in the invariants and API contracts lessons.

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

member.hpp
// An entity: a member is the same member for as long as they belong to the
// library, even if their name, e-mail address, and balance all change. The
// ID is that identity; it is set once and never changes.
//
// Invariant: balance() >= $0 (the library never owes a member money).
class Member {
public:
// Takes each part by value and moves it in. The email is already an
// EmailAddress, so it was checked by parse() before it got here. Every
// member starts with a balance of $0.
Member(std::string id, std::string name, EmailAddress email);
[[nodiscard]] const std::string& id() const noexcept { return id_; }
[[nodiscard]] const std::string& name() const noexcept { return name_; }
[[nodiscard]] const EmailAddress& email() const noexcept { return email_; }
[[nodiscard]] Money balance() const noexcept { return balance_; }
// An entity changes: these replace one value with a new one. There is
// deliberately no way to change id_.
void rename(std::string name) { name_ = std::move(name); }
void change_email(EmailAddress email) { email_ = std::move(email); }
// Adds a fee to what the member owes.
// Precondition: amount >= $0. Throws std::invalid_argument otherwise:
// a negative charge would be a bug in the caller.
void charge(Money amount);
// Takes a payment off what the member owes.
// Returns false, changing nothing, if amount is negative or more than
// the balance: a member paying too much is an ordinary refusal, not a
// bug, and refusing it keeps the balance from going below $0.
bool pay(Money amount);
private:
std::string id_;
std::string name_;
EmailAddress email_;
Money balance_ = Money::cents(0); // what the member owes; never below $0
};

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

kinds.cpp
// Both functions check before they change anything, so a refused or
// rejected call leaves the balance as it was.
void Member::charge(Money amount) {
if (amount < Money::cents(0)) throw std::invalid_argument("cannot charge a negative amount");
// Money is immutable, so the balance is not changed in place:
// balance_ + amount makes a new Money, which replaces the old one.
balance_ = balance_ + amount;
}
bool Member::pay(Money amount) {
// `amount > balance_` refuses a payment of more than is owed, which is
// what keeps the invariant balance() >= $0.
if (amount < Money::cents(0) || amount > balance_) return false;
balance_ = balance_ - amount;
return true;
}

Member is an entity:

  • It has an identity, its ID, set once in the constructor with no way to change it.
  • It changes over time: rename, change_email, charge, pay. Those changes are the point of an entity. A member’s history is a sequence of changes to one object.
  • It is made of values. Its name, e-mail address, and balance are values; changing one means replacing it with a new value.
  • It protects an invariant. The balance is never negative: charge rejects negative amounts, and pay refuses to take more than is owed.

An entity is usually long-lived and stored: in a container, a database, or a file. Because it changes, the program must make sure there is one current version of it, and that code holding an old copy does not mistake that copy for the member. The next lesson is about exactly that.

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

late_fees.hpp
// The library's fee rules. A value object: the same numbers are the same
// schedule.
struct FeeSchedule {
Money per_day = Money::cents(25);
Money cap = Money::cents(1'000);
int grace_days = 2; // no fee for returns up to this many days late
};
// A domain service: an operation that belongs to the library's rules, not
// to any one member or value. It has no state of its own apart from its
// configuration.
class LateFeeService {
public:
// Keeps its own copy of the schedule. `explicit` stops a FeeSchedule
// from silently turning into a LateFeeService.
explicit LateFeeService(FeeSchedule schedule) noexcept : schedule_(schedule) {}
// A query: the fee for returning `days_late` days after the due day.
// Days within the grace period are free, each later day costs per_day,
// and the total never exceeds cap. A day count of 0 or less (on time or
// early) costs $0.
[[nodiscard]] Money fee_for(int days_late) const noexcept;
// Charges member the fee for a return days_late days after the due day.
// Returns the amount charged (possibly $0).
// `Member&` is a reference to the caller's member, so the charge changes
// that member. The service uses it only during the call and keeps no
// pointer or reference to it afterwards.
Money charge_late_return(Member& member, int days_late) const;
private:
FeeSchedule schedule_;
};

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

kinds.cpp
Money LateFeeService::fee_for(int days_late) const noexcept {
// Days past the grace period. std::max(0, ...) turns a negative result
// (on time, early, or within the grace days) into 0, so the fee is
// never negative.
const int charged_days = std::max(0, days_late - schedule_.grace_days);
// The daily rate times the charged days, but no more than the cap:
// std::min picks the smaller of the two Money values, using Money's <.
return std::min(schedule_.per_day.times(charged_days), schedule_.cap);
}
// Works out the fee, then asks the member to record it. The service does
// not touch the balance itself; Member::charge applies the member's rule.
// With a schedule of non-negative amounts, such as the default one, fee_for
// never returns a negative amount, so charge does not throw.
Money LateFeeService::charge_late_return(Member& member, int days_late) const {
const Money fee = fee_for(days_late);
member.charge(fee);
return fee;
}
How this code works: LateFeeService::fee_for

fee_for is two lines, but each one clamps a number, and clamping is easy to misread.

  1. Take off the grace days. days_late - schedule_.grace_days is the number of days that cost money. For a return that is on time, early, or within the grace days, that number is 0 or negative, and std::max(0, ...) turns it into 0. So the fee is never negative.
  2. Multiply, then cap. per_day.times(charged_days) is the full fee. std::min compares it with cap and keeps the smaller one, so the fee never goes above the cap.

With the default schedule (25 cents a day, 2 days’ grace, a cap of 1 000 cents), the three returns in the program below work out like this:

days_late days_late - 2 charged_days 25 × charged_days fee (after the cap)
1 −1 0 0 $0.00
6 4 4 100 $1.00
90 88 88 2 200 $10.00

charge_late_return then passes each fee to member.charge, so Ada owes 0 + 100 + 1 000 = 1 100 cents. After paying 500 cents, Ada owes $6.00.

Where should “charge the late fee for a return” live? On Member? The rule (25 cents a day after two days’ grace, at most $10) is not a fact about a member. It is library policy, changes on a different schedule, and might differ between branches. Putting it on Member would make every member carry the fee rules. On Money? Money knows nothing about days or grace periods.

So it is a domain service: LateFeeService, an operation named in the language of the domain, with no state of its own except its configuration. It reads the schedule, computes the fee, and asks the member to record the charge. The member still decides whether the charge is allowed. The service does not reach in and change balance_.

FeeSchedule is itself a value: three numbers that are interchangeable with any other schedule holding the same numbers.

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

main.cpp
// parse returns a std::optional<EmailAddress>; `*` takes the address
// out of it. That is safe only because this text is known to be valid:
// `*` on an empty optional is undefined behaviour.
Member ada("M-1", "Ada Byron", *EmailAddress::parse("ada@example.org"));
// FeeSchedule{} uses the defaults written in the struct.
const LateFeeService fees{FeeSchedule{}}; // 25 cents a day, 2 days' grace, $10 cap
// Each call charges Ada and returns the fee: 1 day is within the grace,
// 6 days is 4 charged days (100 cents), 90 days hits the $10 cap. Ada
// now owes $11.00.
std::cout << "returned 1 day late: charged " << fees.charge_late_return(ada, 1).text() << '\n';
std::cout << "returned 6 days late: charged " << fees.charge_late_return(ada, 6).text() << '\n';
std::cout << "returned 90 days late: charged " << fees.charge_late_return(ada, 90).text()
<< '\n';
ada.rename("Ada Lovelace"); // the same member, with a new name
ada.change_email(*EmailAddress::parse("ada@lovelace.example"));
ada.pay(Money::cents(500)); // $5.00 of the $11.00 owed: allowed, so $6.00 is left
std::cout << ada.id() << ' ' << ada.name() << " <" << ada.email().text() << "> owes "
<< ada.balance().text() << '\n';
// An optional converts to true only if it holds a value; this text has
// no '@', so parse returns an empty one and the answer is "no".
std::cout << "\"ada.example.org\" is an e-mail address: "
<< (EmailAddress::parse("ada.example.org") ? "yes" : "no") << '\n';
Expected output
returned 1 day late: charged $0.00
returned 6 days late: charged $1.00
returned 90 days late: charged $10.00
M-1 Ada Lovelace <ada@lovelace.example> owes $6.00
"ada.example.org" is an e-mail address: no
Entity sketch: an entity made of value objects, and a domain service that works on it
Text description of the diagram

Five boxes, each labelled with its kind. Solid arrows go from Member (an entity) to Money (a value), labelled “owns 1, balance”, and to EmailAddress (a value), labelled “owns 1, email”: a member holds its balance and its e-mail address inside itself. A solid arrow goes from LateFeeService (a service) to FeeSchedule (a value), labelled “owns 1, schedule”, and a solid arrow goes from FeeSchedule to Money, labelled “owns 2, per_day and cap”. A dashed arrow goes from LateFeeService to Member, labelled “charges, during a call”: the service receives a member as a parameter and does not store it.

Diagram source (Mermaid)
flowchart LR
Member["Member (entity)"] -->|owns 1, balance| Money["Money (value)"]
Member -->|owns 1, email| EmailAddress["EmailAddress (value)"]
LateFeeService["LateFeeService (service)"] -->|owns 1, schedule| FeeSchedule["FeeSchedule (value)"]
FeeSchedule -->|owns 2, per_day and cap| Money
LateFeeService -.->|charges, during a call| Member

The class outline shows how each kind looks in C++. The notes name the kind:

class Member

entity: identity is id_, which never changes

Data members

  • std::string id_;
  • std::string name_;
  • EmailAddress email_;
  • Money balance_; zero at the start

Member functions

  • Member(std::string id, std::string name, EmailAddress email);
  • void rename(std::string name);
  • void change_email(EmailAddress email);
  • void charge(Money amount);
  • bool pay(Money amount); false if more than the balance

class Money

value: equal when the amounts are equal; never changes

Data members

  • std::int64*t cents*;

Member functions

  • static constexpr Money cents(std::int64_t amount);
  • constexpr std::int64_t in_cents() const;
  • constexpr Money operator+(Money other) const; a new Money
  • friend constexpr auto operator<=>(Money, Money) = default; non-member: a friend, listed with the class
  • std::string text() const;

class EmailAddress

value: only parse() can create one, so every one is valid

Data members

  • std::string text_;

Member functions

  • static std::optional<EmailAddress> parse(std::string_view text);
  • const std::string& text() const;

struct FeeSchedule

value: the late-fee rules

Data members

  • Money per_day;
  • Money cap;
  • int grace_days = 2; no fee for returns up to this many days late

class LateFeeService

service: no identity, no changing state

Data members

  • FeeSchedule schedule_;

Member functions

  • Money fee_for(int days_late) const;
  • Money charge_late_return(Member& member, int days_late) const;
Class outline: the entity has an ID and operations that change it; the values have none; the service holds only its rules

Look at what each kind’s members say. The entity has an ID and functions that change it (rename, charge). The values have no functions that change them: operator+ returns a new Money. The service’s only data member is its fixed rules, and the Member& parameter of charge_late_return is the dashed arrow in the sketch: the service works on a member without keeping it.

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

test_kinds.cpp
// LLD_TEST(name) defines one test. LLD_CHECK(condition) records a failure if
// the condition is false; LLD_CHECK_EQ(actual, expected) does the same for
// `actual == expected` and prints both values when they differ;
// LLD_CHECK_THROWS(expression, type) fails unless the expression throws
// that type. They come from the site's small test header, lld_test.hpp.
// email("...") is a helper above this snippet: it parses text that is known
// to be valid and returns the EmailAddress (value() would throw if not).
//
// Values: equal amounts are equal, arithmetic makes new values, and the
// original is untouched. static_assert is checked by the compiler, not when
// the test runs: it works because Money's functions are constexpr.
LLD_TEST(money_is_compared_by_value) {
static_assert(Money::cents(150) == Money::cents(100) + Money::cents(50));
static_assert(Money::cents(25).times(4) == Money::cents(100));
static_assert(Money::cents(-1) < Money::cents(0));
const Money price = Money::cents(300);
const Money discounted = price - Money::cents(50); // a new value
LLD_CHECK(price == Money::cents(300)); // the old one is unchanged
LLD_CHECK_EQ(discounted.text(), std::string("$2.50"));
LLD_CHECK_EQ(Money::cents(-50).text(), std::string("-$0.50"));
}
// The rule at its edges: empty text, nothing before '@', nothing after it,
// and two '@'. has_value() is true when parse returned an address.
LLD_TEST(email_addresses_follow_their_rule) {
LLD_CHECK(EmailAddress::parse("a@b").has_value());
LLD_CHECK(!EmailAddress::parse("").has_value());
LLD_CHECK(!EmailAddress::parse("@b").has_value());
LLD_CHECK(!EmailAddress::parse("a@").has_value());
LLD_CHECK(!EmailAddress::parse("a@b@c").has_value());
LLD_CHECK(email("a@b") == email("a@b"));
}
// An entity: its name and address change, its ID does not.
LLD_TEST(member_keeps_identity_while_changing) {
Member member("M-1", "Ada", email("ada@example.org"));
member.rename("Ada L.");
member.change_email(email("ada@new.example"));
LLD_CHECK_EQ(member.id(), std::string("M-1"));
LLD_CHECK_EQ(member.name(), std::string("Ada L."));
}
// The invariant holds after every call, including the refused ones: an
// overpayment and a negative payment change nothing, and a negative charge
// throws.
LLD_TEST(member_balance_never_goes_negative) {
Member member("M-1", "Ada", email("ada@example.org"));
member.charge(Money::cents(300));
LLD_CHECK(!member.pay(Money::cents(301))); // more than owed: refused
LLD_CHECK(!member.pay(Money::cents(-5)));
LLD_CHECK(member.balance() == Money::cents(300));
LLD_CHECK(member.pay(Money::cents(300)));
LLD_CHECK(member.balance() == Money::cents(0));
LLD_CHECK_THROWS(member.charge(Money::cents(-1)), std::invalid_argument);
}
// The fee rules at their boundaries, with the default schedule: -3 days is
// free, day 2 is the last free day, day 3 costs 25 cents, and from day 42
// (40 charged days, 40 x 25 = 1'000) the cap applies. Then one real charge.
LLD_TEST(service_applies_grace_rate_and_cap) {
const LateFeeService fees{FeeSchedule{}};
LLD_CHECK(fees.fee_for(-3) == Money::cents(0));
LLD_CHECK(fees.fee_for(2) == Money::cents(0)); // last day of grace
LLD_CHECK(fees.fee_for(3) == Money::cents(25)); // first charged day
LLD_CHECK(fees.fee_for(42) == Money::cents(1'000)); // 40 charged days: the cap
LLD_CHECK(fees.fee_for(43) == Money::cents(1'000));
Member member("M-1", "Ada", email("ada@example.org"));
LLD_CHECK(fees.charge_late_return(member, 6) == Money::cents(100));
LLD_CHECK(member.balance() == Money::cents(100));
}

Each kind has its own natural tests. Values: equality, arithmetic, and that an operation leaves the original unchanged. Many value checks can be static_asserts, because Money is constexpr. Validated values: each rule, including the edges (@b, a@). Entities: identity survives change, and the invariant holds after every operation, including the refused ones. Services: the rules, at their boundaries (the last day of grace, the first charged day, the cap).

A common shape in growing code is a MemberManager (or LibraryService, MemberHelper, MemberUtils) that holds all members and has functions such as add_member, rename_member, charge_member, compute_fee, validate_email, send_reminder, export_members_csv. The Member class underneath becomes a struct of public fields.

That is not a domain service. It is every responsibility in one place:

  • The rules about a member (the balance never goes negative) live outside Member, so any code can break them.
  • E-mail validation is a function anyone can forget to call, instead of a type that cannot hold bad text.
  • Fee rules, reminders, and CSV export change for different reasons, but every change touches the same class.

A real domain service is narrow and named after one operation of the domain: LateFeeService, TransferService, RenewalPolicy. If the best name you can find for a class is “manager” or “helper”, ask what it does, and whether each part belongs on an entity, on a value, or in its own small service.

  • Giving a value object an ID, or storing it as a long-lived object that others refer to. If two copies with equal values are interchangeable, just copy the value.
  • Making a value object mutable, such as money.add(5) changing it in place. Code that holds the “same” value is then surprised when it changes.
  • Using raw int or std::string for domain values with rules (money, e-mail addresses, ISBNs). The rule is then checked everywhere, or nowhere.
  • Entities as bags of public fields with the rules in a manager.
  • Putting a policy on an entity because the entity is involved, such as fee rules on Member. Ask where the rule comes from and how often it changes.

New requirement: “The library opens a branch in another country, with its own currency.”

The change lands in one place. Money gains a currency, and adding two amounts in different currencies becomes an error. Every function that already takes Money keeps its signature; only the places that create a Money must say which currency. If amounts had been plain int cents everywhere, every one of those places would need finding and changing, and the compiler would not help find them.

The fee rules for the new branch are a different FeeSchedule value given to a LateFeeService, with no change to Member at all, because the rules were never on Member.

Exercise: Classify ten library concepts

Classify each as an entity, a value object, or a domain service, and give the deciding reason.

  1. A book copy on the shelf (barcode C-1001).
  2. An ISBN.
  3. A date range, such as “1 March to 21 March”.
  4. A loan.
  5. “Renew a loan, if the rules allow it.”
  6. A postal address.
  7. A library branch.
  8. A member’s reading-history list.
  9. “Find the nearest branch with a free copy of a title.”
  10. A late-fee schedule.
Show the explained answer
  1. Entity. Two copies of the same edition are physically different objects, lent to different people; the barcode is the identity. Its state (on the shelf, lent, damaged) changes.
  2. Value object. Two equal ISBNs are the same ISBN. It has a rule (format and check digit), so it is a good validated value.
  3. Value object. It is defined by its two dates; “changing” it means a new range. It is a good home for behaviour such as “does it contain this day?”.
  4. Entity. A loan is opened, renewed, and closed; it has a history and must be found again later (“which loan is this fine for?”). Two loans of the same copy to the same member in different months are different loans.
  5. Domain service (or an operation on Loan). If the rule needs only the loan’s own data (its renewal count), it can be a member function of Loan. If it needs other things too (holds by other members, the member’s standing), a service such as RenewalPolicy that asks those objects fits better.
  6. Value object. Equal parts make the same address. (In a postal company’s own system, where addresses are tracked and corrected over time, an address might be an entity: the classification depends on the domain.)
  7. Entity. A branch has a lasting identity while its opening hours, address, and staff change.
  8. Part of an entity rather than a concept of its own: a collection of values (loan records or titles) owned by the member’s entity, or derived from loans. It needs no identity apart from the member’s.
  9. Domain service. It involves branches, copies, and locations, and belongs to none of them.
  10. Value object. It is a set of numbers; equal numbers make the same schedule. The service that applies it is separate.

The pattern: ask “same attributes, same thing?” first. If yes, it is a value. If no, ask whether it is a thing that changes over time (an entity) or an operation (a service). Several answers depend on the domain, which is the real lesson.

  • An entity has a lasting identity and changes over time; a value object is defined only by its values; a domain service is an operation that belongs to no single object.
  • Decide with one question: if two have the same attributes, are they the same thing?
  • Write value objects as small immutable types compared by value, with rules checked when they are created.
  • Keep each entity’s rules inside it; keep services narrow and named after one domain operation; be suspicious of “manager” classes.