Binding an Injection


This tutorial teaches how to bind an injection via a context. Injections are values bound to a type that a mediator or command can depend upon. These values are “configured” in the context. The injection process is called Dependency Injection.

In this tutorial we continue with the result of the previous tutorial. If this is the first time using IoC+, we suggest to read the introduction first.

Adding a Dependency

In our WelcomeCommand we are currently logging the text “Welcome to the Game Context!”. Let’s say we want it to log something else, but we want to configure what we log in the GameContext so we can potentially reuse the WelcomeCommand in different contexts. To do this, we are going to add a dependency in the WelcomeCommand that will be injected by the GameContext. Update the WelcomeCommand to the following code.

using IoCPlus;
using UnityEngine;

public class WelcomeCommand : Command {

    [Inject] string welcomeText;

    protected override void Execute() {


We’ve replaced the hard-coded welcome text with a variable that we assigned with an Inject attribute. This attribute does the magic of injecting the value into this variable that is bound to the string type in the context.

Binding a Value

Let’s open up our context and bind a value to the string type.

using IoCPlus;

public class GameContext : Context {

    protected override void SetBindings() {

        Bind<string>("Welcome! Look at me, I'm injecting a value!");



In the override SetBindings() method, we call the Bind<T>() method to bind a value to the string type. All string typed variables in commands and mediators with the Inject attribute will now be set to the “Welcome! Look at me, I’m injecting a value!” value! Go ahead and hit play, the debugger should log our injected value.

Isn’t this awesome? We’ve decoupled the welcome text value from the WelcomeCommand class, so we can potentially use the command in another context with a different welcome text! We just have to configure the value in its context. Dependency injection doesn’t stop at string values; its true power lies in injecting signals, models and services. These injections are covered in later tutorials.

The IoC+ Monitor

Our new injection binding will be visible in the IoC+ Monitor. Go ahead and open it up, you’ll the String type under the list of injections.

If you don’t see the injection bindings, make sure “Show Injection Bindings” is enabled in the Options of the IoC+ Monitor.


In this tutorial we’ve seen how to add a dependency to a command by adding the inject attribute in front of the field. We learned how to extend the context to bind a value to a string type so that all string typed dependencies in commands and mediators are set to that value. Lastly, we’ve seen how the IoC+ Monitor show us what injection bindings are set in the context.