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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:18:04+00:00 2026-05-21T12:18:04+00:00

I’m working on a legacy project that has heavy use of Singletons. While most

  • 0

I’m working on a legacy project that has heavy use of Singletons. While most of them would be better implemented otherwise, for now the goal is to get them under test. As such, I have the following structure.

public class SomeSingleton
{
   private Dependency someDependency;
   public static readonly SomeSingleton Instance = new SomeSingleton();

   static SomeSingleton() {}

   private SomeSingleton()
   {
      someDependency = new Dependency();
   }
}

So, to make it testable without losing the ‘Singelton’ness’, I’ve tried to add a protected constructor that takes the dependency as a parameter to call from an inheriting class that I can run tests through. I realize that making it open for inheritance is breaking the Singleton pattern as well, but it is only used by the test framework and not done, by convention, in the production code.

Like so:

public class SomeSingleton
{
   private Dependency someDependency;
   public static readonly SomeSingleton Instance = new SomeSingleton();

   static SomeSingleton() {}

   private SomeSingleton() : this(new Dependency()) {}

   protected SomeSingleton(Dependency someDependency)
   {
      this.someDependency = someDependency;
   }
}

public class SomeSingletonTestImplementation : SomeSingleton
{
   public SomeSingletonTestImplementation (Dependency someDependency) 
      : base (someDependency) {}
}

Finally, after the setup, the question:

With this implementation, I expected just the protected constructor in the original Singleton to be called from the inheritor. However, when I debug, the constructor in the inheritor is called, but then, instead of stepping into the protected constructor, the private constructor is called, and then the protected constructor.

Is there any way that the private constructor won’t be called? Also, if anyone has a brief explanation of why it happens the way it does, that’d be great.

Thanks!

  • 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-21T12:18:05+00:00Added an answer on May 21, 2026 at 12:18 pm

    Thanks to the answers that pointed me in the right direction. The solution that I came to ended up being the following, which is probably truer to a typical Singleton implementation anyway.

    Instead of having a public variable, I turned it into a static property that lazily evaluates the Singleton. That way, the private constructor is only called when the actual Instance property is called, which never happens from the test code, but always from the production code. Then I can happily insert mock dependencies via the protected constructor and test all the public methods via the inheriting class.

    public class SomeSingleton
    {
       private Dependency someDependency;
    
       private static SomeSingleton instance;
       public static SomeSingleton Instance
       {
          return instance ?? (instance = new SomeSingleton());
       }
    
       static SomeSingleton() {}
    
       private SomeSingleton() : this(new Dependency()) {}
    
       protected SomeSingleton(Dependency someDependency)
       {
          this.someDependency = someDependency;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.