Invariant (Domain-Driven Design)

An invariant in Domain-Driven Design is a rule that must stay true whenever model objects change, such as each transition matrix row summing to one.

Status
Published, version 1.0.1
Last reviewed
17 Nov 2026, by Dr Darrin Baines
Report a problem
Tell us if something is wrong or out of date

Concept Architecture

Invariant (Domain-Driven Design) in Health Economic Models

In Domain-Driven Design (DDD), an invariant names a condition on the state of a software model that is kept true every time that state is saved, so a change that would break it is refused instead of being repaired later. The most familiar case for a health economist is the requirement that the transition probabilities from each health state of a Markov cohort model, including the probability of staying in it, add up to one, a check that cohort modelling tutorials and verification checklists already describe, though not as a rule owned by a named object. Stating such conditions as invariants gives each one a single written form and a fixed place in the domain model where it is enforced on every run. The qualifier separates this sense from measurement invariance and time-invariant covariates, which describe properties of measures and variables.

Where the term comes from and who uses it

Invariant in this sense is a software design term and is new to health economics. In DDD the word marks a rule stated inside the aggregate, factory and assertions patterns that Eric Evans set out in his 2003 book and summarised in his free reference of 2015, though he names no pattern called invariant, and the wiki label matches the word these sources use.

  • Evans's 2015 reference asks designers to define properties and invariants for an aggregate as a whole and to give enforcement responsibility to its root or to a designated framework mechanism. His assertions pattern asks for invariants of classes and aggregates to be stated, with automated unit tests where the programming language cannot express them. The contents and index of the 2003 book place invariants under aggregates and under factories.
  • Vaughn Vernon, in Implementing Domain-Driven Design (2013), defines an invariant as a business rule that must always be consistent, and his first rule for designing aggregates is to model true invariants in consistency boundaries.

The cohort state-transition tutorial of Alarid-Escudero and colleagues and the TECH-VER checklist describe many of the same checks without the word, while the DARTH coding framework recommends unit tests that confirm a function returns an error for invalid inputs. The Model Execution examples below are the wiki's own application.

What makes a rule an invariant

Vernon separates two kinds of consistency. Transactional consistency is immediate and atomic, while eventual consistency allows a delay, and he keeps the word invariant for rules that need the first kind. His illustration is the rule c=a+bc = a + b, under which cc must be 5 whenever aa is 2 and bb is 3, so any saved state with another value of cc breaks the invariant.

Evans's assertions pattern names post-conditions of operations separately from invariants of classes and aggregates. In the usual software sense, a post-condition says what one operation guarantees when it finishes, whereas an invariant says what is true of an object or an aggregate between any two operations. Evans's factory pattern asks for an entire aggregate to be created as a piece with its invariants enforced, so that a newly created aggregate never starts out in a state that breaks them. For a model run, a run whose inputs fail the rules is then never created, which is a stronger guarantee than a run that is created and flagged afterwards.

Vernon also warns against false invariants, which he calls artificial constraints imposed by developers and not real business rules. Rules that cross aggregates are treated differently, and Vernon quotes Evans's 2003 book that any rule spanning aggregates will not be expected to be up to date at all times, so such rules are met later through events or batch updates.

Invariants of a three-state Markov cohort run

In the wiki's candidate Model Execution context, a model run of a three-state Markov cohort model with the states Well, Sick and Dead is the aggregate whose root enforces the rules on inputs and on the cohort trace, as on the domain model page. The two rules on the result set compare strategies or combine every draw of a probabilistic analysis, so they are checked on the completed result set, which that page treats as an aggregate of its own, and not inside a single run's boundary. Each rule in the table comes from a published modelling source, but grouping the rules as invariants of named objects is the wiki's own illustration.

