Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I prefer not having a DI Container dictate my API design. The container should conform to proper design, not the other way around.</p> <p>Design your classes in a <a href="https://stackoverflow.com/questions/2045904/dependency-inject-di-friendly-library/2047657#2047657">DI-friendly</a> manner, but without making concessions to your DI Container. If you need a connection string, then take a string through the constructor:</p> <pre><code>public class Repository : IRepository { public Repository(string path) { //... } } </code></pre> <p>Many DI Containers can deal with primitive values. As an example, here's one way to do it with Windsor:</p> <pre><code>container.Register(Component.For&lt;IRepository&gt;() .ImplementedBy&lt;Repository&gt;() .DependsOn( new { path = "myPath" } )); </code></pre> <p>However, if your container of choice can't deal with primitive parameters, you can always decorate <code>Repository</code> with an implementation that knows how to find the string:</p> <pre><code>public class ConfiguredRepository : IRepository { private readonly Repository decoratedRepository; public ConfiguredRepository() { string path = // get the path from config, or whereever appropriate this.decoratedRepository = new Repository(path); } // Implement the rest of IRepository by // delegating to this.decoratedRepository } </code></pre> <p>Now you can simply tell your container to map <code>IRepository</code> to <code>ConfiguredRepository</code>, while still keeping the core Repository implementation clean.</p>
 

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