<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Tim Ross - Software Developer</title>
	<atom:link href="http://timross.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://timross.wordpress.com</link>
	<description>Thoughts and experiences in software development</description>
	<lastBuildDate>Sat, 19 Nov 2011 00:27:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='timross.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Tim Ross - Software Developer</title>
		<link>http://timross.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://timross.wordpress.com/osd.xml" title="Tim Ross - Software Developer" />
	<atom:link rel='hub' href='http://timross.wordpress.com/?pushpress=hub'/>
		<item>
		<title>An Introduction to Objective-C for .NET Developers</title>
		<link>http://timross.wordpress.com/2011/06/12/an-introduction-to-objective-c-for-net-developers/</link>
		<comments>http://timross.wordpress.com/2011/06/12/an-introduction-to-objective-c-for-net-developers/#comments</comments>
		<pubDate>Sun, 12 Jun 2011 05:21:57 +0000</pubDate>
		<dc:creator>timross</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">https://timross.wordpress.com/?p=394</guid>
		<description><![CDATA[Over the past year or so I have gone from primarily developing applications using .NET/C# and ASP.NET MVC, to writing iOS applications using Objective-C/Cocoa and using Ruby/Rails for web development. It&#8217;s been an amazing experience, but it can take some time to get used to a new language and platform. I have found the more [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=394&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Over the past year or so I have gone from primarily developing applications using .NET/C# and ASP.NET MVC, to writing iOS applications using Objective-C/Cocoa and using Ruby/Rails for web development.</p>
<p>It&#8217;s been an amazing experience, but it can take some time to get used to a new language and platform.</p>
<p>I have found the more languages I learn, the easier it gets to pick up new ones. Many of the core concepts are the same, it&#8217;s just a matter of learning the peculiarities of each language/platform (unless you&#8217;re learning learning a whole new paradigm, like a functional language).</p>
<p>One thing I find useful when learning a new language is to try and relate certain features back to a language I know and understand. Of course, it&#8217;s important to learn the correct conventions for a new language, but that usually happens over time.</p>
<p>For the purpose of this post, I&#8217;m going to create a &#8220;Document&#8221; class in both C# and Objective-C so you can see the differences side-by-side. Let&#8217;s start with a quick overview of Objective-C.</p>
<h3>What is Objective-C?</h3>
<p>Objective-C is the main language used for developing application on Mac OS X and iOS platforms. It is a set of object-oriented classes built on top of C, therefore you can also use C code within an Objective-C Class.</p>
<p>Objective-C is quite different from C#, but there are some similarities. Objective-C isn&#8217;t nearly as scary as many people think. Sure, it can be a hassle doing things like manual memory management, but once you understand the patterns, it becomes second-nature. I even think it helps you to consider the lifetime of your objects more and reduce the scope of your objects, which can help improve code design.</p>
<h3>Classes</h3>
<p>An Objective-C class has both an interface and an implementation. These are usually stored in separate files (ClassName.h for interface, ClassName.m for implementation). The interface defines the instance variables, properties and methods your class has. The implementation is where the methods are implemented. It&#8217;s worth noting that an Objective-C interface is not the same as an interface in C# &#8211; instead it&#8217;s part of the class declaration itself.</p>
<p>Here is an example of a our Document class definition:</p>
<p>C#:<br />
<pre class="brush: csharp;">
// Document.cs
public class Document
{
    public Document(string title)
    {
        // Initialise properties, etc.
    }
}
</pre></p>
<p>Objective-C:<br />
<pre class="brush: objc;">
// Document.h
@interface Document : NSObject

- (id)initWithTitle:(NSString *)aTitle;
@end

// Document.m
@implementation Document

- (id)initWithTitle:(NSString *)aTitle {
    if((self = [super init])) {
        // Initialise properties, etc.
    }
    return self;
}
@end
</pre></p>
<p>Init methods are used as object constructors. They return an instance of the object. The &#8220;id&#8221; return type is a dynamic type and can refer to any type of object. We also ensure that the super object has successfully returned a new object before setting any properties.</p>
<h3>Objects and Memory</h3>
<p>You may have heard that Objective-C requires you to manually manage the memory for objects. This is really not has bad as it sounds. We simply have to be aware of the scope of our objects and &#8220;release&#8221; them soon as we&#8217;ve finished using them.</p>
<p>To create an instance of a class we need to allocate and initialise a block of memory for it on the heap:</p>
<p>C#:<br />
<pre class="brush: csharp;">
Document document = new Document(&quot;My New Document&quot;);
</pre></p>
<p>Objective-C:<br />
<pre class="brush: objc;">
Document *document = [[Document alloc] initWithTitle:@&quot;My New Document&quot;];
</pre></p>
<p>If you don&#8217;t come from a C background, you may be wondering what the * symbol is all about. Well, this tells us that we&#8217;re using a &#8220;pointer&#8221; to the object. This is similar to reference types in .NET. We refer (or &#8220;point&#8221;) to the location of that object in memory, not to the object itself. For values types (BOOL, int, etc), we don&#8217;t need to use a pointer, as we have a copy of the value itself. For more information <a href="http://theocacao.com/document.page/234" target="_blank">here is a good intro to pointers</a>.</p>
<p>Although the Objective-C code above will work, it will cause a memory leak unless we clean up properly. For every object you allocate, you must remember to release that memory afterwards by calling the <em>release</em> method on the object:</p>
<p><pre class="brush: objc;">
Document *document = [[Document alloc] initWithTitle:@&quot;My New Document&quot;];
// Do some stuff...
[document release];
</pre></p>
<p>You can also &#8220;retain&#8221; an object. This is used to ensure an object that was passed to you won&#8217;t be cleaned up before you are done using it.</p>
<p>Objective-C uses something called reference counting to handle memory. When you allocate or retain an object, the reference count is increased by one for that object. Each time you release the object, the reference count decreases by one. When a release causes the reference count to reach 0, then <em>dealloc</em> is called and the object is removed from memory.</p>
<p>If you don&#8217;t want to manually release an object from memory, you can set it to &#8220;autorelease&#8221;:</p>
<p><pre class="brush: objc;">
Document *document = [[[Document alloc] initWithTitle:@&quot;My New Document&quot;] autorelease];
// Do some stuff...
// No need to manually release
</pre></p>
<p>This is useful when you return an object from a method and you don&#8217;t want to burden the caller with releasing the memory for that object. However, autoreleased objects can cause unexpected behaviour, so you should explicitly use &#8220;release&#8221; whenever possible.</p>
<p>If you are developing an application for Mac OS X you have the option to enable garbage collection. Unfortunately this is not available on iOS. There are some exciting new developments in iOS 5 around memory management, which unfortunately I can&#8217;t talk about until the NDA is lifted. Suffice it to say that iOS 5 is going to make all of this a lot easier!</p>
<h3>Protocols</h3>
<p>Objective-C protocols are similar to .NET interfaces; they specify a list of methods that a class can implement.</p>
<p>In our example the Document class has a Printing protocol in Objective-C and an IPrintable interface in C#. The Printing protocol has a Print method and an optional Preview method. The IPrintable interface only has the Print method, as C# does not allow optional methods on an interface.</p>
<p>C#:<br />
<pre class="brush: csharp;">
// IPrintable.cs
public interface IPrintable
{
    Print();
}

// Document.cs
public class Document : IPrintable
{
    public void Print()
    {
        Console.WriteLine(@&quot;Printing...{0}&quot;, this.Title);
    }
}
</pre></p>
<p>Objective-C:<br />
<pre class="brush: objc;">
// Printable.h
@protocol Printing &lt;NSObject&gt;

- (void)print;
@optional
- (void)preview;

@end

// Document.h
@interface Document : NSObject&lt;Printing&gt;

@end

// Document.m
@implementation Document

- (void)print {
    NSLog(@&quot;Printing %@&quot;, self.title);
}
@end
</pre></p>
<h3>Methods</h3>
<p>Calling methods in Objective-C has quite a different syntax than C#. In fact it took me a while to get used to it. Let&#8217;s look an a comparison:</p>
<p>C#:<br />
<pre class="brush: csharp;">
Document document = new Document();
document.Print();
</pre></p>
<p>Objective-C:<br />
<pre class="brush: objc;">
Document *document = [[Document alloc] init];
[document print];
[document release];
</pre></p>
<p>So that was quite easy. The big difference comes when you need to pass arguments. In Objective-C, the full name of a method includes the names of the arguments:</p>
<p>C#:<br />
<pre class="brush: csharp;">
public class Document : IPrintable
{
    public bool SaveAs(string fileName, string filePath)
    {
        return true;
    }
}
...
Document document = new Document();
bool success = document.SaveAs(&quot;MyFile.txt&quot;, &quot;C:\Temp&quot;);
</pre></p>
<p>Objective-C:<br />
<pre class="brush: objc;">
// Document.h
@interface Document : NSObject&lt;Printing&gt;

- (BOOL)saveAs:(NSString *)fileName toPath:(NSString *)filePath;

@end

// Document.m
@implementation Document
...
- (BOOL)saveAs:(NSString *)fileName toLocation:(NSString *)filePath {
    // Add code to save file to path...
    return YES;
}
@end
...
Document *document = [[Document alloc] init];
BOOL success = [document saveAs:@&quot;MyFile.txt&quot; toLocation:@&quot;~/Temp&quot;];
[document release];
</pre></p>
<p>So we don&#8217;t have a method called &#8220;SaveAs&#8221; that takes two parameters, instead we have a method called &#8220;saveAs:ToLocation:&#8221; that includes the two arguments. It&#8217;s a bit confusing at first, but gets easier the more you do it.</p>
<p>In Objective-C we usually say we are &#8220;sending a message to an object&#8221; rather than &#8220;calling a method on that object&#8221;. In the case above we are sending a print message to our document object. It&#8217;s more of a naming convention but it does help you to understand how those messages are handled. For instance you can send a message to a nil object without consequence:</p>
<p><pre class="brush: objc;">
Document *document = nil;
[document print]; // Doesn't cause an error
</pre></p>
<p>In .NET however, calling a method on a null object would cause a NullReferenceException.</p>
<h3>Properties</h3>
<p>Objective-C has properties much like C#. They are declared on the class interface using the @property keyword. The purpose of a property is to encapsulate access to data within an object. This is done using getter and setter methods. We can tell the compiler to create the getter and setter methods for us by using the &#8220;synthesize&#8221; keyword. This also generates an instance variable that holds the assigned value.</p>
<p>C#:<br />
<pre class="brush: csharp;">
public class Document : IPrintable
{
    public string Title { get; set; }

    public Document(string title)
    {
        this.Title = title;
    }
}
</pre></p>
<p>Objective-C:<br />
<pre class="brush: objc;">
// Document.h
@interface Document : NSObject&lt;Printing&gt;

- (id)initWithTitle:(NSString *)aTitle;
@property (nonatomic, copy) NSString *title;

@end

// Document.m
@implementation Document

@synthesize title;

- (id)initWithTitle:(NSString *)aTitle {
    if((self = [super init])) {
        self.title = aTitle;
    }
    return self;
}

- (void)dealloc {
    [title release];
    [super dealloc];
}
@end
</pre></p>
<p>The @property declaration defines storage mechanics. In this case we&#8217;re saying we want to &#8220;copy&#8221; the string instead of keeping a pointer to the original. The &#8220;nonatomic&#8221; keyword means the getter and setter methods that are generated are not thread-safe, but are considerably faster.</p>
<p>Properties are useful because they automatically handle releasing the old object and retaining the new object. Because a property retains the object that is assigned to it, you need to release the property in the <em>dealloc</em> method. It&#8217;s also important to ask the superclass to do its cleanup.</p>
<h3>Categories</h3>
<p>We use Categories in Objective-C to add functionality to a class without the need for inheritance. This behaviour is similar to extension methods in C#. This is useful for adding functionality to classes you don&#8217;t own, such as the NSString class.</p>
<p>Here is an example of a category added to the Objective-C NSString class which reverses a string and similar functionality added as an extension method to the .NET System.String class:</p>
<p>C#:<br />
<pre class="brush: csharp;">
public static class StringExtensions
{
    public static string Reverse(this string s)
    {
        char[] arr = s.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }
};
</pre></p>
<p>Objective-C:<br />
<pre class="brush: objc;">
// NSString+Reverse.h
@interface NSString (Reverse)

- (NSString *)reverse;

@end

// NSString+Reverse.m
@implementation NSString (Reverse)

- (NSString *)reverse {
    NSMutableString *reversedStr;
    int len = [self length];
    
    reversedStr = [NSMutableString stringWithCapacity:len];     
    
    while (len &gt; 0)
        [reversedStr appendString:
         [NSString stringWithFormat:@&quot;%C&quot;, [self characterAtIndex:--len]]];   
    
    return reversedStr;
}
@end
</pre></p>
<p>The &#8220;reverse&#8221; method now appears on instances of our String classes:</p>
<p>C#:<br />
<pre class="brush: csharp;">
Document document = new Document(&quot;My New Document&quot;);
Console.WriteLine(document.title.Reverse());
</pre></p>
<p>Objective-C:<br />
<pre class="brush: objc;">
#import &quot;NSString+Reverse.h&quot;
...
Document *document = [[Document alloc] initWithTitle:@&quot;My New Document&quot;];
NSLog(@&quot;Reversed title: %@&quot;, [document.title reverse]);
[document release];
</pre></p>
<h3>More information</h3>
<p>That was a basic run-down of some core differences between .NET/C# and Objective-C. Here are some useful resources for learning Objective-C:</p>
<p><a href="http://designthencode.com/scratch/" target="_blank">http://designthencode.com/scratch/</a><br />
<a href="http://cocoadevcentral.com/d/learn_objectivec/" target="_blank">http://cocoadevcentral.com/d/learn_objectivec/</a><br />
<a href="http://www.otierney.net/objective-c.html" target="_blank">http://www.otierney.net/objective-c.html</a></p>
<p>If you&#8217;d like to know more about Objective-C or iOS development, I&#8217;d be happy to continue with some more posts on this topic. Please feel free to post any questions or comments below.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timross.wordpress.com/394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timross.wordpress.com/394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timross.wordpress.com/394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timross.wordpress.com/394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timross.wordpress.com/394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timross.wordpress.com/394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timross.wordpress.com/394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timross.wordpress.com/394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timross.wordpress.com/394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timross.wordpress.com/394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timross.wordpress.com/394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timross.wordpress.com/394/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timross.wordpress.com/394/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timross.wordpress.com/394/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=394&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://timross.wordpress.com/2011/06/12/an-introduction-to-objective-c-for-net-developers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">timross</media:title>
		</media:content>
	</item>
		<item>
		<title>Fear</title>
		<link>http://timross.wordpress.com/2011/03/29/fear/</link>
		<comments>http://timross.wordpress.com/2011/03/29/fear/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 20:53:17 +0000</pubDate>
		<dc:creator>timross</dc:creator>
				<category><![CDATA[Life]]></category>

		<guid isPermaLink="false">https://timross.wordpress.com/?p=386</guid>
		<description><![CDATA[Since leaving the safety of corporate life and starting a business, I often have feelings of fear. Fear of the unknown road ahead, fear of what people will think if I don&#8217;t succeed, fear of not making enough money. Whenever I start to feel this way, I begin to feel stressed, demotivated and think that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=386&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Since leaving the safety of corporate life and <a href="http://elucidcode.com">starting a business</a>, I often have feelings of fear. Fear of the unknown road ahead, fear of what people will think if I don&#8217;t succeed, fear of not making enough money. Whenever I start to feel this way, I begin to feel stressed, demotivated and think that I&#8217;ve made a terrible mistake. But it only takes one moment of insight, reading one inspiring article or just talking to someone about an idea, then the passion kicks in and defeats the fear. Just like that. I now realise this fear is only thoughts and there is no real situation here to be fearful of. </p>
<h4>Thoughts are not experiences</h4>
<p>There is a difference between real fear and simply feeling scared. Fear is a natural survival instinct. If you come across a man-eating lion, it is natural to feel fear. Adrenaline pumps into your veins and your heart-rate increases to prepare you to flee for your life. These days we don&#8217;t normally have to fear man-eaters on a day-to-day basis (unless you count your boss). This residual instinct has been translated into us feeling the symptoms of fear whenever we face an uncertain situation. Feeling scared is often irrational. Our mind likes to make up scenarios in order to prepare us for some improbable future event. Unfortunately this often inhibits our sense of what really is and causes us to make up conclusions based on nothing more than day-dreams. </p>
<blockquote><p>You can always cope with the present moment, but you cannot cope with something that is only a mind projection &#8211; you cannot cope with the future. &#8211; Eckhart Tolle</p>
</blockquote>
<p>So if you have dreams you aren&#8217;t following, or ideas you aren&#8217;t pursuing because your mind is making excuses for you, try to put your thoughts into perspective. After all, they can&#8217;t eat you.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timross.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timross.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timross.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timross.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timross.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timross.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timross.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timross.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timross.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timross.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timross.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timross.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timross.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timross.wordpress.com/386/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=386&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://timross.wordpress.com/2011/03/29/fear/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">timross</media:title>
		</media:content>
	</item>
		<item>
		<title>Go Out and Create</title>
		<link>http://timross.wordpress.com/2011/02/21/go-out-and-create/</link>
		<comments>http://timross.wordpress.com/2011/02/21/go-out-and-create/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 04:00:22 +0000</pubDate>
		<dc:creator>timross</dc:creator>
				<category><![CDATA[Conferences]]></category>

		<guid isPermaLink="false">http://timross.wordpress.com/?p=368</guid>
		<description><![CDATA[Last week I attended the annual Webstock conference in Wellington, New Zealand. I have attended several tech conferences in recent years, but this one was by far the best. The calibre of speakers, quality of presentations and attention to detail was incredible. Everything from the opening video presentation to the program guide was beautifully crafted. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=368&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last week I attended the annual <a href="http://www.webstock.org.nz/">Webstock</a> conference in Wellington, New Zealand. I have attended several tech conferences in recent years, but this one was by far the best. The <a href="https://www.webstock.org.nz/11/speakers/">calibre of speakers</a>, <a href="https://www.webstock.org.nz/11/programme/presentations.php">quality of presentations</a> and attention to detail was incredible. Everything from the opening video presentation to the program guide was beautifully crafted. Most conference swag I receive quickly gets relegated to gym-gear, but I&#8217;ll be wearing my coveted Webstock 2011 bag with pride!</p>
<p>Webstock, <a href="http://en.wikipedia.org/wiki/Webstock">started back in 2006</a>, attracts high-profile speakers from all over the world that give interesting and inspirational talks about everything Web-related. It&#8217;s not just about the technology, but also what it means to apply that technology to our daily lives. How the web impacts the lives of non-techies was clearly demonstrated in presentations by <a href="http://www.webstock.org.nz/11/speakers/palmer.php">Amanda Palmer</a> and <a href="http://www.webstock.org.nz/11/speakers/webley.php">Jason Webley</a> &#8211; two musicians who talked about how the web has enabled them to manage their careers, interact with fans and reach audiences around the world.</p>
<p>I won&#8217;t cover all the talks here, as this has already been done, including <a href="http://www.nzherald.co.nz/technology/news/article.cfm?c_id=5&amp;objectid=10707335">this excellent report</a> by Chris Barton from the NZ Herald.</p>
<p>It was not just the speakers that made Webstock so great. During the two-day conference, creative people from all over the country gathered to share ideas and experiences.</p>
<p>For me, the message from the conference was nicely summed-up in the closing remarks by organiser Natasha Lampard:</p>
<blockquote><p>&#8220;Everything you learn is a means to an end. The end is actually getting out there and doing something with it.&#8221;</p></blockquote>
<p>Many people come up with great ideas. Few act on those ideas and actually go out and create something. It&#8217;s too easy to sit back and over-analyze an idea. Sometimes fear can take over and you begin to think your idea is not &#8220;good enough&#8221;. I know this has happened many times to me.</p>
<blockquote><p>&#8220;What you actually do within 24 hours of having a creative idea will spell the difference between success and failure&#8221; &#8211; Buckminster Fuller.</p></blockquote>
<p>If you have a great idea, go out and start creating it &#8211; now! Talk to people, get feedback and refine the idea. Don&#8217;t keep it to yourself for fear that others may steal your idea. Ideas themselves are worthless, it&#8217;s the execution that matters.</p>
<p>In the words of presenter <a href="https://www.webstock.org.nz/11/speakers/cohen.php">Jason Cohen</a>:</p>
<blockquote><p>&#8220;Less reading. Less worrying. More doing.&#8221;</p></blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timross.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timross.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timross.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timross.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timross.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timross.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timross.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timross.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timross.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timross.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timross.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timross.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timross.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timross.wordpress.com/368/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=368&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://timross.wordpress.com/2011/02/21/go-out-and-create/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">timross</media:title>
		</media:content>
	</item>
		<item>
		<title>A New Chapter</title>
		<link>http://timross.wordpress.com/2011/01/18/a-new-chapter/</link>
		<comments>http://timross.wordpress.com/2011/01/18/a-new-chapter/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 05:09:33 +0000</pubDate>
		<dc:creator>timross</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://timross.wordpress.com/?p=360</guid>
		<description><![CDATA[After an amazing 4 years living and working in London, my fiancé and I have moved back home to New Zealand. I learned an incredible amount as a contractor working on enterprise software development. I love software development, but I never felt fulfilled developing software for the enterprise. In my spare time I would work [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=360&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After an amazing 4 years living and working in London, my fiancé and I have moved back home to New Zealand. I learned an incredible amount as a contractor working on enterprise software development. I love software development, but I never felt fulfilled developing software for the enterprise. In my spare time I would work on my own projects, but always wished I had more time to devote to my product ideas.</p>
<p>So after a lot of consideration, I&#8217;ve taken the plunge and started an app development company with <a href="http://martyb.com/">a good friend of mine</a>. We will be focused mainly on creating iOS apps for the iPhone and iPad. Developing for this platform is something I truly enjoy doing and the App Store is a great place to find customers who are willing to pay for quality apps.</p>
<p>Last year we created and launched <a href="http://nzrugbyresults.com/">our first iPhone app</a> in the App Store. We both loved the process of creating and launching a product to paying customers and improving it based on the feedback we received.</p>
<p>Our goal is not to grow a business with dozens of employees and large monthly overheads (I would <em>hate</em> that), but to simply earn enough revenue to make a decent living and have the freedom to work from where we want, for customers we care about, on products we are passionate about.</p>
<blockquote><p>&quot;Build a life around what you&#8217;re really talented at and you&#8217;ll be many times more successful than if you base your work on what you&#8217;re ok at.&quot; &#8211; John Williams.</p>
</blockquote>
<p>I&#8217;m spending time at the moment learning about product development and researching several product ideas. I am very wary of wasting time and money creating a product that no one wants, so I&#8217;m researching ideas thoroughly before thinking about design or writing a single line of code. I will discuss this process further in my next post.</p>
<p>Am I worried about the uncertainty of starting a business? Of course I am &#8211; I still have bills to pay and setting up a flat in Auckland is not cheap. I know sometimes it&#8217;s going to get hard and I&#8217;m going to need passion and motivation to stick in there. But I know deep down that this is what I want to do and now is the time to do it.</p>
<p>Life&#8217;s too short not to enjoy what you do every day. Make 2011 the year you get paid for doing what you love. If you&#8217;re interested in taking the plunge, I highly recommend reading <a href="http://www.amazon.co.uk/gp/product/0273730932?ie=UTF8&amp;tag=timrossofdev-21&amp;linkCode=as2&amp;camp=1634&amp;creative=6738&amp;creativeASIN=0273730932">Screw Work, Let&#8217;s Play: How to Do What You Love and Get Paid for it</a><img style="border-style:none!important;margin:0;" border="0" alt="" src="http://www.assoc-amazon.co.uk/e/ir?t=timrossofdev-21&amp;l=as2&amp;o=2&amp;a=0273730932" width="1" height="1" /> by John Williams.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timross.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timross.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timross.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timross.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timross.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timross.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timross.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timross.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timross.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timross.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timross.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timross.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timross.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timross.wordpress.com/360/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=360&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://timross.wordpress.com/2011/01/18/a-new-chapter/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">timross</media:title>
		</media:content>

		<media:content url="http://www.assoc-amazon.co.uk/e/ir?t=timrossofdev-21&#038;l=as2&#038;o=2&#038;a=0273730932" medium="image" />
	</item>
		<item>
		<title>Refactoring To Functional Code</title>
		<link>http://timross.wordpress.com/2010/04/13/refactoring-to-functional-code/</link>
		<comments>http://timross.wordpress.com/2010/04/13/refactoring-to-functional-code/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 16:51:45 +0000</pubDate>
		<dc:creator>timross</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://timross.wordpress.com/?p=345</guid>
		<description><![CDATA[In my previous post I explained that some problems are better suited to a functional approach than traditional, imperative code. I showed an example of a problem that suits a functional approach and demonstrated turning several lines of imperative code with nested for-each loops and if-statements into a single line of functional code. Paul Harrington [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=345&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://timross.wordpress.com/2010/04/05/an-example-of-functional-vs-imperative-programming-in-c/">previous post</a> I explained that some problems are better suited to a functional approach than traditional, imperative code. I showed an example of a problem that suits a functional approach and demonstrated turning several lines of imperative code with nested for-each loops and if-statements into a single line of functional code. <a href="http://implementingagile.wordpress.com/" target="_blank">Paul Harrington</a> posted a comment asking what individual steps I took to refactor the code. Here’s my explanation of the refactoring:</p>
<p>First, here is the original, imperative method.</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:e74deab6-8c93-4385-beef-12724b4da1dc" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp; pad-line-numbers: true;">
public int[] Translate(string word)
{
    ArrayList numbers = new ArrayList();

    foreach (char character in word)
    {
        foreach(KeyValuePair&lt;int, char[]&gt; key in keys)
        {
            foreach(char c in key.Value)
            {
                if(c == character)
                {
                    numbers.Add(key.Key);
                }
            }
        }
    }

    return (int[]) numbers.ToArray(typeof (int));
}
</pre></p>
</div>
<p>As you can see, it takes some effort to determine what’s really going on. From this code we can determine that:</p>
<p><em>For each character in a word, <strong>select</strong> the <strong>first </strong></em><em>key in the list of keys that <strong>contains</strong> the character and add it<strong> to an array</strong> of numbers to return.</em></p>
<p>Now, lets express the statement above as functional code:</p>
<p>To select each character in a word, we need to convert the string to an array of characters. The LINQ <em><a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.select.aspx" target="_blank">Select</a></em> method projects each element of a sequence into a new form. In this case, a string translates to a sequence of characters:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:d7ee1b60-ab83-4d42-b184-b648b5618afa" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp;">
IEnumerable&lt;char&gt; characters = word.Select(c =&gt; c);
</pre></p>
</div>
<p>For each character, we need to find the corresponding key that contains the character. The <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.first(v=VS.100).aspx" target="_blank"><em>First</em></a> method returns the first element of a sequence:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:49a338cf-37e2-4a0c-8e65-7b64f55ce52d" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp;">
int key = keys.First().Key;
</pre></p>
</div>
<p>In our case we need the first key in the list of keys that contains the character. The <em>First</em> method has an overload that returns the first element in a sequence that satisfies a specified condition.  To specify our condition we use the <em><a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.contains(v=VS.100).aspx" target="_blank">Contains</a></em> method, which determines whether a sequence contains a specified element:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:e9b86db3-57a8-48ae-980b-e2f3fde6440e" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp;">
char c = 'a';
int number = keys.First(k =&gt; k.Value.Contains(c)).Key;
</pre></p>
</div>
<p>When we combine the code that selects each character with the code that gets the first key containing the character, we end up with this:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:b2c43183-dc74-445b-a1dc-7e5eef73d5bc" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp;">
IEnumerable&lt;int&gt; numbers = word.Select(c =&gt;
        keys.First(k =&gt; k.Value.Contains(c)).Key);
</pre></p>
</div>
<p>Finally, we use the <em><a href="http://msdn.microsoft.com/en-us/library/bb298736(v=VS.100).aspx" target="_blank">ToArray</a></em> method to return an array of type <em>int</em>. Now we have our final, refactored method:</p>
<div id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:7fc33bda-a41f-442f-a1bf-e224888267db" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;"><pre class="brush: csharp;">
public int[] Translate(string word)
{
    return word.Select(c =&gt;
        keys.First(k =&gt; k.Value.Contains(c)).Key).ToArray();
}
</pre></p>
</div>
<p>I hope this helps to explain the steps I took to refactor imperative code to functional code. You can get really clever with functional code, but remember, readability is what’s most important. Sometimes it’s best to stick with good old-fashioned for-loops and if-statements, but for some problems, like above, a functional approach can lead to more readable, clean and concise code.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timross.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timross.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timross.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timross.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timross.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timross.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timross.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timross.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timross.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timross.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timross.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timross.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timross.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timross.wordpress.com/345/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=345&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://timross.wordpress.com/2010/04/13/refactoring-to-functional-code/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">timross</media:title>
		</media:content>
	</item>
		<item>
		<title>An Example of Functional vs Imperative Programming in C#</title>
		<link>http://timross.wordpress.com/2010/04/05/an-example-of-functional-vs-imperative-programming-in-c/</link>
		<comments>http://timross.wordpress.com/2010/04/05/an-example-of-functional-vs-imperative-programming-in-c/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 17:20:37 +0000</pubDate>
		<dc:creator>timross</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://timross.wordpress.com/?p=331</guid>
		<description><![CDATA[Last week I went to a talk presented by Mike Wagg and Mark Needham from ThoughtWorks on Mixing Functional and Object-Oriented Approaches to Programming in C#. Mike and Mark discussed using a functional approach with LINQ to solve problems in C#. I have come to realise lately that some problems are much better suited to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=331&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Last week I went to a talk presented by <a href="http://mikewagg.blogspot.com/" target="_blank">Mike Wagg</a> and <a href="http://www.markhneedham.com/blog/" target="_blank">Mark Needham</a> from ThoughtWorks on <a href="http://www.markhneedham.com/blog/2010/04/02/ldnug-mixing-functional-and-object-oriented-approaches-to-programming-in-c/" target="_blank">Mixing Functional and Object-Oriented Approaches to Programming in C#</a>. Mike and Mark discussed using a <a href="http://en.wikipedia.org/wiki/Functional_programming" target="_blank">functional</a> approach with <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx" target="_blank">LINQ</a> to solve problems in C#.</p>
<p>I have come to realise lately that some problems are much better suited to a functional approach than traditional imperative programming. Many problems that involve selecting, filtering or performing actions on a list of items are best suited to functional programming, which can significantly reduce the amount of code required to solve the problem.</p>
<p>Here is a simple example of a problem that is best solved with a functional rather than an imperative approach.</p>
<p>Some businesses advertise their phone number as a word, phrase or combination of numbers and alpha characters. This is easier for people to remember than a number. You simply dial the numbers on the keypad that correspond to the characters. For example, “1-800 FLOWERS” translates to 1-800 3569377.</p>
<p>We will write a simple program that translates a word into a list of corresponding numbers.</p>
<p><a href="http://timross.files.wordpress.com/2010/04/phonekeypad.png"><img style="display:inline;border-width:0;" title="phone-keypad" border="0" alt="phone-keypad" src="http://timross.files.wordpress.com/2010/04/phonekeypad_thumb.png?w=244&#038;h=199" width="244" height="199" /></a></p>
<p>First, let’s start with a dictionary that contains each number and the corresponding characters:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:85de93b8-049c-43a0-8138-3188e2a3dd17" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp; pad-line-numbers: true;">
private readonly Dictionary&lt;int, char[]&gt; keys =
            new Dictionary&lt;int, char[]&gt;()
                {
                    {1, new char[] {}},
                    {2, new[] {'a', 'b', 'c'}},
                    {3, new[] {'d', 'e', 'f'}},
                    {4, new[] {'g', 'h', 'i'}},
                    {5, new[] {'j', 'k', 'l'}},
                    {6, new[] {'m', 'n', 'o'}},
                    {7, new[] {'p', 'q', 'r', 's'}},
                    {8, new[] {'t', 'u', 'v'}},
                    {9, new[] {'w', 'x', 'y', 'z'}},
                    {0, new[] {' '}},
                };
</pre>
</pre>
</div>
<p>Next, we create a Translate method that takes a word and returns an array of corresponding numbers.</p>
<p>With a traditional, imperative approach, we would use for-each loops and if-statements to iterate through characters and populate an array of matching numbers:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:32ce371a-30c2-4e9b-b751-06b6b90240ce" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp;">
public int[] Translate(string word)
{
    ArrayList numbers = new ArrayList();

    foreach (char character in word)
    {
        foreach(KeyValuePair&lt;int, char[]&gt; key in keys)
        {
            foreach(char c in key.Value)
            {
                if(c == character)
                {
                    numbers.Add(key.Key);
                }
            }
        }
    }

    return (int[]) numbers.ToArray(typeof (int));
}
</pre>
</pre>
</div>
<p>Alternatively, with a functional approach we can use LINQ to select elements from the dictionary and transform the output to an array of matching numbers:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:0dc0547c-7711-4f07-b0cc-f9ed3bbe1828" class="wlWriterEditableSmartContent">
<pre>
<pre class="brush: csharp;">
public int[] Translate(string word)
{
    return word.Select(c =&gt; 
        keys.First(k =&gt; k.Value.Contains(c)).Key).ToArray();
}
</pre>
</pre>
</div>
<p>And there you have it. Several lines of nested for-each loops replaced with a single line of succinct functional code. Much nicer!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timross.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timross.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timross.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timross.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timross.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timross.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timross.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timross.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timross.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timross.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timross.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timross.wordpress.com/331/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timross.wordpress.com/331/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timross.wordpress.com/331/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=331&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://timross.wordpress.com/2010/04/05/an-example-of-functional-vs-imperative-programming-in-c/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">timross</media:title>
		</media:content>

		<media:content url="http://timross.files.wordpress.com/2010/04/phonekeypad_thumb.png" medium="image">
			<media:title type="html">phone-keypad</media:title>
		</media:content>
	</item>
		<item>
		<title>If You Must Rewrite</title>
		<link>http://timross.wordpress.com/2010/03/15/if-you-must-rewrite/</link>
		<comments>http://timross.wordpress.com/2010/03/15/if-you-must-rewrite/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 22:05:05 +0000</pubDate>
		<dc:creator>timross</dc:creator>
				<category><![CDATA[Software Practices]]></category>

		<guid isPermaLink="false">http://timross.wordpress.com/?p=316</guid>
		<description><![CDATA[As a general rule, you should never rewrite your software. I have (barely) survived several rewrites in the past and they all shared the same horrible experience: The rewrite took much longer than expected, during which time the business stood still while the market moved on. Functionality written over several years had to be re-implemented [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=316&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As a general rule, you should never rewrite your software. I have (barely) survived several rewrites in the past and they all shared the same horrible experience:</p>
<ul>
<li>The rewrite took much longer than expected, during which time the business stood still while the market moved on. </li>
<li>Functionality written over several years had to be re-implemented in a fraction of the time. </li>
<li>The development team was immediately pressured to “get it done”. This created a sense of always being behind schedule. </li>
<li>The business became frustrated and could not understand why it was taking so long. </li>
<li>The rewrite provided little or no new functionality to the business. </li>
<li>There were hidden business rules that had been long forgotten and no one knew what they meant. </li>
<li>The old system could not be turned off until all the features were migrated to the new system. </li>
<li>The old system still had to be maintained while the new system was under development. </li>
</ul>
<p>There has been plenty written about the problems with software rewrites. Here are some great articles on the subject:</p>
<ul>
<li><a href="http://www.joelonsoftware.com/articles/fog0000000069.html" target="_blank">Things You Should Never Do</a> – Joel Spolsky. </li>
<li><a href="http://onstartups.com/tabid/3339/bid/2596/Why-You-Should-Almost-Never-Rewrite-Your-Software.aspx" target="_blank">Why You Should (Almost) Never Rewrite Your Software</a> &#8211; Dharmesh Shah. </li>
<li><a href="http://jamesshore.com/Blog/How-to-Survive-a-Rewrite.html" target="_blank">How To Survive a Software Rewrite</a> – James Shore. </li>
<li><a href="http://chadfowler.com/2006/12/27/the-big-rewrite" target="_blank">The Big Rewrite</a> – Chad Fowler. </li>
<li><a href="http://blog.objectmentor.com/articles/2009/01/09/the-big-redesign-in-the-sky" target="_blank">The Big Redesign in the Sky</a> – “Uncle” Bob Martin. </li>
<li><a href="http://findarticles.com/p/articles/mi_hb6365/is_200903/ai_n32318217/" target="_blank">How To Rewrite Software And When Not To</a> &#8211; Daniel Chudnov. </li>
</ul>
<p>The general consensus from each of these articles is to <strong>never rewrite software unless you really have to.</strong></p>
<p>So when do you really have to rewrite? I mean <em>reeeeaaally</em> have to. Well, there are times when a rewrite is completely unavoidable. In one such case I came across recently, the company no longer had access to the source code of their core product. That’s a pretty tough position to be in and a rewrite is pretty much the only option for moving forward.</p>
<p>So, here are a few retrospective tips from my experience enduring rewrites. This is not a prescriptive list and is only meant to represent ideas that might have helped in the cases I was involved in.</p>
<h3>If you must rewrite…</h3>
<p><strong>The rewrite will always take longer than you think.</strong> Be prepared for a lot of work. An effective strategy is crucial – time spent rewriting the system is time lost from adding new features to grow the business.</p>
<p><strong>Involve the product owner.</strong> They will be eager to complete the rewrite as it is costing them money to re-implement functionality they already have. Have them on-hand to prioritise features and answer questions. Keep them aware of progress.</p>
<p><strong>Identify the core set of features the business can not function without.</strong> Focus on these features and plan to release as soon as you have a minimum feature set.</p>
<p><strong>Prioritise the features.</strong> Ensure you are always working on the highest-priority feature.</p>
<p><strong>Limit work in progress.</strong> Don’t try to re-implement everything at once. Complete a feature before moving onto the next.</p>
<p><strong>Don’t rewrite everything.</strong> Be harsh – throw out anything you don’t absolutely need. Remember, every feature you rewrite costs adding a new feature that could grow the business.</p>
<p><strong>Simplify your business processes</strong>. Use this as an opportunity to refine and simplify your existing business processes. Don’t just re-implement a feature because “that’s how it works in the old system”.</p>
<p><strong>Use a well-known, established technology.</strong> Don’t be tempted to use the latest and greatest just because you get to start over. Speed is the key here, you don’t want to be thrashing with an unfamiliar technology.</p>
<p><strong>Release as soon as possible, but not a moment sooner.</strong> Your current customers won’t be happy if the new system doesn’t function correctly or important features are missing.</p>
<p><strong>Migrate existing data early.</strong> Don’t leave data migration to the last minute. You need to know your new system can work with the data you currently have. I was once involved in a rewrite where the migration happened at the last minute – the entire system failed to function and we lost more time rewriting parts to improve performance.</p>
<p><strong>Implement whole features at a time.</strong> Don’t focus on horizontal layers of the application, e.g. database layer, services, UI, etc.</p>
<p><strong>If you can, replace parts of your system at a time.</strong> If you can rewrite parts of the new system that integrate the old system, then you can stagger the rewrite and release sooner.</p>
<p><strong>Use off-the-shelf software.</strong> If you have a generic feature, such as a forum or CMS, consider using a commercial or open-source alternative to avoid reinventing the wheel.</p>
<p><strong>Don’t create a mess.</strong> Write adequate tests. The only way to go fast is to go well. Buggy software will cost more time and money, even in the short-term.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timross.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timross.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timross.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timross.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timross.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timross.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timross.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timross.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timross.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timross.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timross.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timross.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timross.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timross.wordpress.com/316/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=316&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://timross.wordpress.com/2010/03/15/if-you-must-rewrite/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">timross</media:title>
		</media:content>
	</item>
		<item>
		<title>DDD8</title>
		<link>http://timross.wordpress.com/2010/01/30/ddd8/</link>
		<comments>http://timross.wordpress.com/2010/01/30/ddd8/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 20:05:40 +0000</pubDate>
		<dc:creator>timross</dc:creator>
				<category><![CDATA[Events]]></category>

		<guid isPermaLink="false">http://timross.wordpress.com/?p=309</guid>
		<description><![CDATA[Today I went to the DeveloperDeveloperDeveloper day at the Microsoft campus in Thames Valley Park, Reading. It was a fantastic day with many great sessions covering a wide range of topics. The first session I attended was “Mixing functional and object oriented approaches to programming in C#“ by Mark Needham from Thoughtworks. Mark discussed using [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=309&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I went to the <a href="http://www.developerdeveloperdeveloper.com/ddd8" target="_blank">DeveloperDeveloperDeveloper</a> day at the Microsoft campus in Thames Valley Park, Reading. It was a fantastic day with many great sessions covering a wide range of topics.</p>
<p>The first session I attended was “<a href="http://www.developerdeveloperdeveloper.com/ddd8/ViewSession.aspx?SessionID=405" target="_blank">Mixing functional and object oriented approaches to programming in C#</a>“ by <a href="http://www.markhneedham.com/blog/" target="_blank">Mark Needham</a> from Thoughtworks. Mark discussed using functional programming techniques in C#. The talk focussed on using features of LINQ to replace imperative operations, such as for-each loops and if-else statements. Mark applied functional techniques at the operation-level and also at the structural-level, demonstrating how some common “<a href="http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612" target="_blank">Gang Of Four</a>” patterns can implemented using a functional approach, by passing functions instead of interface implementations.</p>
<p>Mark recommended the book “<a href="http://www.amazon.com/Real-World-Functional-Programming-Examples/dp/1933988924" target="_blank">Real-World Functional Programming</a>” as a good resource on functional programming techniques.</p>
<p>The next session I attended was &#8220;<a href="http://www.developerdeveloperdeveloper.com/ddd8/ViewSession.aspx?SessionID=428" target="_blank">Commercial Software Development &#8211; Writing Software Is Easy, Not Going Bust Is The Hard Bit</a>” by <a href="http://geekswithblogs.net/twickers/" target="_blank">Liam Westley</a>. Liam gave an informative and entertaining talk on tactics he found useful when running a software development company. His tips included:</p>
<ul>
<li>Offering customers an alternative to phone-based support to avoid interruptions.</li>
<li>Using automated unit tests and functional tests to prevent bugs from reaching production only to be discovered by your users. Liam said for him, automated testing didn’t immediately increase productivity, but over time has enabled him to develop high-quality software faster, resulting in satisfied customers.</li>
<li>Having good logging and error notifications. This enabled Liam to quickly identify and fix problems, sometimes before the customer had even raised the issue.</li>
<li>Get organised by tracking time, thoroughly reading and understanding contracts, always having an agenda for meetings and keeping a detailed history of support calls.</li>
<li>Improve your sales pitches by researching the business you are selling to and focusing on delivering value to the business, not just a set of features. He also recommends using light-weight specification documents that allow for change and spreading costs over time.</li>
</ul>
<p>Liam suggested a good book on software product pricing called “<a href="http://www.amazon.co.uk/Dont-Just-Roll-Dice-Usefully/dp/1906434387" target="_blank">Don&#8217;t Just Roll The Dice</a>”.</p>
<p>The last session for the morning was “<a href="http://www.developerdeveloperdeveloper.com/ddd8/ViewSession.aspx?SessionID=400" target="_blank">C# 4</a>” by <a href="http://stackoverflow.com/users/22656/jon-skeet" target="_blank">StackOverflow superstar</a>, <a href="http://msmvps.com/blogs/jon_skeet/" target="_blank">Jon Skeet</a>. Jon discussed the new features of C#4, including named parameters, improved COM-interop, generic variance (covariants, contravariants and invariants, oh my!) and dynamic typing.</p>
<p>During the lunch break there was a series of <a href="http://wiki.developerdeveloperdeveloper.com/Default.aspx?Page=DDD8-Grok-Talks" target="_blank">Grok talks</a>. Several presenters gave short talks on using <a href="http://blogs.msdn.com/webdevtools/archive/2009/01/29/t4-templates-a-quick-start-guide-for-asp-net-mvc-developers.aspx" target="_blank">T4 templates</a>, the features of <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/" target="_blank">CodeRush Xpress</a>, tracking tasks using a <a href="http://www.jobshy.com/shift-in/post/2010/01/05/Personal-Kanban.aspx" target="_blank">personal Kanban board</a> and an introduction to <a href="http://www.lostechies.com/blogs/derickbailey/archive/2009/09/23/albacore-a-suite-of-rake-build-tasks-for-net-solutions.aspx" target="_blank">Albacore</a>: a non-XML based build system.</p>
<p>The first session I attended after lunch was “<a href="http://developerdeveloperdeveloper.com/ddd8/ViewSession.aspx?SessionID=396" target="_blank">C# on the iPhone with Monotouch</a>” presented by <a href="http://weblogs.asp.net/chrishardy/" target="_blank">Chris Hardy</a>. <a href="http://monotouch.net/" target="_blank">MonoTouch</a> allows you develop iPhone apps using C# on <a href="http://mono-project.com/" target="_blank">Mono</a>, an open-source version of the .NET Framework. I was amazed at the tooling support and how well it integrated with the iPhone development tools. Chris said the MonoTouch team released support for the iPad within 24 hours of the SDK being released by Apple. You still need a Mac to develop, but the fact you can use C# and standard .NET libraries makes it easy for .NET developers to reuse their existing skills. Chris stressed that it’s still important to learn how to read Objective-C to follow the Apple documentation. The talk was very inspirational and I now might have to invest in a MacBook Pro <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The last talk I went to was “<a href="http://developerdeveloperdeveloper.com/ddd8/ViewSession.aspx?SessionID=439" target="_blank">Testing C# and ASP.Net applications using Ruby</a>” presented by <a href="http://blog.benhall.me.uk/" target="_blank">Ben Hall</a> (who also happed to be celebrating his birthday, resulting in birthday cake and a chorus of Happy Birthday To You). Ben showed that Ruby can be used to create more readable tests than C#. He gave an example of testing a .NET web application using Cucumber, a Ruby-based Behaviour-Driven Development (BDD) framework. He used <a href="http://www.ironruby.net/" target="_blank">IronRuby</a> for calling standard .NET libraries and demonstrated using <a href="http://wiki.github.com/brynary/webrat/" target="_blank">WebRat</a> for running the tests through a browser. Ben explained the tests can be run in the background by using a “headless“ browser. The <a href="http://www.jetbrains.com/ruby/index.html" target="_blank">RubyMine</a> IDE includes extensive refactoring support and a Visual Studio-like experience. The browser-based tests ran much slower than standard unit tests and Ben suggests only automating high-value, happy-path scenarios.</p>
<p>Further information can be found in the book, <a href="http://www.amazon.com/Testing-ASP-NET-Applications-Jeff-McWherter/dp/0470496649/" target="_blank">Testing ASP.NET Web Applications</a>, which Ben co-authored.</p>
<p>Thanks to the organisers, Microsoft and other sponsors for putting on a fantastic day.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timross.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timross.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timross.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timross.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timross.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timross.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timross.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timross.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timross.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timross.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timross.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timross.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timross.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timross.wordpress.com/309/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=309&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://timross.wordpress.com/2010/01/30/ddd8/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">timross</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating a Simple IoC Container</title>
		<link>http://timross.wordpress.com/2010/01/21/creating-a-simple-ioc-container/</link>
		<comments>http://timross.wordpress.com/2010/01/21/creating-a-simple-ioc-container/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 20:52:27 +0000</pubDate>
		<dc:creator>timross</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Patterns]]></category>

		<guid isPermaLink="false">http://timross.wordpress.com/?p=281</guid>
		<description><![CDATA[Inversion of Control (IoC) is a software design principle that describes inverting the flow of control in a system, so execution flow is not controlled by a central piece of code. This means that components should only depend on abstractions of other components and are not be responsible for handling the creation of dependent objects. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=281&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Inversion_of_control" target="_blank">Inversion of Control</a> (IoC) is a software design principle that describes inverting the flow of control in a system, so execution flow is not controlled by a central piece of code. This means that components should only depend on abstractions of other components and are not be responsible for handling the creation of dependent objects. Instead, object instances are supplied at runtime by an <a href="http://martinfowler.com/articles/injection.html" target="_blank">IoC container</a> through <a href="http://en.wikipedia.org/wiki/Dependency_Injection" target="_blank">Dependency Injection</a> (DI).</p>
<p>IoC enables better software design that facilitates reuse, loose coupling, and easy testing of software components.</p>
<p>At first IoC might seem complicated, but it’s actually a very simple concept. An IoC container is essentially a registry of abstract types and their concrete implementations. You request an abstract type from the container and it gives you back an instance of the concrete type. It’s a bit like an object factory, but the real benefits come when you use an IoC container in conjunction with dependency injection.</p>
<p>An IoC container is useful on all types of projects, both large and small. It’s true that large, complex applications benefit most from reduced coupling, but I think it’s still a good practice to adopt, even on a small project. Most small applications don’t stay small for long. As <a href="http://twitter.com/jbogard/status/7945546868" target="_blank">Jimmy Bogard recently stated</a> on Twitter: <em>“the threshold to where an IoC tool starts to show its value is usually around the 2nd hour in the life of a project</em>”.</p>
<p>There are many existing containers to choose from. These have subtle differences, but all aim to achieve the same goal, so it’s really a matter of personal taste which one you choose. Some common containers in .NET are:</p>
<ul>
<li><a href="http://structuremap.sourceforge.net/Default.htm" target="_blank">StructureMap</a> </li>
<li><a href="http://www.castleproject.org/container/index.html" target="_blank">Castle Windsor</a> </li>
<li><a href="http://ninject.org/" target="_blank">Ninject</a> </li>
<li><a href="http://code.google.com/p/autofac/" target="_blank">Autofac</a> </li>
<li><a href="http://www.codeplex.com/unity" target="_blank">Unity</a> </li>
</ul>
<p>While I recommended using one of the containers already available, I am going to demonstrate how easy it is to implement your own basic container. This is primarily to show how simple the IoC container concept is. However, there might be times when you can’t use one of the existing containers, or don’t want all the features of a fully-fledged container. You can then create your own fit-for-purpose container.</p>
<h3>Using Dependency Injection with IoC</h3>
<p>Dependency Injection is a technique for passing dependencies into an object’s constructor. If the object has been loaded from the container, then its dependencies will be automatically supplied by the container. This allows you to consume a dependency without having to manually create an instance. This reduces coupling and gives you greater control over the lifetime of object instances.</p>
<p>Dependency injection makes it easy to test your objects by allowing you to pass in mocked instances of dependencies. This allows you to focus on testing the behaviour of the object itself, without depending on the implementation of external components or services.</p>
<p>It is good practice to reduce the number of direct calls to the container by only resolving top-level objects. The rest of object-graph will be resolved through dependency injection. This also prevents IoC-specific code from becoming scattered throughout the code base, making it easy switch to a different container if required.</p>
<h3>Implementing a Simple IoC Container</h3>
<p>To demonstrate the basic concepts behind IoC containers, I have created a simple implementation of an IoC container. </p>
<p><strong><a href="http://www.timross.info/uploads/simpleioc.zip">Download the source and sample code here</a></strong></p>
<p>This implementation is loosely based on <a href="http://weblogs.asp.net/seanmcalinden/archive/2010/01/13/custom-ioc-container-for-dependency-injection-with-an-asp-net-mvc-website-usage-example.aspx" target="_blank">RapidIoc</a>, created by Sean McAlindin. It does not have all the features of a full IoC container, however, it should be enough to demonstrate the main benefits of using a container.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, &#39;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> SimpleIocContainer : IContainer</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> IList&lt;RegisteredObject&gt; registeredObjects = <span style="color:#0000ff;">new</span> List&lt;RegisteredObject&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Register&lt;TTypeToResolve, TConcrete&gt;()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        Register&lt;TTypeToResolve, TConcrete&gt;(LifeCycle.Singleton);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Register&lt;TTypeToResolve, TConcrete&gt;(LifeCycle lifeCycle)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        registeredObjects.Add(<span style="color:#0000ff;">new</span> RegisteredObject(<span style="color:#0000ff;">typeof</span> (TTypeToResolve), <span style="color:#0000ff;">typeof</span> (TConcrete), lifeCycle));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> TTypeToResolve Resolve&lt;TTypeToResolve&gt;()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">return</span> (TTypeToResolve) ResolveObject(<span style="color:#0000ff;">typeof</span> (TTypeToResolve));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> Resolve(Type typeToResolve)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">return</span> ResolveObject(typeToResolve);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">object</span> ResolveObject(Type typeToResolve)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        var registeredObject = registeredObjects.FirstOrDefault(o =&gt; o.TypeToResolve == typeToResolve);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">if</span> (registeredObject == <span style="color:#0000ff;">null</span>)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> TypeNotRegisteredException(<span style="color:#0000ff;">string</span>.Format(</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">                <span style="color:#006080;">&quot;The type {0} has not been registered&quot;</span>, typeToResolve.Name));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">return</span> GetInstance(registeredObject);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">object</span> GetInstance(RegisteredObject registeredObject)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">if</span> (registeredObject.Instance == <span style="color:#0000ff;">null</span> || </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">            registeredObject.LifeCycle == LifeCycle.Transient)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">            var parameters = ResolveConstructorParameters(registeredObject);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">            registeredObject.CreateInstance(parameters.ToArray());</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">return</span> registeredObject.Instance;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">private</span> IEnumerable&lt;<span style="color:#0000ff;">object</span>&gt; ResolveConstructorParameters(RegisteredObject registeredObject)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        var constructorInfo = registeredObject.ConcreteType.GetConstructors().First();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">foreach</span> (var parameter <span style="color:#0000ff;">in</span> constructorInfo.GetParameters())</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">            <span style="color:#0000ff;">yield</span> <span style="color:#0000ff;">return</span> ResolveObject(parameter.ParameterType);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">}</pre>
</p></div>
</div>
<p>The SimpleIocContainer class has two public operations: <em>Register</em> and <em>Resolve</em>.</p>
<p><em>Register</em> is used to register a type with a corresponding concrete implementation. When type is registered, it is added to a list of registered objects.</p>
<p><em>Resolve</em> is used to get an instance of a type from the container. Depending on the Lifecycle mode, a new instance is created each time the type is resolved (Transient), or only on the first request with the same instance passed back on subsequent requests (Singleton). </p>
<p>Before a type is instantiated, the container resolves the constructor parameters to ensure the object receives its dependencies. This is a recursive operation that ensures the entire object graph is instantiated.</p>
<p>If a type being resolved has not been registered, the container will throw a TypeNotRegisteredException.</p>
<h3>Using the Simple IoC Container with ASP.NET MVC</h3>
<p>Now I am going to demonstrate using the container by creating a basic order processing application using ASP.NET MVC.</p>
<p>First we create a custom controller factory called SimpleIocControllerFactory that derives from DefaultControllerFactory. Whenever a page is requested, ASP.NET calls GetControllerInstance to get an instance of the page controller. We can then pass back an instance of the controller resolved from our container.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, &#39;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> SimpleIocControllerFactory : DefaultControllerFactory</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> IContainer container;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> SimpleIocControllerFactory(IContainer container)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">this</span>.container = container;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> IController GetControllerInstance(Type controllerType)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">return</span> container.Resolve(controllerType) <span style="color:#0000ff;">as</span> Controller;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">}</pre>
</p></div>
</div>
<p>We now need to set SimpleIocControllerFactory as the current controller factory in the global Application_Start handler.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, &#39;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> MvcApplication : System.Web.HttpApplication</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Application_Start()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        var container = <span style="color:#0000ff;">new</span> SimpleIocContainer();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        BootStrapper.Configure(container);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        ControllerBuilder.Current.SetControllerFactory(<span style="color:#0000ff;">new</span> SimpleIocControllerFactory(container));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">}</pre>
</p></div>
</div>
<p>In order for the SimpleIocControllerFactory to resolve an instance of the OrderController, we need to register the OrderController with the container. </p>
<p>Here I have created a static Bootstrapper class for registering types with the container.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, &#39;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">class</span> BootStrapper</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Configure(IContainer container)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        container.Register&lt;OrderController, OrderController&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">}</pre>
</p></div>
</div>
<p>The controllers do not contain state, therefore we can use the default singleton lifecycle to create an instance of the controller only once per request.</p>
<p>When we run the application, the OrderController should be resolved and the page will load.</p>
<p>It is worth noting that the controller factory should be the only place we need to explicitly resolve a type from the container. The controllers are top-level objects and all our other objects stem from these. Dependency Injection is used to resolve dependencies down the chain.</p>
<p>To place an order we need to make a call to OrderService from the controller. We inject a dependency to the order service by passing the IOrderService interface into the OrderController constructor. </p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, &#39;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> OrderController : Controller</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> IOrderService orderService;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> OrderController(IOrderService orderService)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">this</span>.orderService = orderService;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> ActionResult Create(<span style="color:#0000ff;">int</span> productId)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">int</span> orderId = orderService.Create(<span style="color:#0000ff;">new</span> Order(productId));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        ViewData[<span style="color:#006080;">&quot;OrderId&quot;</span>] = orderId;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">return</span> View();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">}</pre>
</p></div>
</div>
<p>When we build and run the application we should get an error: “The type IOrderService has not been registered”. This means the container has tried to resolve the dependency, but the type has not been registered with the container. So we need to register IOrderService and its concrete implementation, OrderService, with the container.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, &#39;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">class</span> BootStrapper</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Configure(IContainer container)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        container.Register&lt;OrderController, OrderController&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        container.Register&lt;IOrderService, OrderService&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">}</pre>
</p></div>
</div>
<p>The OrderService in turn has a dependency on IOrderRepository which is responsible for inserting the order into a database. </p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, &#39;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> OrderService : IOrderService</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> IOrderRepository orderRepository;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> OrderService(IOrderRepository orderRepository)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">this</span>.orderRepository = orderRepository;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">&#160;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> Create(Order order)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">int</span> orderId = orderRepository.Insert(order);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        <span style="color:#0000ff;">return</span> orderId;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">}</pre>
</p></div>
</div>
<p>As the OrderService was resolved from the container, we simply need to register an implementation for IOrderRepository for OrderService to receive its dependency.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, &#39;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">class</span> BootStrapper</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">{</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Configure(IContainer container)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        container.Register&lt;OrderController, OrderController&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">        container.Register&lt;IOrderService, OrderService&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">        container.Register&lt;IOrderRepository, OrderRepository&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">    }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &#39;background-color:white;border-style:none;margin:0;padding:0;">}</pre>
</p></div>
</div>
<p>Any further types are that required simply need to be registered with the container then passed as an argument on the constructor.</p>
<p>Most full-featured IoC containers support some form of auto-registration. This saves you from having do to a lot of one-to-one manual component mappings.</p>
</p>
</p>
</p>
<p>I hope I have demonstrated that IoC containers are not magic. They are in fact a simple concept that, when used correctly, can help to create flexible, loosely-coupled applications.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timross.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timross.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timross.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timross.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timross.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timross.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timross.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timross.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timross.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timross.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timross.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timross.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timross.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timross.wordpress.com/281/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=281&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://timross.wordpress.com/2010/01/21/creating-a-simple-ioc-container/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">timross</media:title>
		</media:content>
	</item>
		<item>
		<title>Software Development and Chaos Theory</title>
		<link>http://timross.wordpress.com/2010/01/17/software-development-and-chaos-theory/</link>
		<comments>http://timross.wordpress.com/2010/01/17/software-development-and-chaos-theory/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 16:30:27 +0000</pubDate>
		<dc:creator>timross</dc:creator>
				<category><![CDATA[Agile Software Development]]></category>

		<guid isPermaLink="false">http://timross.wordpress.com/?p=277</guid>
		<description><![CDATA[After watching an excellent BBC documentary called The Secret Life of Chaos, I was interested to see if anyone had written about the association between chaos theory and the unpredictable nature of software development. I came across these papers written in 1995 by L.B.S. Raccoon: The Chaos Model and the Chaos Life Cycle Raccoon (1995) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=277&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After watching an excellent BBC documentary called <a href="http://www.bbc.co.uk/programmes/b00pv1c3" target="_blank">The Secret Life of Chaos</a>, I was interested to see if anyone had written about the association between <a href="http://en.wikipedia.org/wiki/Chaos_theory" target="_blank">chaos theory</a> and the unpredictable nature of software development.</p>
<p>I came across these papers written in 1995 by <a href="http://pages.swcp.com/raccoon/" target="_blank">L.B.S. Raccoon</a>:</p>
<h3>The Chaos Model and the Chaos Life Cycle</h3>
<p><a href="http://www.swcp.com/raccoon/papers/chaos93.wpd">Raccoon (1995) The Chaos Model and the Chaos Life Cycle</a>, in ACM Software Engineering Notes, Volume 20, Number 1, Pages 55 to 66, January 1995, ACM Press. (PDF available <a href="http://www.timross.info/uploads/chaos93.pdf">here</a>).</p>
<p>The chaos model combines a linear problem-solving loop with fractals to suggest that a project consists of many interrelated levels of problem solving. The behaviour of a complex system emerges from the combined behaviour of the smaller building blocks.</p>
<p>The chaos life cycle defines the phrases of the software development life cycle in terms of fractals that show that all phrases of the life cycle occur within other phrases.</p>
<p>This suggests an iterative, outside-in development process, which is one of the fundamental principles of <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development" target="_blank">Behaviour-Driven Development</a> (BDD).</p>
<h3>The Chaos Strategy</h3>
<p><a href="http://www.swcp.com/raccoon/papers/stratx23.wpd">Raccoon (1995) The Chaos Strategy</a>, in ACM Software Engineering Notes, Volume 20, Issue 5, Pages 40 to 47, December 1995, ACM Press. (PDF available <a href="http://www.timross.info/uploads/stratx23.pdf">here</a>).</p>
<p>The main rule in chaos strategy is always resolve the most important issue first. This reflects the <a href="http://en.wikipedia.org/wiki/Just_In_Time_(business)" target="_blank">Just-in-Time</a> production ideology as promoted by <a href="http://en.wikipedia.org/wiki/Lean_software_development" target="_blank">Lean Software Development</a>.</p>
<p>It&#8217;s interesting to note that today&#8217;s popular agile software methodologies use some of the principles of chaos theory as presented in these papers. It seems that software development is recognised as an example of complexity at work.</p>
<p>Agile software development follows a natural, evolving, chaotic cycle. The smallest variation in conditions can result in a massive difference in outcome. There will never be a way of accurately predicting the outcome of a complex software project. We simply have to accept chaos as a fact of life and embrace an evolutionary development process.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/timross.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/timross.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/timross.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/timross.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/timross.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/timross.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/timross.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/timross.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/timross.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/timross.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/timross.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/timross.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/timross.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/timross.wordpress.com/277/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=timross.wordpress.com&amp;blog=2219574&amp;post=277&amp;subd=timross&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://timross.wordpress.com/2010/01/17/software-development-and-chaos-theory/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">timross</media:title>
		</media:content>
	</item>
	</channel>
</rss>