RuleObject it constrainsKind of rulePublished basis
Every transition probability lies between 0 and 1Transition matrixInvariant on an inputAlarid-Escudero and colleagues
Each row of the transition matrix sums to oneTransition matrixInvariant on an inputAlarid-Escudero and colleagues
No health state holds a negative number of peopleCohort traceInvariant on the run's stateTECH-VER
State occupancies add up to the cohort sizeCohort tracePost-condition of each cycleTECH-VER
Acceptability curves sum to one at every willingness-to-pay valueResult setInvariant on outputsTECH-VER
Reported ICERs in a fully incremental analysis do not decreaseResult setInvariant on outputsTECH-VER

The rule on each row of the transition matrix and the rule on the cohort trace are linked. The tutorial computes the next cycle's state vector as the matrix product of the current state vector and the transition matrix, and summing that product over states shows why conservation of the cohort follows from the row-sum invariant.

∑j=1nSmt+1,j=∑j=1nS∑i=1nSmt,i pij=∑i=1nSmt,i∑j=1nSpij=∑i=1nSmt,i\sum_{j=1}^{n_S} m_{t+1,j} = \sum_{j=1}^{n_S} \sum_{i=1}^{n_S} m_{t,i} \, p_{ij} = \sum_{i=1}^{n_S} m_{t,i} \sum_{j=1}^{n_S} p_{ij} = \sum_{i=1}^{n_S} m_{t,i}

where mt,im_{t,i} is the number of people in health state ii at cycle tt, pijp_{ij} is the probability of moving from state ii to state jj in one cycle and nSn_S is the number of health states. The last step uses the row-sum rule for every starting state ii, so the check on the cohort trace confirms the propagation code while the row-sum invariant guarantees the result in principle.

The tutorial also builds rows that meet the rule by construction, and its healthy-state row holds (1−pHD)(1−pHS1)(1 - p_{HD})(1 - p_{HS1}) for staying healthy, (1−pHD) pHS1(1 - p_{HD})\,p_{HS1} for becoming sick and pHDp_{HD} for death, where pHDp_{HD} is the probability of dying and pHS1p_{HS1} the probability of becoming sick for those who survive the cycle. These entries add up to one whatever the two probabilities are, and the tutorial still verifies every matrix once all are created.

Writing an invariant as a Domain Layer record

The curators' plan for the Domain Layer gives invariants their own tab, Rules and Invariants, in which each rule is a record with a stable address, the domain model it belongs to, a statement, its applicability and a verification status. A later plan for mathematical component pages, such as cohort propagation, asks for each constraint to be testable, with a tolerance and an error message, for example that each row of the transition matrix sums to 1 within 0.000001. These pages are planned, not yet published, and are meant to cite the rule record by its address instead of copying it.

In a model written in R or in a spreadsheet, the same rule can appear as a validity function called before each run and as a unit testing case that feeds the function a broken matrix and expects an error.

Worked example choosing a tolerance for the row-sum rule

All numbers in this example are illustrative. The row-sum invariant is tested as an absolute difference compared with a tolerance.

∣∑j=1nSpij−1∣≤εfor every i=1,…,nS\left| \sum_{j=1}^{n_S} p_{ij} - 1 \right| \le \varepsilon \quad \text{for every } i = 1, \dots, n_S

where pijp_{ij} and nSn_S are as defined above and ε\varepsilon is the tolerance, here 0.000001.

  1. An analyst types rounded published values. The row for Well is entered as 0.846 for staying Well, 0.105 for moving to Sick and 0.048 for death. The sum is 0.846 plus 0.105 plus 0.048, which equals 0.999. The difference from one is 0.001, larger than 0.000001, so the run is refused with a message naming row 1 (Well) and its sum.
  2. What the rule prevents. Take 1,000 people starting in Well, a Sick row of 0.80 for staying Sick and 0.20 for death, and an absorbing Dead state. After one cycle the trace holds 846 in Well, 105 in Sick and 48 in Dead, a total of 999, so one person has left the model without dying. After the second cycle Well holds 846 times 0.846, which equals 715.716. Sick holds 846 times 0.105 plus 105 times 0.80, which is 88.83 plus 84, or 172.83. Dead holds 846 times 0.048 plus 105 times 0.20 plus 48, which is 40.608 plus 21 plus 48, or 109.608. The total is 998.154. Each cycle loses 0.001 of the people in Well, 1 person and then 0.846, so costs and QALYs are understated a little more every cycle.
  3. The correction. Computing the probability of staying Well as the complement, 1 minus 0.105 minus 0.048, gives 0.847. The row then sums to 1.000 and the run starts.
  4. Why neither exact equality nor a loose tolerance. Probabilities computed from rates or by multiplying complements are stored to finite precision, so a test for exact equality could refuse a correct row because of rounding in the last digits. A tolerance of 0.01 would accept the row in step 1, whose error of 0.001 is ten times smaller.

