Announcement Announcement Module
No announcement yet.
When should I not use IoC/DI? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • When should I not use IoC/DI?

    I was getting all fired up to use dependency injection with some presentation layer classes, for example, by creating a setLog(Log log) method to inject the class Log (or instead maybe the Log Factory) for each class to use...

    Then I looked at the "Spring How-To" for building an MVC application step-by-step and noted with dismay that the example code is calling:

    Hmmm... No DI there. So my questions are:
    • Is this an overly simple example or are system utilities simply inappropriate for dependency injection?
    • Are some dependencies more appropriate for DI than others?

    I await your philosophical discourse! --Dan

  • #2

    If you want a "philosophical" discourse to this, then logging is a cross-cutting concern, and you should handle it via AOP! Spring offers dynamic proxies for you to be able to achieve this. If you still want finer grained logging then look at AspectJ.

    If you want a more practical approach, then well, you can go with either of the methods you've described above. Let's face it, as long as you have abstracted your logging out by DI or behind a LogFactory, you can change the implementation (if you ever need to - I've never changed logging implementations really).