---
title: "Principles & Patterns"
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.middag.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Principles & Patterns

The engineering principles and design patterns the MIDDAG Framework (`middag-io/framework`) is built
on, each tied to the classes that show it. They are decision filters for design and code review, not
a checklist: a principle helps you name a problem, it does not mandate a structure. Every item here
is backed by code on the framework's `main`; the one common pattern it does not use, Abstract Factory, appears only to say so.

This page does not repeat the architecture. The layering, the OSS boundary, the ownership map and the
locked decisions are in [Architecture](/framework/explanation/architecture/). Where a file lives inside `src/` is in
[Module Layout Convention](/framework/reference/module-layout-convention/).

Classes below are named relative to `Middag\Framework\`. Only classes tagged `@api` are extension
points; a class tagged `@internal` may change in any release (see
[Versioning and compatibility](/libraries/reference/versioning-and-compatibility)).

## Principles

### Proportionality

Add a layer only when it solves a problem you have now. The framework keeps concrete, stable helpers
concrete: `Shared\Util\Arr` and `Shared\Util\Inflector` are `final` classes with no interface,
because nobody needs a second implementation of array merging or pluralisation. An interface, a hook
or a policy object is worth its cost only where a real variation exists, which the next principles
make concrete.

### One reason to change

A class should have one reason to change. Use it as a review question: does this class mix
orchestration with persistence, integration or formatting? Would changing how data is stored force
an edit to a business rule?

The framework splits along those lines. `Database\Contract\ConnectionAdapterInterface` composes three
focused contracts (`ConnectionInterface` for raw SQL, `RecordConnectionInterface` for record CRUD and
streaming, `SqlDialectInterface` for dialect rendering) and adds only capability reporting. It speaks
tables, records and SQL, never domain types: mapping rows to entities happens above it, in
repositories.

Two interfaces that share a name show the same rule. `Bus\Retry\RetryPolicyInterface` decides whether
a queued item is retried and how long to wait, with no host dependency. `Http\Contract\RetryPolicyInterface` decides the same for an outbound HTTP attempt, from the
attempt number, the response status and a `Retry-After` header. Each has its own reason to change, so they stay separate.

The same test settles a split in your own code. Parts that are edited for different reasons, or a
test of one part that needs elaborate fakes of the other, point to two classes. Parts that are always
edited together stay in one class: splitting them would only add wiring.

### Depend on a contract where variation is real

Introduce an interface when at least one of these holds:

- the dependency is an external integration (mail, storage, error reporting, a database);
- the component is a published extension point of the framework;
- it is the boundary between a rule and the mechanism that carries it out;
- an isolated test gains real value from a fake.

When none holds, a concrete class is the better choice.

The framework pairs each such contract with an OSS default, so it runs with no host adapter installed:

| Contract | OSS default | Other implementation in the framework |
|---|---|---|
| `Mail\Contract\MailerInterface` | `Mail\NullMailer` (discards) | – |
| `Observability\Contract\ErrorReporterInterface` | `Observability\NullErrorReporter` | `Observability\SentryErrorReporter` (needs `sentry/sentry`) |
| `Filesystem\Contract\FilesystemInterface` | `Filesystem\LocalFilesystem` | `Filesystem\FlysystemFilesystem` (needs `league/flysystem`) |
| `Kernel\Contract\MaintenanceGateInterface` | `Kernel\Bootstrap\NullMaintenanceGate` | – |
| `Http\Contract\ExceptionRendererInterface` | `Http\DefaultExceptionRenderer` | – |

Host adapters implement the same contracts against the host. The full list of ports an adapter
fills is in [Bridge Contracts](/framework/reference/bridge-contracts/).

Every contract in the table has either a second implementation in the framework or a host that
supplies its own. A new contract with neither is a single class behind an extra name: keep the class
concrete until the second implementation is real.

### Composition over inheritance

Inherit only for a real subtype relation, from a base class designed to be extended, where any
consumer of the base can take the subclass without surprise. The framework's abstract bases are those
designed extension points: `Http\Controller\AbstractController` (host adapters extend it with page
setup, auth and rendering), `Kernel\Module\AbstractModule`, `Persistence\Repository\AbstractRepository`
and `Bus\Command\AbstractCommand`. `Exception\MiddagException` roots a typed is-a hierarchy (see
[Exception Hierarchy](/framework/reference/exception-hierarchy/)). `Database\MysqlSqlDialect` is deliberately
not `final`, so a host on the MySQL family can override only its host-specific idioms and inherit the
MySQL SQL as is.

Compose when the variation is behavioural, configurable or an integration. The kernel does not expect
to be subclassed for a themed error page: `Http\HttpKernel` resolves an `ExceptionRendererInterface`
from its argument or the container, and falls back to `DefaultExceptionRenderer`. `Kernel\ContainerFactory` takes a
`BootFailurePolicyInterface` in its constructor.
Most concrete classes are `final`, which keeps inheritance an explicit choice of the base class.

The test is substitutability. A host controller is still an `AbstractController` to the code that
runs it, and a host dialect on the MySQL family is still a `MysqlSqlDialect`. A subclass that only
borrows code from its parent is composition written as inheritance; inject the collaborator instead.

### Explicit extension points

Open the code to extension only at points that are deliberate and documented. In the framework, an
extension point is a published `@api` contract, attribute or base class; `@api` is also the surface
the [Versioning and compatibility](/libraries/reference/versioning-and-compatibility) promise covers.
An `@internal` class is not an extension point even when it implements a public contract:
`Kernel\Bootstrap\BootRethrowFailurePolicy` is `@internal`, while `BootFailurePolicyInterface`, which
it implements, is `@api`. Bind your own implementation of the contract instead of extending it.

A seam can exist with no OSS implementation when the behaviour belongs to a tier above.
`Bus\Contract\SignalDispatcherInterface` is the publish-side contract for signals, and the framework
ships no implementation of it: a host adapter or a library downstream provides one. Do not add a hook,
a contract or an interface on speculation; if nothing extends a point, it did not need to exist.

## Patterns

Each pattern below says when to skip it. Read that part first: an unneeded pattern costs as much as a
missing one.

### Strategy

Use it when the same intent has several real algorithms or policies, selected by type, configuration
or context, or when a growing `if`/`switch` on a type is appearing. Skip it for a small local variation
a plain `if` handles, or when only one implementation exists and none is planned.

In the framework, a strategy is a small contract resolved through the container or passed to a
constructor:

- `Kernel\Contract\BootFailurePolicyInterface`: `BootRethrowFailurePolicy` (the default, loud in
  development) or `BootIsolateFailurePolicy` (log at `critical` and boot the remaining modules), given
  to `ContainerFactory`. See [Bootstrap Lifecycle](/framework/explanation/bootstrap-lifecycle/).
- `Bus\Retry\RetryPolicyInterface`, implemented by `Bus\Retry\MultiplierRetryPolicy`: it has no
  host dependency, so queue implementations that only agree on the attempt shape can share it. In the
  framework, `Bus\Command\CommandWorker` and `Runtime\StandaloneKernel` take it.
- `Database\Contract\SqlDialectInterface`: `StandardSqlDialect` (ANSI, a near-passthrough) or
  `MysqlSqlDialect` (MySQL and MariaDB idioms).
- `Http\Contract\ExceptionRendererInterface`, bound by an adapter to wrap the host layout.

### Decorator

Use it to add a cross-cutting concern over an existing contract, when the concern does not belong in
the decorated class and may apply to more than one implementation. A decorator implements the same contract as
the object it wraps and delegates everything it does not change. Skip it for a one-off case, or when
the behaviour is essential to the class rather than cross-cutting.

`Database\PrefixingSqlDialect` wraps a prefixless `SqlDialectInterface` such as `StandardSqlDialect`,
prefixes the logical table name in `table()` and passes every other method through. A host dialect
that already prefixes its tables is not wrapped.

Where a pipeline exists, the framework adds cross-cutting behaviour as middleware instead of a
decorator. `Bus\Middleware\ProfilingMiddleware` records every dispatched message "without decorating
the bus or the handlers", prepended to the stack through `Bus\MessageBusFactory`. On HTTP,
`Http\Contract\RouteMiddlewareInterface` plays the same role per route (see
[PSR-15 HTTP boundary and middleware](/framework/reference/psr15-bridge-and-middleware/)).

### Adapter

Use it to isolate an external API behind a stable internal contract, to translate a host API into
the framework's terms, or to absorb version differences. Skip it for a wrapper that isolates nothing.

- `Database\Contract\ConnectionAdapterInterface` is the single seam every host database implements;
  the framework's own implementation is `Database\PdoConnectionAdapter`, and the host adapters
  implement it over the host's database API.
- `Filesystem\FlysystemFilesystem` bridges the filesystem port onto any Flysystem adapter.
- `Observability\SentryErrorReporter` forwards to the Sentry SDK behind `ErrorReporterInterface`.

The host adapters as a whole apply this pattern to the framework's ports; see
[Bridge Contracts](/framework/reference/bridge-contracts/).

### Observer and hooks

Use it when other code reacts to a fact without the emitter depending on it, and new reactions may
appear without editing the emitter: auditing, notification, side integrations. Skip it when the
reaction is an essential step of the main flow, or when it would hide a dependency that should be
explicit.

- `Kernel\Manager\HookManager`, the default `HookManagerInterface`: per-instance filter and action
  hooks with priorities. See [Hooks — Public Surface and Naming](/framework/reference/hooks-public-surface/).
- `Kernel\Contract\HostEventBridgeInterface`: synchronous broadcast of named host events to void
  listeners, implemented by the host adapters.
- `Bus\Contract\SignalDispatcherInterface`: the object-as-signal publish contract, implemented
  downstream.

### Command

Use it for work that can be deferred, retried or serialised, when the intent should be separate from
its execution, or when the same work runs both synchronously and asynchronously. Skip it for logic
that should simply be called, or for trivial operations.

- `Bus\Contract\CommandInterface` makes a command round-trip through a primitives-only payload
  (`toPayload()`, `fromPayload()`), so it can be persisted and replayed.
- `Bus\Command\AbstractCommand` is the behaviour-free base for module commands and the anchor of the
  `#[Schedule]` attribute; `#[AsCommandHandler]` marks its handler.
