<?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>Luminis Software Development &#187; .NET</title>
	<atom:link href="http://lsd.luminis.eu/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://lsd.luminis.eu</link>
	<description></description>
	<lastBuildDate>Sun, 03 Mar 2013 08:42:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>nl</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Threading made easy in .Net 4.0 (2/2)</title>
		<link>http://lsd.luminis.eu/threading-made-easy-in-net-4-0-22/</link>
		<comments>http://lsd.luminis.eu/threading-made-easy-in-net-4-0-22/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 15:06:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[technical]]></category>

		<guid isPermaLink="false">http://lsd.luminis.eu/?p=1429</guid>
		<description><![CDATA[.net threading system.threading.task twitter parallel 4.0 microsoft dotnet ]]></description>
			<content:encoded><![CDATA[<h3>Threading made Easy (2/2)</h3>
<p>This blog is part two of a little example I wrote to test the new threading stuff in .Net 4.0. To read part 1 follow this <a href="http://lsd.luminis.eu/treading-made-ease-in-net-4-0/">link</a>.</p>
<p>Short recap from part 1 first. This is what I wrote:<br />
The thing I have to do is get some data from the web in the following pseudo manner:<br />
<code><br />
foreach subject from the list<br />
search for the last 100 tweets on this subject<br />
</code></p>
<p>The part left over was to show some results from the threaded calls from part 1. It so happens to be that, somewhere near the end, I showed the following call as the most nicest and clean threading mechanism:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> GetParallel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Parallel.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>UrlList, url <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#123;</span>
            Get<span style="color: #000000;">&#40;</span>url<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h4>Threadsafe collections</h4>
<p>From the example it shows we&#8217;re not doing anything useful with the result which is returned by Twitter. A byte array is returned and the thing I want to do is to store the byte array in a collection of byte arrays. Pretty simple huh? Well almost because a potential threading problem is kickin&#8217; in. The standard collections in .Net are <em>not</em> threadsafe. We used to write code with a locking object and lock the piece of code which makes changes to the collection. The new .Net 4.0 has a more smooth solution to this problem: System.Collections.Concurrent.BlockingCollection. And the code above is changed in:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// use a thread safe collection for all threads</span>
BlockingCollection<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span> data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BlockingCollection<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
Parallel.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>UrlList, url <span style="color: #008000;">=&gt;</span>
<span style="color: #000000;">&#123;</span>
    data.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>Get<span style="color: #000000;">&#40;</span>url<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// return an ordinary collection (non thread safe) to the outside world</span>
<span style="color: #0600FF;">return</span> data.<span style="color: #0000FF;">ToList</span><span style="color: #008000;">&lt;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>So each thread use a thread safe collection to store its results and when we&#8217;re ready with all threads the non-threadsafe counterpart is returned because when processing the result we don&#8217;t need any multithreaded handling.</p>
<h4>Twitter data structure</h4>
<p>Twitter returns its data in JSON format. The search API returns the data in the following structure (see also the <a href="http://apiwiki.twitter.com/w/page/22554756/Twitter-Search-API-Method:-search">Twitter API documentation</a>), here is an example:<br />
<code><br />
{<br />
&nbsp;&nbsp;"results":<br />
&nbsp;&nbsp;[<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"text":"@twitterapi  http:\/\/tinyurl.com\/ctrefg",<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"to_user_id":396524,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"to_user":"TwitterAPI",<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"from_user":"jkoum",<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "metadata":<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"result_type":"popular",<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"recent_retweets": 109<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"id":1478555574,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"from_user_id":1833773,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"iso_language_code":"nl",<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"source":"&lt;a href="http:\/\/twitter.com\/"&gt;twitter&lt;\/a&gt;",<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/118412707\/2522215727_a5f07da155_b_normal.jpg",<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"created_at":"Wed, 08 Apr 2009 19:22:10 +0000"<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... truncated ...<br />
&nbsp;&nbsp;],<br />
&nbsp;&nbsp;"since_id":0,<br />
&nbsp;&nbsp;"max_id":1480307926,<br />
&nbsp;&nbsp;"refresh_url":"?since_id=1480307926&amp;q=%40twitterapi",<br />
&nbsp;&nbsp;"results_per_page":15,<br />
&nbsp;&nbsp;"next_page":"?page=2&amp;max_id=1480307926&amp;q=%40twitterapi",<br />
&nbsp;&nbsp;"completed_in":0.031704,<br />
&nbsp;&nbsp;"page":1,<br />
&nbsp;&nbsp;"query":"%40twitterapi"<br />
}</code></p>
<p>The list of byte arrays we have is a list of bytes in JSON format. Each byte array represents the results of a query and consists of some meta data describing the query and a list with references to all tweets returned by the search query. Each of these byte arrays needs to be transformed in something more meaningful and useful. Here JSON comes to the rescue, it is not only blazingly fast it is also simple. All we have to do is just define the interesting elements in the structure and the serialization will do its work, silently and fast. The structure in a .Net understandable format is:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>DataContract<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TwitterSearchResultList
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> TwitterSearchResult<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> results <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> query <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">double</span> completed_in <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>DataContract<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TwitterSearchResult
<span style="color: #000000;">&#123;</span>
     <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#93;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> from_user <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#93;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> text <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#93;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> profile_image_url <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Transforming one byte array with twitter results is now as easy as:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span>TwitterSearchResultList<span style="color: #008000;">&gt;</span> tweets <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>TwitterSearchResultList<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> tweetlist <span style="color: #0600FF;">in</span> allTweetLists<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
        MemoryStream tweetStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span>tweetlist<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        DataContractJsonSerializer serializer <span style="color: #008000;">=</span> 
                <span style="color: #008000;">new</span> DataContractJsonSerializer<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>TwitterSearchResultList<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        TwitterSearchResultList tsrl <span style="color: #008000;">=</span> 
                <span style="color: #000000;">&#40;</span>TwitterSearchResultList<span style="color: #000000;">&#41;</span>serializer.<span style="color: #0000FF;">ReadObject</span><span style="color: #000000;">&#40;</span>tweetStream<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h4>Rendering to html</h4>
<p>To render the TwitterSearchReturnList to a presentable format I generated some html. This can be done much cleaner and nicer with XSLT and/or CSS but for the sake of the example I just coded the bare minimum. It looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">StringBuilder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
stringBuilder.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;TABLE&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>TwitterSearchResult tweet <span style="color: #0600FF;">in</span> list.<span style="color: #0000FF;">results</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     stringBuilder.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;TR&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">// the tweeters' avatar</span>
     stringBuilder.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;TD&gt;&lt;IMG src=<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span> width=<span style="color: #008080; font-weight: bold;">\&quot;</span>48<span style="color: #008080; font-weight: bold;">\&quot;</span> heigth=<span style="color: #008080; font-weight: bold;">\&quot;</span>48<span style="color: #008080; font-weight: bold;">\&quot;</span> /&gt;&lt;/TD&gt;&quot;</span>, tweet.<span style="color: #0000FF;">profile_image_url</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #008080; font-style: italic;">// tweeters' name</span>
     stringBuilder.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;TD&gt;{0}&lt;/TD&gt;&quot;</span>, tweet.<span style="color: #0000FF;">from_user</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #008080; font-style: italic;">// tweet text; with all urls as an href</span>
     stringBuilder.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;TD&gt;{0}&lt;/TD&gt;&quot;</span>, Regex.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span>tweet.<span style="color: #0000FF;">text</span>, <span style="color: #666666;">&quot;(http:/[<span style="color: #008080; font-weight: bold;">\\</span>S/]*)&quot;</span>, <span style="color: #666666;">&quot;&lt;a href=<span style="color: #008080; font-weight: bold;">\&quot;</span>$1<span style="color: #008080; font-weight: bold;">\&quot;</span>&gt;$1&lt;/A&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    stringBuilder.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;/TR&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
stringBuilder.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&lt;/TABLE&gt;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Resulting in the following output:<br />
 <a href="http://lsd.luminis.eu/wp-content/uploads/2011/02/ThreadingTestOutput.png"><img src="http://lsd.luminis.eu/wp-content/uploads/2011/02/ThreadingTestOutput-1024x657.png" alt="ThreadingTestOutput" title="ThreadingTestOutput" width="750" height="480" class="aligncenter size-medium wp-image-1465" /></a></p>
<h4>Source code</h4>
<p>The full source code for this example can be downloaded <a href="http://lsd.luminis.eu/wp-content/uploads/2011/02/ThreadingTest.zip">here</a>.<br />
Thank you for reading, questions will be answered, suggestions are more than welcome!<br />
have fun,<br />
florisz </p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/threading-made-easy-in-net-4-0-22/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Threading made easy in .Net 4.0 (1/2)</title>
		<link>http://lsd.luminis.eu/treading-made-ease-in-net-4-0/</link>
		<comments>http://lsd.luminis.eu/treading-made-ease-in-net-4-0/#comments</comments>
		<pubDate>Sat, 08 Jan 2011 12:27:22 +0000</pubDate>
		<dc:creator>Floris Zwarteveen</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://lsd.luminis.eu/?p=1356</guid>
		<description><![CDATA[.net threading threads system.threading.task twitter parallel 4.0 microsoft dotnet ]]></description>
			<content:encoded><![CDATA[<h3>Threading made Easy (1/2)</h3>
<p>In my opinion it isn’t possible to have these two words in one sentence. I’ve been exposed to thread programming numerous times in the past. Despite the almost heroic nature of being a ‘thread programmer’, I’ve never had the guts to expose this in public. Brag over a few beers about success stories on how well my thread-aware and thread-safe software was running? It has never been part of my vocabulary. One simple reason: it’s way too complicated. Well, at least for me. Even with the aid of thorough design, good examples and extensive mock tests my code remained complicated and incomprehensible. And yes, I tried to refactor, redesign, recode, restructure, whatever, I never could get it right.</p>
<p>Well, up until .Net 4 and its component: System.Threading.Tasks. Included in the very core of the framework. Right out of the box, support for threading for the masses.</p>
<h4>Why threading?</h4>
<p>It is good to step back for one moment. The 1-million-dollar-question to always ask yourself is: do I really need it? The simple answer is: yes. Because why would you let anyone wait while your machine is sitting there doing nothing? Especially when it is not necessary to wait.</p>
<blockquote><p>So: yes I need threading to make software as performant as can be to the best of my abilities as a software engineer.</p></blockquote>
<p>Do I always have to use threading? Of course not. In its most essential form, current CPU’s can do multiple things at the same time (especially when having more cores) and are very fast and therefore often waiting for some other things (like database or web I/O stuff) to end before they can continue. So if you’re in such a situation, and I can assure you, this is most often, threads come to the rescue to really utilize the CPU to its full capacity.</p>
<h4>The example</h4>
<p>Let’s apply threading to a simple example to show how the new .Net threadinglayer can be utilized to increase performance 40-70% in a very easy manner.</p>
<p>Suppose you want to do a multiple query on Twitter (most popular demo environment) in which you need to return the first 100 hits for a couple of subjects. I have tried this with my webbrowser and you have to do some manual work to return such a list in one piece.</p>
<p>Beforehand I do know getting 100 tweets from Twitter isn’t that difficult, especially with their JSON based REST API (that’s a lot of cool keywords in one sentence!). So one task is easy to implement. I also know that getting something from the internet usually sets my CPU in a comfortable waiting state because network transfer is still way slower than my CPU. So the task is also suitable for multiplexing a couple of times.</p>
<h4>Some preparation</h4>
<p>First I have to define my testcases. I don’t want to get bogged down into testclasses (although I do agree it would have been better to use them) because hey, this is demo code anyway.</p>
<p>The thing I want to show you is:<br />
<img class="alignnone size-full wp-image-1390" title="ThreadingScenario" src="http://lsd.luminis.eu/wp-content/uploads/2011/01/ThreadingScenario.png" alt="ThreadingScenario" width="284" height="219" /><br />
In short, I want to show the performance differences between four separate scenarios<br />
-      A non threaded one<br />
-      A classic, pre 4.0, threadpool version<br />
-      A 4.0 version using the new TaskFactory<br />
-      And finally the version using the new Parallel class</p>
<p>These scenarios all do exactly the same thing; only the scheduling of the tasks differs. The thing they have to do is get some data from the web in the following pseudo manner:<br />
<code><br />
foreach subject from the list<br />
search for the last 100 tweets on this subject<br />
</code></p>
<p>The search API in Twitter is pretty straightword. For example, to perform the above for the subject ‘luminis’ the following url will suffice:</p>
<p><code>http://search.twitter.com/search.json?&amp;q=luminis&amp;rpp=100<br />
</code><br />
To get data from the web I’ve defined a small routine which returns a byte array with the required data. Fortunately WCF made getting data from the web pretty straightforward in .Net, this resulted in the very simple basic routine:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> Get<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> url<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> data <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
    WebClient wc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">try</span>
    <span style="color: #000000;">&#123;</span>
        data <span style="color: #008000;">=</span> wc.<span style="color: #0000FF;">DownloadData</span><span style="color: #000000;">&#40;</span>url<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">catch</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// probable time out, for this demo just quit</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">finally</span>
    <span style="color: #000000;">&#123;</span>
        wc.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> data<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This routine is part of a class called WebContent. This class also has a public property UrlList which can contain the list of urls for which the content must be read.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> List _UrlList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> List UrlList
<span style="color: #000000;">&#123;</span>
    get
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> _UrlList<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>What I have to do now is to iterate over this list and call my simple Get routine for each of the four scenarios.</p>
<h4>#1: The non-threaded version</h4>
<p>The simple version is the one without any threading at all. We do need this one to baseline our tests so we can do a comparison with the threaded counterparts. The only thing we need to do is read all the urls and the code pretty much resembles the pseudo code shown previously:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> GetSequential<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> url <span style="color: #0600FF;">in</span> UrlList<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Get<span style="color: #000000;">&#40;</span>url<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h4>#2: The 2.0 ThreadPool version</h4>
<p>Actually I didn’t want to make the ThreadPool version because as said earlier I don’t get this ‘old’ thread programming very well. But as a reference I’ve hacked a version for which I can’t take any liabilities. It is just here for reference purposes. Note that I need two routines to achieve the same goal. Additionally I’ve changed the thread of the main to [MTAThreadAttribute] otherwise you will receive to message:<br />
<img class="aligncenter size-full wp-image-1393" title="PerfSTAError" src="http://lsd.luminis.eu/wp-content/uploads/2011/01/PerfSTAError.png" alt="PerfSTAError" width="456" height="236" /><br />
Anyway the code looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> GetWithThreadPool<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    _ResetEvents <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ManualResetEvent<span style="color: #000000;">&#91;</span>UrlList.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
    Random rand <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Random<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> UrlList.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        _ResetEvents<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ManualResetEvent<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        ThreadPool.<span style="color: #0000FF;">QueueUserWorkItem</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> WaitCallback<span style="color: #000000;">&#40;</span>DoThreadPoolWork<span style="color: #000000;">&#41;</span>, <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span><span style="color: #000000;">&#41;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    WaitHandle.<span style="color: #0000FF;">WaitAll</span><span style="color: #000000;">&#40;</span>_ResetEvents<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> DoThreadPoolWork<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> o<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> index <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>o<span style="color: #008000;">;</span>
&nbsp;
    Get<span style="color: #000000;">&#40;</span>UrlList<span style="color: #000000;">&#91;</span>index<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    _ResetEvents<span style="color: #000000;">&#91;</span>index<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Pretty impressive but incomprehensible, huh?<br />
It doesn&#8217;t feel right that you have to add all these complex things just to get a bit of multi-threading. To me, the only easy part is the Get call somewhere near the end. All the other stuff is thread overhead. And if you would need more that 25 threads this code won’t work. But let’s not dwell on these passed issues and move over to the 4.0 versions.</p>
<h4>#3: The  4.0 TaskFactory version</h4>
<p>The task factory shows what the thread overhead could be if the framework is more powerful than the 2.0 version. Still it is some overhead but the code looks more like the pseudo code than the previous implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> GetWithTaskFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Task<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> tasks <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Task<span style="color: #000000;">&#91;</span>UrlList.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> counter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> url <span style="color: #0600FF;">in</span> UrlList<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var task <span style="color: #008000;">=</span> Task.<span style="color: #0000FF;">Factory</span>.<span style="color: #0000FF;">StartNew</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span>
                                 Get<span style="color: #000000;">&#40;</span>url<span style="color: #000000;">&#41;</span>, TaskCreationOptions.<span style="color: #0000FF;">LongRunning</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        tasks<span style="color: #000000;">&#91;</span>counter<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> task<span style="color: #008000;">;</span>
        counter<span style="color: #008000;">++;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    Task.<span style="color: #0000FF;">WaitAll</span><span style="color: #000000;">&#40;</span>tasks<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The thing I need to do is to create a task for each subject and, with a lambda expression, call my good old private Get() on this task. Finally I just have to wait until all tasks are finished.</p>
<h4>#4: The 4.0 Parallel version</h4>
<p>The ultimate version is left to the end. The new Parallel class makes it possible to write threaded code without almost no (thread) overhead. It looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> GetParallel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Parallel.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>UrlList, url <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#123;</span>
            Get<span style="color: #000000;">&#40;</span>url<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Isn’t that just beautiful? It also resembles the pseudo code pretty well. Okay you have to understand the lambda expression with the anonymous delegate syntax and semantics but if you do it is just an ordinary for-loop. As far as I’m concerned: this is as it should be.</p>
<h4>Performance results</h4>
<p>So far the differences in the source code of the four implementations. To start each of the separate scenarios a little bit of timing code is needed to calculate the total of milliseconds the code runs.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> GetContentDelegate<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> DoTimedOperation<span style="color: #000000;">&#40;</span>GetContentDelegate getForumContentDelegate<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    DateTime start <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// perform the actual operation</span>
    getForumContentDelegate<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    DateTime end <span style="color: #008000;">=</span> DateTime.<span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
    TimeSpan elapsed <span style="color: #008000;">=</span> end <span style="color: #008000;">-</span> start<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> elapsed.<span style="color: #0000FF;">TotalMilliseconds</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;ms&quot;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And with the aid of a delegate the timing function is called four times as in:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> StartSequential_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">lblSequentialTime</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span>
        DoTimedOperation<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> GetContentDelegate<span style="color: #000000;">&#40;</span>WebContent.<span style="color: #0000FF;">GetSequential</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> StartThreadPool_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">lblThreadPoolTime</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span>
        DoTimedOperation<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> GetContentDelegate<span style="color: #000000;">&#40;</span>WebContent.<span style="color: #0000FF;">GetWithThreadPool</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> StartTaskFactoryl_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">lblTaskFactoryTime</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span>
        DoTimedOperation<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> GetContentDelegate<span style="color: #000000;">&#40;</span>WebContent.<span style="color: #0000FF;">GetWithTaskFactory</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> StartParallel_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">lblParallelTime</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span>
        DoTimedOperation<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> GetContentDelegate<span style="color: #000000;">&#40;</span>WebContent.<span style="color: #0000FF;">GetParallel</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I ran a few tests on my machine and one of them resulted in the following numbers: (By the way my machine is a Macbook Pro running Windows 7 in a Virtual Box with 1 processor and 2048 Mb memory.)<br />
<img class="aligncenter size-full wp-image-1394" title="PerfResults" src="http://lsd.luminis.eu/wp-content/uploads/2011/01/PerfResults.png" alt="PerfResults" width="284" height="217" /><br />
Of course you have to try this on your own machine but it is safe to say that the performance increase is worth the overhead in thread programming.</p>
<p>I have analyzed the thread behavior with the Performance Monitor (PerfMon). In the analysis I was interested in:<br />
-      # of processor threads (blue)<br />
-      # of logical threads for the process (green)<br />
-      # of physical threads for the process (red)<br />
These are the results of one test I have run:<br />
<img class="aligncenter size-full wp-image-1396" title="Dia1" src="http://lsd.luminis.eu/wp-content/uploads/2011/01/Dia1.jpg" alt="Dia1" width="720" height="434" /><br />
You can see the framework too needs some threads when the non threaded version is started. When the threadpool scenario starts you clearly see the increase in threads. The threadpool is managed by .Net so this amount doesn&#8217;t decrease when the scenario is finished. This does happen with the third scenario. Here the threads are in control of the routine and you can clearly see that the threads are released (at least to the system, the framework has a different view on it). In the fourth scenario again the threads are fully controlled by the framework.<br />
Interesting to see is: in both the threadpool and the parallel scenario only a limited amount of threads are created (approximately 5 or 6) while in the Taskfactory 12 threads are created (for each subject one). It is hard to say what exactly happens behind the scenes. Do all logical threads have a physical thread associated? And do these threads run &#8217;simultaneously&#8217; or not? I have to say I don&#8217;t know.<br />
Then there is one thing I don&#8217;t understand at all. How is it possible (near the end) that the total number of physical threads of the process is higher than the system amount of physical threads?! If somebody can tell me this I would be very hapy!</p>
<h4>Conclusion</h4>
<p>In a next blog I will extend this little example. So far I have concentrated on threads, starting and synchronizing them to show the performance differences. I have experimental proof it is worth taking an effort to learn more on the new .Net 4.0 threading stuff.<br />
The part that is left out is the handling of the data and the presentation of the results.<br />
This part is described in part 2 and can be found <a href="http://lsd.luminis.eu/threading-made-easy-in-net-4-0-22/">here</a>.</p>
<p>have fun, florisz</p>
<p>Download the zip for the full source code <a href="http://lsd.luminis.eu/wp-content/uploads/2011/01/ThreadingTest.zip">here</a>.</p>
<h4>References</h4>
<p><a href="http://apiwiki.twitter.com/w/page/22554756/Twitter-Search-API-Method:-search">Twitter Search API</a></p>
<p><a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.aspx">MSDN on System.Threading.Tasks</a></p>
<p><a href="http://www.codethinked.com/post/2010/01/25/NET-40-and-SystemThreadingTasks.aspx">.Net 4.0 and System.Threading.Tasks</a></p>
<p><a href="http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-threadpool">Threadpool example</a></p>
<p><a href="http://www.eggheadcafe.com/tutorials/aspnet/21013a52-fe11-4af8-bf8b-50cfd1a51577/task-parallelism-in-c-40-with-systemthreadingtasks.aspx">Task Parallelism in C# 4.0 with System.Threading.Tasks</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/treading-made-ease-in-net-4-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Howto use MvcContrib.Pagination with a ViewModel</title>
		<link>http://lsd.luminis.eu/howto-use-mvccontrib-pagination-with-a-viewmodel/</link>
		<comments>http://lsd.luminis.eu/howto-use-mvccontrib-pagination-with-a-viewmodel/#comments</comments>
		<pubDate>Fri, 31 Dec 2010 14:12:53 +0000</pubDate>
		<dc:creator>Erik Sanders</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://lsd.luminis.eu/?p=1335</guid>
		<description><![CDATA[MvcContrib.Pagination is a very elegant way of using paging in ASP.MVC. With the provided templates and helpers it is reduced to simply wiring the parts. Because I use a ViewModel to decouple from the domain I needed some extra work by using the provided generic CustomPagination<T> class. An explicit helper for this scenario would simplify the usage]]></description>
			<content:encoded><![CDATA[<h1>Howto use MvcContrib.Pagination with a ViewModel</h1>
<p class="MsoNormal">I was looking for a simple solution for paging in a ASP.MVC2 project. Though we already use MVCContrib.Grid this was the first place I searched.</p>
<p class="MsoNormal">The solution MVCContrib offers is elegant in the way that they separated the concept of paging, a ui element to show paging (next , previous, …) and a grid to show the content. This separation allows us to using paging on a custom table based page as well.</p>
<p class="MsoNormal">Because we are using ViewModels a little extra effort was needed to retain the paging information from the domain layer in the view layer.</p>
<h1><strong>Step by step:</strong></h1>
<p class="MsoNormal">
<p class="MsoNormal">* Get a Querable from repository using Domain Objects</p>
<p class="MsoNormal">
<pre>IQueryable&lt;DomainObject&gt; domainObjects = rep.GetAll()</pre>
<p class="MsoNormal">
<p class="MsoNormal">* Filter and Sort using Linq</p>
<p class="MsoNormal">
<pre>domainObjects = domainObjects.Where(...).OrderBy(...)</pre>
<p class="MsoNormal">
<p class="MsoNormal">* Get the data</p>
<p class="MsoNormal">
<pre>IPagination&lt;DomainObject&gt; pageOfData domainObjects.AsPagination( page, pageSize)</pre>
<p class="MsoNormal">
<p class="MsoNormal">* Map to the ViewModel</p>
<p class="MsoNormal">
<pre>IPagination&lt;DomainObjectViewModel&gt; model = DomainObjectViewModel.Map( pageOfData )</pre>
<p class="MsoNormal">
<p class="MsoNormal">* Mapping needs to retain the page information from the domain</p>
<p class="MsoNormal">
<pre>var list = new List&lt;DomainObjectViewModel&gt;();
foreach (var domainObject in domainObjects)
{
list.Add(Map(domainObject));
}
new CustomPagination&lt;DomainObjectViewModel&gt;(list.AsEnumerable(),
    pageOfData.PageNumber, 
    pageOfData.PageSize, 
    pageOfData.TotalItems);</pre>
<p class="MsoNormal">
<p class="MsoNormal">
<p class="MsoNormal">* Show the information on a page in a grid</p>
<p class="MsoNormal">
<pre>&lt;%= Html.Grid(Model).AutoGenerateColumns() %&gt;</pre>
<p class="MsoNormal">
<p class="MsoNormal">* Show the pager</p>
<p class="MsoNormal">
<pre>&lt;%= Html.Pager(Model).First( "&lt;&lt;").Last("&gt;&gt;").Next("&gt;").Previous("&lt;")
.Format( "Item {0} - {1} van {2} ") %&gt;</pre>
<p class="MsoNormal">
<p class="MsoNormal">
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Step by step:The ====# HowTo====</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">* Get a Querable from repository using Domain Objects</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">@@IQueryable&lt;DomainObject&gt; domainObjects = rep.GetAll()@@</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">* Filter and Sort using Linq</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">@@domainObjects = domainObjects.Where(&#8230;).OrderBy(&#8230;)@@</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">* Get the data</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">@@IPagination&lt;DomainObject&gt; pageOfData domainObjects.AsPagination( page, pageSize)@@</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">* Map to the ViewModel</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">@@IPagination&lt;DomainObjectViewModel&gt; model = DomainObjectViewModel.Map( pageOfData )@@</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">* Mapping needs to retain the page information from the domain</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">@@var list = new List&lt;DomainObjectViewModel&gt;();</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">foreach (var domainObject in domainObjects)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">{</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">list.Add(Map(domainObject));</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">new CustomPagination&lt;DomainObjectViewModel&gt;(list.AsEnumerable(), pageOfData.PageNumber, pageOfData.PageSize, pageOfData.TotalItems);</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">@@</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">* Show the information on a page in a grid</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">* Show the pager</div>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/howto-use-mvccontrib-pagination-with-a-viewmodel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Being a .NET developer in a Mac OSX world: serializing XmlDates</title>
		<link>http://lsd.luminis.eu/being-a-net-developer-in-a-mac-osx-world-serializing-xmldates/</link>
		<comments>http://lsd.luminis.eu/being-a-net-developer-in-a-mac-osx-world-serializing-xmldates/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 11:39:43 +0000</pubDate>
		<dc:creator>Richard de Zwart</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[mac os x]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[MonoDevelop]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=1099</guid>
		<description><![CDATA[I blogged before on differences in implementation between the Mono and the Microsoft implementation of the .NET framework. In this post I investigate a difference in XmlSerialization.
Dates in XSD
When you write an XSD you have (at least) 3 options to use dates and/or times:

xsd:date
xsd:dateTime
xsd:time

Most of the time you probably choose the &#8220;dateTime&#8221;, since that maps [...]]]></description>
			<content:encoded><![CDATA[<p>I blogged before on <a href="http://lsd.luminis.nl/being-a-net-developer-in-a-mac-osx-world-storing-null-values-in-a-database/" target="_blank">differences in implementation</a> between the Mono and the Microsoft implementation of the .NET framework. In this post I investigate a difference in XmlSerialization.</p>
<h3>Dates in XSD</h3>
<p>When you write an XSD you have (at least) 3 options to use dates and/or times:</p>
<ul>
<li><a href="http://www.w3.org/TR/xmlschema-2/#date" target="_blank">xsd:date</a></li>
<li><a href="http://www.w3.org/TR/xmlschema-2/#dateTime" target="_blank">xsd:dateTime</a></li>
<li><a href="http://www.w3.org/TR/xmlschema-2/#time" target="_blank">xsd:time</a></li>
</ul>
<p>Most of the time you probably choose the &#8220;dateTime&#8221;, since that maps nicely to the DateTime type in .NET. But you might choose &#8220;date&#8221; for a birthday for example, and you might even choose &#8220;time&#8221; for uh, well for a time of some sort&#8230;</p>
<p>For his post I&#8217;ll use the following XSD. It&#8217;s a bit contrived but I like this better than &#8220;MyDate&#8221; and &#8220;ADateTimeField&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:schema</span> <span style="color: #000066;">xmlns:xsd</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Book&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;BookType&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/xsd:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:complexType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;BookType&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:sequence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Title&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:string&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Price&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:float&quot;</span> <span style="color: #000066;">nillable</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;PrintedAt&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:date&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;SoldAt&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:dateTime&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;WillReadTodayAt&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:time&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsd:sequence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsd:complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsd:schema<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>Generate classes from your XSD</h3>
<p>Both Mono and Microsoft have command-line tools that generate classes from your XSD. Very convenient and a good thing to do when you need simple entities. We did this in a project where we had both C# and Java code. On both sides we generated the classes from the same XSD.</p>
<p>The syntax of both tools is more or less the same:</p>
<pre>xsd book.xsd /classes /namespace:BookStore.Books</pre>
<p>will work on both systems.<br />
The outcome of both generators is quite different. The Mono version uses public fields, the MS version use properties with back-up private fields. They both adorn the fields/properties with attributes from the XmlSerialization namespace.<br />
For Microsoft it looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> BookStore.<span style="color: #0000FF;">Books</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Xml.Serialization</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">CodeDom</span>.<span style="color: #0000FF;">Compiler</span></span>.<span style="color: #0000FF;">GeneratedCodeAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;xsd&quot;</span>, <span style="color: #666666;">&quot;2.0.50727.42&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">SerializableAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">DebuggerStepThroughAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">ComponentModel</span></span>.<span style="color: #0000FF;">DesignerCategoryAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;code&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span>.<span style="color: #0000FF;">Serialization</span></span>.<span style="color: #0000FF;">XmlRootAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Book&quot;</span>, <span style="color: #0600FF;">Namespace</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;&quot;</span>, IsNullable<span style="color: #008000;">=</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> BookType <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> titleField<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Nullable</span> priceField<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">DateTime</span> printedAtField<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">DateTime</span> soldAtField<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">DateTime</span> willReadTodayAtField<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span>.<span style="color: #0000FF;">Serialization</span></span>.<span style="color: #0000FF;">XmlElementAttribute</span><span style="color: #000000;">&#40;</span>Form<span style="color: #008000;">=</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span>.<span style="color: #0000FF;">Schema</span>.<span style="color: #0000FF;">XmlSchemaForm</span>.<span style="color: #0000FF;">Unqualified</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Title <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">titleField</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">titleField</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span>.<span style="color: #0000FF;">Serialization</span></span>.<span style="color: #0000FF;">XmlElementAttribute</span><span style="color: #000000;">&#40;</span>Form<span style="color: #008000;">=</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span>.<span style="color: #0000FF;">Schema</span>.<span style="color: #0000FF;">XmlSchemaForm</span>.<span style="color: #0000FF;">Unqualified</span>, IsNullable<span style="color: #008000;">=</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Nullable</span> Price <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">priceField</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">priceField</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span>.<span style="color: #0000FF;">Serialization</span></span>.<span style="color: #0000FF;">XmlElementAttribute</span><span style="color: #000000;">&#40;</span>Form<span style="color: #008000;">=</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span>.<span style="color: #0000FF;">Schema</span>.<span style="color: #0000FF;">XmlSchemaForm</span>.<span style="color: #0000FF;">Unqualified</span>, DataType<span style="color: #008000;">=</span><span style="color: #666666;">&quot;date&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">DateTime</span> PrintedAt <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">printedAtField</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">printedAtField</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span>.<span style="color: #0000FF;">Serialization</span></span>.<span style="color: #0000FF;">XmlElementAttribute</span><span style="color: #000000;">&#40;</span>Form<span style="color: #008000;">=</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span>.<span style="color: #0000FF;">Schema</span>.<span style="color: #0000FF;">XmlSchemaForm</span>.<span style="color: #0000FF;">Unqualified</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">DateTime</span> SoldAt <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">soldAtField</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">soldAtField</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span>.<span style="color: #0000FF;">Serialization</span></span>.<span style="color: #0000FF;">XmlElementAttribute</span><span style="color: #000000;">&#40;</span>Form<span style="color: #008000;">=</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span>.<span style="color: #0000FF;">Schema</span>.<span style="color: #0000FF;">XmlSchemaForm</span>.<span style="color: #0000FF;">Unqualified</span>, DataType<span style="color: #008000;">=</span><span style="color: #666666;">&quot;time&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">DateTime</span> WillReadTodayAt <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">willReadTodayAtField</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">willReadTodayAtField</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>That could have been a bit more readable by removing all the fully-qualified namespaces (since there is a &#8220;using&#8221; at the top), the empty comments and the back-up fields (if you generate for .NET after version 3).</p>
<p>For Mono it is like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> BookStore.<span style="color: #0000FF;">Books</span> <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">///</span>
    <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span>.<span style="color: #0000FF;">Serialization</span></span>.<span style="color: #0000FF;">XmlRootAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Book&quot;</span>, <span style="color: #0600FF;">Namespace</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;&quot;</span>, IsNullable<span style="color: #008000;">=</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BookType <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Title<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span>.<span style="color: #0000FF;">Serialization</span></span>.<span style="color: #0000FF;">XmlElementAttribute</span><span style="color: #000000;">&#40;</span>IsNullable<span style="color: #008000;">=</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Single</span> Price<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span>.<span style="color: #0000FF;">Serialization</span></span>.<span style="color: #0000FF;">XmlElementAttribute</span><span style="color: #000000;">&#40;</span>DataType<span style="color: #008000;">=</span><span style="color: #666666;">&quot;date&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">DateTime</span> PrintedAt<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #0600FF;">public</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">DateTime</span> SoldAt<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">///</span>
        <span style="color: #000000;">&#91;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span>.<span style="color: #0000FF;">Serialization</span></span>.<span style="color: #0000FF;">XmlElementAttribute</span><span style="color: #000000;">&#40;</span>DataType<span style="color: #008000;">=</span><span style="color: #666666;">&quot;time&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">DateTime</span> WillReadTodayAt<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Much more concise. But with an important omission: the Price has the right XmlAttribute, but should have been defined as &#8220;Nullable&#8221;. I&#8217;ve seen some remarks on this in the mailing-list/forum for Mono, but could not find out if this was already fixed. So I use the Microsoft XSD.exe whenever my XSD changes, copy paste the result to my MonoDevelop environment and recompile. I would rather call the MonoXSD on the background whenever my XSD changes (has anyone used scripts as Custom Tools in MonoDevelop?)  so that the class gets re-generated automatically. Maybe the upcoming <a href="http://www.mono-project.com/Roadmap#Upcoming_Releases">Mono 2.8</a> will fix it.</p>
<h3>It&#8217;s all a DateTime</h3>
<p>As you can see, all the fields are generated as DateTimes. Makes sense, since there is nothing else in the framework to hold time-related information. So what happens when you read an Xml file and serialize the Xml into a Book object?</p>
<p>Take this Xml:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>How to be a Mac Developer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PrintedAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1965-02-16<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PrintedAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SoldAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1965-02-16<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/SoldAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;WillReadTodayAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>11:00:01<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/WillReadTodayAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>And process it with this code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #FF0000;">class</span> MainClass
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> bookXml <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;
            &lt;Book&gt;
                &lt;Title&gt;How to be a Mac Developer&lt;/Title&gt;
                &lt;PrintedAt&gt;1965-02-16&lt;/PrintedAt&gt;
                &lt;SoldAt&gt;1965-02-16&lt;/SoldAt&gt;
                &lt;WillReadTodayAt&gt;11:00:01&lt;/WillReadTodayAt&gt;
            &lt;/Book&gt;
            &quot;</span><span style="color: #008000;">;</span>
&nbsp;
            BookType book <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>BookType<span style="color: #000000;">&#41;</span> ToObject<span style="color: #000000;">&#40;</span>bookXml, <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>BookType<span style="color: #000000;">&#41;</span>, Encoding.<span style="color: #0600FF;">Default</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;PrintedAt:{0}&quot;</span>, book.<span style="color: #0000FF;">PrintedAt</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;SoldAt:{0}&quot;</span>, book.<span style="color: #0000FF;">SoldAt</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;WillReadAt:{0}&quot;</span>, book.<span style="color: #0000FF;">WillReadTodayAt</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">object</span> ToObject<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> xml, Type xmlObjectType, Encoding encoding<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            MemoryStream xmlStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span>encoding.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>xml<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            StreamReader reader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>xmlStream, encoding, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            XmlReaderSettings readerSettings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlReaderSettings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            readerSettings.<span style="color: #0000FF;">CloseInput</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
            XmlReader xmlReader <span style="color: #008000;">=</span> XmlReader.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>reader, readerSettings<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            XmlSerializer xmlSerializer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlSerializer<span style="color: #000000;">&#40;</span>xmlObjectType<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> xmlSerializer.<span style="color: #0000FF;">Deserialize</span><span style="color: #000000;">&#40;</span>xmlReader<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Different ideas on default dates</h3>
<p>You get this output in Windows (running the exe build by MonoDevelop directly on my VMWare-WindozeXP):<br />
<a href="http://lsd.luminis.nl/wp-content/uploads/2010/09/Screen-shot-2010-09-21-at-11.26.27-AM.png"><img src="http://lsd.luminis.nl/wp-content/uploads/2010/09/Screen-shot-2010-09-21-at-11.26.27-AM.png" alt="Screen shot 2010-09-21 at 11.26.27 AM" title="Screen shot 2010-09-21 at 11.26.27 AM" width="269" height="85" class="alignnone size-full wp-image-1109" /></a></p>
<p>And this output on the Mac:<br />
<a href="http://lsd.luminis.nl/wp-content/uploads/2010/09/Screen-shot-2010-09-21-at-11.30.34-AM.png"><img src="http://lsd.luminis.nl/wp-content/uploads/2010/09/Screen-shot-2010-09-21-at-11.30.34-AM.png" alt="Screen shot 2010-09-21 at 11.30.34 AM" title="Screen shot 2010-09-21 at 11.30.34 AM" width="285" height="82" class="alignnone size-full wp-image-1111" /></a></p>
<p>Interesting difference, right? Windows sets the date part of &#8220;WillReadTodayAt&#8221; to DateTime.MinValue and Mono assumes it is today. It doesn&#8217;t matter that much, since I&#8217;m going to ignore the date-part anyway for that property.<br />
The time part of the date-olny property &#8220;PrintedAt&#8221; is set to &#8220;12:00:00AM&#8221; in Windows and &#8220;00:00:00&#8243; in Mono. Again, I&#8217;m going to ignore the time-part, but what will happen when I compare dates for equality? I prefer the Mono setting.</p>
<h3>Invalid time data</h3>
<p>Take this slightly modified Xml:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>How to be a Mac Developer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PrintedAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1965-02-16<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PrintedAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;WillReadTodayAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2010-09-21T11:00:01<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/WillReadTodayAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SoldAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1965-02-16<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/SoldAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Running this on Mono result in an FormatException: <a href="http://lsd.luminis.nl/wp-content/uploads/2010/09/Screen-shot-2010-09-21-at-11.41.53-AM.png"><img src="http://lsd.luminis.nl/wp-content/uploads/2010/09/Screen-shot-2010-09-21-at-11.41.53-AM-300x208.png" alt="Screen shot 2010-09-21 at 11.41.53 AM" title="Screen shot 2010-09-21 at 11.41.53 AM" width="300" height="208" class="alignnone size-medium wp-image-1112" /></a><br />
Fair enough, the data in the xml is not a Time, it is a DateTime. So an exception makes sense.</p>
<p>Wonder what Windows will do? After you have started an instance of Visual Studio to see the error, you get this:<br />
<a href="http://lsd.luminis.nl/wp-content/uploads/2010/09/Screen-shot-2010-09-21-at-11.45.46-AM.png"><img src="http://lsd.luminis.nl/wp-content/uploads/2010/09/Screen-shot-2010-09-21-at-11.45.46-AM-300x184.png" alt="Screen shot 2010-09-21 at 11.45.46 AM" title="Screen shot 2010-09-21 at 11.45.46 AM" width="300" height="184" class="alignnone size-medium wp-image-1113" /></a></p>
<p>So both platforms agree that the data is invalid. Unfortunately the position that Windows gives us is right after the offending data, at not before as I was expecting. So it took some time (way more than an hour) to realize that the problem was not in the &#8220;SoldAt&#8221;, but in the &#8220;WillReadTodayAt&#8221; just before it. Be warned.</p>
<h3>Invalid date data</h3>
<p>Now pass in a date-with-time on the field that expects only a date:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>How to be a Mac Developer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PrintedAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1965-02-16T08:01:03<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PrintedAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;WillReadTodayAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>11:00:01<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/WillReadTodayAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SoldAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1965-02-16T09:00:02<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/SoldAt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Mono doesn&#8217;t like it. Same error as before, no indication on what line and what xml-tag, alas.<br />
Windows doesn&#8217;t like it either. Same error as before, positioned right before &#8220;WillReadTodayAt&#8221;.</p>
<h3>Conclusions</h3>
<p>There&#8217;s a big difference in the C# classes that get generated. The Mono implementation is missing the support for nullables, the MS implementation is a bit too verbose.<br />
The handling of incorrect date/time information is okay in both systems.<br />
The date-part in a xsd:time gets defaulted to &#8220;1-1-1&#8243; in MS and &#8220;today&#8221; in Mono. The time-part gets defaulted to &#8220;0:0:0&#8243; in Mono and &#8220;12:0:0AM&#8221; in MS.</p>
<p><a href='http://lsd.luminis.nl/wp-content/uploads/2010/09/XmlDateDemoForArticle.zip'>The code for this article</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/being-a-net-developer-in-a-mac-osx-world-serializing-xmldates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Being a .NET developer in a Mac OSX world: storing null values in a database</title>
		<link>http://lsd.luminis.eu/being-a-net-developer-in-a-mac-osx-world-storing-null-values-in-a-database/</link>
		<comments>http://lsd.luminis.eu/being-a-net-developer-in-a-mac-osx-world-storing-null-values-in-a-database/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 13:40:50 +0000</pubDate>
		<dc:creator>Richard de Zwart</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[ADO.NET]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Mono]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=982</guid>
		<description><![CDATA[When developing on Mac OSX, while targeting the Windows platform, you can get into some really nasty stuff when the Mono implementation of the .NET framework turns out to be different from (i.e. better than) the Microsoft implementation.]]></description>
			<content:encoded><![CDATA[<p>In this post I want to shine a light on a different aspect of developing on one OS to deliver on a different OS: different implementations of the .NET framework can have different behaviour. What runs on one, might crash on the other. That happened to me when accessing my SQL Server database using ADO.NET.</p>
<h3>Storing data using plain ADO.NET</h3>
<p>Ok, you might argue that there is no need to talk about plain ADO.NET, since nobody in his right mind is actually doing that anymore. Of course we -developers- have evolved and use document-databases like <a href="http://couchdb.apache.org/" target=_blank>CouchDB</a> and <a href="http://ravendb.net/" target=_blank>Raven DB</a> or ORMs like <a href="http://nhforge.org/Default.aspx" target=_blank>NHibernate</a> and <a href="http://msdn.microsoft.com/en-us/data/aa937723.aspx" target=_blank>Entity Framewok</a>.</p>
<p>But every now and then you need code like the code below. In my case because we were building a demo and needed to be done quickly. Which we were not, because of the problems I ran into when trying to store null values in the database.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Book book <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Book <span style="color: #000000;">&#123;</span>
	Id <span style="color: #008000;">=</span> Guid.<span style="color: #0000FF;">NewGuid</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
	ISBN <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;123-456-222&quot;</span>,
	Title <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Through the looking glass&quot;</span>
<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>SqlConnection connection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SqlConnection <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;some connections string&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	connection.<span style="color: #0000FF;">Open</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>SqlCommand cmd <span style="color: #008000;">=</span> connection.<span style="color: #0000FF;">CreateCommand</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		cmd.<span style="color: #0000FF;">CommandText</span> <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;INSERT INTO Book ([Id], [ISBN], [Title], [Publisher], [SuggestedPrice])
		       VALUES (@Id, @ISBN, @Title, @Publisher, @SuggestedPrice)&quot;</span><span style="color: #008000;">;</span>
&nbsp;
		cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">AddWithValue</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@Id&quot;</span>, book.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">AddWithValue</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@ISBN&quot;</span>, book.<span style="color: #0000FF;">ISBN</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">AddWithValue</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@Title&quot;</span>, book.<span style="color: #0000FF;">Title</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">AddWithValue</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@Publisher&quot;</span>, book.<span style="color: #0000FF;">Publisher</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">AddWithValue</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@SuggestedPrice&quot;</span>, book.<span style="color: #0000FF;">RetailPrice</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		var rowsAffected <span style="color: #008000;">=</span> cmd.<span style="color: #0000FF;">ExecuteNonQuery</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>rowsAffected <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;No rows inserted!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The sql to create the table is like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Book <span style="color: #66cc66;">&#40;</span>
	Id			uniqueidentifier	<span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	ISBN			varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>		<span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	Title			varchar	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span>	<span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	Publisher		varchar	<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span>	<span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	SuggestedPrice	money 			<span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">PRIMARY</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<p>Finally the Book class is this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> Book
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> Guid Id <span style="color: #000000;">&#123;</span>
		get<span style="color: #008000;">;</span>
		set<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> ISBN <span style="color: #000000;">&#123;</span>
		get<span style="color: #008000;">;</span>
		set<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Title <span style="color: #000000;">&#123;</span>
		get<span style="color: #008000;">;</span>
		set<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Publisher <span style="color: #000000;">&#123;</span>
		get<span style="color: #008000;">;</span>
		set<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span><span style="color: #008000;">?</span> RetailPrice <span style="color: #000000;">&#123;</span>
		get<span style="color: #008000;">;</span>
		set<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The reason to use SqlXXXX objects in stead of programming against interfaces like IDbConnection and IDbCommand is that I know I have a SQL Server database and will not change that in the course of this project and the fact that I have this nice simple syntax for setting parameters on the Command object:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">cmd.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">AddWithValue</span> <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@Title&quot;</span>, book.<span style="color: #0000FF;">Title</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The AddWithValue-method is not on the IDbCommand interface, but is available as a public method on the SqlCommand class.</p>
<p>The book object comes from somewhere outside of this piece of code normally, and I cannot make any assumptions on the properties that are filled or not. For example the RetailPrice property is a nullable decimal, so it might therefore be null. No problem, since the database column SuggestedPrice is also nullable.</p>
<p>No problem? No problem in Mono. Run this code in MonoDevelop and store a book with a null value for the retaill-price and the publisher. Sweet.</p>
<h3>A problem on Windows</h3>
<p>Copy this code (or the compiled assemblies) to your Windows VM and run it there. You get an exception:<br />
<a href="http://lsd.luminis.nl/wp-content/uploads/2010/08/SqlException.png"><img src="http://lsd.luminis.nl/wp-content/uploads/2010/08/SqlException.png" alt="SqlException" title="SqlException" width="452" height="90" class="alignnone size-full wp-image-991" /></a><br />
Why is that? Why would .NET complain about the Publisher parameter, suggesting it is not there? It contains a null value, right, but the parameter itself is there anyway.</p>
<p>So the message itself is confusing. It took me quite some time (and the use of the great <a href="http://sites.google.com/site/sqlprofiler/" target=_blank>SqlProfiler from AnjLab</a>) to find out that the problem was in the null values. That must have happened to other people before. So Google to the rescue? Even that took some time before I found a thread on a <a href="http://www.pcreview.co.uk/forums/thread-2140022.php" target=_blank>PC Review forum</a> from 2005 that suggested to go through all the parameters in your command to see if they are null or not and then set them to DBNull.</p>
<h3>The platform independent version</h3>
<p>Are you kidding me? Every time and everywhere I have a Command object and add Parameters to it, I must go through all the parameters like this?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>SqlParameter parameter <span style="color: #0600FF;">in</span> cmd.<span style="color: #0000FF;">Parameters</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>parameter.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        parameter.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> DBNull.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And that has been like that since .NET 2.0? No improvements in the last 5 years? Probably, the reason you haven&#8217;t bumped into this problem lately, is that you have been using NHibernate (I did) and that solved it for you. I haven&#8217;t tried it with Entity Framework yet, but I guess that would solve it too. </p>
<p>I really think Mono does a better job here. When I set a parameter on a DbCommand object to null, I expect that object to translate that null value to something that the database understands. As is does for any other value. </p>
<p><a href='http://lsd.luminis.nl/wp-content/uploads/2010/08/NullValuesInADO.zip'>Download the code</a></p>
<p>P.S. At one time in the process of finding a solution , I got so frustrated with Microsoft .NET and Visual Studio 2008 (since that somehow stopped building properly) that I decided to run Mono on Windows too. It took me 5 minutes to download and install Mono on my VM, then 5 more minutes to discover the existence of XPS (the Mono web-server) and I had my webservice running! Unfortunately, that was not an option for our production server, but to me it proved that the guys at Mono do a really good job.</p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/being-a-net-developer-in-a-mac-osx-world-storing-null-values-in-a-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Being a .NET developer in a Mac OSX world: connecting Mono to SQL Server</title>
		<link>http://lsd.luminis.eu/being-a-net-developer-in-a-mac-osx-world-connecting-mono-to-sql-server/</link>
		<comments>http://lsd.luminis.eu/being-a-net-developer-in-a-mac-osx-world-connecting-mono-to-sql-server/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 17:54:48 +0000</pubDate>
		<dc:creator>Richard de Zwart</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[MonoDevelop]]></category>
		<category><![CDATA[SQL server]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=928</guid>
		<description><![CDATA[How did I get here?
I&#8217;m a long time Windows developer.  Started with VB3 en FoxPro and such, and couldn&#8217;t imagine needing another plaform. Like the Mac that my geeky artistic brother-in-law loved so much. Windows was cool and I knew all about it and the Mac was for people that didn&#8217;t know how to [...]]]></description>
			<content:encoded><![CDATA[<h3>How did I get here?</h3>
<p>I&#8217;m a long time Windows developer.  Started with VB3 en FoxPro and such, and couldn&#8217;t imagine needing another plaform. Like the Mac that my geeky artistic brother-in-law loved so much. Windows was cool and I knew all about it and the Mac was for people that didn&#8217;t know how to use a PC.</p>
<h3>The dawn of a new era</h3>
<p>Then I came with my current employer and they provided a mobile phone and a laptop of course (like all companies in Dutch IT do). But this was about an iPhone and a MacBook Pro.<br />
I decided to leave OSX on the machine, although I could have erased the disk and installed Windows Something, it&#8217;s got an Intel processor after all.<br />
But leaving OSX on it, meant I had to install VMWare and run Windows and Visual Studio and such within that. And even though my machine is pretty fast, you notice the performance hit.</p>
<p>I kept trying, and started to like OSX and the applications on it. But what I liked most of all: hardly any updates and no reboots. I shut down my machine once a week or once every two weeks because I feel it&#8217;s something I should do. But I don&#8217;t have to: OSX keeps on running. Just close the lid, simply know that everything is suspended and know that everything will run as before when you open the lid the next day.</p>
<p>Amazing! I would shut down my Windows machine every night, just to make sure I had a stable system the next morning. My wife still has a Windows machine and to ensure that she makes backups every day, we configured the backup software to run &#8220;on system shutdown&#8221;. Because that is what you do every day, as a Windows user.</p>
<h3>And then there was Mono</h3>
<p>I&#8217;ve written about the <a href="http://www.mono-project.com/Main_Page" target=_blank>Mono project</a> before, albeit in the context of <a href="http://monotouch.net" target=_blank>MonoTouch</a> and iPhone development. Mono brings .NET to a lot of platforms, including Mac OSX. And it is really good. The Mono team is really close behind the Microsoft team in porting new API&#8217;s and Framework versions. .NET 4.0 is recently available on Windows, but Mono is already compatible.<br />
And the amazing thing is that you can take your Windows assemblies and use them straight away on OSX! That might not be very imported for assemblies that you have the sources for, but it is very convenient for third party libraries you use, like <a href="http://logging.apache.org/log4net/" target=_blank>log4net</a> (although that has a Mono version) and <a href="http://www.ayende.com/projects/rhino-mocks.aspx" target=_blank>Rhino Mocks</a>.<br />
And one of the best things the Mono team delivers is <a href="http://monodevelop.com/" target=_blank>MonoDevelop</a>: an excellent IDE for .NET development that will feel really comfortable when you come from Visual Studio.</p>
<h3>Being a .NET developer, not necessarily a Windows developer</h3>
<p>I still like the .NET framework, but I no longer feel that automatically implies I am a Windows developer. There are so many good tools that allow you to do everything on the Mac that you are normally doing on Windows.  So I decided that in the new project I recently started (in which we <em>will</em> deliver on Windows) I will try to go as far as possible to develop my code on Mac OSX.<br />
I will blog about my experiences in a series of posts. This first one is about connecting to SQL Server.</p>
<h3>I can&#8217;t do without SQL Server</h3>
<p>We will deliver our project an a SQL Server database, so it makes sense to use that during development also. It&#8217;s not that you have to, there are other options. If you have something like a nice Data Access Layer, you can run any SQLLite or MySql database and easily move to SQL Server later.<br />
There also is a <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=0d2357ea-324f-46fd-88fc-7364c80e4fdb&#038;displaylang=en">file-based no-install version of SQL Server</a> these days that might run on Mono. Haven&#8217;t tried it yet, but could be promising.<br />
But if you need SQL Server, you can still work in MonoDevelop in your comfortable Mac environment.</p>
<h3>Inside the VM</h3>
<p>SQL Server will run on Windows in your VM, but that will be the only thing you&#8217;ll need your VM for. SImply minimize the window after all the configuration is done and don&#8217;t think about it anymore.</p>
<h4>Step one: opening up your Windows</h4>
<p>You need access to your VM from your Mac, so you have to open up some things.</p>
<p>Go into Control Panel and choose  Windows Firewall. Go to the advanced tab and add some ports. Port 1433 for SQL Server over TCP/IP and port 1434 for SQL Server over UDP. Maybe you can do without the UDP version, haven&#8217;t tried yet.</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-12-at-7.45.38-PM.png"><img class="size-full wp-image-934 alignnone" title="Firewall Exceptions" src="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-12-at-7.45.38-PM.png" alt="Firewall Exceptions" width="430" height="518" /></a></p>
<h4>Step two: configure SQL Server</h4>
<p>First, make sure the SQL browser is running.</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-12-at-7.35.00-PM.png"><img class="size-full wp-image-929 alignnone" title="SQL Browser" src="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-12-at-7.35.00-PM.png" alt="SQL Browser" width="295" height="142" /></a></p>
<p>Then enable TCP/IP in the Client Protocols</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-12-at-7.35.45-PM.png"><img class="size-full wp-image-930 alignnone" title="Protocols" src="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-12-at-7.35.45-PM.png" alt="Protocols" width="601" height="165" /></a></p>
<p>And check that the Default Port is on 1433.</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-12-at-7.36.13-PM.png"><img class="size-full wp-image-932 alignnone" title="Poort " src="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-12-at-7.36.13-PM.png" alt="Poort " width="387" height="435" /></a></p>
<p>In SQL Server Management Console open the properties of the server and make sure you have mixed authentication.</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-12-at-7.48.59-PM.png"><img class="size-full wp-image-935 alignnone" title="Mixed Authentication" src="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-12-at-7.48.59-PM.png" alt="Mixed Authentication" width="705" height="218" /></a></p>
<h4>Step three: connect from your code or MonoDevelop</h4>
<p>In your connection string use the IP-address of your VMWare instance. Something like</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;connectionStrings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dossiers&quot;</span> <span style="color: #000066;">connectionString</span>=<span style="color: #ff0000;">&quot;Server=172.16.86.128;Database=mydefaultdatabase;User ID=sa;Password=bladiebla&quot;</span> <span style="color: #000066;">providerName</span>=<span style="color: #ff0000;">&quot;System.Data.SqlClient&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/connectionStrings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>That is! You can test your connection from MonoDevelop: Go to Tools/Database/Add database connection/SQL server:<br />
<img src="http://lsd.luminis.nl/wp-content/uploads/2010/07/MonoDevelopDatabaseTools.png" alt="MonoDevelopDatabaseTools" title="MonoDevelopDatabaseTools" width="661" height="179" class="alignnone size-full wp-image-955" /><br />
<img src="http://lsd.luminis.nl/wp-content/uploads/2010/07/Screen-shot-2010-07-18-at-9.33.05-PM.png" alt="Screen shot 2010-07-18 at 9.33.05 PM" title="Screen shot 2010-07-18 at 9.33.05 PM" width="612" height="379" class="alignnone size-full wp-image-956" /></p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/being-a-net-developer-in-a-mac-osx-world-connecting-mono-to-sql-server/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Windows Phone 7 Developer Hub</title>
		<link>http://lsd.luminis.eu/windows-phone-7-developer-hub/</link>
		<comments>http://lsd.luminis.eu/windows-phone-7-developer-hub/#comments</comments>
		<pubDate>Fri, 11 Jun 2010 20:43:41 +0000</pubDate>
		<dc:creator>Erik Sanders</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[mobility]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[News Item]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=913</guid>
		<description><![CDATA[Ik heb vorige week de Windows Phone 7 Developer Hub bijgewoond met de verwachting in een keer een compleet overzicht te krijgen van de nieuwe Microsoft smartphone. Ik heb niet de intentie in deze blog alles even uit te leggen, dat kan Microsoft prima zelf. Ik wil hier alleen het beeld dat ik heb gekregen [...]]]></description>
			<content:encoded><![CDATA[<p>Ik heb vorige week de Windows Phone 7 Developer Hub bijgewoond met de verwachting in een keer een compleet overzicht te krijgen van de nieuwe Microsoft smartphone. Ik heb niet de intentie in deze blog alles even uit te leggen, dat kan Microsoft prima zelf. Ik wil hier alleen het beeld dat ik heb gekregen schetsen vanuit het perspectief van de gebruiker, ontwikkelaar en business.</p>
<h2>Gebruiker</h2>
<p>Ik heb de telefoon niet zelf bedient maar heb toch een redelijke indruk gekregen. Zelf ben ik een iPhone gebruiker en zie wel wat verbeteringen en interessante ontwikkelingen</p>
<ul>
<li>Het gebruik is opgezet rond hubs. Er is bijvoorbeeld een social hub, op deze hub komt alle sociale media bij elkaar en bieden ze mogelijkheden tot integratie. Er zijn dus geen aparte ingangen meer voor linked-in, contacts, facebook e.d. maar alle informatie wordt gebundeld weergegeven.</li>
<li>Drie verplichte toetsen op de telefoon waarbij naast de enige toets op de iPhone om applicaties te starten er ook een<strong> zoek<span style="font-weight: normal;"> toets</span> </strong>is die de standaard zoek functionaliteit heeft maar ook kan worden gebruikt voor zoeken binnen de applicatie. Daarnaast is er de <strong>back </strong>toets hiermee kan je terug naar de applicatie die een andere heeft aangeroepen. Een typisch voorbeeld waar ik mij vaak aan heb geërgerd is: Je leest je mail daar staat een link in die je opent en vervolgens zit je in safari. De enige manier om die te verlaten is stoppen en je mail opnieuw opstarten.</li>
<li>Het metro concept van de user interface (Kort samengevat: beperkt grafisch en meer tekst) aangevuld met een panorama view en pivot view is wel een verfrissende aanpak waarbij je direct informatie ziet die je nodig hebt en niet alleen een menu.</li>
</ul>
<h2>Ontwikkelaar</h2>
<p>Voor een .Net ontwikkelaar is er eigenlijk niets nieuws. Je kunt namelijk gewoon ontwikkelen met je opgedane kennis in XAML (WPF en Silverlight) in je vertrouwde ontwikkelomgeving. Ik wil echter wel een aantal punten benadrukken</p>
<ul>
<li><span style="font-size: 13.3333px;">Een virtuele machine met de phone OS op de ontwikkel PC dus g</span>een emulator of simulator dus betere test omgeving</li>
<li>Naast API&#8217;s die telefoonfunctionaliteit geven zijn er ook API&#8217;s voor bijbehorende services in de cloud</li>
<li>Er is een gratis omgeving bestaande uit visual studio express voor ontwikkelaars en Blend express voor designers</li>
</ul>
<p>Het lijkt mij een eitje om de look en feel van de quote eetgids (een iphone app gemaakt door luminis) te bouwen voor W7</p>
<h2>Business</h2>
<ul>
<li>Beperkt aantal modellen en veel verplichte onderdelen zoals een grafische processor en sensoren als GPS e.d. Dit maakt de herkenbaarheid groter</li>
<li>Geen exclusieve contracten met KPN&#8217;s en de TMobile&#8217;s</li>
<li>Vergelijkbare appstore als Apple waarin een applicatie gegarandeerd binnen dagen wordt geplaatst.</li>
<li>Office applicaties en zelf een sharepoint front-end</li>
</ul>
<h2>Het evenement</h2>
<p>Het evenement zelf was wat mij betreft teleurstellend. Op een developer event verwacht ik concrete presentaties met duidelijke concepten en voorbeelden. Helaas bleven de presentaties erg oppervlakkig ook al aangemoedigd door de vele bizarre vragen die gesteld werden. Wat mij wel duidelijk is geworden dat Microsoft serieus hun best doet om weer aansluiting te vinden of zoals ze zelf beweren een voorsprong te nemen maar dat ze nog  heel goed hun best moeten doen om alles in de winkels te krijgen voor de kerst.</p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/windows-phone-7-developer-hub/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building iPad applications using MonoTouch: the UISplitView</title>
		<link>http://lsd.luminis.eu/building-ipad-applications-using-monotouch-the-uisplitview/</link>
		<comments>http://lsd.luminis.eu/building-ipad-applications-using-monotouch-the-uisplitview/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 05:00:03 +0000</pubDate>
		<dc:creator>Richard de Zwart</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[mobility]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[MonoDevelop]]></category>
		<category><![CDATA[monotouch]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=625</guid>
		<description><![CDATA[First of all, I bow deeply to the people that bring us MonoTouch. It was less than 2 days after the introduction of the iPad and the release of a beta of the iPhone/iPad SDK that a version of MonoTouch (and MonoDevelop) was available that covers the new API.
To build iPad apps, you need a [...]]]></description>
			<content:encoded><![CDATA[<p>First of all, I bow deeply to the people that bring us MonoTouch. It was less than 2 days after the introduction of the iPad and the release of a beta of the iPhone/iPad SDK that a version of MonoTouch (and MonoDevelop) was available that covers the new API.</p>
<p>To build iPad apps, you need a couple of things that are all listed on the <a href="http://monotouch.net/ipad" target="_blank">MonoTouch iPad page</a>.</p>
<p>I couldn&#8217;t wait to build something that used the way bigger UI-surface that the iPad has. The first thing that attracted my attention when I fired up the Interface Builder was the UISplitViewController.<br />
The idea behind the <a href="https://developer.apple.com/iphone/prerelease/library/documentation/General/Conceptual/iPadProgrammingGuide/UserInterface/UserInterface.html" target="_blank">SplitView</a> is that you have some navigation on the left (like a NavigationController, or just a TableViewController) and a data view on the right. That is, only when the display is in landscape mode. As soon as you turn the iPad (the iPad SImulator, of course) to portrait, the left side disappears and all the screen is available to the data view.<br />
That behaviour is entirely taken care of by the UISplitViewController, at least if you stick to the conventions!</p>
<p>I decided to make a simple app with a list of places on the left side, and a map-view on the right:</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/01/Landscape.png"><img class="alignleft size-medium wp-image-626" title="Landscape" src="http://lsd.luminis.nl/wp-content/uploads/2010/01/Landscape-300x232.png" alt="Landscape" width="352" height="272" /></a><a href="http://lsd.luminis.nl/wp-content/uploads/2010/01/Portrait.png"><img class="size-medium wp-image-627 alignleft" title="Portait version of the UI" src="http://lsd.luminis.nl/wp-content/uploads/2010/01/Portrait-233x300.png" alt="Portait version of the UI" width="233" height="300" /></a></p>
<h3>The obvious start</h3>
<p>When you start a new iPad application from the New Project menu, you get the well-known set-up of files in your project. Double-click the MainWindow.xib to fire up Interface Builder. Then drag the Split View Controller from the Library Window and drop it below the Window object in de MainWindow:<br />
<a href="http://lsd.luminis.nl/wp-content/uploads/2010/01/MainWindow1.png"><img class="alignleft size-medium wp-image-629" title="MainWindow" src="http://lsd.luminis.nl/wp-content/uploads/2010/01/MainWindow1-300x230.png" alt="MainWindow" width="300" height="230" /></a></p>
<p>As you can see, you get a lot for free, including stuff you don&#8217;t want. Now it gets less obvious. After clicking Cmd-Backspace a thousand times and positioning my cursor everywhere, I had an epiphany. Since the SplitViewController won&#8217;t work without two other controllers I had to add something new before I could remove the old!</p>
<p>No kidding, that was really the solution. So  I added a UITableViewController, Interface Builder magically removed the default NavigationController, and I added a MKMapView to the view-controller on the right.</p>
<p>This is the result:</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/01/SplitView.png"><img class="alignleft size-medium wp-image-632" title="SplitView" src="http://lsd.luminis.nl/wp-content/uploads/2010/01/SplitView-300x233.png" alt="SplitView" width="300" height="233" /></a> I then added outlets to the AppDelegate for the map, the tableview and the splitview:</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/01/Outlets.png"><img class="alignleft size-full wp-image-633" title="Outlets" src="http://lsd.luminis.nl/wp-content/uploads/2010/01/Outlets.png" alt="Outlets" width="290" height="238" /></a></p>
<h3>The less obvious code</h3>
<p>To make the controllers react properly when the orientation of the UI changes, you need to override the ShouldAutorotateToInterfaceOrientation() method in each of your controllers. How do you do that?<br />
The first step is to add a class to your project, make it inherit from your controller (a UITableViewController in my case) and override the ShouldAutorotateToInterfaceOrientation() method as below:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PlacesController <span style="color: #008000;">:</span> UITableViewController
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> PlacesController <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> ShouldAutorotateToInterfaceOrientation <span style="color: #000000;">&#40;</span>UIInterfaceOrientation toInterfaceOrientation<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Repeat the step for the second controller:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Map <span style="color: #008000;">:</span> UIViewController
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> Map <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> ShouldAutorotateToInterfaceOrientation <span style="color: #000000;">&#40;</span>UIInterfaceOrientation toInterfaceOrientation<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This should still compile. But it doesn&#8217;t work yet.</p>
<h3>The even less obvious link from the code to the UI</h3>
<p>The objects in the Interface Builder are standard objects. They need to be of the types that we just defined, to make the overridden methods work. See we need to make our own classes known to Interface Builder. That&#8217;s done by adding a Register-atribute and overriding the constructor with one that accepts an IntPtr.<br />
The Map class changed in something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Register<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Map&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Map <span style="color: #008000;">:</span> UIViewController
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> Map <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> Map<span style="color: #000000;">&#40;</span>IntPtr p<span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span>p<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> ShouldAutorotateToInterfaceOrientation <span style="color: #000000;">&#40;</span>UIInterfaceOrientation toInterfaceOrientation<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The final step is setting the right class on the controller. Go to Interface Builder, select the UIViewController (e.g.) in the MainWindow, then go to the Inspector Window and type your classname over the default one:</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/01/Inspector.png"><img class="alignleft size-full wp-image-638" title="Inspector" src="http://lsd.luminis.nl/wp-content/uploads/2010/01/Inspector.png" alt="Inspector" width="286" height="243" /></a></p>
<p>And then it works! The map will re-orientate itself when the iPad is turned, and the Table View appears and disappears.<br />
Of course you want some location data in the table and the map to show the locations when clicked on, but that&#8217;s all in the <a href="http://lsd.luminis.nl/wp-content/uploads/2010/04/SplitViewDemo.zip">code you can download</a>.</p>
<p>Enjoy making software for a device that makes you need to rethink the way you always made software&#8230;!!!</p>
<p>Mmmmh. re-reading that last line, I realize it&#8217;s a little hard to read. What I meant was something like <a href="http://joehewitt.com/post/ipad/" target="_blank">Joe Hewitt</a> wrote.</p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/building-ipad-applications-using-monotouch-the-uisplitview/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Presentaties AgileMDD kennissessie &#8211; 30 maart 2010</title>
		<link>http://lsd.luminis.eu/agilemdd-300310/</link>
		<comments>http://lsd.luminis.eu/agilemdd-300310/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 12:28:56 +0000</pubDate>
		<dc:creator>Richard van der Laan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[social]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[AgileMDD]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[kennissessie]]></category>
		<category><![CDATA[mda]]></category>
		<category><![CDATA[mdd]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=759</guid>
		<description><![CDATA[Op 30 maart organiseerde luminis samen met ArchitecIT een kennissessie over model-driven development. In deze kennissessie stond het delen van praktijkervaringen centraal. De presentaties van deze avond zijn inmiddels online beschikbaar.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-300310-5.jpg"><img class="aligncenter size-full wp-image-834" title="AgileMDD-300310-5" src="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-300310-5.jpg" alt="" width="638" height="235" /></a></p>
<p>Op 30 maart organiseerde luminis samen met <a href="http://www.architecit.nl">ArchitecIT</a> een kennissessie over model-driven development. Aan de hand van vijf verschillende thema&#8217;s deelden sprekers van diverse organisaties hun praktijkervaringen met MDD. De volgende organisaties waren als deelnemer vertegenwoordigd: <strong>ArchitecIT, Delphino Consultancy, luminis, Ministerie van Defensie, Nedap, Nuon, PANalytical, Radboud Universiteit Nijmegen, Sogeti, Tennet en Thales.</strong></p>
<p>De presentaties van deze avond zijn inmiddels online beschikbaar en kunnen hieronder worden gedownload.</p>
<p><a href="http://www.agilemdd.nl"></a><a href="http://www.agilemdd.nl"><img class="alignleft size-full wp-image-509" title="agilemdd_logo" src="http://lsd.luminis.nl/wp-content/uploads/2009/09/agilemdd_logo.png" alt="agilemdd_logo" width="141" height="45" /></a><br />
<strong> </strong><br />
<strong> </strong><br />
<strong> </strong></p>
<p>In de praktijk zijn er bij softwareontwikkeling nog veel communicatie (overdrachtsmomenten) en bestaan de meeste ontwikkeltaken uit veel handwerk. Op basis van een MDD aanpak kunnen ontwikkeltaken worden geautomatiseerd en kan de onderlinge communicatie worden verbeterd. Hierbij is het echter wel belangrijk om te weten hoe MDD het beste kan worden toegepast en wat hierbij de meest voorkomende valkuilen zijn. Vanuit onze AgileMDD filosofie moet bij model-driven development een pragmatische en doelgerichte aanpak vooral centraal staan. Zo kan de bestaande ontwikkelkracht in de organisatie slimmer worden ingezet.<br />
<img class="alignright size-full wp-image-762" src="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-300310-1.jpg" alt="" width="160" height="230" /></p>
<p><strong></strong><br />
<strong>Programma kennissessie 30 maart:</strong></p>
<ul>
<li><a href="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-kennissessie-Trends-en-ontwikkelingen-Deel-1.pdf">Agile MDD: huidige trends en ontwikkelingen &#8211; Deel 1 (Richard van der Laan)</a></li>
<li><a href="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-kennissessie-Trends-en-ontwikkelingen-Deel-2.pdf">Agile MDD: huidige trends en ontwikkelingen &#8211; Deel 2 (Tony Sloos)</a></li>
<li><a href="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-kennissessie-Realtime-heterogeneous-systems.pdf">Thales case: MDA in realtime heterogene systemen (Rene van Hees, Alexander Broekhuis)</a></li>
<li><a href="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-kennissessie-DSL-toolkit.pdf">Defensie case: Microsoft DSL Toolkit in de praktijk (Gerrit Jan Timmermans, Erik Sanders)</a></li>
<li><a href="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-kennissessie-MDSD-en-software-architectuur.pdf">MDD en software architectuur (Angelo Hulshout)</a></li>
<li><a href="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-kennissessie-MDE-Opportunities-and-Risks.pdf">MDD kansen en risico&#8217;s (Andriy Levytskyy)</a></li>
</ul>
<p>Wil je op de hoogte blijven van aankomende AgileMDD sessies of geïnteresseerd in advies op maat? Neem dan contact op met Inge Dokter (<a href="mailto:inge.dokter@luminis.nl?SUBJECT=Interesse%20in%20AgileMDD">inge.dokter@luminis.nl</a>) of bel 026-3653470.</p>
<table>
<td><img class="alignleft size-full wp-image-773" src="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-300310-4.jpg" alt="" width="276" height="187" /><img class="alignright size-full wp-image-780" title="Discussie resultaat vorige MDD kennissessie" src="http://lsd.luminis.nl/wp-content/uploads/2010/04/AgileMDD-300310-3.jpg" alt="Discussie resultaat vorige MDD kennissessie" width="305" height="187" /></td>
</table>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/agilemdd-300310/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC: Editor Templates</title>
		<link>http://lsd.luminis.eu/asp-net-mvc-editor-templates/</link>
		<comments>http://lsd.luminis.eu/asp-net-mvc-editor-templates/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 11:09:17 +0000</pubDate>
		<dc:creator>Richard de Zwart</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=690</guid>
		<description><![CDATA[Last week I sat with a colleague who is building an ASP.NET MVC application. Since I&#8217;m currently on a not-so-interesting application, I was really jealous and decided to out-smart him, showing off with some hard-core MVC code.
Here we go.
What&#8217;s your problem, dude?
Well, we are all very Web 2.0 and so, using Ajax and jQuery and [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I sat with a colleague who is building an ASP.NET MVC application. Since I&#8217;m currently on a not-so-interesting application, I was really jealous and decided to out-smart him, showing off with some hard-core MVC code.</p>
<p>Here we go.</p>
<h3>What&#8217;s your problem, dude?</h3>
<p>Well, we are all very Web 2.0 and so, using Ajax and jQuery and maybe even single-page-applications, but on almost any site I fill in a form, a textbox is just a plain textbox. You can type text, like digits and characters, even if they need only my age. Come-on guys, an age is expressed as a number, so why allow me to type in anything else but digits?<br />
Sure, you validate at the server and maybe even at the client, but still I can make errors that I have to fix later. Please, help me by preventing the error up-front.</p>
<h3>The solution, part 1: jQuery-plugin</h3>
<p>The solution starts with a nice jQuery-plugin by Paulo P. Marinas called <a href="http://www.itgroup.com.ph/alphanumeric/" target="_blank">jQuery AlphaNumeric</a>. There is also a nice <a href="http://plugins.jquery.com/project/maskedinput" target="_blank">MaskedInput-plugin</a>, but I found that too limiting.<br />
It is simple to hook it up to my textbox:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#mytextboxid&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">numeric</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And voila, nothing but digits allowed!</p>
<p>Cool, I want that on all the fields that I know are numbers, but I surely don&#8217;t want to repeat the above Javascript.</p>
<h3>The solution, part 2: EditorFor</h3>
<p>Starting with MVC 2 there is an Html-helper called Html.EditorFor(). It uses a lambda as a parameter and derives a lot of interesting information from that. If you type</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;">        <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">EditorFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>model <span style="color: #006600; font-weight: bold;">=&gt;</span> model.<span style="color: #9900cc;">Length</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>You get code generated like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;editor-field&quot;&gt;
&lt;input id=&quot;Length&quot; name=&quot;Length&quot; type=&quot;text&quot; value=&quot;7&quot; /&gt;&lt;/div&gt;</pre></div></div>

<p>De ViewEngine determines that you have a Integer field, generates a textbox with the name/id of the field and sets the value. If you have client-side data-validation on, you get the extra span-tag for the validation message. More on that in a minute. Maybe this doesn&#8217;t look like much, but you get different output for different field types; like radio-buttons for a Boolean field and appropriate formatting for a Decimal field.<br />
The EditorForModel() method even generates code for all the fields on your class.</p>
<h3>The solution, part 3: Turning validation on</h3>
<p>I was hoping that the EditorFor() method would be all I needed. It supports validation, both client-side and server-side, and (as argued before) what better client-side validation then preventing false input to begin with!<br />
Alas, it doesn&#8217;t do that. But in combination with DataAnnotations (<a href="http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx" target="_blank">Scott Guthrie has a nice blog about that</a>) it adds range-checks to your Integer-field. So if I can limit the characters that can be typed into my textbox with a line of jQueury and can check for the upper- and lower-limit of the value with client-side validation, then I&#8217;m happy!</p>
<p>But that means that I have to change the behavior of the EditorFor() method. Can that be done? Well&#8230;&#8230;, sort of.</p>
<h3>The solution, part 4: Convention over configuration</h3>
<p>The nice thing about the MVC framework is that it takes convention over configuration, meaning you do not have to configure anything to get a working application, as long as you adhere to the rules/conventions. Nothing new for Ruby on Rails programmers maybe, but for me as a long-time Microsoft-ee it is close to revolutionary.</p>
<p>In this case, the interesting convention is that there can be a EditorTemplates sub-directory in my View directory. If it is in a normal View directory it works for only those Views, but if you put it in the Shared directory it works for all your views:<br />
<a href="http://lsd.luminis.nl/wp-content/uploads/2010/03/SharedViews.png"><img class="size-full wp-image-695 alignnone" title="SharedViews" src="http://lsd.luminis.nl/wp-content/uploads/2010/03/SharedViews.png" alt="SharedViews" width="227" height="181" /></a></p>
<p>If you have a user-control in there named Decimal.ascx it will be used everywhere the EditorFor() encounters a Decimal property. There is a <a href="http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html">bunch of default user-controls</a> that are described by Brad Wilson.</p>
<h3>What I would have liked to be &#8220;The solution, part 5: Overloading the decimal template&#8221;</h3>
<p>Since I like the server-side formatting of the default template, and only want to add some jQuery, I would have liked to do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">@</span> Control Language<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;C#&quot;</span> AutoEventWireup<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;true&quot;</span> Inherits<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;System.Web.Mvc.ViewUserControl&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">EditorFor</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
&lt;script type=&quot;text/javascript&quot;&gt;
    $(&quot;#mytextboxid&quot;).numeric();
&lt;/script&gt;</pre></div></div>

<p>I simply delegate to the original implementation, passing in the data I have available. But that doesn&#8217;t work since the EditorFor() expects a lambda as a first parameter. And I don&#8217;t know any way to convert the data I have at that point in time to a lambda. If you know, please tell me and you&#8217;ll be my hero (hmmmm, I hope it&#8217;s not going to be that colleague coming up with the solution, that would really ruin my post&#8230;).</p>
<h3>The actual &#8220;The solution, part 5: Overloading the decimal template&#8221;</h3>
<p>The best I could do was copying the default template for the Decimal from the blogpost of Brad Wilson. Yes that&#8217;s a shame, copying code, having to maintain code that isn&#8217;t even mine, not profiting of the improvements Microsoft makes in the default template. Nevertheless, here it is:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">@</span> Control Language<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;C#&quot;</span> AutoEventWireup<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;true&quot;</span> Inherits<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;System.Web.Mvc.ViewUserControl&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
&lt;script runat=&quot;server&quot;&gt;
    private object ModelValue {
        get {
            if (ViewData.TemplateInfo.FormattedModelValue == ViewData.ModelMetadata.Model) {
                return String.Format(System.Globalization.CultureInfo.CurrentCulture,
                                     &quot;{0:0.00}&quot;, ViewData.ModelMetadata.Model);
            }
&nbsp;
            return ViewData.TemplateInfo.FormattedModelValue;
        }
    }
&lt;/script&gt;
<span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">TextBox</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&quot;</span>, ModelValue, <span style="color: #0000ff; font-weight: bold;">new</span> <span style="color: #006600; font-weight:bold;">&#123;</span> <span style="color: #006600; font-weight: bold;">@</span><span style="color: #0000ff; font-weight: bold;">class</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;text-box single-line&quot;</span> <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
&lt;script type=&quot;text/javascript&quot;&gt;
    $('#<span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> ViewData.<span style="color: #9900cc;">ModelMetadata</span>.<span style="color: #9900cc;">PropertyName</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>').numeric({ allow: '<span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> System.<span style="color: #9900cc;">Globalization</span>.<span style="color: #9900cc;">CultureInfo</span>.<span style="color: #9900cc;">CurrentCulture</span>.<span style="color: #9900cc;">NumberFormat</span>.<span style="color: #9900cc;">CurrencyDecimalSeparator</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>'});
&lt;/script&gt;</pre></div></div>

<p>So, there is some server-side code that I copied, and then some client-side code I added myself.<br />
There are two interesting things to mention about my own single line of code:</p>
<ul>
<li>The jQuery selector references the name of the property, thus guaranteeing that the javascript is coupled to the right text-box. Other solutions make you use a fixed class or prefix or postfix, but that only opens the possibility of name clashes. The name-property is available in the model-metadata. See the Bradd Wilson series mentioned above for more info</li>
<li>The allowed character (the decimal separator) for entering money is retrieved from the CurrentCulture and not hard-coded. Of course.</li>
<p>As ScottGu would say: hope this helps.</ul>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/asp-net-mvc-editor-templates/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
