Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3662122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:22:52+00:00 2026-05-19T01:22:52+00:00

Something that has been bugging me since I read an answer on another stackoverflow

  • 0

Something that has been bugging me since I read an answer on another stackoverflow question (the precise one eludes me now) where a user stated something like “If you’re calling the Service Locator, you’re doing it wrong.“

It was someone with a high reputation (in the hundred thousands, I think) so I tend to think this person might know what they’re talking about. I’ve been using DI for my projects since I first started learning about it and how well it relates to Unit Testing and what not. It’s something I’m fairly comfortable with now and I think I know what I’m doing.

However, there are a lot of places where I’ve been using the Service Locator to resolve dependencies in my project. Once prime example comes from my ModelBinder implementations.

Example of a typical model binder.

public class FileModelBinder : IModelBinder {
    public object BindModel(ControllerContext controllerContext,
                            ModelBindingContext bindingContext) {
        ValueProviderResult value = bindingContext.ValueProvider.GetValue("id");

        IDataContext db = Services.Current.GetService<IDataContext>();
        return db.Files.SingleOrDefault(i => i.Id == id.AttemptedValue);
    }
}

not a real implementation – just a quick example

Since the ModelBinder implementation requires a new instance when a Binder is first requested, it’s impossible to use Dependency Injection on the constructor for this particular implementation.

It’s this way in a lot of my classes. Another example is that of a Cache Expiration process that runs a method whenever a cache object expires in my website. I run a bunch of database calls and what not. There too I’m using a Service Locator to get the required dependency.

Another issue I had recently (that I posted a question on here about) was that all my controllers required an instance of IDataContext which I used DI for – but one action method required a different instance of IDataContext. Luckily Ninject came to the rescue with a named dependency. However, this felt like a kludge and not a real solution.

I thought I, at least, understood the concept of Separation of Concerns reasonably well but there seems to be something fundamentally wrong with how I understand Dependency Injection and the Service Locator Pattern – and I don’t know what that is.

The way I currently understand it – and this could be wrong as well – is that, at least in MVC, the ControllerFactory looks for a Constructor for a Controller and calls the Service Locator itself to get the required dependencies and then passes them in. However, I can understand that not all classes and what not have a Factory to create them. So it seems to me that some Service Locator pattern is acceptable…but…

  1. When is it not acceptable?
  2. What sort of pattern should I be on the look out for when I should rethink how I’m using the Service Locator Pattern?
  3. Is my ModelBinder implementation wrong? If so, what do I need to learn to fix it?
  4. In another question along the lines of this one user Mark Seemann recommended an Abstract Factory – How does this relate?

I guess that’s it – I can’t really think of any other question to help my understanding but any extra information is greatly appreciated.

I understand that DI might not be the answer to everything and I might be going overboard in how I implement it, however, it seems to work the way I expect it to with Unit Testing and what not.

I’m not looking for code to fix my example implementation – I’m looking to learn, looking for an explanation to fix my flawed understanding.

I wish stackoverflow.com had the ability to save draft questions. I also hope whoever answers this question gets the appropriate amount of reputation for answering this question as I think I’m asking for a lot. Thanks, in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:22:52+00:00Added an answer on May 19, 2026 at 1:22 am

    Consider the following:

    public class MyClass
    {
      IMyInterface _myInterface;
      IMyOtherInterface _myOtherInterface;
    
      public MyClass(IMyInterface myInterface, IMyOtherInterface myOtherInterface)
      {
        // Foo
    
        _myInterface = myInterface;
        _myOtherInterface = myOtherInterface;
      }
    }
    

    With this design I am able to express the dependency requirements for my type. The type itself isn’t responsible for knowing how to instantiate any of the dependencies, they are given to it (injected) by whatever resolving mechanism is used [typically an IoC container]. Whereas:

    public class MyClass
    {
      IMyInterface _myInterface;
      IMyOtherInterface _myOtherInterface;
    
      public MyClass()
      {
        // Bar
    
        _myInterface = ServiceLocator.Resolve<IMyInterface>();
        _myOtherInterface = ServiceLocator.Resolve<IMyOtherInterface>();
      }
    }
    

    Our class is now dependent on creating the specfic instances, but via delegation to a service locator. In this sense, Service Location can be considered an anti-pattern because you’re not exposing dependencies, but you are allowing problems which can be caught through compilation to bubble up into runtime. (A good read is here). You hiding complexities.

    The choice between one or the other really depends on what your building on top of and the services it provides. Typically if you are building an application from scratch, I would choose DI all the time. It improves maintainability, promotes modularity and makes testing types a whole lot easier. But, taking ASP.NET MVC3 as an example, you could easily implement SL as its baked into the design.

    You can always go for a composite design where you could use IoC/DI with SL, much like using the Common Services Locator. You component parts could be wired up through DI, but exposed through SL. You could even throw composition into the mix and use something like the Managed Extensibility Framework (which itself supports DI, but can also be wired to other IoC containers or service locators). It’s a big design choice to make, generally my recommendation would be for IoC/DI where possible.

    Your specific design I wouldn’t say is wrong. In this instance, your code is not responsible for creating an instance of the model binder itself, that’s up to the framework so you have no control over that but your use of the service locator could probably be easily changed to access an IoC container. But the action of calling resolve on the IoC container…would you not consider that service location?

    With an abstract factory pattern the factory is specialised at creating specific types. You don’t register types for resolution, you essentially register an abstract factory and that builds any types that you may require. With a Service Locator it is designed to locate services and return those instances. Similar from an convention point of view, but very different in behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something that has been bugging me since I took up AS3 a
I saw this same question for VIM and it has been something that I
This is something that has been bugging me for a while as it is
Not a massive problem but something that has been bugging the life out of
This is one thing that has been bugging me for a while about DDD.
This is a question that has been bugging me for a while and I
I'm guessing the StackOverflow code has something along the lines of a UsersController that
This is probably a variation on something that has been solved dozens of times
Something has been bugging me, like people realize something I don't. I'm looking at
Ok, this has been bugging me for a while now. And I wonder how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.