When setting up my ninject bindings, I'm using the .ToMethod to specify particular parameters for specific connectionstrings, and the WhenInjectedInto method to constrain the binding to specific types:
Bind(Of IDbConnection).ToMethod(Function(context) New OracleConnection(ConnectionStringFactory.GetConnection(DBC.ConnectionStrings.Oracle))).WhenInjectedInto(Of AccountBalancesLookup)()
Bind(Of IDbConnection).ToMethod(Function(context) New OracleConnection(ConnectionStringFactory.GetConnection(DBC.ConnectionStrings.Oracle))).WhenInjectedInto(Of MFUtility)()
My question is, can I do something like this:
Bind(Of IDbConnection).ToMethod(Function(context) New OracleConnection(ConnectionStringFactory.GetConnection(DBC.ConnectionStrings.Oracle))).WhenInjectedInto(Of AccountBalancesLookup, MFUtility)()
Specifying more than one destination for the binding at once, rather than having multiple lines?
Not out of the box. But you can create your own extension to
When(Func<IRequest,bool>)
which does exactly that. For example:used like:
Note that
ComputeMatchConditions
is kind of a hack because it relies on ninject internals.. if these (the implementation ofWhenInjectedInto
) change then this may stop to work. Of course you're free to provide your own alternative implementation. You could also copy the code fromWhenInjectedInto
, see: BindingConfigurationBuilder.cs methodWhenInjectedInto(Type parent)