Ninject Cheat Sheet


image

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 Samurai type that was defined in the WarriorModule.
  • (1) The kernel asks the Samurai‘s TransientBehavior to resolve an instance.
  • (1) The Samurai’s TransientBehavior asks the binding’s provider to create a new instance.
  • (2) The Samurai’s StandardProvider asks the kernel to resolve an instance of IWeapon.
  • (2) The kernel resolves the binding from IWeapon to Sword that was defined in the WarriorModule.
  • (2) The kernel asks the Sword‘s TransientBehavior to resolve an instance.
  • (2) The Sword‘s TransientBehavior asks the binding’s provider to create a new instance.
  • (2) The Sword‘s StandardProvider calls the Sword‘s parameterless constructor.
  • (2) The kernel returns the newly-created instance of Sword.
  • (1) The Samurai‘s StandardProvider calls the Samurai‘s injection constructor, passing it the instance of Sword as an argument.
  • (1) The kernel returns the newly-created instance of Samurai to the site of the method call in Main().

 

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/

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s