Skip to content
nkohari edited this page Sep 13, 2010 · 1 revision

One of the more powerful (and complex) features of Ninject is its contextual binding system. We mentioned earlier that you can register more than one binding for a type. Up until this point, we’ve only been talking about default bindings — bindings that are used unconditionally, in any context. There’s another kind of binding that’s available, a conditional binding.

As is suggested by its name, this sort of binding has a condition associated with it. This condition (represented by the ICondition<T> interface) examines the context in which the activation is occurring, and decides whether or not the binding should be used. Through the use of conditional bindings, you can design pretty much any sort of binding scheme you can dream up.

As you no doubt noticed, the ICondition<T> interface is generic, meaning it can be used to test things other than contexts. It’s also used to evaluate methods for interception, and for declaring selection heuristics. More on those things later. Right now, we’re only talking about examining the activation context, and the interface is really simple. Here it is:

interface ICondition<T> {
  bool Matches(T obj);
}

Conditions that test the activation context are generally created via the fluent interface, rooted in the When class in Ninject.Conditions. Since not all projects will require conditional binding, this namespace is actually stored in a separate assembly from Ninject.Core. To use it in your project you’ll need to first add a reference to it. Here’s an example of the fluent interface:

When.Context.Service.Name.StartsWith("Foo");

This condition will resolve to true when the service’s type name starts with Foo.

To define a conditional binding, the two fluent interfaces (binding and condition) work together. Here’s an example:

Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Shuriken>().Only(When.Context.Target.HasAttribute<RangeAttribute>());

With these two bindings defined, whenever an instance of IWeapon is requested, by default, Ninject will activate an instance of Sword. However, if the [target|Injection Target] that is being injected is decorated with a [Range] attribute, Ninject will activate an instance of Shuriken instead.

The [Range] attribute is just a simple attribute that you create yourself, and is used as a marker for Ninject:

public class RangeAttribute : Attribute {}

Then, you can create two different implementations of a warrior, like so:

public class Swordsman {
  [Inject] public IWeapon Weapon { get; set; }
}
public class Ninja {
  [Inject] public IWeapon MeleeWeapon { get; set; }
  [Inject, Range] public IWeapon RangeWeapon { get; set; }
}

If you’d rather not create your own attributes, Ninject supplies a [Tag] attribute that you can use instead:

public class Ninja {
  [Inject] public IWeapon MeleeWeapon { get; set; }
  [Inject, Tag("range")] public IWeapon RangeWeapon { get; set; }
}

Then, your bindings would instead look like this:

Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Shuriken>().Only(When.Context.Tag == "range");

However, since string-based identifiers can be prone to typing mistakes, it’s usually a good idea to just create the attributes yourself.

Continue reading: Conventions-Based Binding