The domain
The domain holds your business rules, in plain TypeScript. It imports no framework, no database, no clock: only three small types of CAITS. So its rules test in milliseconds, and outlive your stack.
The files
The domain of a context
src/backend/messages/domain/
├── entity/Message.ts an entity: its data, and its rules
├── value-object/<ValueObject>.ts a value object
├── error/InvalidMessageError.ts an error of the domain
├── event/<DomainEvent>.ts what the context tells the others
└── port/message-repository/ what the domain needs from outside- The domain holds only these five folders. Each file exports one name: its class, or, for a port, its interface and its token.
- A frontend context has a domain too: the entities it shows, and the ports that call the backend. Its events live in
event/, at the root of the context. - Ports have a page of their own, and events go with contexts.
An entity
An entity has an identity, and holds the rules of its data. Its constructor is private: create checks the rules, so an invalid message never exists.
src/backend/messages/domain/entity/Message.ts
import { InvalidMessageError } from "#context/domain/error/InvalidMessageError.js";
export class Message {
static readonly MAX_LENGTH = 280;
private constructor(readonly id: string, readonly text: string) {}
static create(id: string, text: string): Message {
const trimmed = text.trim();
if (trimmed === "") throw new InvalidMessageError("A message cannot be empty.");
if ([...trimmed].length > Message.MAX_LENGTH) throw new InvalidMessageError(`A message has at most ${Message.MAX_LENGTH} characters.`);
return new Message(id, trimmed);
}
static restore(id: string, text: string): Message {
return new Message(id, text);
}
edit(text: string): Message {
return Message.create(this.id, text);
}
}restorerebuilds a message that an adapter kept, without checking it again.editreturns a new message: the entity never changes in place.- An entity never calls a port. The handler calls the ports, then gives the entity what it needs: here, the id comes from the repository.
A value object
A value object has no identity: two with the same values are the same. Like an entity, it checks its rules when it is made. The example has none; in an app with a context accounts:
src/backend/accounts/domain/value-object/EmailAddress.ts
import { InvalidEmailAddressError } from "#context/domain/error/InvalidEmailAddressError.js";
export class EmailAddress {
private constructor(readonly value: string) {}
static of(text: string): EmailAddress {
const value = text.trim();
if (!/^[^\s@]+@[^\s@]+$/.test(value)) throw new InvalidEmailAddressError(value);
return new EmailAddress(value);
}
equals(other: EmailAddress): boolean {
return other.value.toLowerCase() === this.value.toLowerCase();
}
}An error
A domain error is a class named …Error that extends Error. Give it its name: the logs show it.
src/backend/messages/domain/error/MessageNotFoundError.ts
export class MessageNotFoundError extends Error {
constructor(id: string) {
super(`There is no message with the id ${id}.`);
this.name = "MessageNotFoundError";
}
}The controller turns each error of the domain into a response: see the controller. For a failure that the caller must handle, a method can return a Result instead: ok(value) or err(error), from @ingenioz-it/caits/Result.
What the domain may import
- From CAITS, only
Result,TokenandDomainEvent. No package, no Node API:quality/imports.mjslists them. - The time, chance, a new id, a file, the network: put them behind a port, with a fake for the tests.
- To allow a package anyway, add it to the list of the domain: see The packages of each layer.
Test it
Through the use cases: their application tests run the real domain, with every rule. If no test of a use case reaches a rule, no use case needs it: remove it.
An entity or a value object may also have its own test, next to it, for what the use cases can't see. The example has one, Message.test.ts: it checks the names of the errors, which a use case test doesn't compare.
The rules of the domain and of the packages of each layer check this page.