2

Is there are way to force a singleton instance to reset whenever a user update the database?

public class Example{ private Dictionary<int, string> _list; private Example(){ _list = new Dictionary<int, string>(); //Get data from database here and add the data to _list } private class Nested{ static Nested(){} internal static readonly Example Instance = new Example(); } public static Dictionary<int, string> GetExample(){ return Nested.Instance._list } } 

So, I do I reset _list when database is updated?

Thanks!

2
  • 1
    How do you know the database has been updated? And what is the usefulness of the Nested class? Commented Mar 4, 2011 at 19:11
  • better to use Cache and set SQL dependency expiration, either way depending on your consumers of the data you have to protect for race conditions when updating Commented Mar 4, 2011 at 19:14

2 Answers 2

7

The short answer is: No.

The closest thing you can get is to reset the dictionary:

public void Reset(){ _list.Clear(); } 

Now the long answer...

Why are you using a singleton? I'm assuming it's because you read the GoF design pattern book and thought it looked like a fun idea. The singleton pattern breaks every tenet of object oriented design. You can't inherit from them, you break encapsulation and there's no chance of polymorphism.

In the example you've posted you're handing the inner workings of your class (the dictionary) to anyone who wants it. What if they change it? How will this work with threads?

If you only want one instance of your class in your application then only call new once, or use a dependency injection container with a singleton life style, but that's another topic altogether.

Urgh, I hate singletons.

Sign up to request clarification or add additional context in comments.

6 Comments

So what pattern / mechanism do you suggest for caching? This is how it's done in ASP.net and many other frameworks.
ASP.net has built in caching which will store page fragments for you. If you want to do record level caching or active expiry and want to get tricky there are plenty of options like memcached or redis. If you need to stick to the microsoft stack then appfabric (velocity) can help with that.
@RQDQ - A cache class? Do you have something that shows ASP.net objects are instantiated as singletons, or do you just get a single instance of them. There's a difference between having a single object who's lifetime is managed by a factory or supervisor, and an object that controls its own lifetime. Singletons run counter to the principle of Inversion-of-Control.
BTW, I have serious latent singleton rage issues. I am taking meds for it.
@LordofScripts var book = new Book(); var form = new Form(book); var otherForm = new OtherForm(book);. I don't see the problem here.
|
0

One way to do this is to use lazy loading and provide an "Invalidate" or "Reset" method.

public class Example { private static Dictionary<int, string> _list; public static void Invalidate() { _list = null; } public static Dictionary<int, string> GetExample() { if (_list == null) _list = new Dictionary<int, string>(); return _list; } } 

One thing to watch out for is this isn't set up for multithreaded at all. The Dictionary class isn't recommended for re entrant code and there needs to be some sort of locking in place in the GetExample and Invalidate methods.

1 Comment

Won't this always return null?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.