<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.Net Smoothie &#187; concurrency</title>
	<atom:link href="http://www.richardbushnell.net/tag/concurrency/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardbushnell.net</link>
	<description></description>
	<lastBuildDate>Wed, 30 Dec 2009 11:42:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The LinqDataSource and the Hidden Viewstate</title>
		<link>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/</link>
		<comments>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 08:00:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quaility]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LinqDataSource]]></category>
		<category><![CDATA[ViewState]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/</guid>
		<description><![CDATA[Yesterday I thought I&#8217;d learn about the LinqDataSource in ASP.Net 3.5, and got an interesting surprise. The new LinqDataSource can also be used with a LINQ-to-SQL model to perform updates. You simply add the DataSource to your page, set the table name, and set EnableUpdate to true. Then, using a standard DataControl, you can make [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F21%2Fthe-linqdatasource-and-the-hidden-viewstate%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Yesterday I thought I&#8217;d learn about the <strong>LinqDataSource</strong> in ASP.Net 3.5, and got an interesting surprise.</p>
<p>The new <strong>LinqDataSource</strong> can also be used with a LINQ-to-SQL model to perform updates. You simply add the <strong>DataSource</strong> to your page, set the table name, and set <strong>EnableUpdate</strong> to true. Then, using a standard <strong>DataControl</strong>, you can make updates to your data entities.</p>
<p>The question is, how does this work? It appears to be a bit magical. <span id="more-55"></span></p>
<h2>The Magic of LINQ</h2>
<p>In a <a href="http://richardbushnell.net/index.php/2008/02/18/how-to-update-data-with-linq-to-sql/">previous post</a>, I mentioned that LINQ-to-SQL updates can be done in two ways: either you make a call to retrieve a row, then update it, or you provide known values for all fields and try to update using optimistic concurrency.</p>
<p>If the <strong>LinqDataSource</strong> used the first technique, the performance might be bad. Plus, it would have to only update those fields that had been updated by the <strong>DataControl</strong>, and ignore the rest. That wouldn&#8217;t make sense, so I correctly supposed that was not what was happening.</p>
<p>It makes more sense that the <strong>LinqDataSource</strong> would use the values input by the user to make the changes. It could use optimistic concurrency to compare all the entered values.</p>
<p>The problem is, I don&#8217;t always display all fields from a particular row on the form, so how would the <strong>LinqDataSource</strong> know what the missing fields were. Remember, all the fields involved in the update are required for the optimistic concurrency to work. That is usually all the fields in a row, and they aren&#8217;t usually all displayed.</p>
<p>So, have a guess. Where does the <strong>LinqDataSource</strong> store it&#8217;s values?</p>
<p>Let&#8217;s think now. Where would be the <em>easiest </em>place to keep it? To keep it really simple, I&#8217;ll let you ignore <a href="http://richardbushnell.net/index.php/2007/12/23/qualities-of-a-dot-net-application-design/">all other aspects of building an application</a>, like security or performance.</p>
<p>Yep, you guessed it &#8211; <strong>ViewState</strong>.</p>
<p>The ViewState actually contains all values for all the fields in a LINQ-to-SQL entity by default. Those values are sent to the client, even if the user isn&#8217;t supposed to be able to see them.</p>
<p>Aaaarrgghhhh!</p>
<p>Now, let&#8217;s not get overexcited. The ViewState is encoded, so it&#8217;s not easy to just go and change it. But it&#8217;s not impossible. In general, I don&#8217;t think it&#8217;s such a great idea to have all data sent to a client by default. Someone is going to overlook something one day, and there&#8217;ll be a costly mistake.</p>
<p>And what if you display a large grid using the LinqDataSource. Well, you aren&#8217;t just getting the ViewState from the grid sent back and forth with your page, but the LinqDataSource is going to store <em>all your data</em> retrieved in the ViewState too.</p>
<p>Bummer!</p>
<p>Great control. Large overhead.</p>
<h2>Kicking out the ViewState</h2>
<p>The good news is that the ASP.Net team realized that this was a problem and did something about it.</p>
<p>If you look carefully, you will notice that the <strong>LinqDataSource</strong> has a property <strong>StoreOriginalValuesInViewState</strong>. By default (sigh) that property is set to true. If you set it to false, the control won&#8217;t work any more. Simple as that.</p>
<p>Here&#8217;s what the official documentation has to say about that:</p>
<blockquote><p>If you set the <strong>StoreOriginalValuesInViewState</strong> property to false, the original values are not persisted in view state for the data-bound control. In that case, LINQ to SQL cannot verify the integrity of the data. LINQ to SQL will throw an exception that indicates a data conflict even if the data in the data source has not actually changed.</p></blockquote>
<p>That&#8217;s in the remarks section of the <strong>LinqDataSource</strong>.<strong>StoreOriginalValuesInViewState</strong> property documentation.</p>
<p>Funnily enough, the same section states the problem with stuffing data into the ViewState:</p>
<blockquote><p>Storing the original values in view state can cause the page size to become unnecessarily large and can expose sensitive data to a malicious user.</p></blockquote>
<p>Now, I don&#8217;t know about you, but that looks like a pretty serious warning to me. Why isn&#8217;t it in a more obvious place, like as a comment that pops in up Intellisense whenever you use a LinqDataSource?</p>
<h2>Check the UpdateCheck</h2>
<p>So how do you handle it? How can you use the <strong>LinqDataSource</strong>?</p>
<p>The answer lies in the generation of the LINQ-to-SQL model which you generate. Each entity field has a <strong>Column </strong>attribute which allows you to specify if it is used for checking during an optimistic concurrency field comparison.</p>
<p>For example, you could create a Model with the following Property:</p>
<pre class="code">[<span style="color: #2b91af">Column</span>(Storage=<span style="color: #a31515">"_AddressID"</span>, UpdateCheck=<span style="color: #2b91af">UpdateCheck</span>.Never, ...
<span style="color: blue">public int </span>AddressID
...</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Notice the <strong>UpdateCheck.Never </strong>value. That means that the field will not be used in the optimistic concurrency check.</p>
<p>The documentation helps a little again:</p>
<blockquote><p>By default, when update and delete operations have been enabled, the <a href="ms-help://ms.vscc.v90/9b2f476d-7f28-2aa8-8143-3082edcf11d5.htm">LinqDataSource</a> control stores the original values for all the records in view state. The <a href="ms-help://ms.vscc.v90/9b2f476d-7f28-2aa8-8143-3082edcf11d5.htm">LinqDataSource</a> control stores values for all primary keys and all properties not marked with UpdateCheck.Never in the Column attribute. You set the UpdateCheck property of the Column attribute in the O/R Designer.</p></blockquote>
<p>Admittedly, I haven&#8217;t yet tried that, so I don&#8217;t really know how if you can change that setting for a field without changing generated code, which would be a big no-no. I&#8217;ll just have to get back to you on that.</p>
<h2>Stamp In, Please</h2>
<p>However, there is a shortcut trick.</p>
<p>Basically, if you add a field of type <strong>timestamp</strong> to your record, only that field will be used for the concurrency check, and only that field&#8217;s data will be stored in the ViewState.</p>
<blockquote><p>If the underlying data source contains a timestamp field that is automatically updated during an update, you can store only that value in view state. In that case, the timestamp property in the entity class is set to IsVersion=true and all the properties are set to UpdateCheck.Never. Because a timestamp field is automatically updated by the database every time that data in that record changes, LINQ to SQL determines from that value if data has changed. This helps reduce the size of view state, and no sensitive data is exposed. LINQ to SQL will check for data consistency by comparing the timestamp value in view state with the timestamp value in the database.</p></blockquote>
<p>The documentation is great, if you know where to find it. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>So, the answer is, if you use the <strong>LinqDataSource</strong> to throw a quick application together, make sure you use a <strong>timestamp</strong> field in your table. Call it <strong>UpdatedOn</strong>, or something. It will make the use of the <strong>LinqDataSource</strong> a lot easier, if only because you&#8217;ll sleep better not worrying about it.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F21%2Fthe-linqdatasource-and-the-hidden-viewstate%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

