I’m looking for advice on how best to bind a couple of connection
strings which recur throughout my dependencies.
Currently I have (using ninject):
Bind<IFoo>().To<SqlFoo>()
.WithConstructorArgument("db1ConnStr", db1ConnectionString)
.WithConstructorArgument("db2ConnStr", db2ConnectionString);
Bind<IBar>().To<SqlBar>()
.WithConstructorArgument("db1ConnStr", db1ConnectionString)
.WithConstructorArgument("db2ConnStr", db2ConnectionString);
etc.
which obviously is not the most elegant code.
Is there a way to bind the value of db1ConnectionString to every string constructor parameter named “db1ConnStr”?
I would create a class which holds the connection strings:
Note: You can also create an interface
IConnectionStringProviderfor it if you want.Then the classes
SqlFooandSqlBarcan use it as a dependencyAnd the registration would look like this: