Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I agree with your concerns. It's a code smell to take dependencies for the sole purpose of passing them on to other dependencies.</p> <p>Depending on the exact interaction between those dependencies, you have a couple of options:</p> <p><strong>If only one instance of the dependency is needed</strong></p> <p>If your Controller only needs a single instance of a dependency, then just take a dependency on that instead.</p> <p>(apologies for the c# code)</p> <p><strong>Don't</strong> do this:</p> <pre><code>public class MyController { public MyController(IDb db) { var dep = new MyDependency(db); // Use dep or save it for later } } </code></pre> <p>Instead, you can do this:</p> <pre><code>public class MyController { public MyController(MyDependency dep) { // Use dep or save it for later } } </code></pre> <p>You may want to consider <code>MyDependency</code> behind an interface itself. See also <a href="http://blog.ploeh.dk/2010/02/02/RefactoringToAggregateServices.aspx" rel="nofollow noreferrer">Refactoring to Aggregate Services</a>.</p> <p><strong>If you need to create multiple instances during the Controller's lifetime</strong></p> <p>Sometimes, however, you need to create multiple instances dynamically. This is often the case when you need a value that's only available at run-time before you can fully populate a dependency.</p> <p>In this case, an <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern" rel="nofollow noreferrer">Abstract Factory</a> is an <a href="https://stackoverflow.com/questions/1926826/cant-combine-factory-di/1927167#1927167">excellent and universal solution</a>.</p>
    singulars
    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