For everyone — A stage manager who brings every prop

Imagine a theater troupe where every actor, on top of performing, had to fetch their own costumes, build their own props, and set up the lighting for their own scenes. The play would quickly become unmanageable. In a well-run theater, there is a stage manager: for every actor, they bring at the right moment whatever is needed, and each person can focus on their role. "Inversion of control" (IoC) is exactly that for a program: instead of each piece of the software building what it needs by itself, a "container" supplies it automatically.

Why should you care? Because it is the condition for a living project to stay modifiable. When every piece is connected through this trusted third party, you can swap one out without dismantling the rest, and check each piece in isolation. It is the daily reality of all large software — and, more broadly, of any organization that grows and delegates instead of doing everything itself.

For Yokul, it is vital: the creatures of a virtual world are assembled like legos — perception, decision, behaviors — and it must be possible to change one brick without rewriting the entire world. The pattern carried this assembler role in the project, first embodied by Google Guice (see Google Guice), then by CDI — the standardized dependency injection of Jakarta EE, which came along with Apache Quarkus. And if the assembler's engine was swapped twice without ever breaking the play, that is precisely the proof that the wiring had been well designed.

The full text

The Inversion of Control pattern

Inversion of control is a design pattern that consists in delegating the creation and management of objects to a framework or an IoC container. First and foremost, an IoC container rests on two principles:

  • separation of concerns: the application's logic is broken up into distinct components, each responsible for one precise task;
  • dependency injection: each component receives from the container the dependencies it needs, rather than building or fetching them itself.

The word "inversion" should be read literally: in a classic program, the component controls its dependencies — it runs its new whenever it wants, from wherever it wants. With IoC, that control is inverted: the component declares what it needs, and it is the container that decides when and how to supply it. The flow shifts from "I take" to "I am given" — the same inversion as the stage manager's: the actor no longer runs to the shop, the shop comes to them.

On how the term spread around containers, the reference is Martin Fowler ("Inversion of Control Containers and the Dependency Injection pattern", martinfowler.com, 2004) — his distinction between the container (the infrastructure) and injection (the pattern) is the one this paper unfolds below.

The container identifies these dependencies through annotations or configuration, then creates and wires them accordingly. There are gains on three fronts: dependency management is delegated once and for all, the code becomes modular (each brick has a clear role), and every component can be tested in isolation — you inject a stand-in for its dependencies, and the measurement no longer depends on the rest of the system.

Be careful not to confuse the two levels: the IoC container is the infrastructure that manages the creation and configuration of objects; dependency injection is the design pattern that this container implements to supply those dependencies to components. The first without the second is a factory without an assembly line; the second without the first is an assembly line without a factory.

The three ways to inject

Three styles coexist in every IoC container, to be chosen as the moment dictates:

Constructor injection — the variant to prefer: dependencies are explicit and the class can be immutable.

public class MyClass {
    private final Dependency dependency;

    @Inject
    public MyClass(Dependency dependency) {
        this.dependency = dependency;
    }
}

Setter injection: handy for optional dependencies, or ones replaceable during the object's lifetime.

public class MyClass {
    private Dependency dependency;

    @Inject
    public void setDependency(Dependency dependency) {
        this.dependency = dependency;
    }
}

Field injection — the most compact, but the least testable outside the container; the writing comfort is paid for in invisibility: reading the class no longer tells you what it lives on.

public class MyClass {
    @Inject
    private Dependency dependency;
}

Scopes: an object's lifetime

The container also plays a role that the classic new left to the luck of the code: deciding how long an object lives. A scope is the rule that fixes that lifetime: a single instance shared across the whole application (singleton), one instance per request or per session in a web application — the same actor class can thus get a "one per world" scope, or a "one per scene" scope, without changing a single line of its code. It is typically in the choice of scopes that a system's coherence is decided: two components that must share state must share a scope.

What IoC really changes

At bottom, the IoC pattern formalizes a design decision older than one might think: separating what a thing does (the intent) from who provides the means (the execution). The same spirit runs, as it happens, through the whole project — the separation of the description and the execution of behaviors (see Behavior Model) or the definition of processes without prescribing the means (see Workflow Engine). IoC is simply its application to the software components themselves: the container is to assembly what the execution graph is to behavior.

For Yokul

This is exactly the role the chain of assemblers played in the project: the assembly of the virtual world's bricks — one single composition mechanism for perception, decision, behaviors — with none of those bricks knowing about each other. First carried by Google Guice (until 2017, see Google Guice), then taken over by CDI under Apache Quarkus, the principle remains: assemble, don't couple.

See also