Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using Dependency Injection in my code (with Ninject) and thought I was doing quite well until I came across a performance problem that was caused by a misunderstanding of where DI containers fit into your code. There seems to be a lot of information on how to use DI frameworks but not too much on where not to use them or how best to use them (at least that I could find)

I thought I would write out what I thought were some best practices and see if other people agree with me and what other best practises people can came up with.

  • Use one kernel per application or AppDomain
  • Use the DI container for long-lived Singleton objects only, use factories (or other methods) for short-lived transient objects)
  • Prefer Constructor Injection over Property or Field injection
  • Request objects, don't build them
  • others?? pointers to good blog entires/articles??
share|improve this question

2 Answers 2

up vote 6 down vote accepted

Here's a short list of the most important points (some of which also appear in the OP):

  • Code should be unaware of which DI Container (if any) is used
  • Compose the entire application in the root of the application (the Composition Root)
  • Favor Constructor Injection

I can't say I agree with your point about Singleton vs. Transient objects. The whole point of DI is that an external mechanism (such as a DI Container) determines the life-time of any given dependency, not someone else, so you need to have all dependencies managed by the DI Container.

share|improve this answer

Use the DI container for long-lived Singleton objects only, use factories (or other methods) for short-lived transient objects)

But do use DI to inject the factories into where there needed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["        <li><a href=\"http://area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"http://careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2014 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2014.8.21.1791\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </nos"], "copyright__copyright__copyright": ["need to have all dependencies managed by the DI Container.\n\nshare|improve this answer\n\nUse the DI container for long-lived Singleton objects only, use factories (or other methods) for short-lived transient objects)\n\nBut do use DI to inject the factories into where there needed.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}