Business invariant
A business rule that must be true at all times, such as “a slot cannot be booked twice”, which the model enforces on every change.
Invariants are what an aggregate exists for: its border is drawn around the data that one rule needs to check in a single transaction. “The total equals the sum of the lines”, “an appointment ends after it starts”, “a doctor has no overlapping visits” are all invariants. Small ones live in value objects, which cannot be created invalid.
Not every rule is an invariant. “A profile must have a photo before it is published” only matters when publishing, which is contextual validation. Rules spanning several aggregates are usually enforced eventually, with a saga or a check that reports violations, not with one big transaction.
Related terms
- Aggregate (DDD): A cluster of objects changed as one unit, with a single root that enforces the business rules and marks the boundary of one transaction.
- Value object: A small immutable object defined only by its values, like money or a date range; two with the same values are the same thing.
- Anemic domain model: Domain objects that only hold data with getters and setters, while all business rules live in service classes around them.
- Contextual validation: Checking whether an object is valid for a specific action, such as “ready to publish”, instead of one isValid() that answers for every situation.
