If you are new to the Inversion of Control (IoC) pattern then you’re in the right spot. This page explains the basics of IoC, gives an introduction to IoC+ and explains what IoC+ adds to the standard IoC foundation so it can serve as much more.
IoC is a design principle that can be used as a foundation for an entire game or application. It focuses on keeping classes decoupled so they can easily be managed, extended and unit tested. IoC+ consists out of 5 component types: mediators, views, commands, contexts and signals.
Views are objects we can see and/or interact with. A mediator controls a view and a command performs a task. The mediators and commands use signals to communicate. Contexts link the other components together and inject dependencies into the mediators and commands. Having mediators, commands and their dependencies decoupled means that they can easily be swapped, reused and unit tested.
Added value of working with Inversion of Control:
- Sets the foundation for a decoupled, modular code base
- Keeps classes focused on what they are designed for
- Great for both individuals and teams
- Enforces a clear structure for projects of any scale
- Allows for maximum unit testing
The following table explains the components of IoC+ in more detail.
A view can display an object, receive user input and/or check for collisions. It is controlled by a mediator.
Views are the only Unity MonoBehaviours in IoC+
A mediator controls a view. It listens to signals from commands and its view and makes the view act accordingly.
In IoC+, the type of mediator that is attached to a view is based on bindings set in a context.
A command is preferably made to do only one thing. Like instantiating a view, loading a save-file or opening a door.
In IoC+, commands are executed as a response to a signal, defined in a context.
A context sets injection values, associates mediators to views and assigns commands as responses to signals.
In IoC+, contexts are able to have child contexts and can easily be switched during run-time.
Signals allow views to trigger their mediator, mediators to trigger commands and commands to trigger mediators.
Why we made IoC+
We’ve been focusing a lot on code quality and find that patterns like MVC and IoC are a great foundation to enforce quality in code. A framework that enforces such a pattern is a great benefit for both beginning and skilled programmers. IoC+ takes inspiration from our previous work, Code Control (MVC for Unity), and from our experience using the StrangeIoC framework, which does a great job implementing IoC in Unity. IoC+ builds on those and aims to be the most complete IoC framework for Unity.
Why use IoC+
In traditional IoC development, a bunch of contexts are set to define a game’s (or application’s) behavior but the set of contexts do not change during run-time. The contexts live next to each other, their bindings can’t be scoped and contexts generally do not overwrite each other’s bindings. For applications this is usually enough, but games often require more control.
IoC+ makes it easy to instantiate contexts during run-time. A game can have multiple context instances that each control its own, for example, robot without having to worry about its signals leaking to other contexts as the injections are isolated in each context. It’s also possible to have nested contexts in IoC+. The robot contexts could all live in a parent level context. This level context could listen for any robot to be hit and make all robots act accordingly as signal injection bindings of parent contexts will be injected in each child context.
Another feature that makes IoC+ unique is its combination of the finite state machine pattern and IoC. A child context can act as its parent’s state. With simple commands this state can be changed, calling the state’s enter / leave signal accordingly to conveniently enter or leave game menu’s, robot behaviors or even switch entire levels. This is a big benefit for games.
Last but not least, everything that happens in IoC+ can be monitored using the IoC+ context monitor. The monitor gives a live representation of the present contexts, their bindings and their parent/child/state relations. Signals and commands light up when dispatched or executed and signals can even be dispatched interactively from the monitor. The monitor can pause to walk through its history step by step and associated C# code files can conveniently be opened. The IoC+ context monitor really helps keeping track of contexts and is a big help during code reviews and knowledge transfer when working in teams.
To summarize, here are some of the added values of working with IoC+ when using Inversion of Control:
- Easy to add / remove contexts during run-time
- Nested contexts with isolated context injections
- Switchable contexts during run-time to change behavior
- Integrated Finite State Machine in Inversion of Control
- Live monitoring and interaction in Unity Editor