Ok Dependency Ninja's, here's one for you...
I want to change the way Ninject builds a dependency based upon the type I'm requesting from the Kernel.
I have a DbConnectionFactory class with the following constructors:
public DbConnectionFactory()
: this(MyDatabase.ConnectionString)
{
}
public DbConnectionFactory(string connectionString)
: this(DbProviderFactories.GetFactory("System.Data.SqlClient"), connectionString)
{
}
For the "default" binding, I want Ninject to use the parameterless constructor:
this.Bind<IDbConnectionFactory>().To<DbConnectionFactory>();
Certain classes in my code need Ninject to supply the connectionString paramter. I've tried to setup the binding like so:
this.Bind<IDbConnectionFactory>().To<DbConnectionFactory>().Only(
When.Context.InstanceOf(typeof(IRepository))).WithArgument(
"connectionString", MyOtherDatabase.ConnectionString);
However, I only ever get Ninject to use the default constructor.
I must be missing something obvious!