23

How I can use multiple parameters in Ninject syntax like following?

Bind<IMyRepository>()
.To<SqlMyRepository>()
.WithConstructorArgument("connectionString",
 ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString
 );

What if more than one parameter need to be passed?

rem
  • 16,745
  • 37
  • 112
  • 180

1 Answers1

64

You can chain the calls to WithConstructorArgument:

Bind<IMyRepository>()
    .To<SqlMyRepository>()
    .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString)
    .WithConstructorArgument("timeout", 10000);
Martin Owen
  • 5,221
  • 3
  • 35
  • 31
  • if your argument name changes in the ctor, you won't get a compile error and ninject will fail to bind. – seabass Aug 16 '18 at 17:02
  • @seabass this is the downside of dependency injection, the flexibility of defining things at runtime _can_ cause runtime errors. Integration testing should help flush them out. – Martin Owen Sep 07 '18 at 15:31