The test that starts a database
PayInvoiceTest checks one rule of a home insurer's claims system: a claim is not paid past the amount its adjuster approved. Before it can ask whether a €1,300 contractor invoice is refused on a claim approved at €12,000 with €11,000 already paid, the test starts a PostgreSQL container, creates fourteen tables, and inserts a policy, a customer, an adjuster and the claim. It takes eleven seconds.
The reason is in the service under test. PayInvoice runs SELECT SUM(amount) FROM claim_payments WHERE claim_id = ?, compares the sum with a column it reads from claims, and runs an INSERT if there is room. The rule is woven into SQL inside the service, so the only way to exercise it is to give it the tables, and every other piece of code that writes to claim_payments has to remember to repeat the check.
Half of the fix is a Claim class whose pay(amount) method holds the rule, next to the data it protects; that half belongs to Aggregate. The other half is how PayInvoice gets hold of a Claim without knowing about tables.
Ask for the claim, not for its rows
A Repository puts the claims behind something that looks like a collection held in memory. Domain code asks claims.get(claimId) and receives a Claim, changes it through the claim's own methods, and hands it back with claims.save(claim); a claim just opened goes in with claims.add(claim). The code does not know that there is a claims table, a claim_lines table for the damaged items and a claim_payments table, or which join puts them together. Some repositories have a remove. This one does not, because at this insurer a claim is closed, never deleted, and the interface offers only what the business does.
What comes back is the whole aggregate: the claim with its lines and every payment made on it, because in this model pay checks its rule by adding up the claim's payments, and it cannot add up payments it was not given. The repository hands that unit over complete and takes it back the same way.
PayInvoice shrinks to three steps: get the claim, call claim.pay(invoice.amount), save. It no longer names a table, and that is what will make its test cheap. Saving is not yet committing, though: in this design save hands the change to a Unit of Work, which sends the writes when the use case commits, and the commit makes them durable. That is also where two invoices paid against the same version of the claim can be caught, if the claim carries a version that the write checks and bumps in one conditional UPDATE: the second update changes no row, and the unit of work, checking that count, rolls the transaction back instead of committing.
The service never touches a payment directly. It gets the whole claim from the repository, and the claim's own method decides whether a payment is allowed.
Why there is no payment repository
The old code had a class per table: ClaimDao, ClaimLineDao, PaymentDao. With PaymentDao.insert available, a nightly job that pays contractors added payments without ever loading the claim, so no check ran. A payment means nothing outside its claim, so the new design has no payment repository: repositories exist for aggregate roots, and everything inside an aggregate is reached through its root. A generic DAO renamed as a repository, with findAll and update for any table, would put the tables back on the interface along with that bypass.
The interface lives with the domain, in the claims module, and uses its words: get, add, save, and the few lookups the business really asks for, such as openClaimsForPolicy(policyId) when a new claim has to be checked against the policy's other open ones. The implementation lives in infrastructure, with the tables and the SQL or ORM configuration that turn rows into a Claim and back, and Active Record vs Data Mapper shows how that mapping is organised. Because the domain depends only on the interface, the claims could move to another store through a second implementation without changing Claim or its services, the port-and-adapter split of Hexagonal (Ports & Adapters).
The repository is the easy way in, not the only one. Nothing in the language stops the next job from inserting into claim_payments again; module boundaries, the database permissions each component gets, and review keep that door shut.
The adjusters' queue
Three months later the adjusters ask for a queue: open claims in their region, older than ten days, with no photos, largest approved amount first, fifty to a page. The obvious change is findOpenByRegionOlderThanWithoutPhotosOrderByApproved(region, days, page) on the repository, and the next screen brings the next method, each one SQL-shaped, leaking the schema back into the interface that was meant to hide it. It also loads the wrong thing: every Claim returned is the whole aggregate, lines and payments included, to print six columns. A read for a screen can take a separate path that returns rows in the screen's own shape, which is the split CQRS makes.
When the domain itself selects claims by a business rule, the rule can travel as an object, a Specification such as overdueForReview, rather than as one more method name.
A test with no database
Back to the eleven seconds. PayInvoice now depends on an interface, so its tests can use an in-memory repository: a map from claim id to claim, with get, add and save in a dozen lines. The test from the opening puts in a claim approved at €12,000 with €11,000 paid, runs the service for an invoice of €1,300, and checks that the payment is refused and the claim still shows €11,000. No container starts, and the test runs in milliseconds.
Two habits keep the fake honest. Have save store a copy and get return a copy, because in this contract a change reaches storage only through save; a fake that hands back the same object lets a test of an accepted payment pass even when the service forgets to call save. And keep tests for the real implementation against a real database, because the map exercises the use case, not the SQL, the mapping, or what happens when two saves collide.
Draw the invoice path
The Aggregate & Repository Challenge asks for an application service that obtains aggregates only through their repository and then invokes behaviour on the root. The invoice path from this article fits it: the service, the claim repository, and the claim with its lines and payments, with edges labelled in the domain's verbs, get, pay and save, rather than table names, and no edge from the service to a payment. The Challenge also asks for a Domain Event, and a claim paid up to its approved amount is the fact the rest of the insurer wants to hear about.