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??