Contexts
A context holds one part of the business, such as messages, billing or accounts, with its own words and rules. It never imports another context. To tell the others something, it publishes an event, which the context maps translate through the context shared. So you can change one context without breaking another.
The files
The contexts of an app
src/
├── backend/
│ ├── messages/ a context
│ │ ├── context-map.ts what it tells the others, and what it hears (optional)
│ │ ├── di.ts its bindings: CAITS finds the context by this file
│ │ └── package.json its shortcut, #context/
│ ├── statistics/ another context
│ └── shared/ what the backend contexts share (optional)
└── frontend/
├── messages/ a context of the frontend
└── shared/ what the frontend contexts share (optional)A backend context and a frontend context may have the same name: they are still two contexts, which talk only through the HTTP API.
Add a context
- Make the folder
src/backend/<context>/orsrc/frontend/<context>/. - Copy
package.jsonfrom another context. It gives the shortcut#context/, which leads to the files of the context:
src/backend/<context>/package.json
{
"type": "module",
"imports": {
"#context/*": "./*"
}
}- Add
di.ts, withexport default [ … ]. The list can be empty. Addtest.di.tswhen a port needs a fake in the tests. - Add its parts as you need them: use cases, the domain, ports. A frontend context also has its events, its store and its views: see A frontend use case.
When the context must talk to another one, give it a context map: see Connect two contexts.
Connect two contexts
A context publishes an event of its own. Its context map shares it as an event of shared, on the shared bus, which every context of the side hears. The map of each context that listens turns it into its own input. Two contexts never name each other, and the publisher never waits: each listener reacts afterwards, in its own use case.
In this example, a backend context statistics counts the words of each new message. The example app has one context, so these names are for this page.
- In
messages, the event, indomain/event/. It extendsDomainEvent:
src/backend/messages/domain/event/MessageAdded.ts
import { DomainEvent } from "@ingenioz-it/caits/DomainEvent";
/** The handler of add-message publishes it once the message is kept. */
export class MessageAdded extends DomainEvent {
constructor(readonly id: string, readonly text: string) {
super();
}
}The handler takes an
EventPublisherof its events, and publishes it:this.events.publish(new MessageAdded(message.id, message.text)). Its test checks it withexpectItToPublish.The event of shared, which carries only what the others need:
src/backend/shared/domain/event/MessagePosted.ts
import { DomainEvent } from "@ingenioz-it/caits/DomainEvent";
/** A message was posted: the contexts that need its text hear it. */
export class MessagePosted extends DomainEvent {
constructor(readonly text: string) {
super();
}
}- The context map of
messagesshares its event as the event of shared:
src/backend/messages/context-map.ts
import { share } from "@ingenioz-it/caits/messaging/ContextMap";
import { MessagePosted } from "#shared/domain/event/MessagePosted.js";
import { MessageAdded } from "#context/domain/event/MessageAdded.js";
export default [
share(MessageAdded, event => [new MessagePosted(event.text)])
];- The context map of
statisticsturns it into an input of its own:
src/backend/statistics/context-map.ts
import { when } from "@ingenioz-it/caits/messaging/ContextMap";
import { MessagePosted } from "#shared/domain/event/MessagePosted.js";
import { CountWordsCommand } from "#context/command/count-words/count-words.dto.js";
export default [
when(MessagePosted, event => [new CountWordsCommand(event.text)])
];CAITS sends each DTO to its handler in a request of its own, with the correlation id of the request that published the event. An error in statistics is reported, never thrown back to messages.
In the frontend
The same two kinds of lines, from @ingenioz-it/caits/messaging/FrontendContextMap. Here, messages tells the others that the server saved a change, and notice shows "Saved.":
src/frontend/messages/context-map.ts
import { share } from "@ingenioz-it/caits/messaging/FrontendContextMap";
import { MessagesSaved } from "#frontend-shared/event/MessagesSaved.js";
import { MessagesChanged } from "#context/event/MessagesChanged.js";
export default [
share(MessagesChanged, () => [new MessagesSaved()])
];src/frontend/notice/context-map.ts
import { when } from "@ingenioz-it/caits/messaging/FrontendContextMap";
import { MessagesSaved } from "#frontend-shared/event/MessagesSaved.js";
import { NoticeShown } from "#context/event/NoticeShown.js";
export default [
when(MessagesSaved, () => [new NoticeShown("Saved.")])
];In the frontend, share starts from any event of the context, from a handler, a view or a component. when gives events of the context, which its store reads. A context that hears an event is built if no view asked for it yet.
What a context map may do
A context map only translates, and the rules keep it so:
- It exports one list of lines, made with
shareandwhen.sharegoes from an event of the context to events of shared;when, from an event of shared to what the context reacts to. - It imports only events, the input DTOs of its own context, and the values of the domain.
- It reads no clock, no chance, nothing of the page, and keeps no state: the event holds all it needs.
- In the frontend, the maps make no cycle: a cycle would never end.
- Only the map imports the events of shared. Handlers, views and stores name only the events of their own context.
Test a context map
Test each map next to it, in context-map.test.ts: what each line gives, for the event it hears. In the backend, ContextMap reads the lines, and backendTest runs a flow through the whole backend:
src/backend/messages/context-map.test.ts
import { backendTest, ContextMap, expect, test } from "@ingenioz-it/caits/testing/backend/context-map";
// … the imports of the events, the DTO and the lines
const map = new ContextMap(lines);
test("an added message is shared as a posted message, with its text", () => {
expect(map.eventsFor(new MessageAdded("m1", "Hello world"))).toEqual([new MessagePosted("Hello world")]);
});
backendTest("adding a message tells the other contexts", async ({ app }) => {
await app.send(new AddMessageCommand("Hello world"));
expect(app.publishedOf(MessagePosted)).toEqual([new MessagePosted("Hello world")]);
});src/backend/statistics/context-map.test.ts
const map = new ContextMap(lines);
test("a posted message has its words counted", () => {
expect(map.inputsFor(new MessagePosted("Hello world"), { eventId: "evt-1", correlationId: "req-1" }))
.toEqual([new CountWordsCommand("Hello world")]);
});
backendTest("the words of a posted message add to the count", async ({ app }) => {
app.publish(new MessagePosted("Hello brave world"));
await app.settled(); // wait until every context has reacted
expect(await app.send(new GetWordCountQuery())).toEqual(new GetWordCountOutput(3));
});map.eventsFor(event)gives the events of shared for an event of the context;map.inputsFor(event, envelope), the inputs of the context for an event of shared.app.publishedOf(<Event>)lists the events of shared,app.publish(event)puts one on the shared bus, andapp.settled()waits for every reaction.- In the frontend,
eventsFor(map, event), from@ingenioz-it/caits/testing/frontend/context-map, gives the events of a line, either way.
No test crosses contexts: a test reads only its own context and shared. To check a flow between two contexts, test each side, as above: what messages shares, then how statistics reacts to that event of shared.
The rules of contexts and of shared and the context maps check this page.