mostlylucid

STATIC ARCHIVE of mostlylucid.co.uk of old
posts - 892, comments - 676, trackbacks - 11

My Links

News

follow me on twitter!

Archives

Post Categories

Misc. Coding

Grrr...poor use of singletons and a very cool Generic Singleton pattern!

I posted earlier that I'm switching to blogengine.net, as part of this I've been fiddling around with the code (as is my way..I'll contribute back to the source when I've finished). One of my major pet hates is poor use of the singleton pattern, especially as there's a definitive article on the pattern in .NET and how to do it well. It's actually likely that this pattern is overkill in this case and a ReaderWriterLockSlim could be better (though it has it's own problems) . Anyway, on the assumption that a Singleton is the best choice here, let's look at the current code:

 

public static BlogSettings Instance

        {

            get

            { 

                if (blogSettingsSingleton == null)

                {

                    blogSettingsSingleton = new BlogSettings();

                }

                return blogSettingsSingleton;

            }

        }

 

If you look at the article I mentioned above you'll see that this is the version which is specifically called out as follows:

"the above is not thread-safe. Two different threads could both have evaluated the test if (instance==null) and found it to be true, then both create instances, which violates the singleton pattern. Note that in fact the instance may already have been created before the expression is evaluated, but the memory model doesn't guarantee that the new value of instance will be seen by other threads unless suitable memory barriers have been passed.'

The common 'best' singleton pattern (well, it's debatable...but generally the best...) is a lot more wordy (see version 5 in that article) so I was please to find this post on a Generic Singleton (actually this was posted a while ago on Codeproject)...really nice. In the Utils class I added this:

 

    /// <summary>

        /// Provides a Singleton implementation using Generics.

        /// </summary>

        /// <typeparam name="T">Type of singleton instance</typeparam>

        public sealed class Singleton<T> where T : new()

        {

            Singleton() { }

 

            public static T Instance

            {

                get

                {

                    return Nested.instance;

                }

            }

 

            class Nested

            {

                // Explicit static constructor to tell C# compiler

                // not to mark type as beforefieldinit

                static Nested() { }

 

                internal static readonly T instance = new T();

            }

        }

 

The code for returning the instance then becomes:

        public static BlogSettings Instance

        {

            get

            {

                return Utils.Singleton<BlogSettings>.Instance;

            }

        }

You have to also change the constructor for BlogSettings to public to allow this this to work which does let devs shoot themselves in the foot (by ignoring the singleton)  and you of course have to balance the benefit agains that risk...

Print | posted on Wednesday, April 30, 2008 10:42 PM | Filed Under [ .NET Code Snippets ASP.NET Tips ]

Feedback

# Interesting Finds: 2008.05.02

.NET Grrr...poor use of singletons and a very cool Generic Singleton pattern! Writing a Simple Role Playing
5/1/2008 8:09 PM | gOODiDEA.NET

# Interesting Finds: 2008.05.02

.NETGrrr...pooruseofsingletonsandaverycoolGenericSingletonpattern!WritingaSimpleRol...
5/1/2008 8:09 PM | gOODiDEA
Gravatar

# re: Grrr...poor use of singletons and a very cool Generic Singleton pattern!

check out Microsoft unity application blocks for easy implementation of singleton objects.
5/2/2008 1:12 AM | Marshal

# re: Grrr...poor use of singletons and a very cool Generic Singleton pattern!

@Marshal...I'm sure it does but it's overkill just to replace about 20 lines of code...
5/6/2008 9:59 PM | Scott Galloway
Gravatar

# re: Grrr...poor use of singletons and a very cool Generic Singleton pattern!

That looks like a factory to me, not a singleton. In particular, the new() constraint means that BlogSettings has to have a public parameterless constructor, which means that this code would work:

BlogSettings bs1 = new BlogSettings();
BlogSettings bs2 = new BlogSettings();

As soon as you've got two instances of something, you can tell it's not a singleton. I'm not saying the factory isn't useful, just that calling it a singleton is wrong/misleading.

(I personally would almost always use the 4th pattern on my page, btw - and often without the static constructor. I rarely mind that much if the instance is created a little bit early, and it makes the pattern very simple indeed.)

Jon
9/11/2008 9:23 AM | Jon Skeet

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 2 and type the answer here:

Powered by: