0
votes

I am new to ninject, I am wondering how I can run custom initizlisation code when constructing the injected objects? ie. I have a Sword class which implements IWeapon, but I want to pass an hit point value to the Sword class constructor, how do I achieve that? Do I need to write my own provider?

A minor question, IKernel kernel = new StandardKernel(new Module1(), new Module2(), ...); what is the actual use of having multiple modules in Kernel? I sorta understand it, but could someone give me a formal explaination and use case?

Thanks a lot!

James

1

1 Answers

1
votes

If you have a class Sword with this constructor:

public Sword(int hitPoints)
    ...

Rather than implementing a Provider, you may prefer to instantiate Swords like this:

IWeapon sword1 = kernel.Get<IWeapon>(With.Parameters.ConstructorArgument("hitPoints", 10));
IWeapon sword2 = kernel.Get<IWeapon>(With.Parameters.ConstructorArgument("hitPoints", 20));