Note that there are some explanatory texts on larger screens.

plurals
  1. POConfusion about DI, IoC and service locators
    text
    copied!<p>I have read various articles about IoC, DIP, DI and Service Locators but I'm a bit confused which is which because some articles have too vague examples and some other articles have just some specific examples without mentioning other cases. </p> <p><strong>Could you please clear this up for me, looking at the examples below and shortly explaining which examples match which pattern?</strong></p> <ul> <li><p>manually passing interface to constructor:</p> <pre><code>class Consumer { public Consumer(IStore store){...} } ... Consumer c = new Consumer(new ConcreteStore()); </code></pre></li> <li><p>the same as the first example but using some 3rd party library (Unity, Windsor, Ninject)</p></li> <li><p>the same as the first example but using BaseStore class instead of IStore interface</p></li> <li><p>passing dependency to some other method, not to constructor:</p> <pre><code>class Consumer { public BySomething(IStore store){...} } ... Consumer c = new Consumer(); c.BySomething(new ConcreteStore()); </code></pre></li> <li><p>passing dependencies masked inside of some other interface (bonus for this solution - when some other things are invented in the "world" and Consumer wishes to use them, we don't have to change constructor argument but just update IWorld; and we can completely replace entire World with something else when testing):</p> <pre><code>interface IWorld { IDictionary&lt;string,IStore&gt; Stores { get; set; } IDictionary&lt;string,ICityMap&gt; Maps { get; set; } ... } class Consumer { public Consumer(IWorld world){...} public BySomething(string store, string city){...} } ... IWorld myWorld = new HeavenlyWorld(); ... // adding stores, maps and whatnot Consumer c = new Consumer(myWorld); </code></pre> <p>a sub-question: in this case, is IWorld a service locator or not exactly?</p></li> <li><p>passing a call-back function or delegate (.NET Action in this case):</p> <pre><code>c.BySomething(store, city, new Action(() =&gt; {...} )); </code></pre> <p>I added this case because the article <a href="http://devmethodologies.blogspot.com/2012/06/inversion-of-control.html" rel="nofollow">Inversion of Control</a> states that every callback is IoC. Is it true?</p></li> </ul>
 

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