Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Singleton is bad, no doubt about it.</p> <p>In the case you describe, the database object is an <strong>implementation detail</strong> of the User object. The layers above need only know about the User, not the database object.</p> <p>This becomes much more apparent if you <strong>hide the user object behind an interface</strong> and only consume that interface from the layers above.</p> <p>So the page controller should deal only with the interface, not the concrete class that depends on the database object, but how does in create new instances? It uses an injected <strong>Abstract Factory</strong> to create instances of the interface. It can deal with any implementation of that interface, not only the one that relies on a database object.</p> <p>Once more, you hide the page controller behind an interface. This means that the concrete implementation's reliance on the Abstract Factory becomes another implementation detail. The Application Controller only consumes the page controller interface.</p> <p>You can keep wrapping objects like that like without ever needing to pass around instances. Only in the <a href="https://stackoverflow.com/questions/1475575/where-should-i-do-dependency-injection-with-ninject-2/1475861#1475861">Composition Root</a> do you need to wire all dependencies together.</p> <p>See here for a related answer with examples in C#: <a href="https://stackoverflow.com/questions/2386487/is-it-better-to-create-a-singleton-to-access-unity-container-or-pass-it-through-t/2386636#2386636">Is it better to create a singleton to access unity container or pass it through the application?</a></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