I’m trying to learn (remember I’m a newbie) the most effective ways to handle parameters in Ninject 2.0. This wasn’t a result of refactoring an actual project, I was just curious. I started out trying to bind a primitive type in a constructor.
Parameters and Attributes
Let’s start by just binding a primitive type parameter. You can see below that the WorkoutRoom class takes a string in the constructor.- public class RoomAttribute : Attribute { }
- public class WorkoutRoom
- {
- public WorkoutRoom([Room] string roomName)
- {
- RoomName = roomName;
- }
- public string RoomName { get; set; }
- }
_kernel.Bind<string>().ToConstant(ROOM_NAME).WhenTargetHas<RoomAttribute>();
The ConstructorArgument
If you don’t mind coupling the injection wiring to the actual names of parameters you can use ConstructorArguments. Given the previous example I can rely on the fact that the parameter is named “roomName” and get rid of the attributes and use the following:const string BIG_ROOM_NAME = "Room to move";
var room_arg = new ConstructorArgument("roomName",BIG_ROOM_NAME);
var my_room = _kernel.Get<WorkoutRoom>(room_arg);
var room_arg = new ConstructorArgument("roomName",BIG_ROOM_NAME);
var my_room = _kernel.Get<WorkoutRoom>(room_arg);
const string ROOM_NAME = "Parameter name is no longer roomName - CHANGE IT BACK you dweeb!";
Assert.AreEqual(BIG_ROOM_NAME,my_room.RoomName);
Assert.AreEqual failed. Expected:<Room to move>. Actual:<Parameter name is no longer roomName - CHANGE IT BACK you dweeb!>.
[Inject]
public Barracks(string theNickName, string theFormalName)
{
NickName = theNickName;
FormalName = theFormalName;
}
public Barracks(string theNickName, string theFormalName)
{
NickName = theNickName;
FormalName = theFormalName;
}
[TestMethod]
public void TestSimpleParameter2()
{
const string NICK_NAME = "Dojo Sweet Dojo";
var nick_name_arg = new ConstructorArgument("theNickName", NICK_NAME);
const string FORMAL_NAME = "SweatShop";
var formal_name_arg = new ConstructorArgument("theFormalName", FORMAL_NAME);
var my_barracks = _kernel.Get<Barracks>(nick_name_arg,formal_name_arg);
Assert.AreEqual(NICK_NAME, my_barracks.NickName);
Assert.AreEqual(FORMAL_NAME, my_barracks.FormalName);
Assert.AreEqual(ROOM_NAME, my_barracks.Room.RoomName);
}
public void TestSimpleParameter2()
{
const string NICK_NAME = "Dojo Sweet Dojo";
var nick_name_arg = new ConstructorArgument("theNickName", NICK_NAME);
const string FORMAL_NAME = "SweatShop";
var formal_name_arg = new ConstructorArgument("theFormalName", FORMAL_NAME);
var my_barracks = _kernel.Get<Barracks>(nick_name_arg,formal_name_arg);
Assert.AreEqual(NICK_NAME, my_barracks.NickName);
Assert.AreEqual(FORMAL_NAME, my_barracks.FormalName);
Assert.AreEqual(ROOM_NAME, my_barracks.Room.RoomName);
}
Where the barracks class has a constructor that looks like
public Barracks( IWarrior warrior )
{
Warrior = warrior;
}
{
Warrior = warrior;
}
var constructorArgument = new ConstructorArgument("warrior", new Samurai(new Sword()));
var barracks = kernel.Get<Barracks>(constructorArgument);
var barracks = kernel.Get<Barracks>(constructorArgument);

Previous Post
