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

  • SEARCH
  • Home
  • 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 8161851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:35:13+00:00 2026-06-06T18:35:13+00:00

Suppose I have a BaseForm which depends on an ILogger or IResourceManager or something

  • 0

Suppose I have a BaseForm which depends on an ILogger or IResourceManager or something like that. Currently it resolves the correct implementation of the required service using the service locator which I know is an anti-pattern.

  • Is using the constructor injection the right way to resolve this kind of dependency?
  • Do I have to register my BaseForm (and its’ derived types) in the container in order to create instances of them with resolved dependencies? Doesn’t that complicate everything?
  • Is it bad to use a static factory wrapped around a service locator?
  • Unit-testing aside, will I really be punished because of using service locator anti-pattern?

Sorry about asking many questions at once. I’ve read the following SO questions and many others but reading them only added to my confusion:

  • How to use Dependency Injection and not Service Locator
  • What’s the difference between the Dependency Injection and Service Locator patterns?
  • How to avoid Service Locator Anti-Pattern?
  • 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-06-06T18:35:14+00:00Added an answer on June 6, 2026 at 6:35 pm

    If possible, you should always go with dependency injection, since it has a few clear strength. With UI technologies however, it is not always possible to use dependency injection, since some UI technologies (in .NET space, Win Forms and Web Forms, for instance) only allow your UI classes (forms, pages, controls, etc) to have a default constructor. In that case you will have to fall back to something else, which is service locator.

    In that case I can give you the following advice:

    • Only fall back to Service Locator for UI classes that can’t be created by your container using dependency injection, and for stuff that you aren’t unit testing anyway.
    • Try to implement as less logic as possible in those UI classes (as Humble objects with only view related stuff). This allows you to unit test as much as possible.
    • Wrap the container around a static method to hide the container from the rest of the application. Make sure that a call to this static method fails, when the dependency cannot be resolved.
    • Resolve all dependencies in the (default) constructor of that type. This allows the application to fail fast when one of its dependencies cannot be resolved when that type is created, instead of later on when some button is clicked.
    • Check during app start-up (or using a unit test), if all those UI types can be created. This saves you from having to go through the whole application (by opening all forms) to see if there is an error in the DI configuration.
    • When types cannot be built by the container, there is no reason to register them in the container. If they can be created by the container (such as with ASP.NET MVC Controller classes), it can be useful to register them explicitly, because some containers allow you to verify the configuration up front, which will detect configuration errors in those types right away.

    Besides unit testing, there are two other important arguments against the use of the Service Locator, which are given by Mark Seemann in his famous blog post Service Locator is an Anti-Pattern:

    • Service Locator “hides a class’ dependencies, causing run-time errors instead of compile-time errors”
    • Service Locator is “making the code more difficult to maintain”
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an HTML page that looks something like this: <html><body> 00123 <input
Suppose I have a data frame, df, that looks like: f t1 t2 t3
Suppose I have a ASP.NET Web API controller that uses a service, which in
Suppose i have one table that holds Blogs. The schema looks like : ID
Suppose i have an XML file, that i use as local database, like this):
Suppose I have an array that looks like this array ([apple] => 1, [dog]=>2,
Suppose I have a vertical menu that is coded like this: <ul class=sub-menu> <li
Suppose I have a pure virtual method in the base interface that returns to
Suppose I have a static method of my class that returns an object of
Suppose we have the name written in any none-latin letters - languages, like Arabic,

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.