- `Bus\Contract\MessageBusInterface` dispatches it, inline or to a transport, and
  `Bus\Command\CommandWorker` drains the transports later.

Details: [Command bus, handlers and scheduling](/framework/reference/command-bus/).

### Factories, not abstract factories

Creation goes through the container first, and through a plain factory when assembling an object
graph takes real work: `Kernel\ContainerFactory`, `Http\HttpKernelFactory`, `Bus\MessageBusFactory`
and `Logging\LoggerFactory`. The framework has no abstract factory: no family of related objects is
swapped as a unit. Reach for one only when such a family really exists; a single object needs a plain
factory, and a choice of algorithm needs a strategy.

## Combining them

In the framework the patterns meet at a few points:

- `Kernel\ContainerFactory` takes a `BootFailurePolicyInterface`: a strategy chosen when the container
  is built, with `BootRethrowFailurePolicy` as the default;
- `Database\PrefixingSqlDialect` is itself a `SqlDialectInterface`: the decorator and the strategy it
  wraps share one contract, so the code that renders SQL does not know it is there;
- commands travel through `Bus\Contract\MessageBusInterface`, and `Bus\Middleware\ProfilingMiddleware`,
  when it is in the stack, sees each one on the way: the command carries the work, the middleware adds the cross-cutting
  concern.

## See also

- [Architecture](/framework/explanation/architecture/): layers, the OSS boundary and the ownership map.
- [Module Layout Convention](/framework/reference/module-layout-convention/): where a new contract or class
  lives.
- [Bridge Contracts](/framework/reference/bridge-contracts/): the ports a host adapter implements.
- [Pre-PR Boundary Checklist](/framework/how-to/pre-pr-boundary-checklist/): what to check before opening a
  pull request against an OSS library.

Source: https://docs.middag.dev/framework/explanation/principles-and-patterns/index.mdx
