<?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; MonoDevelop</title>
	<atom:link href="http://lsd.luminis.nl/tag/monodevelop/feed/" rel="self" type="application/rss+xml" />
	<link>http://lsd.luminis.nl</link>
	<description></description>
	<lastBuildDate>Mon, 19 Jul 2010 06:45:31 +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>Being a .NET developer in a Mac OSX world: connecting Mono to SQL Server</title>
		<link>http://lsd.luminis.nl/being-a-net-developer-in-a-mac-osx-world-connecting-mono-to-sql-server/</link>
		<comments>http://lsd.luminis.nl/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.nl/being-a-net-developer-in-a-mac-osx-world-connecting-mono-to-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building iPad applications using MonoTouch: the UISplitView</title>
		<link>http://lsd.luminis.nl/building-ipad-applications-using-monotouch-the-uisplitview/</link>
		<comments>http://lsd.luminis.nl/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.nl/building-ipad-applications-using-monotouch-the-uisplitview/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.nl/building-iphone-applications-using-monotouch-howto-using-a-view-without-a-controller/</link>
		<comments>http://lsd.luminis.nl/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.nl/building-iphone-applications-using-monotouch-howto-using-a-view-without-a-controller/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Building iPhone applications using MonoTouch, part 5: software design considerations</title>
		<link>http://lsd.luminis.nl/building-iphone-applications-using-monotouch-part-5-software-design-considerations/</link>
		<comments>http://lsd.luminis.nl/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.nl/building-iphone-applications-using-monotouch-part-5-software-design-considerations/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
