CAITSGet started

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

  1. Make the folder src/backend/<context>/ or src/frontend/<context>/.
  2. Copy package.json from another context. It gives the shortcut #context/, which leads to the files of the context:

src/backend/<context>/package.json

{
  "type": "module",
  "imports": {
    "#context/*": "./*"
  }
}
  1. Add di.ts, with export default [ … ]. The list can be empty. Add test.di.ts when a port needs a fake in the tests.
  2. 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.

The shared context

Each side can have a context named shared. Every context of its side may import it: it is the only exception to the isolation of the contexts. It holds what they have in common.

  • In the backend, src/backend/shared/ holds the events of shared (domain/event/), the values that several contexts use, and the bindings that every backend context gets, in its di.ts. A context imports it with #shared/.
  • In the frontend, src/frontend/shared/ holds the events of shared (event/), and the components that every context can show. A context imports it with #frontend-shared/.
  • A component of shared is a Marko tag: presentation/component/<name>/index.marko, which src/frontend/marko.json gives to Marko. A context places it by its tag, such as <badge label=text/>, and never imports its file.
  • Shared has no context map: it hears no one.

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.

  1. In messages, the event, in domain/event/. It extends DomainEvent:

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();
  }
}
  1. The handler takes an EventPublisher of its events, and publishes it: this.events.publish(new MessageAdded(message.id, message.text)). Its test checks it with expectItToPublish.

  2. 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();
  }
}
  1. The context map of messages shares 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)])
];
  1. The context map of statistics turns 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 share and when. share goes 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, and app.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.