Infovark Underground

  • news
    • infoblog
    • underground
  • product
  • download
  • buy
  • support
  • about
    • ← Review: Domain-Driven Design
    • Reflecting on reflection in .NET →

    The Service Locator pattern is the new global variable

    18 Jun 2010 by Dean in .NET, Programming / 1 Comment

    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.

    As I was wrestling with yet another third-party software component that made heavy use of an inversion-of-control (IoC) container, I realized that service locators are the new global variable.

    Global variables are a code smell and should be used sparingly.

    This means that you should use a service locator only as a last resort, not as the default way of wiring bits of your application together.

    And if you’re using an IoC container, make sure that you are using it to inject dependencies through your object constructors, rather than calling its static Resolve() methods from within your classes.

    Related posts

    1. Reflecting on reflection in .NET
    2. Avoiding the “new”
    3. The Curse of the Singleton
    • Tweet
    • Tags:
    • .NET
    • C#
    • dependency injection
    • inversion of control
    • patterns
    • service locator

    1 Comment

    • 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 dangerous patterns [...]

      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.