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 7182967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:53:00+00:00 2026-05-28T17:53:00+00:00

I am working on a large legacy C# application and a task assigned to

  • 0

I am working on a large legacy C# application and a task assigned to me is to remove all usages of a static factory class ServiceLocator.GetObject<T>() and replace with constructor injected dependencies throughout.

For the most part, this is simple, however there are around 50 cases in the application codebase where this is a bit tricky. For instance, Servicelocator is used in a static class, or extension method, or even a WPF MarkupExtension!.

For instance, if you were faced with a code snippet like this, what would you do? (apart from cry)

public static class MyExtensions
{
    private static ISingletonServiceOne _ServiceOne = null;
    private static ISingletonServiceTwo _ServiceTwo = null; // etc ... 

    public static SummaryHeader GetBannerSummary(this IModel rfq, object requester)
    {
        Guard.ArgumentNotNull(rfq, "rfq");
        Guard.ArgumentNotNull(requester, "requester");

        if (_ServiceOne == null)
        {
            _ServiceOne = ServiceLocator.GetService<ISingletonServiceOne>(requester);
            Guard.ArgumentNotNull(_ServiceOne, "_ServiceOne");
        }

        return _ServiceOne.GetBannerSummary(rfq);
    }

In the above the ServiceLocator.GetObject() method has been used in an Extension Method on IModel to locate a singleton registered service and execute a method on that using IModel.

The question is:

  • Are there any patterns/practices to avoid this sort of thing – a DI container needed in a static class, value-converter or extension method
  • Are there any patterns/practices to deal with cyclic dependencies in DI?
  • What would you do in the above given there is a trade off between good code and time to delivery?

I am thinking to move the GetBannerSummary() method out of extensions and only IModel in this case, however (don’t laugh) there are also cases of the same ServiceLocator being used in ValueConverters (WPF) and MarkupExtensions :0

Your comments/suggestions appreciated

  • 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-28T17:53:01+00:00Added an answer on May 28, 2026 at 5:53 pm

    The only time I would ever use ServiceLocator is in static methods, e.g. extension methods, and IValueConverters as, unfortunately, there really isn’t any other good way of getting dependencies.

    The only (slightly) better solution is to move the ServiceLocator call out to a lazy loaded property, so that the dependency can be injected during unit testing.

    In your case, however, that won’t fly as you have a requester property being passed to the GetService, in which case you ideally need to add an IServiceOneFactory dependency to which you can pass your requester object. So:

    public interface IServiceOneFactory
    {
        ISingletonServiceOne Create(object requester);
    }
    
    public static class MyExtensions
    {
        public static IServiceOneFactory ServiceOneFactory
        {
            get 
            {
                if( _ServiceOneFactory==null)
                    _ServiceOneFactory = ServiceLocator.GetService<IServiceOneFactory>();
                return _ServiceOneFactory;
            }
            set { _ServiceOneFactory = value; }
        }
    
        private static IServiceOneFactory _ServiceOneFactory = null;
        private static ISingletonServiceOne _ServiceOne = null;
        private static ISingletonServiceTwo _ServiceTwo = null; // etc ... 
    
        public static SummaryHeader GetBannerSummary(this IModel rfq, object requester)
        {
            Guard.ArgumentNotNull(rfq, "rfq");
            Guard.ArgumentNotNull(requester, "requester");
    
            if (_ServiceOne == null)
            {
                _ServiceOne = ServiceOneFactory.Create(requester);
                Guard.ArgumentNotNull(_ServiceOne, "_ServiceOne");
            }
    
            return _ServiceOne.GetBannerSummary(rfq);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a large legacy application using stateless session beans that has recently
We are working on a large legacy application and we're redesigning the business layer
I'm working on a very large, data-intensive legacy application. Both the code base &
I am currently working on a very large legacy MFC MDI application. It has
I'm in a 10 person team working on a large legacy code base with
We are working with large amounts of data, all tagged in UTC (in Java).
I am working with a large Java web application from a commercial vendor. I've
I am working in a very large legacy C++ code base which shall remain
I'm recently working on this platform for which a legacy codebase issues a large
I'm working on legacy code that looks very similar to this: public class BaseParser

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.