Ninject Cheat Sheet
Constructor Injection
class Samurai {
private readonly IWeapon _weapon;
[Inject] // limit to 1 constructor ONLY, use Property Injection if you need more than 1 however the order is non-deterministic.
public Samurai(IWeapon weapon) {
_weapon = weapon;
}
public void Attack(string target) {
_weapon.Hit(target);
}
}
Activation Process
For instance,
//note: type bindings are collected into groups called modules.
//You can create as many modules as you'd like, and pass them all to the kernel's constructor.
//e.g. IKernel kernel = new StandardKernel(new Module1(), new Module2(), ...);
class WarriorModule : StandardModule {
public override Load() {
//note: Module isn’t limited to a boring, static collection of bindings. You can always do more creative things like conditional bindings & contextual bindings.
Bind<IWeapon>().To<Sword>(); // type binding
Bind<Samurai>().ToSelf(); // self-bound, only allow in concrete type.
}
}
class Program {
public static void Main() {
IKernel kernel = new StandardKernel(new WarriorModule());
Samurai warrior = kernel.Get<Samurai>();
warrior.Attack("the evildoers");
}
}
The activation process for the Samurai type is as follows. The number to the left indicates the depth at which the activation is occurring, and some steps have been left out for clarity:
- (1) The kernel resolves the self-binding for the
Samuraitype that was defined in theWarriorModule. - (1) The kernel asks the
Samurai‘sTransientBehaviorto resolve an instance. - (1) The Samurai’s
TransientBehaviorasks the binding’s provider to create a new instance. - (2) The Samurai’s
StandardProviderasks the kernel to resolve an instance ofIWeapon. - (2) The kernel resolves the binding from
IWeapontoSwordthat was defined in theWarriorModule. - (2) The kernel asks the
Sword‘sTransientBehaviorto resolve an instance. - (2) The
Sword‘sTransientBehaviorasks the binding’s provider to create a new instance. - (2) The
Sword‘sStandardProvidercalls theSword‘s parameterless constructor. - (2) The kernel returns the newly-created instance of
Sword. - (1) The
Samurai‘sStandardProvidercalls theSamurai‘s injection constructor, passing it the instance ofSwordas an argument. - (1) The kernel returns the newly-created instance of
Samuraito the site of the method call inMain().
Activation Behavior
There are four built-in behaviors available in Ninject. Attribute is class level:
TransientBehavior [Transient]
A new instance of the type will be created each time one is requested.
SingletonBehavior [Singleton]
Only a single instance of the type will be created, and the same instance will be returned for each subsequent request.
OnePerThreadBehavior [OnePerThread]
One instance of the type will be created per thread.
OnePerRequestBehavior [OnePerRequest]
One instance of the type will be created per web request, and will be destroyed when the request ends.
Instead of relying on attributes, you can also define the behavior when you declare the binding:
Bind<Shogun>().ToSelf().Using<SingletonBehavior>();
Contextual Binding
For instance,
Bind<IWeapon>().To<Sword>();
Bind<IWeapon>().To<Shuriken>().Only(When.Context.Target.HasAttribute<RangeAttribute>()); // using attrib
Bind<IWeapon>().To<Shuriken>().Only(When.Context.Tag == "range"); // using attrib: [Inject, Tag("range")]
Bind<IWeapon>().To<Shuriken>().Only(When.Context.Service.Name.StartsWith(“Foo”); // using convention
Bind<IWeapon>().To<Shuriken>().Only(When.Context.Target.Name.BeginsWith(“Remote”); // using convention
// note: IWeapon is the “Service Type”(generally an interface of abstract type)
// while Sword/Shuriken are “Impl/Target Type”
For a complete tutorial visit http://dojo.ninject.org/
No comments yet.
