Can I use Ninject ConstructorArguments with strong naming?
Posted
by stiank81
on Stack Overflow
See other posts from Stack Overflow
or by stiank81
Published on 2010-04-14T11:21:49Z
Indexed on
2010/04/14
12:33 UTC
Read the original article
Hit count: 366
Well, I don't know if "strong naming" is the right term, but what I want to do is as follows.
Currently I use ConstructorArgument like e.g. this:
public class Ninja
{
private readonly IWeapon _weapon;
private readonly string _name;
public Ninja(string name, IWeapon weapon)
{
_weapon = weapon;
_name = name;
}
// ..more code..
}
public void SomeFunction()
{
var kernel = new StandardKernel();
kernel.Bind<IWeapon>().To<Sword>();
var ninja = ninject.Get<Ninja>(new ConstructorArgument("name", "Lee"));
}
Now, if I rename the parameter "name" (e.g. using ReSharper) the ConstructorArgument won't update, and I will get a runtime error when creating the Ninja. To fix this I need to manually find all places I specify this parameter through a ConstructorArgument and update it. No good, and I'm doomed to fail at some point even though I have good test coverage. Renaming should be a cheap operation.
Is there any way I can make a reference to the parameter instead - such that it is updated when I rename the parameter?
© Stack Overflow or respective owner