<?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/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://lsd.luminis.eu</link>
	<description></description>
	<lastBuildDate>Wed, 28 Dec 2011 20:44:50 +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>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>4</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>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>
		<item>
		<title>Building iPhone applications using MonoTouch, howto: using a view without a controller</title>
		<link>http://lsd.luminis.eu/building-iphone-applications-using-monotouch-howto-using-a-view-without-a-controller/</link>
		<comments>http://lsd.luminis.eu/building-iphone-applications-using-monotouch-howto-using-a-view-without-a-controller/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 20:30:13 +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[Interface Builder]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[MonoDevelop]]></category>
		<category><![CDATA[monotouch]]></category>
		<category><![CDATA[NIB]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=615</guid>
		<description><![CDATA[Of course, a view without a controller is not very useful in itself, but in this post I want to show you how I solved a small re-usability problem.
I&#8217;m working on a small open-source project on Codeplex that aims to build a player for the Hanselminutes podcasts.  It is nothing very special, but it [...]]]></description>
			<content:encoded><![CDATA[<p>Of course, a view without a controller is not very useful in itself, but in this post I want to show you how I solved a small re-usability problem.</p>
<p>I&#8217;m working on a small open-source project on <a href="http://hanselminutesiphone.codeplex.com" target="_blank">Codeplex</a> that aims to build a player for the <a href="http://www.hanselminutes.com/"  target="_blank">Hanselminutes podcasts</a>.  It is nothing very special, but it gives me a nice playground to find out new stuff.</p>
<p>So there is a &#8220;Loading playlist&#8221; message and a &#8220;Buffering audio&#8221; message displayed at the appropriate moments. You can do that in a lot of ways, but in this application, a semi-transparent view was chosen that is overlaying the current view. It has a text (of course) and a UIActivityIndicatorView (who comes up with these names????).</p>
<p>I try to follow three rules when building the UI:</p>
<ol>
<li>All the UI is done with the Interface Builder. That allows me to leave the actual design to someone who knows designing.</li>
<li>Every view is in a separate NIB. That way not all the UI has to be loaded at startup, which improves user-perceived performance</li>
<li>Loosely couple the view, to make reuse easier. That means a simple interface (as in &#8216;programming interface&#8217;) to the rest of the world, and all the view-related code inside the NIB</li>
</ol>
<p>In this case I want a view that can be called from different controllers (re-usability), so when I add a file to my MonoDevelop project I choose &#8220;View Interface Definition&#8221;:</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/01/Screen-shot-2010-01-25-at-8.34.03-PM.png"><img class="alignleft size-medium wp-image-616" style="margin-left: 10px; margin-right: 10px;" title="Screen shot 2010-01-25 at 8.34.03 PM" src="http://lsd.luminis.nl/wp-content/uploads/2010/01/Screen-shot-2010-01-25-at-8.34.03-PM-300x196.png" alt="Screen shot 2010-01-25 at 8.34.03 PM" width="300" height="196" /></a></p>
<p>It&#8217;s no so different form what you do (at least, what I do) most of the time when you choose View Interface Definition with Controller. You add your UI-elements, define some outlets, but then  you want to activate this view from some controller, any controller.</p>
<p>What I came up with, is the following.</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> UIHelper
<span style="color: #000000;">&#123;</span>
  UIViewController _controller<span style="color: #008000;">;</span>
  BusyView _busyView<span style="color: #008000;">;</span>
  <span style="color: #0600FF;">public</span> UIHelper <span style="color: #000000;">&#40;</span>UIViewController controller<span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    _controller <span style="color: #008000;">=</span> controller<span style="color: #008000;">;</span>
    NSArray views <span style="color: #008000;">=</span> NSBundle.<span style="color: #0000FF;">MainBundle</span>.<span style="color: #0000FF;">LoadNib</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;BusyView&quot;</span>, _controller, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    _busyView <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BusyView<span style="color: #000000;">&#40;</span>views.<span style="color: #0000FF;">ValueAt</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    _controller.<span style="color: #0000FF;">View</span>.<span style="color: #0000FF;">AddSubview</span><span style="color: #000000;">&#40;</span>views<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    _controller.<span style="color: #0000FF;">View</span>.<span style="color: #0000FF;">SendSubviewToBack</span><span style="color: #000000;">&#40;</span>_busyView<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>

<p>It is a helper class that loads the view at construction time. It gets passed in the controller that will display the view. Then I load the NIB where the view is in. This gives me back an array. Of what? Well as the <a href="http://developer.apple.com/IPhone/library/documentation/UIKit/Reference/NSBundle_UIKitAdditions/Introduction/Introduction.html" target="_blank">documentation</a> says:</p>
<blockquote><p>An array containing the top-level objects in the nib file. The array does not contain references to the File’s Owner or any proxy objects; it contains only those objects that were instantiated when the nib file was unarchived. You should retain either the returned array or the objects it contains manually to prevent the nib file objects from being released prematurely.</p></blockquote>
<p>So you get the top-elements (actually: the IntPtr&#8217;s of them) from the NIB. In a file with only one view the first one is probably (&#8230;fingers crossed) the right one.<br />
I add the view to the SubViews of the controller and make sure it does not show before it was meant to.</p>
<h3>Showing the view</h3>
<p>Whenever I need the view to display I call:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	_busyView.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span>message<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	_controller.<span style="color: #0000FF;">View</span>.<span style="color: #0000FF;">BringSubviewToFront</span><span style="color: #000000;">&#40;</span>_busyView<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The first line calls a method on the partial class in the NIB to set the text of a label. The second line makes the view visible.</p>
<h3>What&#8217;s not to like about this solution</h3>
<p>Well, the magical string with the name of the NIB, &#8220;BusyView&#8221;. And the magical number 0 the denotes the right object in the list of objects in the NIB.</p>
<p>Any ideas for improvement? Please react!</p>
<p><a href='http://lsd.luminis.nl/wp-content/uploads/2010/01/NibDemo.zip'>Download the code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/building-iphone-applications-using-monotouch-howto-using-a-view-without-a-controller/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Building iPhone applications using MonoTouch, part 5: software design considerations</title>
		<link>http://lsd.luminis.eu/building-iphone-applications-using-monotouch-part-5-software-design-considerations/</link>
		<comments>http://lsd.luminis.eu/building-iphone-applications-using-monotouch-part-5-software-design-considerations/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 21:26:55 +0000</pubDate>
		<dc:creator>Richard de Zwart</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[mobility]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[MonoDevelop]]></category>
		<category><![CDATA[monotouch]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=574</guid>
		<description><![CDATA[(English) In this post I try to determine where some of my code should go and how I can keep the my application loosely coupled and tightly coherent.]]></description>
			<content:encoded><![CDATA[<p>In the previous 4 posts (<a href="http://lsd.luminis.nl/building-iphone-applications-using-monotouch-part-4-the-uisearchdisplaycontroller/">4</a>, <a href="http://lsd.luminis.nl/building-iphone-applications-using-monotouch-part-3-the-interface-builder/">3</a>, <a href="http://lsd.luminis.nl/building-iphone-applications-using-monotouch-2/">2</a>, <a href="http://lsd.luminis.nl/building-iphone-applications-using-monotouch-1/">1</a>) I gave a lot of attention to the overall structure of an iPhone application. In this post I want to talk about  a topic that is more concerned with general software design issues: where do I do what?</p>
<h3>Get SOLID</h3>
<p>There are two things that I always try to keep in mind when making software: loose coupling and tight cohesion. In other words:</p>
<ol>
<li>make sure your classes don&#8217;t know things that they don&#8217;t need to know</li>
<li>make sure your classes are good at one thing and one thing only</li>
</ol>
<p>These guidelines are part of the <a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">5 SOLID principles</a> of class design by <a href="http://www.objectmentor.com/omTeam/martin_r.html">Uncle Bob Martin</a>:</p>
<ol>
<li>Single Responsibility Principle</li>
<li>Open Closed Principle</li>
<li>Liskov Substitution Principle</li>
<li>Interface Segregation Principle</li>
<li>Dependency Inversion Principle</li>
</ol>
<p>This stuff is really interesting and sort-of scientific, but it is also like making your database comply to the 5-th Normal Form: nobody does that. You&#8217;re happy with a 3rd Normal Form database. So I&#8217;m happy when I see code that complies with at least two of the above principles.</p>
<p>So what I do when building my iPhone apps is constantly asking myself: should this code be in this place? Sometimes I know right away that the answer is No, but still leave it there until I have a better idea on where to put it then. And with the (for me) rather unusual structure that the Cocao Framework forces upon me, it may take some time before I eventually find out where to put it.</p>
<p>Let me give you an example.</p>
<p>When you want to load data into a UITableView you must create a UITableViewDataSource and assign that to the DataSource property of your UITableViewController. Like this:</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;">override</span> <span style="color: #FF0000;">bool</span> FinishedLaunching <span style="color: #000000;">&#40;</span>UIApplication app, NSDictionary options<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	tableView.<span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	window.<span style="color: #0000FF;">AddSubview</span> <span style="color: #000000;">&#40;</span>tableView<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	window.<span style="color: #0000FF;">MakeKeyAndVisible</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The class instantiated at line 3 is something like this:</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> LeesPlankjeDataSource <span style="color: #008000;">:</span> UITableViewDataSource
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> woordjes <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;aap&quot;</span>, <span style="color: #666666;">&quot;noot&quot;</span>, <span style="color: #666666;">&quot;mies&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> UITableViewCell GetCell <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		UITableViewCell cell <span style="color: #008000;">=</span> tableView.<span style="color: #0000FF;">DequeueReusableCell</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>cell <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			cell <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UITableViewCell<span style="color: #000000;">&#40;</span>UITableViewCellStyle.<span style="color: #0600FF;">Default</span>, <span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		cell.<span style="color: #0000FF;">TextLabel</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> woordjes<span style="color: #000000;">&#91;</span>indexPath.<span style="color: #0000FF;">Row</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> cell<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">int</span> RowsInSection <span style="color: #000000;">&#40;</span>UITableView tableview, <span style="color: #FF0000;">int</span> section<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> woordjes.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>On  line 3 you see the actual &#8220;data store&#8221;, and on line 4 an important override. This method gets called by Cocoa when loading data in your UITableView. So the controller has a data source and the appropriate methods get called by the framework.</p>
<h3>On to a more realistic implementation</h3>
<p>If I want to advance my class a bit, I could imagine that the data is not a fixed array of strings, but gets passed in at construction time. Maybe I read from a file or from a URL.</p>
<p>That changes my class to this:</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> LeesPlankjeDataSource <span style="color: #008000;">:</span> UITableViewDataSource
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> _dataStorage<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> data<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_dataStorage <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> UITableViewCell GetCell <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		UITableViewCell cell <span style="color: #008000;">=</span> tableView.<span style="color: #0000FF;">DequeueReusableCell</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>cell <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			cell <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UITableViewCell<span style="color: #000000;">&#40;</span>UITableViewCellStyle.<span style="color: #0600FF;">Default</span>, <span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		cell.<span style="color: #0000FF;">TextLabel</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> _dataStorage<span style="color: #000000;">&#91;</span>indexPath.<span style="color: #0000FF;">Row</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> cell<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">int</span> RowsInSection <span style="color: #000000;">&#40;</span>UITableView tableview, <span style="color: #FF0000;">int</span> section<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> _dataStorage.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And the main.cs gets these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	<span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> woordjes <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">ReadAllLines</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;woordjes.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	tableView.<span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span>woordjes<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Make sure when you have files that you want to be deployed with your app to set the build-action on the file to &#8220;content&#8221;:</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2009/11/BuildAction.png"><img class="alignleft size-medium wp-image-577" style="margin-left: 5px; margin-right: 5px;" title="BuildAction" src="http://lsd.luminis.nl/wp-content/uploads/2009/11/BuildAction-300x255.png" alt="BuildAction" width="300" height="255" /></a>That&#8217;s all neat. The DataSource gets it data from the outside and doesn&#8217;t care if it comes from a file or a network-connection.</p>
<p>So now we want some action when the user taps a row. You have no choice but to implement this in a delegate (a UITableViewDelegate of course) and then override the RowSelected() method. This method is called for you by Cocoa and as parameters you get a UITableView that the tapping happened on and the Row number that was tapped:</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> LeesPlankjeViewDelegate <span style="color: #008000;">:</span> UITableViewDelegate
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> RowSelected <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>But what can I do in this method? Let&#8217;s say I want to do a MessageBox-ish thing to show what word was chosen. All I have is an index to the row in my data source. But I don&#8217;t have the data source itself in this class. I need some way to acces my original data store.</p>
<p>I could do somehting like this:</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;">override</span> <span style="color: #0600FF;">void</span> RowSelected <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #FF0000;">string</span> message <span style="color: #008000;">=</span> tableView.<span style="color: #0000FF;">DataSource</span>.<span style="color: #0000FF;">GetCell</span><span style="color: #000000;">&#40;</span>tableView,  indexPath<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">TextLabel</span>.<span style="color: #0000FF;">Text</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var alert <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UIAlertView <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span>, message, <span style="color: #0600FF;">null</span>, <span style="color: #666666;">&quot;OK&quot;</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		alert.<span style="color: #0000FF;">Show</span> <span style="color: #000000;">&#40;</span><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>

<p>So I ask the TableView for its data source, then ask the data source to give me the cell that is on the given index, and then ask the cell for the text of the label. It works, but it is butt-ugly. Why? Because now the delegate knows about the data source too. And I think it shouldn&#8217;t, because all the delegate needs to do is handle UI-interactions from the user. It should say &#8220;Hey, someone tapped me on this row, do something with it&#8221; and then leave the actual work to someone else.</p>
<p>I think it would be reasonable to leave the work to the controller. For me, a controller is always the man-in-the-middle, doing the real work brokering between the model and the view. But if I choose that, I have to pass in the controller when constructing the Delegate, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">tableView.<span style="color: #FF0000;">Delegate</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeViewDelegate<span style="color: #000000;">&#40;</span>tableViewController<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>And since the tableViewController is only known so far in the Interface Builder, I have to make the controller available in my code by creating an outlet. Sigh&#8230;. even more code in my FinishedLaunching() method. I don&#8217;t want that, I want to hook up UI-parts with each other using the Interface Builder, not in code.</p>
<p>So, what to do now? I don&#8217;t know yet. Let me first deal with a problem that I didn&#8217;t tell you about yet. It is in the code of the LeesPlankjeDataSource. I gave my LeesPlankjeDataSource a constuctor that accepts a string array, thereby enabling me to pass in the data that the data source needs to build a UITableView from.</p>
<p>The problem is, that the UISearchDisplayController re-uses my UITableViewController, including the DataSource. When I click search, it first simply overlays my view, but when I start typing in the search box, the ShouldReloadForSearchString () method gets called and that one resets the SearchResultDataSource with a filtered version of my LeesPlankjeDataSource:</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;">override</span> <span style="color: #FF0000;">bool</span> ShouldReloadForSearchString <span style="color: #000000;">&#40;</span>UISearchDisplayController controller, <span style="color: #FF0000;">string</span> forSearchString<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;In ShouldReloadForSearchtring&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	controller.<span style="color: #0000FF;">SearchResultsDataSource</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span>forSearchString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And how am I gonna feed this baby with the right data? How is the SearchResultsDataSource going to get a filtered list of the words in my &#8220;woordjes.txt&#8221; file? I chose to filter by using an overloaded constructor, but I could move that code to a normal method. That allows me to use the other constructor, passing in the data as before in the main.cs:</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;">override</span> <span style="color: #FF0000;">bool</span> ShouldReloadForSearchString <span style="color: #000000;">&#40;</span>UISearchDisplayController controller, <span style="color: #FF0000;">string</span> forSearchString<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;In ShouldReloadForSearchString&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	var woordjes <span style="color: #008000;">=</span> <span style="color: #008000;">?????????</span>
	var data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span>woordjes<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	controller.<span style="color: #0000FF;">SearchResultsDataSource</span> <span style="color: #008000;">=</span> data.<span style="color: #0000FF;">FilterOn</span><span style="color: #000000;">&#40;</span>forSearchString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>But where am I gonna get the &#8220;woordjes&#8221; variable from? Should I load the file again, like I did in main.cs? Or maybe my DataSource should know something about the model? In that case I will not pass data from the outside, but let the DataSource find out itself where to get the data. But that is so against the Dependency Inversion Principle (or Inversion of Control)! If classes depend on other classes, these dependencies had best be passed in from the outside, probably on construction time.</p>
<p>Shoot, I love the IoC pattern. Should I let it go, for the sake of simplicity? After all, I argued before that even the MVC-pattern was maybe to much of a burden for the simple applications we build on the iPhone most of the time.</p>
<p>For tonight, I give up. The <a href="http://lsd.luminis.nl/wp-content/uploads/2009/11/SearchProblems2.zip">code you can download</a> contains the solution that goes against IoC, but works none the less.</p>
<p>I would love to hear your ideas about how to build iPhone applications that are tightly coherent and loosely coupled. I know that we can get in some philosophical or religious discussions, but we&#8217;ll see what to do then. I just want to learn from you guys, as much as I want to teach you where I can.</p>
<p>P.S.<br />
Thanks to <a href="http://www.alexyork.net/blog/post/UINavigationController-with-MonoTouch-Building-a-simple-RSS-reader-Part-1.aspx">Alex York&#8217;s excellent post</a> (see his comment below) I was able to improve on my code. I had seen the idea of nesting the DataSource and Delegate into the UITableViewController before, but didn&#8217;t like it then. That dislike mostly came from the fact that you have to inherit from another class. There is no tighter couling between two classes then inheritance, so I believe you should only use it when absolutely necessary. Well, by now I think that it is absolutely necessary to inherit from the classes in the Cocoa framework. It is simply the way you work.</p>
<p>When working on the new solution I also renamed some classes (no more Dutch names, only Dutch words in the data&#8230;) and added a class that implements the model:</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> WordsModel
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> List _dataStorage<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> WordsModel <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_dataStorage <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">ReadAllLines</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;woordjes.txt&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		_dataStorage.<span style="color: #0000FF;">Sort</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: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> Data <span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _dataStorage.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> FilterOn<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> searchText<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		searchText <span style="color: #008000;">=</span> searchText.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		var result <span style="color: #008000;">=</span> _dataStorage.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>t <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> t.<span style="color: #0000FF;">ToLowerInvariant</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">StartsWith</span><span style="color: #000000;">&#40;</span>searchText<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> result.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><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>

<p>It is the place where the knowledge of words, where they come from and how you filter them, resides.</p>
<p>But Alex&#8217;s solution did not solve the problem that is typical of the use of the UISearchDisplayController: you have two instances of your DataSource: the one for your initial view, that just displays all the data (words in my case), and the one that is called upon by the UISearchDelegate when you start to search and filter.</p>
<p>I solved this by implementing a general WordsDataSource that uses the WordsModel and has a constuctor for normal use and for filtering:</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> WordsDataSource <span style="color: #008000;">:</span> UITableViewDataSource
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> WordsModel model <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WordsModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> _dataStorage<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> WordsDataSource<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_dataStorage <span style="color: #008000;">=</span> model.<span style="color: #0000FF;">Data</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> WordsDataSource<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> filter<span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_dataStorage <span style="color: #008000;">=</span> model.<span style="color: #0000FF;">FilterOn</span><span style="color: #000000;">&#40;</span>filter<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: #FF0000;">string</span> GetAt<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> position<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> _dataStorage<span style="color: #000000;">&#91;</span>position<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> UITableViewCell GetCell <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		UITableViewCell cell <span style="color: #008000;">=</span> tableView.<span style="color: #0000FF;">DequeueReusableCell</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>cell <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			cell <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UITableViewCell<span style="color: #000000;">&#40;</span>UITableViewCellStyle.<span style="color: #0600FF;">Default</span>, <span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		cell.<span style="color: #0000FF;">TextLabel</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> _dataStorage<span style="color: #000000;">&#91;</span>indexPath.<span style="color: #0000FF;">Row</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> cell<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">int</span> RowsInSection <span style="color: #000000;">&#40;</span>UITableView tableview, <span style="color: #FF0000;">int</span> section<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> _dataStorage.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It inherits (yep I used the I-word) from UITableViewDataSource so can be called upon by the Cocoa framework when rendering the data for the UITableView.</p>
<p>And then I used that class in two places:</p>
<ol>
<li>as an internal property in my WordsTableViewController</li>
<li>as a helper-class in the delegate of the UISearchDisplayController.</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;WordsTableViewController&quot;</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> WordsTableViewController <span style="color: #008000;">:</span> UITableViewController
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Constructor invoked from the NIB loader</span>
    <span style="color: #0600FF;">public</span> WordsTableViewController <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: #008080; font-style: italic;">// The data source for our TableView</span>
    <span style="color: #0600FF;">private</span> WordsDataSource TableDataSource
    <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;">TableView</span>.<span style="color: #0000FF;">DataSource</span> <span style="color: #0600FF;">as</span> WordsDataSource<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;">TableView</span>.<span style="color: #0000FF;">DataSource</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;">// This class receives notifications that happen on the UITableView</span>
    <span style="color: #FF0000;">class</span> TableDelegate <span style="color: #008000;">:</span> UITableViewDelegate
    <span style="color: #000000;">&#123;</span>
	WordsTableViewController parentView<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> TableDelegate <span style="color: #000000;">&#40;</span>WordsTableViewController tableViewController<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
		parentView <span style="color: #008000;">=</span> tableViewController<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> RowSelected <span style="color: #000000;">&#40;</span>UITableView tableView, NSIndexPath indexPath<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
		<span style="color: #FF0000;">string</span> selectedWord <span style="color: #008000;">=</span> parentView.<span style="color: #0000FF;">TableDataSource</span>.<span style="color: #0000FF;">GetAt</span><span style="color: #000000;">&#40;</span>indexPath.<span style="color: #0000FF;">Row</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var alert <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UIAlertView <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Selected&quot;</span>, selectedWord, <span style="color: #0600FF;">null</span>, <span style="color: #666666;">&quot;OK&quot;</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			alert.<span style="color: #0000FF;">Show</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ViewDidLoad <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">ViewDidLoad</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        TableView.<span style="color: #FF0000;">Delegate</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TableDelegate <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TableDataSource</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WordsDataSource <span style="color: #000000;">&#40;</span><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>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;WordSearchDelegate&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> WordSearchDelegate <span style="color: #008000;">:</span> UISearchDisplayDelegate
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> ShouldReloadForSearchString <span style="color: #000000;">&#40;</span>UISearchDisplayController controller, <span style="color: #FF0000;">string</span> forSearchString<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		UITableViewDataSource data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WordsDataSource<span style="color: #000000;">&#40;</span>forSearchString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		controller.<span style="color: #0000FF;">SearchResultsDataSource</span> <span style="color: #008000;">=</span> data<span style="color: #008000;">;</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>I&#8217;m pretty happy with what I got by now. You can <a href="http://lsd.luminis.nl/wp-content/uploads/2009/11/SearchProblems3.zip">download the new version</a>, if you like.</p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/building-iphone-applications-using-monotouch-part-5-software-design-considerations/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Building iPhone applications using MonoTouch, part 4: the UISearchDisplayController</title>
		<link>http://lsd.luminis.eu/building-iphone-applications-using-monotouch-part-4-the-uisearchdisplaycontroller/</link>
		<comments>http://lsd.luminis.eu/building-iphone-applications-using-monotouch-part-4-the-uisearchdisplaycontroller/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 20:21:25 +0000</pubDate>
		<dc:creator>Richard de Zwart</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[mobility]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[monotouch]]></category>
		<category><![CDATA[UIKit]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=415</guid>
		<description><![CDATA[In this post I explain how to hook the UISearchDisplayController to your code and vice versa.]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://lsd.luminis.nl/building-iphone-applications-using-monotouch-part-3-the-interface-builder/">my previous post</a> I wrote about the Interface Builder and things like outlets. Last night (with the help of some colleagues) I cracked one of the more advanced Classes in the Interface Builder: the UISearchDisplayController.</p>
<p>In the previous version of my application I had a UITableView and a UISearchBar. I hooked them up with some code and it worked fine. But I didn&#8217;t get the effect that you see in (for instance) the iPod application. When you scroll up,  uncover the search-bar and start typing, the original view is greyed-out. And when your search gives no result, you get a &#8220;No Results&#8221; message. Like this:</p>
<p style="text-align: center;"><img class="size-medium wp-image-462 alignleft" title="SearchScreenShot" src="http://lsd.luminis.nl/wp-content/uploads/2009/11/SearchScreenShot-200x300.jpg" alt="SearchScreenShot" width="200" height="300" /> <a href="http://lsd.luminis.nl/wp-content/uploads/2009/11/NoResults.jpg"><img class="size-medium wp-image-475 aligncenter" title="NoResults" src="http://lsd.luminis.nl/wp-content/uploads/2009/11/NoResults-200x300.jpg" alt="NoResults" width="200" height="300" /></a></p>
<p>For that, you need the <a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UISearchDisplayController_Class/Reference/Reference.html" target="_blank">UISearchDisplayController</a>. This controller does the work of hooking up a couple of UI-elements for you:</p>
<ol>
<li>The search bar</li>
<li>The view with the results from the search (called the searchContentsController)</li>
<li>The delegate that handles all the events that come from the search bar and the results view (called the searchResultsDelegate)</li>
<li>The data source that provides the data to search in (called the searchResultsDataSource)</li>
<li>Your original view</li>
</ol>
<p>When you drag a UISearchDisplayController and drop it at the top of your UITableViewController, all the outlets get connected to your controller automatically. Apparently the Interface Builder thinks that your class can play all these roles. This makes sense when you program Objective-C, since that language is quite capable of inheriting from lots of base classes (as is so eloquently explained in the <a href="http://cocoawithlove.com/2009/10/objective-c-niche-why-it-survives-in.html" target="_blank">Cocao With Love blog</a>), but asks some more attention when used from MonoTouch.</p>
<h3>Designers should not write code, and vice versa</h3>
<p>I will explain again what Interface Builder does for you. Maybe <em>you</em> already know, but I had to get used to it, and have to keep reminding myself that you use it to, well,  build interfaces. Nothing else. Interface Builder provides a clean separation between the GUI and the code, and that&#8217;s a good thing, right? Right. I love Design Patterns, and I try to convince as much progammers as possible to take at least notice of them. But iPhone apps are mostly very small apps with one or two screens, being very good in just one or two things. Do I need to implement this whole pattern for my simple app? Well, apparently. And so will you, so let me try to help you find out how to do some of these things.</p>
<h3>Steps to follow</h3>
<p>Begin with a UITableViewController. Then go to the Library Window, the Objects button and drag-n-drop a UISearchDisplayController just at the top of your UITableViewController.<br />
In the MainWindow you will have the UISearchDisplayController added. Click it there, and then go to the Outlets-tab in the Inspector Window. You will see that all the outlets will be connected to your UITableViewController, except for the searchBar-outlet since that is of course connected to the SearchBar.</p>
<p>When you run your app, you will have the Table-View and the SearchBar above it. When you tap the text-field you will see the desired gray-out and the &#8220;No Result&#8221; if you enter some text. So far so good.</p>
<h3>Providing the view with data</h3>
<p>Now we need to hook up some of our own code to the events that the UISearchDisplayController fires. Let&#8217;s start with some data in our own TableView to filter on.<br />
As said in one of my first posts, data for a view is delivered by a DataSource. In this case a UITableViewDataSource. So add a class to your project that inherits from UITableViewDataSource. Something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LeesPlankjeDataSource <span style="color: #008000;">:</span> UITableViewDataSource
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> woordjes <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;aap&quot;</span>, <span style="color: #666666;">&quot;noot&quot;</span>, <span style="color: #666666;">&quot;mies&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> LeesPlankjeDataSource<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> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> filter<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		woordjes <span style="color: #008000;">=</span> woordjes.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>f <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> f.<span style="color: #0000FF;">StartsWith</span><span style="color: #000000;">&#40;</span>filter<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToArray</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;">override</span> UITableViewCell GetCell <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		UITableViewCell cell <span style="color: #008000;">=</span> tableView.<span style="color: #0000FF;">DequeueReusableCell</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>cell <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			cell <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UITableViewCell<span style="color: #000000;">&#40;</span>UITableViewCellStyle.<span style="color: #0600FF;">Default</span>, <span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		cell.<span style="color: #0000FF;">TextLabel</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> woordjes<span style="color: #000000;">&#91;</span>indexPath.<span style="color: #0000FF;">Row</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> cell<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">int</span> RowsInSection <span style="color: #000000;">&#40;</span>UITableView tableview, <span style="color: #FF0000;">int</span> section<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> woordjes.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The complete code of this solution is at the bottom of this post.</p>
<p>The class has two constructors. The default constructor (at line 5) is called when initializing our own view, the constructor that takes a string (at line 9) will be called when filtering is started.<br />
The GetCell() and RowsInSection() methods need to be implemented to make your data source work. The implementation is pretty straightforward. The GetCell() method will be called &#8220;RowsInSection&#8221; times. The call to <a href="http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006943-CH3-DontLinkElementID_6" target="_blank">DequeueReusableCell</a>() is some trick to limit the amount of resources that your iPhone application will use. Just make sure you pass in some string that you reuse a few lines down.</p>
<p>To be able to set this datasource on the table-view we have to have some programmatic access to the view. Well, we did that before in the previous post. Go to Interface Builder, select the AppDelegate in the Library Window, add an outlet and connect it to your UITableViewController. Then you can have code like this in your main.cs:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> AppDelegate <span style="color: #008000;">:</span> UIApplicationDelegate
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// This method is invoked when the application has loaded its UI and its ready to run</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> FinishedLaunching <span style="color: #000000;">&#40;</span>UIApplication app, NSDictionary options<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		tableView.<span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// If you have defined a view, add it here:</span>
		window.<span style="color: #0000FF;">AddSubview</span> <span style="color: #000000;">&#40;</span>tableView<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		window.<span style="color: #0000FF;">MakeKeyAndVisible</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This method is required in iPhoneOS 3.0</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnActivated <span style="color: #000000;">&#40;</span>UIApplication application<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>We simply set the DataSource on the tableView to our own DataSource and then add the tableView to the current window. Run your app and you will have some data in your view!</p>
<h3>Building the delegate</h3>
<p>Now we need some code to handle the events of the search bar. The most interesting event is &#8220;ShouldReloadForSearchString()&#8221;. </p>
<p>Add a new class to your project that inherits from UISearchDisplayDelegate. The code should look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;LeesPlankjeDelegate&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> LeesPlankjeDelegate <span style="color: #008000;">:</span> UISearchDisplayDelegate
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> ShouldReloadForSearchString <span style="color: #000000;">&#40;</span>UISearchDisplayController controller, <span style="color: #FF0000;">string</span> forSearchString<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;In ShouldReloadForSearchString&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		controller.<span style="color: #0000FF;">SearchResultsDataSource</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span>forSearchString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
		<span style="color: #008080; font-style: italic;">//return base.ShouldReloadForSearchtring (controller, forSearchString);</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The first line is interesting. This is the magic that brings your classes into the Interface Builder. By registering the name of your class it will be added to the XIB (although not visible in the designer file). I&#8217;ll show you in a minute what you do with this in Interface Builder.</p>
<p>In the override of the ShouldReloadForSearchtring() I instantiate a new data source using the constructor that accepts a filter string. I set this on the SearchResultsDataSource property of the passed in controller object. As you can see in the code of the LeesPlankjeDataSource it will use a Lambda to filter the fixed array of words.</p>
<h3>Hooking up the UISearchDisplayController with your Delegate</h3>
<p>The Register-attribute on your delegate class makes it available in Interface Builder. So you go to the Library Window, choose the Objects button, then Controllers folder and then the general NSObject. Something like this: <img class="alignright size-medium wp-image-470" title="LibraryWindow" src="http://lsd.luminis.nl/wp-content/uploads/2009/11/LibraryWindow-252x300.png" alt="LibraryWindow" width="252" height="300" /></p>
<p>Drag it to the MainWindow, select it there, go to the Inspector, choose the Identity tab. Now change the class field to the name of your own class. LeesPlankjeDelegate in my case. Your class will not be listed, but that doesn&#8217;t matter. When you hit Enter, you&#8217;ll see in the MainWindow that both the class name and the instance name have changed. That is just fine.</p>
<p>Now the next magical thing: you have to connect the default delegate of the UISearchDisplayController to your Delegate class. Here is how: select the UISearchDisplayController in the MainWindow, go to the inspector, select the Outlets tab. The first outlet there is called &#8220;delegate&#8221; and is connected to your TableView. Now remove that connection by clicking the X. Then connect this delegate to your own Delegate class in the MainWindow.</p>
<p>Save in Interface Builder, go to MonoDevelop, run! Type something in the search and &#8220;Lo and behold!&#8221; it works!</p>
<p>Ain&#8217;t live sweet?</p>
<p>If it doesn&#8217;t work, feel free to leave a comment. I&#8217;ll see if I can help you.</p>
<p><a title="Download the source code here" href="http://lsd.luminis.nl/wp-content/uploads/2009/11/SearchProblems.zip">Download the source code<br />
</a></p>
<h3>P.S.</h3>
<p>The last step is actually more complex than it should have been. If I make my UISearchDisplayController visible to my AppDelegate by adding an outlet, I can do with just one more line of code in my main.cs:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">searchDisplayController.<span style="color: #FF0000;">Delegate</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDelegate<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>That way I go one-way: from Interface Builder to MonoTouch. But I thought it more interesting to go the other way too: from MonoTouch to Interface Builder.</p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/building-iphone-applications-using-monotouch-part-4-the-uisearchdisplaycontroller/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

