1

The objective is to avoid having to declare the same constructor arguments multiple times when configuring a kernel in the following manner:

Kernel.Bind<ISomeService>().To<SomeService>()
  .WithConstructorArgument("arg", "value");
Kernel.Bind<SomeService>.ToSelf()
  .WithConstructorArgument("arg", "value");

tried with:

Kernel.Bind<ISomeService>().To<SomeService>();
Kernel.Bind<SomeService>().ToSelf().WithConstructorArgument("arg", "value");

Hoping that the "bind chain" will be taken into account, but nope.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andres A.
  • 1,329
  • 2
  • 21
  • 37

1 Answers1

1

You can pack your argument in a type, for example:

public class FooConfig
{
    private readonly string value;

    public FooConfig(string value)
    {
        this.value = value;
    }

    public Value
    { 
        get { return this.value; }
    }
}

then bind it like:

Bind<FooConfig>().ToConstant(new FooConfig("configValue"));

and then adapt the dependencies to have the FooConfig type injected.

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
  • Please do you think you could take at look a this question? http://stackoverflow.com/questions/40198294/how-to-configure-ninject-so-that-it-creates-a-single-instance-per-controller?noredirect=1#comment67673663_40198294 – eddy Oct 23 '16 at 18:28