The tolerance is part of the rule, so it belongs in the rule record, where reviewers can see it and change it in a new version.

Invariants compared with checks and assumptions

Several neighbouring ideas resemble invariants. Telling them apart decides what a rule record should hold and what it should leave to judgement.

  • Black-box testing, in TECH-VER's terms, checks whether calculations are in line with a priori expectations, and items such as comparing sampled parameter means with the point estimate need judgement about how close is close enough. An invariant admits no such exception.
  • The Markov assumption concerns how transitions depend on a patient's history. It is judged against evidence, and no check on the model's own state can enforce it.
  • Input data validation judges whether data are appropriate and well sourced. An invariant can confirm that a probability lies between 0 and 1, but not that it is the right probability for the population.
  • The Shapes Constraint Language states conditions that RDF data graphs must meet, while a DDD invariant belongs to the domain model of one bounded context.

Scope of invariants in HTA work

An invariant holds within one bounded context and is stated in that context's ubiquitous language. The Model Execution rules above are mathematics that holds for any cohort model, while an HTA Appraisal context would carry rules about process. The curators' briefing gives as a potential invariant that a recommendation is associated with a specified technology, indication and decision scope, and notes that such rules differ across agencies, so a rule drawn from one agency's process should not be presented as general HTA practice. No HTA body or health economics guideline read for this page uses DDD invariants.

Invariants help with technical verification, because a rule enforced on every run replaces some manual checks. They do not show that a model's structure fits the disease or that its inputs are the best evidence.

Common misreadings

Health economists who first meet the term tend to misread it in the ways below. Each misreading is carried over from the habits of manual model checking.

  1. Treating every check as an invariant. Expectation checks, such as whether a scenario result is plausible, belong to review. Turning them into rules that refuse a run would block legitimate results.
  2. Making one rule span a whole analysis. Requiring all the parameter draws of a probabilistic sensitivity analysis to be saved together in one transaction binds independent draws into one large unit for no business reason, the kind of false invariant Vernon describes. Summaries across draws, such as the cost-effectiveness acceptability curve, are better checked once all draws are complete.
  3. Writing a rule without a tolerance or a message. A statement that rows must sum to one gives a programmer no threshold and a user no way to find the faulty row.

Sources

Library

Publications

3
  • Book

    Implementing Domain-Driven Design — Vernon V, 2013 (Addison-Wesley)

    Book or book chapter listed in the Sources of the HealthEconomics.wiki Concept Architecture articles on Core Domain, Context Map, Shared Kernel, Entity (Domain-Driven Design), Invariant (Domain-Driven Design), Published Language, Domain Event and Open Host Service, where it is cited as a reference for these concepts.

  • Other

    Domain-Driven Design Reference: Definitions and Pattern Summaries — Evans E, 2015 (Domain Language)

    Source listed in the Sources of the HealthEconomics.wiki Concept Architecture articles on Domain-Driven Design, Domain (Domain-Driven Design), Subdomain (Domain-Driven Design), Core Domain, Bounded Context, Ubiquitous Language, Domain Model, Context Map, Strategic Design, Domain Expert, Entity (Domain-Driven Design), Value Object, Aggregate (Domain-Driven Design), Invariant (Domain-Driven Design), Anticorruption Layer, Published Language, Domain Event, Domain Service, Shared Kernel and Open Host Service, where it is cited as a reference for these concepts.

  • Book

    Domain-Driven Design: Tackling Complexity in the Heart of Software — Evans E, 2003 (Addison-Wesley)

    Book or book chapter listed in the Sources of the HealthEconomics.wiki Concept Architecture articles on Domain-Driven Design, Domain (Domain-Driven Design), Core Domain, Bounded Context, Context Map, Published Language, Open Host Service, Ubiquitous Language, Domain Model, Strategic Design, Domain Expert, Entity (Domain-Driven Design), Invariant (Domain-Driven Design), Anticorruption Layer, Shared Kernel, Value Object and Domain Service, where it is cited as a reference for these concepts.

