<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://www.mostlylucid.net/category/29.aspx</link>
        <description>ASP.NET Tips</description>
        <language>en-US</language>
        <copyright>Scott Galloway</copyright>
        <generator>Subtext Version 2.1.0.5</generator>
        <item>
            <title>ViewState does not suck...part 1</title>
            <link>http://mostlylucid.net/archive/2008/09/09/viewstate-does-not-suck.part-1.aspx</link>
            <description>&lt;p&gt;I was trawling through new posts on the &lt;a href="http://weblogs.asp.net"&gt;ASP.NET blog site&lt;/a&gt; and came across &lt;a href="http://blogs.msdn.com/tess/archive/2008/09/09/asp-net-memory-identifying-pages-with-high-viewstate.aspx"&gt;this one&lt;/a&gt; from &lt;a href="http://blogs.msdn.com/tess"&gt;Tess&lt;/a&gt; (who is someone I should probably know but don’t…). What I found interesting (as well as the post which is excellent) was the first comment:&lt;/p&gt;  &lt;p&gt;  “&lt;em&gt;When will MS simply drop the Viewstate completely?    This is a good example of a solution to a problem that shouldn't be a problem to begin with.&lt;/em&gt; “&lt;/p&gt;  &lt;p&gt;This got me to thinking, ViewState has gotten a &lt;a href="http://www.google.com/search?rlz=1C1GGLS_enUS291&amp;amp;sourceid=chrome&amp;amp;ie=UTF-8&amp;amp;q=viewstate+sucks"&gt;really bad rap&lt;/a&gt; &lt;a href="http://staff.interesource.com/james/aug06/viewstate_postbacks_harmful.htm"&gt;over the years&lt;/a&gt; (with &lt;a href="http://www.hanselman.com/blog/PermaLink.aspx?guid=f96141db-a1db-42b0-b87b-823e91d18352"&gt;notable exceptions&lt;/a&gt;). Let’s look at what ViewState actually is and why it might not be the villan it’s often painted out to be…&lt;/p&gt;  &lt;p&gt;What is ViewState…at it’s simplest it’s a hidden field which contains data about the controls currently on the page. Really, that’s it…now it does this pretty cleverly, serializing a whole bunch of stuff into a Base64 encoded string (which can also be encrypted, validated etc…). &lt;/p&gt;  &lt;p&gt;For those of us who wrote web apps back in the dark ages i.e., before ASP.NET, we remember the days where we had a bunch of hidden fields used to track changes in a page, and it was frankly a pain in the ass…it was far more transparent but a lot of grunt work just to track if a field’s value had changed / store some additional data in the page for use in postback. ViewState solves that problem pretty nicely actually.&lt;/p&gt;  &lt;p&gt;So, what’s the problem people have? Two things:&lt;/p&gt;  &lt;p&gt;1. people misuse it; it’s partly our fault and partly theirs.&lt;/p&gt;  &lt;p&gt;From &lt;a href="http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx"&gt;‘Truly Understanding Viewstate’&lt;/a&gt;:    &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;&lt;em&gt;CASES OF MISUSE&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;a. Forcing a Default      &lt;br /&gt;b. Persisting static data       &lt;br /&gt;c. Persisting cheap data       &lt;br /&gt;d. Initializing child controls programmatically       &lt;br /&gt;e. Initializing dynamically created controls programmatically&lt;/em&gt;&lt;/p&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p /&gt;  &lt;p&gt;Now, as I said it’s not entirely their fault…ViewState is enabled by default…if you bind a bunch of data to a DataBound Control then you’ll get a huge amount of ViewState serialized to and from the browser each time, it’s not that easy to figure out what having no ViewState will break / just enable it for certain controls and not others (which is what &lt;a href="http://haacked.com/archive/2007/03/16/gain-control-of-your-control-state.aspx"&gt;ControlState&lt;/a&gt; lets you do…just not for existing controls).    &lt;br /&gt;At it’s core the problem ViewState really has is that it’s a &lt;a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html"&gt;Leaky Abstraction&lt;/a&gt;; it’s ALMOST there but doesn’t quite hit the sweet spot nowadays…&lt;/p&gt;  &lt;p&gt;So, how do you use ViewState correctly…what can you do to avoid the pitfalls? &lt;/p&gt;  &lt;p&gt;Stay tuned!&lt;/p&gt;&lt;img src="http://mostlylucid.net/aggbug/1300.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Galloway</dc:creator>
            <guid>http://mostlylucid.net/archive/2008/09/09/viewstate-does-not-suck.part-1.aspx</guid>
            <pubDate>Tue, 09 Sep 2008 19:24:50 GMT</pubDate>
            <comments>http://mostlylucid.net/archive/2008/09/09/viewstate-does-not-suck.part-1.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://mostlylucid.net/comments/commentRss/1300.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Grrr...poor use of singletons and a very cool Generic Singleton pattern!</title>
            <link>http://mostlylucid.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.net/aggbug/1276.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Scott Galloway</dc:creator>
            <guid>http://mostlylucid.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>
            <comments>http://mostlylucid.net/archive/2008/04/30/grrr.poor-use-of-singletons-and-a-very-cool-generic-singleton.aspx#feedback</comments>
            <slash:comments>7</slash:comments>
            <wfw:commentRss>http://mostlylucid.net/comments/commentRss/1276.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
