<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>ASP.NET Tips</title>
        <link>http://mostlylucid.homeip.net/category/29.aspx</link>
        <description>ASP.NET Tips</description>
        <language>en-US</language>
        <copyright>Scott Galloway</copyright>
        <managingEditor>blogcomments@mostlylucid.co.uk</managingEditor>
        <generator>Subtext Version 2.0.0.0</generator>
        <item>
            <title>Grrr...poor use of singletons and a very cool Generic Singleton pattern!</title>
            <link>http://mostlylucid.homeip.net/archive/2008/04/30/grrr.poor-use-of-singletons-and-a-very-cool-generic-singleton.aspx</link>
            <description>&lt;p&gt;I &lt;a href="http://www.mostlylucid.net/archive/2008/04/30/changes-afoot.change-to-blogengine.net.aspx"&gt;posted earlier&lt;/a&gt; that I'm switching to &lt;a href="http://www.dotnetblogengine.net/"&gt;blogengine.net&lt;/a&gt;, 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 &lt;a href="http://en.wikipedia.org/wiki/Singleton_pattern"&gt;singleton pattern&lt;/a&gt;, especially as there's a definitive &lt;a href="http://www.yoda.arachsys.com/csharp/singleton.html"&gt;article on the pattern in .NET&lt;/a&gt; and how to do it well. It's actually likely that this pattern is overkill in this case and a &lt;a href="http://www.bluebytesoftware.com/blog/PermaLink,guid,c4ea3d6d-190a-48f8-a677-44a438d8386b.aspx"&gt;ReaderWriterLockSlim&lt;/a&gt; could be better (though it has it's &lt;a href="http://weblogs.asp.net/leftslipper/archive/2008/03/31/mvc-locking-the-routecollection.aspx"&gt;own problems&lt;/a&gt;) . Anyway, on the assumption that a Singleton is the best choice here, let's look at the current code:&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;div style="font-size: 8pt; background: white; color: black; font-family: verdana"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BlogSettings&lt;/span&gt; Instance&lt;/p&gt;    &lt;p style="margin: 0px"&gt;        {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            {  &lt;/p&gt;    &lt;p style="margin: 0px"&gt;                &lt;span style="color: blue"&gt;if&lt;/span&gt; (blogSettingsSingleton == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                    blogSettingsSingleton = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BlogSettings&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; blogSettingsSingleton;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;        }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;If you look at the article I &lt;a href="http://www.yoda.arachsys.com/csharp/singleton.html"&gt;mentioned above&lt;/a&gt; you'll see that this is the version which is specifically called out as follows:&lt;/p&gt;  &lt;p&gt;"&lt;em&gt;the above is not thread-safe. Two different threads could both have evaluated the test &lt;code&gt;if (instance==null)&lt;/code&gt; 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.&lt;/em&gt;'&lt;/p&gt;  &lt;p&gt;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 &lt;a href="http://blog.falafel.com/2008/01/26/AGenericSingleton.aspx"&gt;this post&lt;/a&gt; on a Generic Singleton (actually this &lt;a href="http://www.codeproject.com/KB/cs/genericsingleton.aspx"&gt;was posted a while ago on Codeproject&lt;/a&gt;)...really nice. In the Utils class I added this:&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;div style="font-size: 8pt; background: white; color: black; font-family: verdana"&gt;   &lt;p style="margin: 0px"&gt;    &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; Provides a Singleton implementation using Generics.&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Type of singleton instance&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;sealed&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Singleton&lt;/span&gt;&amp;lt;T&amp;gt; &lt;span style="color: blue"&gt;where&lt;/span&gt; T : &lt;span style="color: blue"&gt;new&lt;/span&gt;()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;        {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            Singleton() { }&lt;/p&gt;    &lt;p style="margin: 0px"&gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;            &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; T Instance&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                    &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Nested&lt;/span&gt;.instance;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            }&lt;/p&gt;    &lt;p style="margin: 0px"&gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;            &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Nested&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                &lt;span style="color: green"&gt;// Explicit static constructor to tell C# compiler&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                &lt;span style="color: green"&gt;// not to mark type as beforefieldinit&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                &lt;span style="color: blue"&gt;static&lt;/span&gt; Nested() { }&lt;/p&gt;    &lt;p style="margin: 0px"&gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;                &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; T instance = &lt;span style="color: blue"&gt;new&lt;/span&gt; T();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;        }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;The code for returning the instance then becomes:&lt;/p&gt;  &lt;div style="font-size: 8pt; background: white; color: black; font-family: verdana"&gt;   &lt;p style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BlogSettings&lt;/span&gt; Instance&lt;/p&gt;    &lt;p style="margin: 0px"&gt;        {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Utils&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;Singleton&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;BlogSettings&lt;/span&gt;&amp;gt;.Instance;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;            }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;        }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;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...&lt;/p&gt;&lt;img src="http://mostlylucid.homeip.net/aggbug/1276.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Galloway</dc:creator>
            <guid>http://mostlylucid.homeip.net/archive/2008/04/30/grrr.poor-use-of-singletons-and-a-very-cool-generic-singleton.aspx</guid>
            <pubDate>Thu, 01 May 2008 05:42:20 GMT</pubDate>
            <wfw:comment>http://mostlylucid.homeip.net/comments/1276.aspx</wfw:comment>
            <comments>http://mostlylucid.homeip.net/archive/2008/04/30/grrr.poor-use-of-singletons-and-a-very-cool-generic-singleton.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://mostlylucid.homeip.net/comments/commentRss/1276.aspx</wfw:commentRss>
            <trackback:ping>http://mostlylucid.homeip.net/services/trackbacks/1276.aspx</trackback:ping>
        </item>
    </channel>
</rss>