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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:19:46+00:00 2026-06-05T11:19:46+00:00

I have a Singleton class that uses the thread-safe Singleton pattern from Jon Skeet

  • 0

I have a Singleton class that uses the thread-safe Singleton pattern from Jon Skeet as seen in the TekPub video. The class represents a cached list of reference data for dropdowns in an MVC 3 UI.

To get the list data the class calls a static method on a static class in my DAL.

Now I’m moving into testing an I want to implement an interface on my DAL class but obviously cannot because it is static and has only one static method so there’s no interface to create. So I want to remove the static implementation so I can do the interface.

By doing so I can’t call the method statically from the reference class and because the reference class is a singleton with a private ctor I can’t inject the interface. How do I get around this? How do I get my interface into the reference class so that I can have DI and I can successfully test it with a mock?

Here is my DAL class in current form

public static class ListItemRepository {

    public static List<ReferenceDTO> All() {
        List<ReferenceDTO> fullList;
        ... /// populate list
        return fullList;
    }
}

This is what I want it to look like

public interface IListItemRepository {
    List<ReferenceDTO> All();
}

public class ListItemRepository : IListItemRepository {

    public List<ReferenceDTO> All() {
        List<ReferenceDTO> fullList;
        ... /// populate list
        return fullList;
    }
}

And here is my singleton reference class, the call to the static method is in the CheckRefresh call

public sealed class ListItemReference {

    private static readonly Lazy<ListItemReference> instance = 
        new Lazy<ListItemReference>(() => new ListItemReference(), true);

    private const int RefreshInterval = 60;
    private List<ReferenceDTO> cache;
    private DateTime nextRefreshDate = DateTime.MinValue;

    public static ListItemReference Instance {
        get { return instance.Value; }
    }

    public List<SelectListDTO> SelectList {
        get {
            var lst = GetSelectList();
            lst = ReferenceHelper.AddDefaultItemToList(lst);
            return lst;
        }
    }

    private ListItemReference() { }

    public ReferenceDTO GetByID(int id) {
        CheckRefresh();
        return cache.Find(item => item.ID == id);
    }

    public void InvalidateCache() {
        nextRefreshDate = DateTime.MinValue;
    }

    private List<SelectListDTO> GetSelectList() {
        CheckRefresh();
        var lst = new List<SelectListDTO>(cache.Count + 1);
        cache.ForEach(item => lst.Add(new SelectListDTO { ID = item.ID, Name = item.Name }));
        return lst;
    }

    private void CheckRefresh() {
        if (DateTime.Now <= nextRefreshDate) return;
        cache = ListItemRepository.All(); // Here is the call to the static class method
        nextRefreshDate = DateTime.Now.AddSeconds(RefreshInterval);
    }
    }
}
  • 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-05T11:19:48+00:00Added an answer on June 5, 2026 at 11:19 am

    You can use the singleton based on instance(not based on static), for which you can declare interface like this.

    public interface IListItemRepository
    {
        List<ReferenceDTO> All();
    }
    
    
    public class ListItemRepository : IListItemRepository
    {
        static IListItemRepository _current = new ListItemRepository();
    
        public static IListItemRepository Current
        {
            get { return _current; }
        }
    
        public static void SetCurrent(IListItemRepository listItemRepository)
        {
            _current = listItemRepository;
        }
    
        public List<ReferenceDTO> All()
        {
            .....
        }
    }
    

    Now, you can mock IListItemRepository to test.

        public void Test()
        {
            //arrange
            //If Moq framework is used,
            var expected = new List<ReferneceDTO>{new ReferneceDTO()};
    
            var mock = new Mock<IListItemRepository>();           
            mock.Setup(x=>x.All()).Returns(expected);
    
            ListItemRepository.SetCurrent(mock.Object);
    
            //act
            var result = ListItemRepository.Current.All();
    
            //Assert
            Assert.IsSame(expected, result);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object of some class that obeys the singleton pattern. I need
I'm implimenting my own ApplicationContext class that uses the singleton pattern. I want to
I have a Windows Mobile 6.5 (.net cf 3.5) that uses a singleton class
I have a class that is a singleton. This singleton is instantiated using GCD
I have a Singleton that is accessed in my class via a static property
I have a singleton class called Manager that holds a list of object instances:
I'm creating a game that uses cards. I have an AppController class with one
I have a singleton class that is shared by some threads. within a method
I have a simple Android application that uses an instance of a class, let's
I have a singleton class that stores three NSMutableArrays . Each array stores about

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.