We're starting to use dependency injection in a fairly large, interactive program. It's early yet, but I have a feeling that the majority of the objects being injected are going to want runtime data passed in to their constructors. Prior to this, I've only used DI in web applications where building everything at the composition root isn't a problem.

When working with runtime data, it sounds like the options are to use manual injection or abstract factories. Is there anything important to consider when deciding between the two or does it mainly come down to personal preference? I'm using Ninject with the factory extension, so going the factory route isn't too onerous. However, if we end up with factories in every class, is that a sign that we're doing something wrong? Should we prefer manual injection when the power of an IoC container isn't needed? Could it simply mean that we're over-using dependency injection?

  • I thought of one drawback to using factories just after posting this message. Any factory you have in production code is going to need one in your unit tests as well, right? Using the factory extension keeps that pretty simple, but it also means using your IoC container in unit tests, which I believe some people are averse to. Personally, I don't have a problem with it, but I wouldn't want to remove the option for other people. – donkey Mar 11 '14 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.