best way to bind connection string constructor parameters?

76 views
Skip to first unread message

Daniel Schobel

unread,
Apr 25, 2012, 10:54:18 PM4/25/12
to nin...@googlegroups.com
Hi all, I'm looking for advice on how best to bind a couple of connection strings which recur throughout my dependencies in ninject.

Currently I have:  

  Bind<IFoo>().To<SqlFoo>()
 .WithConstructorArgument("db1ConStr", db1ConnectionString)
 .WithConstructorArgument("db2ConStr",  db2ConnectionString);

  Bind<IBar>().To<SqlBar>()
 .WithConstructorArgument("db1ConStr", db1ConnectionString)
 .WithConstructorArgument("db2ConStr",  db2ConnectionString)

etc.

which obviously is not the most elegant code.

What the idiomatic way with ninject to bind the value of  db1ConnectionString to every constructor parameter named "db1ConStr"?

Bartelink, Ruben

unread,
Apr 26, 2012, 4:01:50 AM4/26/12
to nin...@googlegroups.com
You could create an extension method for whatever To<T> returns which takes a ConnectionStrings object and then you'd be able to do:

  Bind<IFoo>().To<SqlFoo>().WithOurStandardConnectionStrings( connectionStrings);

Or you could (and I reckon its cleaner) 
Bind<IConnectionStrings>().To...

and then have SqlFoo and SqlBar ask for IConnectionStrings instead

(If you can't change the code for SqlFoo then dervive from them ones that do

--Ruben
Reply all
Reply to author
Forward
0 new messages