<p>I've been using StructureMap recently and have enjoyed the experience thoroughly.  However, I can see how one can easily get carried away with interfacing everything out and end up with classes that take in a boatload of interfaces into their constructors.  Even though that really isn't a huge problem when you're using a dependency injection framework, it still feels that there are certain properties that really don't need to be interfaced out just for the sake of interfacing them.</p>

<p>Where do you draw the line on what to interface out vs just adding a property to the class?</p>

[{'answer_id': 28470, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 2, 'selected': False, 'text': '<p>What do you mean by "just adding a property to a class?"</p>\n\n<p>My rule of thumb is to make the class unit testable.  If your class relies on the implementation details of another class, that needs to be refactored/abstracted to the point that the classes can be tested in isolation.</p>\n\n<p>EDIT: You mention a boatload of interfaces in the constructor.  I would advise using setters/getters instead.  I find that it makes things much easier to maintain in the long run.</p>\n'}, {'answer_id': 28471, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 2, 'selected': False, 'text': "<p>I do it only when it helps with separation of concerns.</p>\n\n<p>Like maybe cross-project I would provide an interface for implementers in one of my library project and the implementing project would inject whatever specific implementation they want in.</p>\n\n<p>But that's about it... all the other cases it'd just make the system unnecessarily complex</p>\n"}, {'answer_id': 28479, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': True, 'text': "<p>Think about your design.  DI allows you to change how your code functions via configuration changes.  It also allows you to break dependencies between classes so that you can isolate and test objects easier.  You have to determine where this makes sense and where it doesn't.  There's no pat answer.</p>\n\n<p>A good rule of thumb is that if its too hard to test, you've got some issues with single responsibility and static dependencies.  Isolate code that performs a single function into a class and break that static dependency by extracting an interface and using a DI framework to inject the correct instance at runtime.  By doing this, you make it trivial to test the two parts separately.</p>\n"}, {'answer_id': 28483, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 1, 'selected': False, 'text': "<p><em>Even with all the facts and processes in the world.. every decision boils down to a judgment call - Forgot where I read that</em><br>\nI think it's more of a experience / flight time call.\nBasically if you see the dependency as a candidate object that may be replaced in the near future, use dependency injection. If I see 'classA and its dependencies' as one block for substitution, then I probably won't use DI for A's deps.</p>\n"}, {'answer_id': 28502, 'author': 'Harpreet', 'author_id': 855, 'author_profile': 'https://Stackoverflow.com/users/855', 'pm_score': 1, 'selected': False, 'text': "<p>The biggest benefit is that it will help you understand or even uncover the architecture of your application.  You'll be able to see very clearly how your dependency chains work and be able to make changes to individual parts without requiring you to change things that are unrelated.  You'll end up with a loosely coupled application.  This will push you into a better design and you'll be surprised when you can keep making improvements because your design will help you keep separating and organizing code going forward.  It can also facilitate unit testing because you now have a natural way to substitute implementations of particular interfaces.</p>\n\n<p>There are some applications that are just throwaway but if there's a doubt I would go ahead and create the interfaces.  After some practice it's not much of a burden.</p>\n"}, {'answer_id': 28620, 'author': 'flipdoubt', 'author_id': 470, 'author_profile': 'https://Stackoverflow.com/users/470', 'pm_score': 0, 'selected': False, 'text': '<p>Another item I wrestle with is <em>where should I use dependency injection?</em> Where do you take your dependency on StructureMap? Only in the startup application? Does that mean all the implementations have to be handed all the way down from the top-most layer to the bottom-most layer?</p>\n'}, {'answer_id': 28736, 'author': 'Jorge Córdoba', 'author_id': 2695, 'author_profile': 'https://Stackoverflow.com/users/2695', 'pm_score': 5, 'selected': False, 'text': '<p>The main problem with dependency injection is that, while it gives the appearance of a loosely coupled architecture, it really doesn\'t.</p>\n\n<p>What you\'re really doing is moving that coupling from the compile time to the runtime, but still if class A needs some interface B to work, an instance of a class which implements interface B needs still to be provided.</p>\n\n<p>Dependency injection should only be used for the parts of the application that need to be changed dynamically without recompiling the base code.</p>\n\n<p>Uses that I\'ve seen useful for an Inversion of Control pattern:</p>\n\n<ul>\n<li>A plugin architecture. So by making the right entry points you can define the contract for the service that must be provided.</li>\n<li>Workflow-like architecture. Where you can connect several components dynamically connecting the output of a component to the input of another one.</li>\n<li>Per-client application. Let\'s say you have various clients which pays for a set of "features" of your project. By using dependency injection you can easily provide just the core components and some "added" components which provide just the features the client have paid.</li>\n<li>Translation. Although this is not usually done for translation purposes, you can "inject" different language files as needed by the application. That includes RTL or LTR user interfaces as needed.</li>\n</ul>\n'}, {'answer_id': 172231, 'author': 'Korbin', 'author_id': 17902, 'author_profile': 'https://Stackoverflow.com/users/17902', 'pm_score': 3, 'selected': False, 'text': "<blockquote>\n  <p>Dependency injection should only be used for the parts of the\n  application that need to be changed dynamically without recompiling\n  the base code</p>\n</blockquote>\n\n<p>DI should be used to isolate your code from external resources (databases, webservices, xml files, plugin architecture). The amount of time it would take to test your logic in code would almost be prohibitive at a lot of companies if you are testing components that DEPEND on a database. </p>\n\n<p>In most applications the database isn't going to change dynamically (although it could) but generally speaking it's almost always good practice to NOT bind your application to a particular external resource. The amount involve in changing resources should be low (data access classes should rarely have a cyclomatic complexity above one in it's methods).</p>\n"}, {'answer_id': 172249, 'author': 'George Mauer', 'author_id': 5056, 'author_profile': 'https://Stackoverflow.com/users/5056', 'pm_score': 0, 'selected': False, 'text': "<p>I use Castle Windsor/Microkernel, I have no experience with anything else but I like it a lot.</p>\n\n<p>As for how do you decide what to inject?  So far the following rule of thumb has served me well: If the class is so simple that it doesn't need unit tests, you can feel free to instantiate it in class, otherwise you probably want to have a dependency through the constructor.  </p>\n\n<p>As for whether you should create an interface vs just making your methods and properties virtual I think you should go the interface route either if you either a) can see the class have some level of reusability in a different application (i.e. a logger) or b) if either because of the amount of constructor parameters or because there is a significant amount of logic in the constructor, the class is otherwise difficult to mock.</p>\n"}]
['https://Stackoverflow.com/questions/28464', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1574/']