Frequently Asked Questions (6)

  • What is Invariant?

    An invariant in Domain-Driven Design is a rule that must stay true whenever model objects change, such as each transition matrix row summing to one.

    Source: Vernon 2013

  • Where does the term Invariant (Domain-Driven Design) come from?

    Invariant (Domain-Driven Design) is a software design term and is new to health economics. Eric Evans's reference for Domain-Driven Design asks designers to define the properties and invariants of each aggregate as a whole and to make its root responsible for enforcing them. Vaughn Vernon, in Implementing Domain-Driven Design, defines an invariant as a business rule that must always be consistent, in the transactional sense of immediate and atomic. Health economic modelling papers describe many of the same checks, such as rows of a transition matrix summing to one, without using the word.

    Source: Vernon 2013

  • How does Invariant (Domain-Driven Design) differ from a plausibility check in model verification?

    Invariant (Domain-Driven Design) names a rule that must hold every time, so a model run that breaks it is refused. The TECH-VER checklist describes black-box tests as checking whether model calculations are in line with a priori expectations, and many of its items call for judgement. Some TECH-VER items are strict rules, such as the number of patients across health states adding up to the cohort size, and these can be written as invariants. Expectation checks, such as whether scenario results are plausible, belong to review and should not stop a run.

    Source: Büyükkaramikli et al. 2019

  • How can the row-sum rule be written as an Invariant (Domain-Driven Design) in a Markov cohort model?

    A tutorial on cohort state-transition models in R by Alarid-Escudero and colleagues states that every transition probability should lie between 0 and 1 and that each row of a correctly specified transition matrix must sum to one. As an Invariant (Domain-Driven Design), that rule is attached to the model run, which refuses to start when any row fails. Because the next cycle's state vector is the current one multiplied by the matrix, the rule also keeps the cohort size constant across cycles. The tutorial checks every matrix once all have been created.

    Source: Alarid-Escudero et al. 2023

  • Why should an Invariant (Domain-Driven Design) be kept within one aggregate?

    Vaughn Vernon advises modelling true invariants within consistency boundaries, so that each Invariant (Domain-Driven Design) can be kept consistent in a single transaction on one aggregate. He warns against false invariants, which he describes as artificial constraints imposed by developers and not real business rules. Rules that span several aggregates are instead met later through events or batch updates. For a probabilistic sensitivity analysis this suggests checking each parameter draw on its own, with summaries across draws checked once all draws are complete.

    Source: Vernon 2013

  • How is an Invariant (Domain-Driven Design) tested in a health economic model?

    Eric Evans asks for invariants of classes and aggregates to be stated as assertions and, where a programming language cannot express them directly, to be written as automated unit tests. In a health economic model, an Invariant (Domain-Driven Design) such as the row-sum rule can appear as a validity function called before each run and as a unit test that passes in a faulty matrix and expects an error. A useful rule also records a numerical tolerance, so that tiny rounding differences pass, and an error message that names the faulty row.

    Source: Evans 2015

Trust Record

Verified by Dr Darrin Baines

British health economist

Professional identity: darrinbaines.org

Verification date: 17 Nov 2026

Content version: 1.0.1

Canonical Identity

Term code
HS-HP-DH-123

Stable URI · Machine-readable · Resolvable · CC BY 4.0

This article is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).

Author: Dr Darrin Baines · Publisher: Intelligent Parameters Ltd · Identifier: https://healtheconomics.wiki/concept/invariant-domain-driven-design