Infovark Underground

  • news
    • infoblog
    • underground
  • product
  • download
  • buy
  • support
  • about
    • ← Check out proposed enhancements to .NET core classes
    • Review: Domain-Driven Design →

    Avoiding the “new”

    20 Apr 2010 by Dean in .NET, Programming / 2 Comments

    In January, Robert Martin posted an article about the overuse of dependency injection in many open source frameworks. At the time, I had only an academic interest in the subject. (And Uncle Bob’s rants are always a fun read.)

    But that was before I started taking a look at several open source frameworks for WPF. Now I’m feeling ranty too!

    Dependency Injection is an important principle of software design, but it can be abused. My biggest concern with overusing inversion-of-control (IoC) containers is that it makes learning a framework ten times harder than it needs to be.

    First, you can’t tell precisely when an object gets created. Sure, it’s nice that your framework provides control over the lifetime of objects, but this should be an optional feature for advanced scenarios. Not every solution requires lazy loading. Sometimes a simple “new” will do.

    Second, by placing all the wiring and object discovery in the container or service locator, you hide important information about how the framework components fit together. Yes, the global object factory knows what classes go with what types, but you don’t. That information is hidden among all the framework plumbing.

    For similar reasons, Microsoft cautions against using factory methods in place of constructors in its Framework Design Guidelines:

    Prefer constructors to factories, because they are generally more usable, consistent, and convenient than specialized construction mechanisms.

    I think that goes double for dependency injection and IoC containers. Eliminating constructors makes for less readable code. I can’t tell what classes other classes depend on to do their jobs.

    My errors should get thrown from my code

    Sure, IoC containers are great when everything gets wired together automagically. But when an error occurs, I want it to be thrown from my code, as close to the source of the error as possible. (Like Jeff Atwood, I assume it’s always my fault.)

    Instead, frameworks that overuse dependency injection throw most of these setup, configuration, and instantiation from its internal IoC container, usually with some horribly generic error message.

    Then it’s up to me to find which dependency failed to resolve, which constructor failed to bind, or which concrete implementation didn’t get registered. And in order to do that, I have to go spelunking in the framework code — which I don’t know well — rather than the code I was trying to write, which I know quite well.

    Frameworks that rely on dependency injection and IoC containers practically require you to have the framework source available and for you to become an expert in its mysterious inner workings. If I need to step through the framework code or use reflector on it, the framework has failed me.

    This is exactly what Uncle Bob was decrying in his article. I don’t want to become an expert in the XYZ framework for ABC. I just want to get my stuff done.

    Related posts

    1. Reflecting on reflection in .NET
    2. The Service Locator pattern is the new global variable
    3. One-Way Serialization
    4. Validation in Domain Driven Design
    5. Using Spark View Engine with C# WebServer
    • Tweet
    • Tags:
    • dependency injection
    • inversion of control
    • WPF

    2 Comments

    • The Service Locator pattern is the new global variable « Infovark Underground

      [...] Mark Seeman, author of an upcoming book about dependency injection, writes that Service Locator is an anti-pattern. He shows several code examples illustrating the difficulty of configuring and troubleshooting various implementations of the Service Locator pattern. His observations tally with my thoughts about the awkwardness of service locator implementations. [...]

      • Reflecting on reflection in .NET « Infovark Underground

        [...] two previous posts, I talked about the misuse of dependency injection and the problems with the service locator pattern. I should include factories among the list of [...]

        Leave a Comment

        Posting your comment...

        Subscribe to these comments via email

        • Categories

          • .NET (41)
          • AJAX (3)
          • Books (7)
          • HTML (9)
          • Infovark (8)
          • Programming (48)
          • REST (11)
          • SQL (3)
          • Testing (3)
          • Tools (13)
          • UI (3)
          • WCF (11)
          • Web Services (8)
          • WPF (4)
          • XML (4)
        • Archives

        • Get future articles


           

        • Blogroll

          • Ajaxian
          • Anne Van Kesteren
          • Brain.Save()
          • Coding Horror
          • Eric Sink
          • Joel Spolsky
          • John Resig
          • Mark Pilgrim
          • Raymond Chen
          • Scott Hansleman
          • Secret Geek
          • Steve Yegge
          • The Daily WTF
          • The Database Programmer
        • Meta

          • Log in
          • Entries RSS
          • Comments RSS
          • WordPress.org
      • Site map

        • News
        • Product
        • Download
        • Buy
        • Support
        • About
      • Recent Posts

        • Review: Brownfield Application Development in .NET
        • Using Modal Dialogs with a Splash Screen in WPF
        • Highlighting query terms in a WPF TextBlock
        • Getting XAML Hyperlink text to wrap
        • How to format the XAML Hyperlink NavigateUri
      • Twitter

        Copyright 2011 Infovark, Inc. All rights reserved.