Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton vs Cache ASP.NET

I have created a Registry class in .NET which is a singleton. Apparently this singleton behaves as if it were kept in the Cache (the singleton object is available to each session). Is this a good practice of should I add this Singleton to the Cache? + do I need to wacth out for concurrency problems with the GetInstance() function?

namespace Edu3.Business.Registry
{
    public class ExamDTORegistry
    {
        private static ExamDTORegistry instance;
        private Dictionary<int, ExamDTO> examDTODictionary;

        private ExamDTORegistry()
        {
            examDTODictionary = new Dictionary<int, ExamDTO>();
        }

        public static ExamDTORegistry GetInstance()
        {
            if (instance == null)
            {
                instance = new ExamDTORegistry();
            }
            return instance;
        }
    }
}
like image 228
Lieven Cardoen Avatar asked Nov 19 '08 14:11

Lieven Cardoen


People also ask

What is singleton in asp net?

Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code. Singleton has almost the same pros and cons as global variables.

Does singleton save memory?

The memory space wastage does not occur with the use of the singleton class because it restricts the instance creation. As the object creation will take place only once instead of creating it each time a new request is made.

Is Imemorycache a singleton?

The code is below. Note that the MemoryCache is a singleton, but within the process. It is not (yet) a DistributedCache. Also note that Caching is Complex(tm) and that thousands of pages have been written about caching by smart people.

Why you should prefer Singleton pattern over a static class?

It is not possible to inherit from a static class, while it is possible with singleton pattern if you want to allow it. So, anyone can inherit from a singleton class, override a method and replace the service. It is not possible to write an extension method to a static class while it is possible for a singleton object.


3 Answers

Well, your GetInstance method certainly isn't thread-safe - if two threads call it at the same time, they may well end up with two different instances. I have a page on implementing the singleton pattern, if that helps.

Does your code rely on it being a singleton? Bear in mind that if the AppDomain is reloaded, you'll get a new instance anyway.

I don't really see there being much benefit in putting the object in the cache though. Is there anything you're thinking of in particular?

like image 62
Jon Skeet Avatar answered Oct 19 '22 03:10

Jon Skeet


Despite their presence in GoF singletons are generally considered bad practice. Is there any reason why you wish to have only one instance?

like image 33
user35149 Avatar answered Oct 19 '22 01:10

user35149


HttpContext.Cache is available to all sessions, but items in the cache can be removed from memory when they expire or if there is memory pressure.

HttpContext.Application is also available to all sessions and is a nice place to store persistent, application-wide objects.

Since you've already created a singleton and it works, I don't see why should use one of the ones built-in singleton collections instead, unless you need the extra functionality that Cache gives you.

like image 41
Greg Avatar answered Oct 19 '22 01:10

Greg