Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, you can't bind "after" - for one thing, web applications are stateless and you have no control over the order of events, but more importantly, Ninject modules define your IoC container and this configuration happens before almost <em>anything</em> else in the application or request lifecycle.</p> <p>If you're saying that the user would pick this from a drop-down, then the act of choosing the repository is part of your <em>application logic</em>, not part of your IoC configuration. The way to deal with this is to create a factory interface. The implementation can be a thin wrapper around the Ninject kernel itself.</p> <pre><code>public interface IRisksRepositoryFactory() { IRisksRepository GetRepository(string name); // Optional: add a GetRepositoryNames() method for populating dropdowns, etc. } public class NinjectRisksRepositoryFactory { private readonly IKernel kernel; public NinjectRisksRepositoryFactory(IKernel kernel) { if (kernel == null) throw new ArgumentNullException("kernel"); this.kernel = kernel; } public IRisksRepository GetRepository(string name) { return kernel.Get&lt;IRisksRepository&gt;(name); } } </code></pre> <p>For this particular implementation you'd want to make sure you use named bindings (although generally speaking you could also use the metadata system), and do each binding explicitly:</p> <pre><code>Bind&lt;IRisksRepository&gt;() .To&lt;MySqlRisksRepository&gt;() .InRequestScope() .Named("mysql") .WithConstructorArgument("connectionString", GetConnectionString("mydb1")); Bind&lt;IRisksRepository&gt;() .To&lt;OracleRisksRepository&gt;() .InRequestScope() .Named("oracle") .WithConstructorArgument("connectionString", GetConnectionString("ordb1")); Bind&lt;IRisksRepositoryFactory&gt;() .To&lt;NinjectRisksRepositoryFactory&gt;(); </code></pre> <p>It's also possible to do this without creating each binding explicitly, particularly if all targets are the same type (i.e. you only have a <code>MySqlRisksRepository</code>), by passing the connection string or related symbol as a parameter to the <code>Get</code> call and binding to a contextual method instead of a type - but I'd recommend against it, since it's really swimming against the current as far as DI in general goes.</p> <p>One more thing: Don't worry that this resembles the "service locator" anti-pattern, because that's all it is - a superficial resemblance. When objects need to be able to create dependencies on the fly, creating special-purpose factories around the IoC container is the recommended solution, as it minimizes kernel exposure to a single class which could be easily replaced if you switched to a different framework.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload