<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>kellabyte</title>
	
	<link>http://kellabyte.com</link>
	<description>One day you will have kellabyte capacity, until then you're just a floppy drive</description>
	<lastBuildDate>Wed, 16 May 2012 23:14:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/kellabyte" /><feedburner:info uri="kellabyte" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Creating boundless experiences with Continuous Client and Transmedia</title>
		<link>http://feedproxy.google.com/~r/kellabyte/~3/4-0qpXpetDw/</link>
		<comments>http://kellabyte.com/2012/04/06/creating-boundless-experiences-with-continuous-client-and-transmedia/#comments</comments>
		<pubDate>Fri, 06 Apr 2012 18:31:31 +0000</pubDate>
		<dc:creator>kellabyte</dc:creator>
				<category><![CDATA[User Experience]]></category>
		<category><![CDATA[continuous client]]></category>
		<category><![CDATA[transmedia]]></category>

		<guid isPermaLink="false">http://kellabyte.com/?p=1008</guid>
		<description><![CDATA[Apps create boundaries and isolated user experiences. It’s been awhile since I blogged about Continuous Client with my last experiment. I’ve had a lot of thoughts since then about application boundaries and how this impacts users. Computing is very app centric especially today with the emergence of App Store’s on mobile devices and desktop computers. [...]]]></description>
			<content:encoded><![CDATA[<h3><span class="drop">A</span>pps create boundaries and isolated user experiences.</h3>
<p>It’s been awhile since I blogged about Continuous Client with my last <a href="http://kellabyte.com/2011/06/26/continuous-client-my-first-attempt-at-multi-device-user-experience-transitions/" target="_blank">experiment</a>. I’ve had a lot of thoughts since then about application boundaries and how this impacts users.</p>
<p>Computing is very app centric especially today with the emergence of App Store’s on mobile devices and desktop computers. We’ve streamlined selling applications directly to the end user and removed as many barriers as possible to get at the users money. </p>
<p>When do we start creating and selling user experiences?</p>
<p>We as developers are creating more and more boundaries and isolated experiences that end users have to deal with. Our operating system and applications are not aware of the users workflow and old solutions like Copy/Paste still exist to aide the user to cross boundaries but this is not intuitive or a good experience.</p>
<p>Most often we find integration API’s like Android intents, Windows Phone and Windows 8 contracts or iOS URL schemes and document types. Microsoft OLE is another abstraction that was created for integration purposes. These solutions help integrate and hop boundaries instead of apps gaining understanding. </p>
<p>Users who are not tech savvy stumble with these boundaries. The boundaries we create between one app and another, and between apps and the operating system cause users confusion.</p>
<p>Every time my mom wants to send me something via email I get a phone call with a question like: </p>
<blockquote><p>“I saved something I wrote and want to email it to you. How do I get it from Word to an email again?”</p>
</blockquote>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/04/boundaries-011.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="boundaries-01" border="0" alt="boundaries-01" src="http://kellabyte.com/wp-content/uploads/2012/04/boundaries-01_thumb1.png" width="623" height="322" /></a></p>
<p><strong>Users struggle with boundaries between applications because that’s where the user experience ends and understanding the system begins.</strong></p>
<p>It is a broken user experience whenever a user has to hop fences of boundaries. Web search for “<em>how to email a file</em>” and you’ll find 713 <em>million</em> results. Here’s <a href="http://office.microsoft.com/en-us/outlook-help/attach-a-file-or-other-item-to-an-e-mail-message-HP001231950.aspx" target="_blank">an example</a> of one for Microsoft Outlook help that I don’t think would help my father.</p>
<p><strong>How much money does it cost the industry to support broken user experiences created by these boundaries?</strong></p>
<h1>Workflow</h1>
<p>Applications and operating systems need to become aware of a users workflow and take part in it rather than expecting the user to navigating around boundaries and understand the system underneath. </p>
<p>Let’s take a look at a users workflow of creating an image in Photoshop, using the image in a document and sending the document to someone via an email.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/04/timeline-012.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="timeline-01" border="0" alt="timeline-01" src="http://kellabyte.com/wp-content/uploads/2012/04/timeline-01_thumb2.png" width="594" height="635" /></a></p>
<p>Can you spot the boundaries?</p>
<p>If we think of these actions as events, it represents a timeline of the workflow. The events I have paired in matching colours (purple and orange) have potential correlation. This gives the application an opportunity to gain awareness of the users workflow instead of directing the user to a boundary. The boundary between applications starts to fade away.</p>
<p>If applications participate in the timeline both as producers and consumers they have the ability to make seamless experiences that help the user in what they are trying to accomplish as a whole and not just within the application.</p>
<p>Since our workflow is a timeline why doesn’t any of our software have an understanding of a timeline? There seems to be a fundamental mismatch with how we do work and how software is written to allow us to do work.</p>
<p>If we flip this vertically we can see the timeline and see how it may look represented as an event stream.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/04/timeline-033.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="timeline-03" border="0" alt="timeline-03" src="http://kellabyte.com/wp-content/uploads/2012/04/timeline-03_thumb3.png" width="867" height="1259" /></a></p>
<p>Having our workflow represented as a stream in the form of a timeline allows applications to have greater awareness than they have using other boundary bridging solutions like integration API’s.</p>
<p>This is not a suggested solution, I mention this to get people thinking.</p>
<h1>User interactions</h1>
<p>The concept of a timeline gives applications awareness of our workflow. Applications become Continuous Clients and participate in a continuous user experience. User interactions could be captured by storing events and timestamps in the events (I didn’t include them for simplicity in the image above) gives these applications the awareness of temporal cohesion of interactions that could span multiple applications and potentially the operating system.</p>
<p>Actions with high temporal cohesion in a workflow are steps in a workflow grouped together during the same task and execute closely together in time.</p>
<h1>Bringing Continuous Client and Transmedia together</h1>
<p>I’ve been talking about and experimenting with Continuous Client now for a couple years and I’m still trying to evolve a lot of ideas. We are starting to see Continuous Client like features here and there but there is still a long way to go yet.</p>
<p>There’s another community that has been working on this problem much longer than the developer community. This community is the <a href="http://en.wikipedia.org/wiki/Transmedia_storytelling" target="_blank">Transmedia</a> story tellers.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/05/transmedia-storytelling1.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="transmedia-storytelling" border="0" alt="transmedia-storytelling" src="http://kellabyte.com/wp-content/uploads/2012/05/transmedia-storytelling_thumb1.jpg" width="635" height="501" /></a></p>
<p>Transmedia was named by Marsha Kinder in 1991. She went on to say:</p>
<blockquote><p>Transmedia intertextuality works to position consumers as powerful players while disavowing commercial manipulation.</p>
</blockquote>
<p>Transmedia is described from Wikipedia as (emphasis mine):</p>
<blockquote><p>Transmedia storytelling (also known as multiplatform storytelling) is the technique of telling a single story or story experience across multiple platforms and formats using current digital technologies, not to be confused with traditional cross-platform media franchises, sequels or adaptations.</p>
<p>From a production standpoint, <font color="#4bacc6">it involves creating content that engages an audience using various techniques to permeate their daily lives</font>.In order to achieve this engagement, a transmedia production will develop stories across multiple forms of media in order to deliver unique pieces of content over multiple channels. Importantly, these pieces of content are not only linked together (overtly or subtly), but are in narrative synchronization with each other.</p>
</blockquote>
<p>We are starting to see increased transmedia story telling in the movie, television and gaming industries but there is a big need to also have this in the operating systems and applications we use. The people working on transmedia today are breaking down boundaries and making continuous user experiences.</p>
<p>What Transmedia and Continuous Client have in common is the goal to remove the boundaries the user faces and deliver a continuous experience that transcends mediums and devices. In computer software this could be taken to an even smaller level and remove application and operating system boundaries.</p>
<p>The way we interact with our computer is a story. A user emailing pictures to a family member on the other side of the world is a story. </p>
<p>If you want someone to fall in love with your product, try to focus on the possible stories and blur the boundaries. Don’t let the boundaries get in their way. Your users will love you for it.</p>
<p>I’d like to thank <a href="https://twitter.com/#!/hoopsomuah" target="_blank">Hoop Somuah</a> and <a href="https://twitter.com/#!/geoffreylong" target="_blank">Geoffrey Long</a> for exposing me to transmedia. Here’s some really interesting videos on the topic.</p>
<p><object width="425" height="355" type="application/x-shockwave-flash" data="http://www.youtube.com/v/wHTZnh2VK-A"><param name="movie" value="http://www.youtube.com/v/wHTZnh2VK-A" />This video was embedded using the YouTuber plugin by <a href="http://www.roytanck.com">Roy Tanck</a>. Adobe Flash Player is required to view the video.</object></p>
<p><object width="425" height="355" type="application/x-shockwave-flash" data="http://www.youtube.com/v/_g6RBA3C7gs"><param name="movie" value="http://www.youtube.com/v/_g6RBA3C7gs" />This video was embedded using the YouTuber plugin by <a href="http://www.roytanck.com">Roy Tanck</a>. Adobe Flash Player is required to view the video.</object></p>
<img src="http://feeds.feedburner.com/~r/kellabyte/~4/4-0qpXpetDw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kellabyte.com/2012/04/06/creating-boundless-experiences-with-continuous-client-and-transmedia/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://kellabyte.com/2012/04/06/creating-boundless-experiences-with-continuous-client-and-transmedia/</feedburner:origLink></item>
		<item>
		<title>Getting started using Apache Cassandra</title>
		<link>http://feedproxy.google.com/~r/kellabyte/~3/Q8oFa-3BQwU/</link>
		<comments>http://kellabyte.com/2012/02/13/getting-started-using-apache-cassandra/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 03:08:42 +0000</pubDate>
		<dc:creator>kellabyte</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[cassandra]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://kellabyte.com/?p=932</guid>
		<description><![CDATA[Cassandra is an open source scalable and highly available “NoSQL” distributed database management system. Cassandra claims to offer fault tolerant linear scalability with no single point of failure. In the diverse solutions available under the “NoSQL” category (I don’t like this term, here’s why) such as ColumnFamily, Document, Graph and Key-Value databases Cassandra sits in [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">C</span>assandra is an open source scalable and highly available “NoSQL” distributed database management system. Cassandra claims to offer fault tolerant linear scalability with no single point of failure. In the diverse solutions available under the “NoSQL” category (I don’t like this term, <a href="http://kellabyte.com/2012/02/12/the-generalization-of-nosql/" target="_blank">here’s why</a>) such as ColumnFamily, Document, Graph and Key-Value databases Cassandra sits in the ColumnFamily camp. Cassandra is a <a href="http://en.wikipedia.org/wiki/BigTable" target="_blank">Google BigTable</a> data model with an <a href="http://en.wikipedia.org/wiki/Dynamo_(storage_system)" target="_blank">Amazon Dynamo</a> like infrastructure with tuneable consistency. The Cassandra data model is designed for large scale distributed data and trades ACID compliant data practices for performance and availability.</p>
<p>Cassandra is optimized for very fast and highly available writes. Cassandra writes first to a commit log on disk for durability then commits to an in-memory structure called a memtable. A write is successful once both commits are complete. Writes are batched in memory and written to disk in a table structure called an SSTable (sorted string table). Memtables and SSTables are created per column family. With this design Cassandra has minimal disk I/O and offers high speed write performance because the commit log is append-only and Cassandra doesn’t seek on writes. In the event of a fault when writing to the SSTable Cassandra can simply replay the commit log. For more information on how Cassandra operates with writes <a href="http://www.datastax.com/docs/0.8/dml/about_writes" target="_blank">read this article</a>. </p>
<p>If you need BIG data and fast writes, Cassandra is worth a look. </p>
<h1>Data Model</h1>
<p>The <a href="http://wiki.apache.org/cassandra/DataModel" target="_blank">Cassandra data model</a> has 4 main concepts which are cluster, keyspace, column family and super column. I’ll leave out super column in this post to keep things simple.</p>
<p>Clusters contain many nodes (machines) and can contain multiple keyspaces.</p>
<p>A keyspace is a namespace to group multiple column families, typically one per application.</p>
<p>A column contains a name, value and timestamp (left out in the diagram).</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/cassandra-column.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="cassandra-column" border="0" alt="cassandra-column" src="http://kellabyte.com/wp-content/uploads/2012/02/cassandra-column_thumb.png" width="142" height="164" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
</p>
<p>&#160;</p>
<p>&#160;</p>
<p>A column family contains multiple columns referenced by a row keys.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/cassandra-columnfamily.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="cassandra-columnfamily" border="0" alt="cassandra-columnfamily" src="http://kellabyte.com/wp-content/uploads/2012/02/cassandra-columnfamily_thumb.png" width="370" height="208" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<h1>Installing</h1>
<p>Cassandra is written in Java and can run on a vast array of operating systems and platform. A lot of this post is relevant no matter what platform you are on. Since we are going to be jumping into how to use Cassandra in C# we will install Cassandra on a Windows machine. Fortunately the great people at <a href="http://www.datastax.com/" target="_blank">DataStax</a> (amazing bunch over there!) have created a <a href="http://www.datastax.com/download/community/versions" target="_blank">Windows installer</a> for their version of Cassandra. The DataStax version of Cassandra comes bundled with a lot of handy things I’ll cover later. The Windows installer will install the Java runtime and makes getting Cassandra up and running a breeze.</p>
<p>After installing you’ll find the following in your start menu.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/image.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://kellabyte.com/wp-content/uploads/2012/02/image_thumb.png" width="234" height="134" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<h1>Using the CLI</h1>
<p>Run the Cassandra CLI Utility. Using the CLI we can create keyspaces, column families, etc. Let’s connect to our local node and create a blog keyspace.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/image1.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://kellabyte.com/wp-content/uploads/2012/02/image_thumb1.png" width="385" height="171" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>Now let’s create the posts column family which will store our posts.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/image2.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://kellabyte.com/wp-content/uploads/2012/02/image_thumb2.png" width="746" height="124" /></a></p>
<p>Now let’s add a couple sample blog posts.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/image3.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://kellabyte.com/wp-content/uploads/2012/02/image_thumb3.png" width="558" height="160" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>Through the CLI we can also view what is stored in the column family.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/image4.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://kellabyte.com/wp-content/uploads/2012/02/image_thumb4.png" width="635" height="171" /></a></p>
<p>&#160;</p>
<h1>Using DataStax OpsCenter</h1>
<p>The DataStax version of Cassandra comes with an application that DataStax developed called OpsCenter. OpsCenter gives us the ability to view all kinds of metrics from our Cassandra cluster node but also allows us to create keyspaces and column families.</p>
<p>Here’s what the main view of OpsCenter looks like.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/image12.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://kellabyte.com/wp-content/uploads/2012/02/image_thumb12.png" width="739" height="425" /></a></p>
<p>On the data modeling view we can see our blog keyspace which also shows us our posts column family.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/image6.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://kellabyte.com/wp-content/uploads/2012/02/image_thumb6.png" width="696" height="544" /></a></p>
<p>The keyspace and column family we built via the CLI we can build through the OpsCenter user interface. The standard version of Cassandra from Apache doesn’t come with OpsCenter and there’s a lot of functionality there. I suggest taking a look at it and poke around.</p>
<p>Using the CQL Shell we can use a SQL-like query language called CQL (Cassandra Query Language) to query our keyspace.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/image7.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://kellabyte.com/wp-content/uploads/2012/02/image_thumb7.png" width="604" height="129" /></a></p>
<h1>Accessing Cassandra in C#</h1>
<p>Now let’s write some code to access the posts we stored in the posts column family. There are 2 Cassandra libraries for .NET that I have used so far. <a href="http://nuget.org/packages/FluentCassandra" target="_blank">Fluent Cassandra</a> and <a href="http://nuget.org/packages/Cassandraemon" target="_blank">Cassandraemon</a>. I’m going to use Cassandraemon for this example.</p>
<p> <script src="https://gist.github.com/1823050.js?file=getting_started_using_cassandra_with_dotnet_cassandraemonsample.cs.cs"></script>
<p>&#160;</p>
<p>There are <a href="http://wiki.apache.org/cassandra/ClientOptions" target="_blank">many client libraries</a> for several languages available to access data from Cassandra making it easy to use from almost anywhere.</p>
<h1>One more thing…</h1>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/Watch-out-for-high-speed-Snowballs-this-Christmas_v2-21.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 5px 14px 6px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="Watch-out-for-high-speed-Snowballs-this-Christmas_v2-21" border="0" alt="Watch-out-for-high-speed-Snowballs-this-Christmas_v2-21" align="right" src="http://kellabyte.com/wp-content/uploads/2012/02/Watch-out-for-high-speed-Snowballs-this-Christmas_v2-21_thumb.jpg" width="236" height="201" /></a>As always I love to think of unconventional things to try so to prove how portable Cassandra and the client C# code is I ventured to make this work on a dual core 1ghz ARM Cortex A9 embedded SBC (Single Board Computer). This device can be compared to the dual core platforms available on smart phones these days.</p>
<p>My goal was to get big data on a little device.</p>
<p>First I had to install a Java runtime for Linux on an ARM platform which I found <a href="http://www.oracle.com/technetwork/java/embedded/downloads/ejre-7u2-download-1375339.html" target="_blank">here</a>. After installing I downloaded the tarball for the DataStax version of Cassandra. I was able to get it running very easily.</p>
<p>After getting Cassandra up and running I needed a Mono runtime for an ARM platform to run my .NET executable, “apt-get mono” was sufficient to get what I needed.</p>
<p>With those installed I was ready to go! I copied my bin\Debug directory to the embedded device and ran the executable that was compiled via Visual Studio. That’s right, I didn’t need to recompile! The binary straight from the Intel box developed with Visual Studio was used and run with Mono on the ARM based embedded device.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/02/image8.png"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://kellabyte.com/wp-content/uploads/2012/02/image_thumb8.png" width="677" height="199" /></a></p>
<p>This is a testament to how portable Mono is. I did not change or recompile Cassandraemon or my application code. It <em>just worked</em>.</p>
<p>With Cassandra not only can we use high performance systems and scale to very big data but we can have big data on little devices such as this one which did all this with only a 5v USB cable plugged in. </p>
<img src="http://feeds.feedburner.com/~r/kellabyte/~4/Q8oFa-3BQwU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kellabyte.com/2012/02/13/getting-started-using-apache-cassandra/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://kellabyte.com/2012/02/13/getting-started-using-apache-cassandra/</feedburner:origLink></item>
		<item>
		<title>The generalization of “NoSQL”</title>
		<link>http://feedproxy.google.com/~r/kellabyte/~3/_DEwUx0X2XY/</link>
		<comments>http://kellabyte.com/2012/02/12/the-generalization-of-nosql/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 18:58:18 +0000</pubDate>
		<dc:creator>kellabyte</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[cassandra]]></category>
		<category><![CDATA[hadoop]]></category>
		<category><![CDATA[hbase]]></category>
		<category><![CDATA[nosql]]></category>

		<guid isPermaLink="false">http://kellabyte.com/?p=901</guid>
		<description><![CDATA[There is growing momentum and a rise in the use of non-relational databases in the last few years. These database engines are often called “NoSQL” databases. It’s more common these days to hear conversations about whether the use of a relational database or “NoSQL” database makes sense for a project. Common things you will hear [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">T</span>here is growing momentum and a rise in the use of non-relational databases in the last few years. These database engines are often called “NoSQL” databases. It’s more common these days to hear conversations about whether the use of a relational database or “NoSQL” database makes sense for a project.</p>
<p>Common things you will hear is how “NoSQL” databases are schema-less and using them to serialize objects removes a lot of the pain in comparison to using and maintaining a RDMS (relational database management systems).</p>
<p>There are 4 high level categories of “NoSQL” databases that I’m aware of which are ColumnFamily, Document, Graph and Key-Value databases. Within the 4 groups there are many options to choose from.</p>
<p>For example Cassandra and HBase (Hadoop) although both modeled based on Google BigTable and both in the ColumnFamily category are implemented very different and share some strengths and weaknesses but also have some that oppose each other.</p>
<p>The lack of schema you hear mentioned a lot in the “NoSQL” camps isn’t exactly accurate either. <a href="http://en.wikipedia.org/wiki/Database_schema" target="_blank">Wikipedia states that a Database schema</a> is:</p>
<blockquote><p>A <b>database schema</b> (pronounced <i><b>skee</b>-ma</i>, <a href="http://en.wikipedia.org/wiki/Wikipedia:IPA_for_English">/ˈski.mə/</a>) of a <a href="http://en.wikipedia.org/wiki/Database_system">database system</a> is its structure described in a <a href="http://en.wikipedia.org/wiki/Formal_language">formal language</a> supported by the <a href="http://en.wikipedia.org/wiki/Database_management_system">database management system</a> (DBMS) and refers to the <em><font color="#ffffff"><strong>organization of data to create a blueprint of how a database will be constructed</strong></font></em> (divided into database tables).</p>
</blockquote>
<p>If you look at the <a href="http://wiki.apache.org/hadoop/Hbase/DataModel" target="_blank">HBase DataModel documentation</a> you will find quotes like:</p>
<blockquote><p>A column family regroups data of a same nature in HBase and has no constraint on the type. The families are part of the <strong><em><font color="#ffffff">table schema</font></em></strong> and stay the same for each row; what differs from rows to rows is that the column keys can be very sparse</p>
</blockquote>
<p>In the <a href="http://wiki.apache.org/cassandra/DataModel" target="_blank">Cassandra DataModel documentation</a> you’ll find documentation describing Keyspaces, Column Families, Columns and Super Columns. Column and Super Column sorting behaviour can be defined as Bytes, Long, Ascii, UTF8, Lexical UUID and Time.</p>
<p>In my opinion this is database schema. Perhaps much less schema than a typical relational database but schema nonetheless.</p>
<p>If we back up to the concept that many people adopt which is serializing objects into a “NoSQL” database then it is true the use of schema is almost non-existent even in Cassandra because as your data model changes your schema wouldn’t change. It’s schema but one that doesn’t change.</p>
<p>Using ColumnFamily, Document, Graph and Key-Value databases and all their diverse implementations as an object serialization store is not using them to their strengths and ignoring their weaknesses. They are very different choices and they often solve a different set of problems although you can misuse them in the same manner. A perfect example is Facebook. Facebook was the initial developer who created Cassandra but Facebook today uses both Cassandra and HBase (along with other database solutions). They use them for their strengths and align them with a specific problem.</p>
<p>For some cases serializing objects to these “NoSQL” databases may make sense but it’s important to consider that as an option and not the only way to use these alternatives.</p>
<p>Based on this information I think the term “NoSQL” is doing all of the non-relational database options a disservice. The term “NoSQL” does help to argue with management that maybe a relational database is not the best option but that’s about where it’s usefulness ends.</p>
<p>Don’t treat them all the same. Learn what the advantages and disadvantages of each are and choose wisely.</p>
<img src="http://feeds.feedburner.com/~r/kellabyte/~4/_DEwUx0X2XY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kellabyte.com/2012/02/12/the-generalization-of-nosql/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://kellabyte.com/2012/02/12/the-generalization-of-nosql/</feedburner:origLink></item>
		<item>
		<title>Arfa</title>
		<link>http://feedproxy.google.com/~r/kellabyte/~3/Z9eVU0uZzPM/</link>
		<comments>http://kellabyte.com/2012/01/15/arfa/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 01:57:49 +0000</pubDate>
		<dc:creator>kellabyte</dc:creator>
				<category><![CDATA[Random]]></category>
		<category><![CDATA[arfa]]></category>
		<category><![CDATA[arfa karim]]></category>
		<category><![CDATA[arfa karim randhawa]]></category>

		<guid isPermaLink="false">http://kellabyte.com/?p=871</guid>
		<description><![CDATA[Arfa Karim Randhawa first discovered computers at the age 5 and was introduced to computers at the age of 6 after insisting to her father she wanted a PC. A computer prodigy was born. 3 years later in 2004 Arfa, a Pakistani student became the worlds youngest Microsoft Certified Professional at the age of 9. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://kellabyte.com/wp-content/uploads/2012/01/arfa_billgate.jpg"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="" border="0" alt="" src="http://kellabyte.com/wp-content/uploads/2012/01/arfa_billgate_thumb.jpg" width="199" height="240" /></a>
<p><a href="http://en.wikipedia.org/wiki/Arfa_Karim" target="_blank"><span class="drop">A</span>rfa Karim Randhawa</a> first discovered computers at the age 5 and was introduced to computers at the age of 6 after insisting to her father she wanted a PC. <strong>A computer prodigy was born.</strong></p>
<p>3 years later in 2004 Arfa, a Pakistani student became the worlds youngest <a href="http://en.wikipedia.org/wiki/Microsoft_Certified_Professional" target="_blank">Microsoft Certified Professional</a> at the age of 9. Arfa prepared for her certification exams during her four month summer break from school.</p>
<p>&#160;</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/01/Arfa-Karim-big-microsoft.jpg"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Arfa-Karim-big-microsoft" border="0" alt="Arfa-Karim-big-microsoft" align="right" src="http://kellabyte.com/wp-content/uploads/2012/01/Arfa-Karim-big-microsoft_thumb.jpg" width="240" height="180" /></a></p>
<p>&#160;</p>
<p>What makes this more amazing is that this feat would be challenging for an adult with a well established career in software development let alone someone<strong> </strong><em>who not long ago touched a keyboard for the first time.</em></p>
<p>August 2nd 2005 Arfa was presented the Fatimah Jinnah Gold Medal in the field of Science and Technology by the then Prime Minister of Pakistan Shaukat Aziz. The Prime Minister also presented her with the Salaam Pakistan Youth Award. </p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/01/Shabaash-Award-038.jpg"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Shabaash Award 038" border="0" alt="Shabaash Award 038" align="left" src="http://kellabyte.com/wp-content/uploads/2012/01/Shabaash-Award-038_thumb.jpg" width="240" height="160" /></a>Arfa also received the President’s award for Pride and Performance, a civil award for people who have shown excellence in their respective fields over a long period of time.</p>
<p>The computer prodigy was honoured by the MKR Foundation with the Shabaash award.</p>
<p>With what most of us would consider a career full of achievements already accomplished at 10 years old Arfa was far from done. Arfa represented Pakistan on many international forums and was invited to join the Pakistan Information Technology Professionals Forum for two weeks in Dubai. Not only did she attend, a dinner reception in her name was hosted with dignitaries of Dubai including the Ambassador of Pakistan. </p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/01/image.jpg"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" align="right" src="http://kellabyte.com/wp-content/uploads/2012/01/image_thumb.jpg" width="176" height="240" /></a>During the trip to Dubai Arfa added more awards to her collection including a laptop. The Dubai trip would be considered a dream for most of us but Arfa had more in mind to accomplish during the tour. <strong>Arfa was not just a computer prodigy but pursued to be pilot</strong>. She flew a plane in a flying club and at the age of 10 received her first flying certificate. Not bad for a 2 week trip.</p>
<p>November 2006 Arfa attended on invitation from Microsoft the Tech-Ed Developers conference themed <em>Get ahead of the game</em> held in Barcelona which she was part of the keynote. She was the only Pakistani among over 5000 developers. A fitting conference title for someone that seemed to be 2 or 3 generations ahead of everyone else in attendance.</p>
<p>In 2010 Arfa was made brand ambassador for the 3G wireless broadband service named EVO from Pakistan Telecommunication Company.</p>
<p>The brilliant Arfa became a national talent and with the pride of a nation behind her.</p>
<h2>Her brilliance and wise beyond her years</h2>
<p>When reading a quote or listening to one of her audio or TV interviews you start to realize in a few seconds that Arfa is an exceptional human being. In her TV interviews she looks relaxed but confident without a hint of uncertainty almost like she has everything all figured out.</p>
<p>In <a href="http://wherearejohnandtodd.com/arfa.mp3" target="_blank">these audio interview highlights</a> from <a href="http://www.geekwire.com/2012/arfa-karim-randhawa-19952012-remembering-philosophy-life" target="_blank">geekwire</a> Arfa reflects on her meeting with Microsoft’s then CEO and Chairman Bill Gates. Most of us would be awestruck but Arfa challenged Mr. Gates.</p>
<p>Arfa said</p>
<blockquote><p>“I asked him why there are no kids and why kids can’t join your company and why they have to wait so much.”</p>
</blockquote>
<p>Followed by</p>
<blockquote><p>“Next I asked him why there aren’t any women around here. There should be an equal amount of men and women.”</p>
</blockquote>
<p>She goes on to discuss her programming with loops and implementing sorting algorithms. Her main language being C# but she was already learning VB.NET and commented on how they are almost the same. At 10 it seems you can’t fool her and she’s already aware of the value to diversify her skills. A budding polyglot programmer in the making.</p>
<p>When asked what she might want to do after college she answers that she wants to engineer satellites and explains that since the world is going wireless there is a need for engineering in that sector.</p>
<p>The most profound quote from the interview highlights for me was at the very end</p>
<blockquote><p>“If you want to do something big in your life, you must remember that shyness is only the mind. If you think shy, you act shy. If you think confident you act confident. Therefore never let shyness conquer your mind.”</p>
</blockquote>
<p>Just as she insisted her desire for a PC at 5 years old Arfa always insisted the areas IT lab be upgraded so it can provide maximum educational facilities to the young people, especially females. Arfa believed that other girls of her village should get opportunities of good education as there was so much talent in them.</p>
<h2>Tragedy strikes</h2>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/01/58269_15190936.jpg"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="58269_15190936" border="0" alt="58269_15190936" src="http://kellabyte.com/wp-content/uploads/2012/01/58269_15190936_thumb.jpg" width="240" height="160" /></a>At the age of 16 in her 2nd year studying her A Levels at the Lahore Grammar School Paragon Campus Arfa suffered from a cardiac arrest after an epileptic seizure and was admitted to Lahore’s Combined Military Hospital in critical condition on December 22nd 2011.</p>
<p>&#160;</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2012/01/pb-120115-arfa1-cannon.photoblog900.jpg"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="pb-120115-arfa1-cannon.photoblog900" border="0" alt="pb-120115-arfa1-cannon.photoblog900" align="right" src="http://kellabyte.com/wp-content/uploads/2012/01/pb-120115-arfa1-cannon.photoblog900_thumb.jpg" width="240" height="159" /></a></p>
<p>&#160;</p>
<p>On January 13th 2012 parts of Arfa’s brain started to show signs of improvement but on January 14th 2012 the 16 year old Arfa Karim died at 9:50PM Pakistan Standard Time at the Combined Military Hospital Lahore. She was buried at her ancestral village <a href="http://en.wikipedia.org/wiki/Ram_Diwali" target="_blank">Chak No. 4JB Ram Dewali, Faisalabad</a>.</p>
<h2>&#160;</h2>
<h2>Personal comments</h2>
<p>Usually I blog because people express interest in a topic but this time I felt compelled to write this post for myself so that I could persist this story and never forget.</p>
<p>I did not know Arfa but I know people who did and they all speak extremely highly of her. I learned about her amazing story when the story broke of her critical condition in December. Due to a medical illness when I was 8 I was minutes from my own life so her story affected me immediately and I prayed for her recovery. </p>
<p>I’m convinced if I had the chance to meet her with my love for learning she would have taught me a thing or two. Or ten. Maybe she could have helped me get over the hump on some things in C# I struggle with. I can’t even imagine what she would have accomplished if she lived to my age. I wish she was given the chance.</p>
<p>I wish the celebration and pride for her brilliance extended further around the world in the same way it did in Pakistan. Pakistani’s realized they had a national treasure in their hands.</p>
<p>There are a couple things I take from her attitude and insights. Never let things like shyness get in your way from your potential and that you can attack and achieve anything you put your mind to, no matter what your age.</p>
<p>On January 12th 2012 the world lost some of it’s potential.</p>
<p>I’ll finish with a tweet that sums up my thoughts exactly from <a href="https://twitter.com/#!/richcampbell/status/158419563292266496" target="_blank">Richard Campbell</a></p>
<blockquote><p>Lost one of the good ones.</p>
</blockquote>
<p>R.I.P. Arfa Karim Randhawa. Our thoughts are with Arfa’s family and friends. </p>
<p><a href="https://twitter.com/#!/clemensv/status/158435926517886977" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="6xxed" border="0" alt="6xxed" src="http://kellabyte.com/wp-content/uploads/2012/01/6xxed2.jpg" width="591" height="498" /></a></p>
<p align="center">Arfa Karim Randhawa and <a href="https://twitter.com/#!/clemensv" target="_blank">Clemens Vasters</a> at the Pakistan Developer Conference in 2005</p>
<p align="center"><a href="http://kellabyte.com/wp-content/uploads/2012/01/294731067_dc98313d0d2.jpg" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="294731067_dc98313d0d" border="0" alt="294731067_dc98313d0d" src="http://kellabyte.com/wp-content/uploads/2012/01/294731067_dc98313d0d_thumb2.jpg" width="589" height="486" /></a></p>
<p align="center">Arfa Karim Randhawa and <a href="https://twitter.com/#!/shanselman" target="_blank">Scott Hanselman</a> in 2006</p>
<p align="center"><a href="http://kellabyte.com/wp-content/uploads/2012/01/kate-and-arfa-barcelona.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="kate and arfa - barcelona" border="0" alt="kate and arfa - barcelona" src="http://kellabyte.com/wp-content/uploads/2012/01/kate-and-arfa-barcelona_thumb.jpg" width="590" height="559" /></a></p>
<p>&#160;</p>
<p align="center">Arfa Karim Randhawa and <a href="https://twitter.com/#!/gregcons" target="_blank">Kate Gregory</a> from 2006 in Barcelona</p>
<img src="http://feeds.feedburner.com/~r/kellabyte/~4/Z9eVU0uZzPM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kellabyte.com/2012/01/15/arfa/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
<enclosure url="http://wherearejohnandtodd.com/arfa.mp3" length="4344292" type="audio/mpeg" />
		<feedburner:origLink>http://kellabyte.com/2012/01/15/arfa/</feedburner:origLink></item>
		<item>
		<title>When Metro design falls off the tracks</title>
		<link>http://feedproxy.google.com/~r/kellabyte/~3/DHdPrENc1Vg/</link>
		<comments>http://kellabyte.com/2011/12/19/when-metro-design-falls-off-the-tracks/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 01:14:08 +0000</pubDate>
		<dc:creator>kellabyte</dc:creator>
				<category><![CDATA[User Experience]]></category>
		<category><![CDATA[metro]]></category>
		<category><![CDATA[win8]]></category>
		<category><![CDATA[windows 8]]></category>
		<category><![CDATA[windows phone 7]]></category>
		<category><![CDATA[wp7]]></category>
		<category><![CDATA[xbox]]></category>
		<category><![CDATA[zune]]></category>

		<guid isPermaLink="false">http://kellabyte.com/?p=706</guid>
		<description><![CDATA[I&#8217;m a big fan of the Metro design language and many of its principles. However, I think in the hands of a developer it arms them with a lot of outs to ignore graphic design, citing the principles as justification for a lack of a vibrant user interface. Microsoft also sets a bad example in [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">I</span>&#8217;m a big fan of the Metro design language and many of its principles. However, I think in the hands of a developer it arms them with a lot of outs to ignore graphic design, citing the principles as justification for a lack of a vibrant user interface. Microsoft also sets a bad example in a lot of cases which doesn’t help raise the bar of applications on it’s platforms. The evolving use of Metro across Microsoft’s product line-up brings a lot of positive benefits to these platforms. The challenge is to reach the level of consistent quality with Metro in all the different divisions, groups and teams at the level Apple does with its designs.</p>
<p>With any post like this it’s important to keep in mind it is my point of view based on what I enjoy or don’t enjoy from Metro. Your opinions may differ. Also keep in mind I’m disregarding the Metro design guidelines from Windows Phone 7 in this post.</p>
<h1>The three pillars of Metro</h1>
<p>In my opinion Metro has 3 pillars of strength and if you leave any one of the 3 out the experience falls apart. These 3 pillars are <em><strong>typography</strong></em>, <strong><em>simplicity</em></strong> and… the one usually left out…</p>
<h2>Typography</h2>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/12/A0CCFE2377BCA85ED89378A66BFD12.jpg" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="A0CCFE2377BCA85ED89378A66BFD1" border="0" alt="A0CCFE2377BCA85ED89378A66BFD1" src="http://kellabyte.com/wp-content/uploads/2011/12/A0CCFE2377BCA85ED89378A66BFD1_thumb2.jpg" width="586" height="321" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>Typography is emphasized in Metro and this benefits the user in many ways. We had a saying, </p>
<blockquote><p>If the user can’t walk down stairs with their eyes alternating between the device and where they are going while navigating with their thumb the design isn’t ready.</p>
</blockquote>
<p>The large text in the screenshot above makes it easy to consume and navigate on the go. It also ensures the content you want the user to consume is front and center because it’s difficult for anything to dominate it. <a href="http://twitter.com/SteveStreza" target="_blank">Steve Streza</a> pointed out on Twitter that Metro scales well to different form factors and I agree.The focus on typography scales well from phones to tablets and consoles (Xbox).</p>
<h2>Simplicity</h2>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/12/GamesChannel-Xbox-Metro-Dahsboard2.jpg" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="GamesChannel-Xbox-Metro-Dahsboard" border="0" alt="GamesChannel-Xbox-Metro-Dahsboard" src="http://kellabyte.com/wp-content/uploads/2011/12/GamesChannel-Xbox-Metro-Dahsboard_thumb2.jpg" width="587" height="351" /></a></p>
<p>The emphasis on typography already contributes to the level of simplicity. The other areas where simplicity appears in Metro is shapes and icons. Single colour square boxes contain flat single colour icons, both of which are prominent on the screen. When the emphasis on typography and the focus on simplicity are combined with the last pillar of strength, the content pops out at you.</p>
<p>Live tiles are a great example of simplicity. They generally don’t have a lot of content crammed within them but they are usually oversized squares that have quite a lot of padding around the icon within them (if there is an icon). The icon itself is one colour and a simple 2D shape.</p>
<p>The screenshot above of the new metrofied Xbox 360 dashboard could be on a tablet and you wouldn’t know the difference because it’s so simple it can scale to different form factors. The same interface could be slightly tweaked to be more vertical and offered on a phone experience. Squares and big text is easy to move around.</p>
<p>In contrast one of the older dashboard implementations isn’t so flexible.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/12/world-record-dashboard1.jpg" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="world-record-dashboard" border="0" alt="world-record-dashboard" src="http://kellabyte.com/wp-content/uploads/2011/12/world-record-dashboard_thumb1.jpg" width="601" height="384" /></a></p>
<p>Although this dashboard contains some similar aspects with the Metro inspired dashboard, the interface wouldn&#8217;t scale as well to a phone, especially the text based menus since they are close together and some of them difficult to read as they fade into the background.</p>
<p>&#160;</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/12/image_41.png" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image_4" border="0" alt="image_4" align="left" src="http://kellabyte.com/wp-content/uploads/2011/12/image_4_thumb1.png" width="240" height="145" /></a></p>
<p>Another example of simplicity is the application bar on Windows Phone 7. For phones, the content should consume the screen real estate. You could argue this is important for many other form factors, but it is especially important on phones. You need menus and navigation but in a minimal fashion.</p>
<p>&#160;</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/12/Navigation_ApplicationBarLabels1.png" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Navigation_ApplicationBarLabels" border="0" alt="Navigation_ApplicationBarLabels" src="http://kellabyte.com/wp-content/uploads/2011/12/Navigation_ApplicationBarLabels_thumb1.png" width="240" height="190" /></a>The app bar uses simple to understand icons but can still offer additional options while educating the user what the icons mean in case of any confusion.</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<h2>Vibrant</h2>
<p>Vibrant is the last of what I consider the 3 pillars of Metro and the one which is often missing. The best Metro experiences, those that really set them apart from other platforms, are the ones that are alive and colourful while at the same time emphasize typography and simplicity.</p>
<p>When I think of Metro I think of this</p>
<p> <iframe height="360" src="http://www.youtube.com/embed/c3RCnYHGHQM" frameborder="0" width="640" allowfullscreen="allowfullscreen"></iframe>
<p>and this</p>
<p><iframe height="360" src="http://www.youtube.com/embed/VHkPiU9YAQk" frameborder="0" width="640" allowfullscreen="allowfullscreen"></iframe></p>
<p>and this </p>
<p><iframe height="360" src="http://www.youtube.com/embed/5S-1eJOP0IM" frameborder="0" width="640" allowfullscreen="allowfullscreen"></iframe></p>
<h1>Metro is not an excuse to stop graphic design</h1>
<p>When only 2 of the 3 pillars of what I consider a great Metro experience are present, it is usually the vibrant pillar that is left out, leaving us with just typography and simplicity. When vibrant is missing you&#8217;re left with an interface that more closely resembles a unix shell experience and that doesn&#8217;t offer much to engage the user.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/12/ScreenDump_2011-16-31-10-16-01-3160-PM2.jpg" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 5px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ScreenDump_2011-16-31-10-16-01-3160-PM" border="0" alt="ScreenDump_2011-16-31-10-16-01-3160-PM" align="right" src="http://kellabyte.com/wp-content/uploads/2011/12/ScreenDump_2011-16-31-10-16-01-3160-PM_thumb2.jpg" width="136" height="236" /></a></p>
<p>In all those videos of great Metro experiences what we don’t see much of is this type of experience on the right. Unfortunately it’s all over the place in Windows Phone 7 and 7.5 “Mango”. </p>
<p>Some apps or screens make sense to be plain text but not as many as we see in the marketplace or in the Windows Phone operating system itself.</p>
<p>&#160;</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/12/homescreens_wp71.png" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 5px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="homescreens_wp7" border="0" alt="homescreens_wp7" align="left" src="http://kellabyte.com/wp-content/uploads/2011/12/homescreens_wp7_thumb1.png" width="305" height="275" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>Live tiles are amazing but swipe to the side and all elegance goes away. What happened? </p>
<p>We can’t have an infinite list of live tile screens but I’m pretty sure some design could have went into this beyond a list with search. Maybe static tiles?</p>
<p>These are just 2 examples of what happens when vibrant is left out.</p>
<p>&#160;</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/12/screen21.png" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 10px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="screen2" border="0" alt="screen2" align="left" src="http://kellabyte.com/wp-content/uploads/2011/12/screen2_thumb1.png" width="194" height="331" /></a></p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/12/699c387d-4013-4037-a0d4-58f4f18612621.png" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="699c387d-4013-4037-a0d4-58f4f1861262" border="0" alt="699c387d-4013-4037-a0d4-58f4f1861262" align="left" src="http://kellabyte.com/wp-content/uploads/2011/12/699c387d-4013-4037-a0d4-58f4f1861262_thumb1.png" width="194" height="331" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>Which one of the above pops out more? I’m guessing the right (Newsroom).</p>
<p>It’s no secret that the Apple community has had a lot of design influence within their developer ecosystem and I think Microsoft has made some gains with investments in designer tooling that work along side the developer workflow (Expression Blend) but Metro opens a big door to creating black background with white text interfaces and calling it a day. It&#8217;s really easy as a developer just to place some solid colour rectangles and oversized text. Maybe it is Metro but its not engaging anyone.</p>
<p>I like that Microsoft as a company is jumping on board in a unified effort to adopt Metro but at times it seems like there are no real designers involved in implementing the Metro experience and that the deltas between one divisions designs and another divisions designs are really far apart.</p>
<h1>Metro offenses on the web</h1>
<p><a href="http://www.windowsazure.com/en-us/" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="windows-azure-01" border="0" alt="windows-azure-01" src="http://kellabyte.com/wp-content/uploads/2011/12/windows-azure-012.jpg" width="320" height="242" /></a>For example, take the Windows Azure website. I’m not sure where this Metro experience was inspired from but it doesn’t resemble the others and if I was a startup it certainly isn’t getting me jazzed about the platform.</p>
<p>&#160;</p>
<p>&#160;</p>
<p><a href="http://www.windowsazure.com/en-us/develop/downloads/" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="windows-azure-02" border="0" alt="windows-azure-02" align="right" src="http://kellabyte.com/wp-content/uploads/2011/12/windows-azure-021.jpg" width="351" height="237" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>Navigate to the developer download page and you experience an even worse page that is slightly better than finger paint.</p>
<p>Windows Azure supports a great list of development platforms (.net, node.js, java, php) tell that story in a compelling Metro experience!</p>
<p>&#160;</p>
<p>&#160;</p>
<p>Engage and excite the user. Show your product off. Two examples of developer tooling websites with great user experiences are Xamarin and the MassTransit websites. Obviously these are not Metro inspired but the point here is even developer tooling should get great user experiences. Good experiences matter.</p>
<p><a href="http://xamarin.com/"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="xamarin-01" border="0" alt="xamarin-01" src="http://kellabyte.com/wp-content/uploads/2011/12/xamarin-01.jpg" width="240" height="180" /></a></p>
<p><a href="http://masstransit-project.com/"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="masstransit-01" border="0" alt="masstransit-01" src="http://kellabyte.com/wp-content/uploads/2011/12/masstransit-01.jpg" width="240" height="179" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>I decided to take a crack at redesigning the Windows Azure developer tools download page with more focus on the aspects of Metro that I enjoy most. In the image below you can see the content is heavily influenced from the current page but includes a background that is a little more alive.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/12/windows-azure-03.jpg" target="_blank"><img style="background-image: none; border-right-width: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="windows-azure-03" border="0" alt="windows-azure-03" src="http://kellabyte.com/wp-content/uploads/2011/12/windows-azure-03_thumb.jpg" width="613" height="514" /></a></p>
<h1>Great examples of Metro</h1>
<p>Here are some other examples of vibrant Metro experiences that I think have done a great job.</p>
<p> <a href="http://kellabyte.com/wp-content/uploads/2011/12/Hub1.jpg" target="_blank"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="Hub" border="0" alt="Hub" src="http://kellabyte.com/wp-content/uploads/2011/12/Hub_thumb1.jpg" width="247" height="167" /></a><a href="http://kellabyte.com/wp-content/uploads/2011/12/9-21-2010-8-42-52-PM2.png" target="_blank"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="9-21-2010-8-42-52-PM" border="0" alt="9-21-2010-8-42-52-PM" src="http://kellabyte.com/wp-content/uploads/2011/12/9-21-2010-8-42-52-PM_thumb2.png" width="227" height="173" /></a><br />
<h1>&#160;</h1>
<h1>&#160;</h1>
<h1><a href="http://kellabyte.com/wp-content/uploads/2011/12/4th-Mayor1.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="4th-Mayor" border="0" alt="4th-Mayor" src="http://kellabyte.com/wp-content/uploads/2011/12/4th-Mayor_thumb1.jpg" width="145" height="240" /></a></h1>
<h1><a href="http://kellabyte.com/wp-content/uploads/2011/12/windows-phone-012.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 5px 14px 6px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="windows-phone-01" border="0" alt="windows-phone-01" src="http://kellabyte.com/wp-content/uploads/2011/12/windows-phone-01_thumb2.jpg" width="140" height="240" /></a></h1>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>What I like most about these experiences is how alive they feel but don’t sacrifice simplicity. The large text, simple shapes and solid colours on the important content doesn’t get overshadowed. Many of those interfaces have active animations and make for a unique experience in comparison to other platforms that usually only animate based on user interaction.</p>
<p>&#160;</p>
<h1>Conclusion</h1>
<p>These days I’m seeing a wide gap between good Metro and bad Metro experiences. I really like Metro but I think no matter what design principles you&#8217;re following, when designing a user experience you should sweat every last pixel and make the best user experience you can.</p>
<img src="http://feeds.feedburner.com/~r/kellabyte/~4/DHdPrENc1Vg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kellabyte.com/2011/12/19/when-metro-design-falls-off-the-tracks/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		<feedburner:origLink>http://kellabyte.com/2011/12/19/when-metro-design-falls-off-the-tracks/</feedburner:origLink></item>
		<item>
		<title>Clarifying REST</title>
		<link>http://feedproxy.google.com/~r/kellabyte/~3/SXNebPmmQpo/</link>
		<comments>http://kellabyte.com/2011/09/04/clarifying-rest/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 02:59:32 +0000</pubDate>
		<dc:creator>kellabyte</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[atom]]></category>
		<category><![CDATA[hypermedia]]></category>
		<category><![CDATA[hypertext]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[restful]]></category>

		<guid isPermaLink="false">http://kellabyte.com/?p=652</guid>
		<description><![CDATA[For many REST is a vastly misunderstood term. The experts in the REST domain know this all too well. To the point they are thinking of renaming it to separate themselves because the confusion is out of control and impossible to re-align with the true definition. Having an HTTP server accept GET requests or POST [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">F</span>or many REST is a vastly misunderstood term. The experts in the REST domain know this all too well. To the point they are thinking of renaming it to separate themselves because the confusion is out of control and impossible to re-align with the true definition.</p>
<p>Having an HTTP server accept GET requests or POST and responding with XML, JSON or some other data format is <strong><em>not</em></strong> automatically RESTful. This statement will seem very odd to many reading this.</p>
<p>Unfortunately many of the HTTP-enabled API&#8217;s out there such as Twitter, who promote themselves as REST, have created confusion on what REST really is.</p>
<p>The definition of REST lost its way when popular services emerged calling themselves REST which were not. An API like Twitter is ignoring the values of the web and the browser by using HTTP as a transport for RPC (Remote Procedure Call). SOAP services also use HTTP in this way. Instead of doing RPC, HTTP should be used to transfer application state which is what REST is designed for.</p>
<p>It’s not bad for a service to not be RESTful if the problem needing a solution calls for it. Each situation is different and REST has it own set of advantages and disadvantages to consider. The problem is the term REST is being used where it does not belong.</p>
<h1>REST constraints</h1>
<p>Roy Fielding, one of the principal authors of the HTTP 1.0 and 1.1 specifications introduced REST in 2000 in his <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm" target="_blank">doctoral dissertation</a>.</p>
<p>Conforming to the constraints for REST from Roy’s dissertation is referred to as being RESTful. As pointed out earlier these terms are used improperly in cases where the constraints are violated. Simply having a HTTP service that responds with JSON or XML does not make a RESTful service.</p>
<h2>Client-Server constraint</h2>
<p>Separation of concerns is the purpose of the client-server constraint which is intended to separate user interface concerns from data storage concerns. With the popularity of HTTP-enabled API’s being an integration point for online services the separation of the client and server was already chosen since the client is typically a 3rd party.</p>
<h2>Stateless constraint</h2>
<p>The client-server interaction must be stateless in nature. Each request from the client to the server must contain all of the data that is necessary to handle the request. Tackling concurrency and scalability becomes drastically easier when you don’t have to transition state between servers and handle scenarios when state has been mutated in multiple locations. Be careful not to confuse state and resources (data).</p>
<p>As Roy mentions in his dissertation making a choice to adopt an architecture style always has trade-offs. One disadvantage of being stateless is the repetition of data sent in a series of requests.</p>
<p>This constraint in my opinion is one of the first ones violated in self proclaimed REST services. Having the server retain state also has its advantages which for one service may make total sense.</p>
<h2>Cache constraint</h2>
<p>The data within a response to a request must be implicitly or explicitly labeled as cacheable or non-cacheable. The advantage of this constraint reduces partially or completely client to server interactions for a request or series of requests. Gains come in many facets such as user perceived performance and reliability (the service isn’t always required). Data reliability is a trade-off however.</p>
<p>Because of the stateless constraint, understanding when a cache is invalid is easier to identify because you don’t have any cases where server-side state has been mutated on one server but not others and consistency is easily achieved.</p>
<h2>Layered system constraint</h2>
<p>The layered constraint was added to address improving internet sized scalability requirements. Each layer cannot see beyond the immediate layer with which it is communicating with. This places boundaries on the overall complexity of the system.</p>
<p>Like most solutions for scalability there are usually trade-offs. Latency is increased with the introduction of layers but the cache-constraint above can certainly help reduce the amount of requests over the network.</p>
<p>I would imagine that many would still consider your service RESTful if you bent the rules on this constraint and solved scalability differently. From the outside your clients could interpret your service as being completely RESTful even if you did this differently.</p>
<p>One has to wonder what other constraints you broke if your layers cross all sorts of boundaries. I imagine violating the stateless constraint would cause all kinds of odd topologies as you start to wrestle with the concurrency challenges that introduces.</p>
<p>Pandoras box opened have you? State you’ve allowed Hmm?</p>
<h2>Code-on-demand constraint</h2>
<p>The code-on-demand constraint allows clients to be extendable by downloading and executing code. Similarly to java script in a web browser this allows you to add functionality without re-deploying client software.</p>
<p>The code-on-demand constraint is <strong><em>optional</em></strong>.</p>
<h2>Uniform interface</h2>
<p>Information is transferred in a standardized form which is certainly a trade-off. You gain in a common standard for information but you lose in efficiency by the inability to handle a specific applications needs.</p>
<p>REST has 4 interface constraints. I’m not going to go through all of them but I’ll give a quick summary:</p>
<h5>Identification of resources</h5>
<p>Each resource has a URI and is access through a defined set of HTTP methods (GET, PUT, POST, DELETE)</p>
<h5>Manipulation of resources through representations</h5>
<p>Each resource can have one or more representations. Such as application/xml, application/json, text/html, etc. Clients and servers negotiate to select representation.</p>
<h5>Self-descriptive messages</h5>
<p>Requests and responses contain not only data but additional headers describing how the content should be handled. Such as if it should be cached, authentication requirements, etc.</p>
<h5>Hypermedia as the engine for application state</h5>
<p>I’m going to focus on the last interface constraint <em>Hypermedia as the engine for application state</em> because I think there is a lot to learn when it comes to hypermedia and why many services are not RESTful by violating this constraint.</p>
<h1>A REST API must be hypertext driven</h1>
<p><em>Hypermedia as the engine for application state</em> is the 4th Uniform interface constraint.</p>
<p>Roy himself has gotten frustrated by the misuse of the term REST and <a href="http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven" target="_blank">blogged about it</a> with some great info on why certain services are not RESTful.</p>
<p>After talking with <a href="http://twitter.com/darrel_miller" target="_blank">Darrel Miller</a> and reading Roy’s post the key awareness I came out of it was that the use of hypermedia, particularly hypertext and linking are <strong><em>very</em></strong> important.</p>
<p>Roy states</p>
<blockquote><p>A REST API must not define fixed resource names or hierarchies (an obvious coupling of client and server). Servers must have the freedom to control their own namespace. Instead, allow servers to instruct clients on how to construct appropriate URIs, such as is done in HTML forms and URI templates, by defining those instructions within media types and link relations. <em>[Failure here implies that clients are assuming a resource structure due to out-of band information, such as a domain-specific standard, which is the data-oriented equivalent to RPC's functional coupling].</em></p></blockquote>
<p>An example of hypertext aware clients are web browsers.</p>
<p>In my observation many services which claim to be REST but are not exclude hypertext and requires deep knowledge of the services API to understand how to navigate the application.</p>
<p>Let’s look into a Twitter response and discover what is missing and what hypertext enables which is required for REST.</p>
<h4>Not RESTful (Twitter)</h4>
<div id="gist-1524349" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;statuses</span> <span class="na">type=</span><span class="s">&quot;array&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nt">&lt;status&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;created_at&gt;</span>Thu Sep 01 22:19:10 +0000 2011<span class="nt">&lt;/created_at&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;id&gt;</span>109389864092442624<span class="nt">&lt;/id&gt;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;text&gt;</span>Login issue on dev.twitter.com is now fixed.<span class="nt">&lt;/text&gt;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;source&gt;</span>web<span class="nt">&lt;/source&gt;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;user&gt;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;id&gt;</span>6253282<span class="nt">&lt;/id&gt;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;name&gt;</span>Twitter API<span class="nt">&lt;/name&gt;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;screen_name&gt;</span>twitterapi<span class="nt">&lt;/screen_name&gt;</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;location&gt;</span>San Francisco, CA<span class="nt">&lt;/location&gt;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;profile_image_url&gt;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://a2.twimg.com/profile_images/1438634086/avatar_normal.png</div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/profile_image_url&gt;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;<span class="nt">&lt;/status&gt;</span></div><div class='line' id='LC17'><span class="nt">&lt;/statuses&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524349/4a71c491705189e61d9f61a1806881e69a05ddc0/clarifying_rest_01.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524349#file_clarifying_rest_01.xml" style="float:right;margin-right:10px;color:#666">clarifying_rest_01.xml</a>
            <a href="https://gist.github.com/1524349">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The Twitter API also has API for looking at a users profile. Notice how the &lt;user&gt; element has no hyperlink to direct the client to the location of where the user resource is located. This means unlike a browser, a Twitter client needs to understand deep knowledge of the Twitter API to navigate to the user resource. If the Twitter API was RESTful within the response the &lt;user&gt; element would have a hyperlink which the client could automatically understand. The benefits of this are many and one of the most obvious ones is if Twitter changes its URI for user profiles the client would require no changes because like a browser the server response includes hyperlinks and the client <strong><em>understands</em></strong> them.</p>
<h4>RESTful (Twitter modified)</h4>
<div id="gist-1524360" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;statuses</span> <span class="na">type=</span><span class="s">&quot;array&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="nt">&lt;status&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;created_at&gt;</span>Thu Sep 01 22:19:10 +0000 2011<span class="nt">&lt;/created_at&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;id&gt;</span>109389864092442624<span class="nt">&lt;/id&gt;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;text&gt;</span>Login issue on dev.twitter.com is now fixed.<span class="nt">&lt;/text&gt;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;source&gt;</span>web<span class="nt">&lt;/source&gt;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;user</span> <span class="na">href=</span><span class="s">&quot;http://api.twitter.com/1/users/show.xml?screen_name=TwitterAPI&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;id&gt;</span>6253282<span class="nt">&lt;/id&gt;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;name&gt;</span>Twitter API<span class="nt">&lt;/name&gt;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;screen_name&gt;</span>twitterapi<span class="nt">&lt;/screen_name&gt;</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;location&gt;</span>San Francisco, CA<span class="nt">&lt;/location&gt;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;profile_image_url&gt;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://a2.twimg.com/profile_images/1438634086/avatar_normal.png</div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/profile_image_url&gt;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;<span class="nt">&lt;/status&gt;</span></div><div class='line' id='LC17'><span class="nt">&lt;/statuses&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524360/0e56ba4e2c134d904cdd30bf1addcfcdfa841554/clarifying_rest_02.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524360#file_clarifying_rest_02.xml" style="float:right;margin-right:10px;color:#666">clarifying_rest_02.xml</a>
            <a href="https://gist.github.com/1524360">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Notice the <strong><em>href</em></strong> in the above XML markup. With the inclusion of hypertext, a RESTful client can navigate this all by it’s own just like a browser does when it makes links clickable by the user.</p>
<p>It is important to point out that in the world of REST a link may have uses outside of user navigation. The client could use links to navigate the data. A link could also be used for previous or next records for example.</p>
<p>Ever read a multi-page article on a website that has a next page link? The browser just knows how to handle that. RESTful clients often behave the same way.</p>
<p>Let’s take an Atom reader for example. The Atom format supports hypertext and you can easily see the benefits over the Twitter response.<br />
<div id="gist-1524363" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;feed</span> <span class="na">xmlns=</span><span class="s">&quot;http://www.w3.org/2005/Atom&quot;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">xmlns:as=</span><span class="s">&quot;http://atomserver.org/namespaces/1.0/&quot;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">xmlns:os=</span><span class="s">&quot;http://a9.com/-/spec/opensearchrss/1.1/&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="nt">&lt;os:totalResults&gt;</span>65801<span class="nt">&lt;/os:totalResults&gt;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="nt">&lt;os:startIndex&gt;</span>0<span class="nt">&lt;/os:startIndex&gt;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="nt">&lt;os:itemsPerPage&gt;</span>2<span class="nt">&lt;/os:itemsPerPage&gt;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="nt">&lt;as:endIndex&gt;</span>153<span class="nt">&lt;/as:endIndex&gt;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;<span class="nt">&lt;link</span> <span class="na">href=</span><span class="s">&quot;/myserver/v1/widgets/acme?start-index=153&amp;amp;max-results=2&quot;</span> <span class="na">rel=</span><span class="s">&quot;next&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="nt">&lt;link</span> <span class="na">href=</span><span class="s">&quot;/myserver/v1/widgets/acme?start-index=0&amp;amp;max-results=2&quot;</span> <span class="na">rel=</span><span class="s">&quot;self&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="nt">&lt;author&gt;</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;name&gt;</span>AtomServer APP Service<span class="nt">&lt;/name&gt;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;<span class="nt">&lt;/author&gt;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;<span class="nt">&lt;title</span> <span class="na">type=</span><span class="s">&quot;text&quot;</span><span class="nt">&gt;</span>acme entries<span class="nt">&lt;/title&gt;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;<span class="nt">&lt;updated&gt;</span>2007-10-05T19:17:42.750Z<span class="nt">&lt;/updated&gt;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;<span class="nt">&lt;id&gt;</span>tag:atomserver.org,2007:v1:acme<span class="nt">&lt;/id&gt;</span></div><div class='line' id='LC17'>&nbsp;&nbsp;<span class="nt">&lt;entry&gt;</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;id&gt;</span>/myserver/v1/widgets/acme/205390.en.xml<span class="nt">&lt;/id&gt;</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;as:entryId&gt;</span>205390<span class="nt">&lt;/as:entryId&gt;</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;title</span> <span class="na">type=</span><span class="s">&quot;text&quot;</span><span class="nt">&gt;</span> Entry: acme 205390.en<span class="nt">&lt;/title&gt;</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;author&gt;</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;name&gt;</span>AtomServer Atom Service<span class="nt">&lt;/name&gt;</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/author&gt;</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;link</span> <span class="na">href=</span><span class="s">&quot;/myserver/v1/widgets/acme/205390.en.xml&quot;</span> <span class="na">rel=</span><span class="s">&quot;self&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;link</span> <span class="na">href=</span><span class="s">&quot;/myserver/v1/widgets/acme/205390.en.xml/2&quot;</span> <span class="na">rel=</span><span class="s">&quot;edit&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC26'>&nbsp;&nbsp;<span class="nt">&lt;/entry&gt;</span></div><div class='line' id='LC27'><span class="nt">&lt;/feed&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524363/15056ccaef4d0c8c3ae2039ca7717a7616de788d/clarifying_rest_03.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524363#file_clarifying_rest_03.xml" style="float:right;margin-right:10px;color:#666">clarifying_rest_03.xml</a>
            <a href="https://gist.github.com/1524363">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>Lines 9 and 10 show how Atom allows the client to understand it’s current location as well as where to navigate for the next collection of results. Atom clients can be far more dynamic than Twitter clients because of the use of hypermedia.</p>
<p>Line 25 you can see the inclusion of an edit link. By using the rel attribute Atom clients can automatically understand where to navigate for editing. The use of hypermedia specifically hypertext enables dynamic scenarios non-RESTful clients don’t get with responses that include no hypermedia. For this reason Hypertext is a constraint. Without including hypermedia a service is not RESTful.</p>
<p>Another good source of information on hypermedia is a blog post from <a href="http://twitter.com/gblock" target="_blank">Glenn Block</a>. He talks about <a href="http://codebetter.com/glennblock/2011/05/09/hypermedia-and-forms/" target="_blank">Hypermedia and forms</a>.</p>
<h1>RESTful Clients</h1>
<p>When we build RESTful systems the way we build clients also changes. The clients behave very much like a browser and the clients start to benefit in the same ways that a browser does. Hypermedia unlocks many scenarios we don’t typically think of when we are accustom to building RPC-style services.</p>
<h1>Conclusion</h1>
<p>What should be clear after reading this is that many of the services you’ve used and interacted with which call their API a REST API don’t exhibit many of the benefits that the true definition of REST provides and violate many of the key constraints. To re-iterate this does not mean the service is poorly built or does not solve the problem set it attempts to solve. It simply means they are not RESTful. REST comes with it own set of advantages and disadvantages. I described some but not all of these in this post.</p>
<p>Understanding how clients could benefit from hypermedia and particularly hypertext was the “AHA” moment for me which encouraged me to look into this deeper. There is so much more to grasp and understand on this topic I’ve only begun.</p>
<p>I highly suggest reading Roy’s <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm" target="_blank">doctoral dissertation</a> where he defines REST.</p>
<p>After spending all weekend on this post I’m going to get some REST myself. Oh who am I kidding…</p>
<img src="http://feeds.feedburner.com/~r/kellabyte/~4/SXNebPmmQpo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kellabyte.com/2011/09/04/clarifying-rest/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		<feedburner:origLink>http://kellabyte.com/2011/09/04/clarifying-rest/</feedburner:origLink></item>
		<item>
		<title>Refactoring to improve maintainability &amp; blendability using IoC part 2: Reducing IoC container coupling</title>
		<link>http://feedproxy.google.com/~r/kellabyte/~3/ThdFpBS3CIU/</link>
		<comments>http://kellabyte.com/2011/07/25/refactoring-to-improve-maintainability-blendability-using-ioc-part-2-reducing-ioc-container-coupling/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 01:29:44 +0000</pubDate>
		<dc:creator>kellabyte</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[composition root]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[inversion of control]]></category>
		<category><![CDATA[ioc]]></category>
		<category><![CDATA[model view viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[view model]]></category>
		<category><![CDATA[viewmodelfactory]]></category>
		<category><![CDATA[viewmodelresolver]]></category>

		<guid isPermaLink="false">http://kellabyte.com/?p=566</guid>
		<description><![CDATA[Recap of part 1 In part 1 of this series we started reducing the Service Locator calls by removing them from the view models and moving it to the ViewModelFactory since this class is responsible for creating the view models. This allowed us to cut the ties in the view models where they were requesting [...]]]></description>
			<content:encoded><![CDATA[<h1><span class="drop">R</span>ecap of part 1</h1>
<p><a href="http://kellabyte.com/2011/07/24/refactoring-to-improve-maintainability-and-blendability-using-ioc-part-1-view-models/" target="_blank">In part 1 of this series</a> we started reducing the Service Locator calls by removing them from the view models and moving it to the <strong><em>ViewModelFactory</em></strong> since this class is responsible for creating the view models. This allowed us to cut the ties in the view models where they were requesting dependencies from a global container.</p>
<p>Some of the benefits we gained from the first iteration of refactoring:</p>
<ol>
<li>Reduced coupling between view models and the IoC container.</li>
<li>View model dependencies are push not pull. This removes coupling between the view model and the source of the dependencies.</li>
<li>View models are more intention revealing. What view models depend on and require is explicit and not hidden.</li>
<li>View models are easier to test. An IoC container isn’t required to initialize a unit test.</li>
</ol>
<p>After all of the changes in the first iteration we still retained Blendability and did not have to compromise the design time experience.</p>
<p>This still left us with a global container which the <strong><em>ViewModelFactory</em></strong> calls to resolve dependencies for view models. Once our view models were up and running again it was time to remove the global container and dramatically reduce the coupling.</p>
<h1>Why a type asking the IoC container to resolve its dependencies is bad</h1>
<p>Having view models use a Service Locator or request the IoC container to compose the dependencies on its behalf couples the view model to the source of its dependencies. The view model does not need the IoC container itself to do any work. It only needs its dependencies. The view model requesting the dependencies has created a requirement on the view model for the IoC container to exist although it is not needed when you look at what the view model is responsible for.</p>
<p>This is brittle and instead should be handled declaratively. With service locator the view model is dependent on the IoC container which needs to be exposed as global state (so that it is accessible) whereas with dependency injection any IoC container can handle the creation.</p>
<p>This includes your tests. The global container is required for your objects to do their work. With dependency injection, testing your view models becomes simpler.</p>
<p>My original idea to minimize the coupling to a <em>specific</em> IoC container was to introduce <strong><em>IServiceLocator</em></strong> so that I could create implementations for various containers like Funq and Unity. What I found out was that it was easy to abstract the resolve methods of these IoC containers but abstracting the register methods was proving to be far more difficult. I was experiencing a lot of friction and it wasn’t clear how I could achieve this.</p>
<p>I learned a couple more things from this experience. It’s really difficult to abstract 2 things that do similar things but are implemented differently. The abstraction can become complex and the benefits start to fade.</p>
<h1>What we did about the IoC container</h1>
<p>As mentioned above rather than abstract the IoC containers with a layer of API that looks just like another IoC container we took the approach of encapsulation. Encapsulating the IoC container instead of abstracting it allowed us to still retain the ability to create new implementations based on different IoC containers but remove the coupling throughout the code base.</p>
<p>We introduced 2 key things to contribute to removing dependency on the IoC container. A <strong><em>Composition Root</em></strong> and <strong><em>ViewModelResolver</em></strong>.</p>
<h1>Composition root</h1>
<p>The concept of a composition root comes from <a href="http://twitter.com/ploeh" target="_blank">Mark Seemann</a>. He writes about it in his <a href="http://affiliate.manning.com/idevaffiliate.php?id=1150_236" target="_blank">excellent book Dependency Injection in .NET</a>. I highly recommend reading it. You won’t regret it!</p>
<p>Mark writes:</p>
<blockquote><p>DEFINITION A Composition Root is a (preferably) unique location in an application where modules are composed together.</p></blockquote>
<blockquote><p>A DI Container should only be referenced from the Composition Root. All other modules should have no reference to the container.</p></blockquote>
<p>We had to bend the rules a <em>little</em> bit to allow for <strong><em>ViewModelResolver</em></strong> but I think you will find it an acceptable solution because we closed off any abilities of container abuse.</p>
<p>If you recall earlier we had an <strong><em>ApplicationHost.Current</em></strong> singleton which exposed an <strong><em>ApplicationHost.Current.Container</em></strong> property so that we could <span style="text-decoration: line-through;">resolve</span> abuse dependencies anywhere we wanted. We replaced this with the Composition Root which we named <strong><em>Bootstrapper</em></strong>.</p>
<h3><span style="color: #ff0000;">Before</span></h3>
<div id="gist-1524422" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">class</span> <span class="nc">ApplicationHost</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="k">static</span> <span class="n">volatile</span> <span class="n">ApplicationHost</span> <span class="n">instance</span><span class="p">;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="k">static</span> <span class="kt">object</span> <span class="n">syncRoot</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Object</span><span class="p">();</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="nf">ApplicationHost</span><span class="p">()</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Execute</span><span class="p">.</span><span class="n">InitializeWithDispatcher</span><span class="p">();</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Initialize the Funq container.</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Container</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Container</span><span class="p">();</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">static</span> <span class="n">ApplicationHost</span> <span class="n">Current</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">get</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">instance</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">lock</span> <span class="p">(</span><span class="n">syncRoot</span><span class="p">)</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">instance</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">instance</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ApplicationHost</span><span class="p">();</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">instance</span><span class="p">;</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="n">Container</span> <span class="n">Container</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">private</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span></div><div class='line' id='LC31'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524422/f4231b05e4e61f170c2ae51fab0622aaf06adb7f/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_01.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524422#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_2_01.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_01.cs</a>
            <a href="https://gist.github.com/1524422">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
<br />
<span style="color: #0000ff;"> </span></p>
<p>As you can see in the singleton I exposed the container making it available for abuse to people writing code against it and coupling to it all over the place. Things can get out of control fast and become difficult to maintain and not flexible to change going forward.</p>
<h3><span style="color: #0000ff;">After</span></h3>
<p><div id="gist-1524426" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">class</span> <span class="nc">Bootstrapper</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="n">Container</span> <span class="n">container</span> <span class="p">=</span> <span class="k">null</span><span class="p">;</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="nf">Bootstrapper</span><span class="p">()</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Initialize</span><span class="p">();</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="k">void</span> <span class="nf">Initialize</span><span class="p">()</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Container</span><span class="p">();</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">IEventAggregator</span><span class="p">&gt;(</span><span class="k">new</span> <span class="n">EventAggregator</span><span class="p">());</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;(</span><span class="k">new</span> <span class="n">Database</span><span class="p">());</span></div><div class='line' id='LC16'><br/></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">MainViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">MainViewModel</span><span class="p">(</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IEventAggregator</span><span class="p">&gt;(),</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;()));</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">BrowserViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">BrowserViewModel</span><span class="p">());</span></div><div class='line' id='LC22'><br/></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">SettingsViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">SettingsViewModel</span><span class="p">(</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;()));</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC26'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524426/2346c2d0573d7560e7497ac4dcd03a2cb0d8e2fc/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_02.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524426#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_2_02.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_02.cs</a>
            <a href="https://gist.github.com/1524426">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>In the <strong><em>Bootstrapper</em></strong> which is our <strong><em>Composition Root</em></strong>, we create the object graph dependencies in the <strong><em>Initialize()</em></strong> method. If you recall in part 1 we modified view models to support constructor injection. Those constructor parameters map to this composition we declare in the <strong><em>Bootstrapper</em></strong>. The types we define will get injected in the constructors of the view models when the IoC container resolves them.</p>
<p>It is important all your dependencies are declared in the <strong><em>Composition Root</em></strong> or else you will get a failure when you try to resolve one of the view models.</p>
<p>Also <em>very</em> key is we have not exposed the IoC container at all. We are completely closing off the ability for arbitrary code being able to call the container directly.</p>
<p>In the <strong><em>App.cs</em></strong> we instantiate the <strong><em>Bootstrapper</em></strong> to kick off the container registrations.</p>
<p>One thing still remains that is referencing the IoC container. In part 1 we moved the container resolve calls from the view models to the <strong><em>ViewModelFactory</em></strong> in our iterative refactoring. Now that view models are not aware of the IoC container and we have a <strong><em>Composition Root</em></strong> in place we need to find a solution to remove the coupling between <strong><em>ViewModelFactory</em></strong> and the IoC container.</p>
<p>We did this by introducing <strong><em>IViewModelResolver.</em></strong></p>
<h1>IViewModelResolver</h1>
<p>My original idea of abstracting the IoC container into an <strong><em>IServiceLocator</em></strong> interface introduced an array of problems such as:</p>
<ol>
<li>Still coupling view models to a generic Service Locator interface. View models still require a Service Locator to do work.</li>
<li>Sacrificing the power of each IoC implementation because the abstract layer on top would be a compromised feature set.</li>
<li>Fighting a lot of friction trying to make API’s that aren’t common work in a common way.</li>
</ol>
<p><strong><em>IViewModelResolver</em></strong> allows us to do the opposite. We can encapsulate the IoC container implementation <em>and</em> leverage that IoC containers full feature set because we aren’t exposing it. We can also implement different IoC container types easily.</p>
<p>In the <strong><em>Composition Root</em></strong> we declared our view models and their dependency composition. The view model resolvers will be used by the <strong><em>ViewModelFactory</em></strong> to resolve the view models. The view models just as before will be injected by their constructors.</p>
<p>As a side benefit we are achieving a better level of <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" target="_blank"><strong><em>Single Responsibility Principle</em></strong></a> because the <strong><em>ViewModelFactory</em></strong> is responsible for providing run time or design time view models and the view model resolvers are responsible for resolving dependencies against a specific IoC container.</p>
<p>The resolver is also very easy to mock making it easier to test the <strong><em>ViewModelFactory</em></strong> than it was when we had the globally exposed IoC container with Service Locator requests.</p>
<p>In our case since we are using Funq, we created a <strong><em>FunqViewModelResolver</em></strong> implementation of <strong><em>IViewModelResolver</em></strong>.<br />
<div id="gist-1524428" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">interface</span> <span class="n">IViewModelResolver</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">IViewModel</span> <span class="nf">Resolve</span><span class="p">();</span></div><div class='line' id='LC4'><span class="p">}</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="k">public</span> <span class="k">class</span> <span class="nc">FunqViewModelResolver</span> <span class="p">:</span> <span class="n">IViewModelResolver</span></div><div class='line' id='LC7'><span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="k">readonly</span> <span class="n">Container</span> <span class="n">container</span><span class="p">;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="n">Dictionary</span><span class="p">&lt;</span><span class="n">type</span>  <span class="p">,</span> <span class="n">MethodInfo</span><span class="p">&gt;</span> <span class="n">methods</span> <span class="p">=</span> <span class="k">null</span><span class="p">;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="n">MethodInfo</span> <span class="n">method</span> <span class="p">=</span> <span class="k">null</span><span class="p">;</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="nf">FunqViewModelResolver</span><span class="p">(</span><span class="n">Container</span> <span class="n">container</span><span class="p">)</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">container</span> <span class="p">=</span> <span class="n">container</span><span class="p">;</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">method</span> <span class="p">=</span> <span class="n">container</span><span class="p">.</span><span class="n">GetType</span><span class="p">().</span><span class="n">GetMethod</span><span class="p">(</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="s">&quot;Resolve&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="n">Type</span><span class="p">[</span><span class="m">0</span><span class="p">]);</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">methods</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Dictionary</span><span class="p">&lt;</span><span class="n">Type</span><span class="p">,</span><span class="n">MethodInfo</span><span class="p">&gt;();</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="n">IViewModel</span> <span class="nf">Resolve</span><span class="p">(</span><span class="n">Type</span> <span class="n">viewModelType</span><span class="p">)</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">MethodInfo</span> <span class="n">genericMethod</span> <span class="p">=</span> <span class="k">null</span><span class="p">;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(!</span><span class="n">methods</span><span class="p">.</span><span class="n">TryGetValue</span><span class="p">(</span><span class="n">viewModelType</span><span class="p">,</span> <span class="k">out</span> <span class="n">genericMethod</span><span class="p">))</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">genericMethod</span> <span class="p">=</span> <span class="n">method</span><span class="p">.</span><span class="n">MakeGenericMethod</span><span class="p">(</span><span class="n">viewModelType</span><span class="p">);</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">methods</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="n">viewModelType</span><span class="p">,</span> <span class="n">genericMethod</span><span class="p">);</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC30'><br/></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">genericMethod</span><span class="p">.</span><span class="n">Invoke</span><span class="p">(</span><span class="n">container</span><span class="p">,</span> <span class="k">null</span><span class="p">);</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="p">(</span><span class="n">IViewModel</span><span class="p">)</span> <span class="n">result</span><span class="p">;</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC34'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524428/c25d10aa0724b66b00dd98eb6fc63b8307fa96a8/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_03.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524428#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_2_03.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_03.cs</a>
            <a href="https://gist.github.com/1524428">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>Now that the Funq version of the resolver is implemented we need to refactor <strong><em>ViewModelFactory</em></strong> again to use the resolvers instead of the Service Locator call that we <span style="text-decoration: line-through;">broke</span> removed when we deleted the globally exposed IoC container.</p>
<h3><span style="color: #ff0000;">Before</span></h3>
<div id="gist-1524433" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">abstract</span> <span class="k">class</span> <span class="nc">ViewModelFactory</span><span class="p">&lt;</span><span class="n">TViewModel</span><span class="p">,</span> <span class="n">TDesignViewModel</span><span class="p">&gt;</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">where</span> <span class="n">TViewModel</span> <span class="p">:</span> <span class="n">IViewModel</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">where</span> <span class="n">TDesignViewModel</span> <span class="p">:</span> <span class="n">TViewModel</span><span class="p">,</span> <span class="k">new</span><span class="p">()</span></div><div class='line' id='LC4'><span class="p">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="nf">ViewModelFactory</span><span class="p">()</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="n">TViewModel</span> <span class="n">ViewModel</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">get</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">TViewModel</span> <span class="n">viewModel</span><span class="p">;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">bool</span> <span class="n">designMode</span> <span class="p">=</span> <span class="n">DetectDesignMode</span><span class="p">();</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">designMode</span><span class="p">)</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// At design time instantiate design time version.</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Populates mock data, dependencies don&#39;t have to be real.</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TDesignViewModel</span><span class="p">();</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Request IoC container to resolve view model &amp; its deps.</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span> <span class="p">=</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ApplicationHost</span><span class="p">.</span><span class="n">Current</span><span class="p">.</span><span class="n">Container</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">TViewModel</span><span class="p">&gt;();</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC28'><br/></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span><span class="p">.</span><span class="n">OnLocated</span><span class="p">();</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">viewModel</span><span class="p">;</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC33'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524433/94bbbd4a062764331a14d9d6e8375b34204ec739/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_04.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524433#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_2_04.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_04.cs</a>
            <a href="https://gist.github.com/1524433">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h3><span style="color: #0000ff;">After</span></h3>
<div id="gist-1524434" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">abstract</span> <span class="k">class</span> <span class="nc">ViewModelFactory</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">protected</span> <span class="k">static</span> <span class="n">IViewModelResolver</span> <span class="n">resolver</span> <span class="p">=</span> <span class="k">null</span><span class="p">;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">static</span> <span class="k">void</span> <span class="nf">InitializeResolver</span><span class="p">(</span><span class="n">IViewModelResolver</span> <span class="n">resolver</span><span class="p">)</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// This is where we set the resolver for the ViewModelFactory.</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// This should get set in the Bootstrapper (Composition Root)</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// to the resolver implementation using the</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// IoC container that has been chosen for the application.</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ViewModelFactory</span><span class="p">.</span><span class="n">resolver</span> <span class="p">=</span> <span class="n">resolver</span><span class="p">;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC12'><span class="p">}</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'><span class="k">public</span> <span class="k">abstract</span> <span class="k">class</span> <span class="nc">ViewModelFactory</span><span class="p">&lt;</span><span class="n">TViewModel</span><span class="p">,</span> <span class="n">TDesignViewModel</span><span class="p">&gt;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">:</span> <span class="n">ViewModelFactory</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">where</span> <span class="n">TViewModel</span> <span class="p">:</span> <span class="n">IViewModel</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">where</span> <span class="n">TDesignViewModel</span> <span class="p">:</span> <span class="n">TViewModel</span><span class="p">,</span> <span class="k">new</span><span class="p">()</span></div><div class='line' id='LC18'><span class="p">{</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="nf">ViewModelFactory</span><span class="p">()</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC22'><br/></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="n">TViewModel</span> <span class="n">ViewModel</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">get</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">TViewModel</span> <span class="n">viewModel</span><span class="p">;</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">bool</span> <span class="n">designMode</span> <span class="p">=</span> <span class="n">DetectDesignMode</span><span class="p">();</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">designMode</span><span class="p">)</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TDesignViewModel</span><span class="p">();</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span></div><div class='line' id='LC35'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">resolver</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">throw</span> <span class="k">new</span> <span class="nf">InvalidOperationException</span><span class="p">();</span></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC40'><br/></div><div class='line' id='LC41'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Use the resolver implementation to resolve</span></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// the view model requested.</span></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span> <span class="p">=</span> <span class="p">(</span><span class="n">TViewModel</span><span class="p">)</span><span class="n">resolver</span><span class="p">.</span><span class="n">Resolve</span><span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="n">TViewModel</span><span class="p">));</span></div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC45'><br/></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span><span class="p">.</span><span class="n">OnLocated</span><span class="p">();</span></div><div class='line' id='LC47'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">viewModel</span><span class="p">;</span></div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC50'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524434/e318c8fcb4efa38ae12bc045d22f2746eb42bdb1/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_05.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524434#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_2_05.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_05.cs</a>
            <a href="https://gist.github.com/1524434">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>We introduced a new static method on the <strong><em>ViewModelFactory</em></strong> named <strong><em>InitializeResolver()</em></strong>. Like the other dependencies in the system we can initialize the <strong><em>ViewModelFactory</em> </strong>with the proper resolver in the <strong><em>Composition Root</em></strong>.</p>
<p>The reason this needs to be static is because the <strong><em>ViewModelFactory</em></strong> is created in XAML as a resource so that it can be statically bound. The static method is to allow the resolver to be injected manually because the <strong><em>ViewModelFactory</em></strong> is not created by the IoC container and cannot resolve it’s dependencies.</p>
<p>Now we need to update the <strong><em>Bootstrapper.Initialize() method.</em></strong></p>
<h3><span style="color: #ff0000;">Before</span></h3>
<div id="gist-1524435" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">private</span> <span class="k">void</span> <span class="nf">Initialize</span><span class="p">()</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Container</span><span class="p">();</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">IEventAggregator</span><span class="p">&gt;(</span><span class="k">new</span> <span class="n">EventAggregator</span><span class="p">());</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;(</span><span class="k">new</span> <span class="n">Database</span><span class="p">());</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">MainViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">MainViewModel</span><span class="p">(</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IEventAggregator</span><span class="p">&gt;(),</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;()));</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">BrowserViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">BrowserViewModel</span><span class="p">());</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">SettingsViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">SettingsViewModel</span><span class="p">(</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;()));</span></div><div class='line' id='LC16'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524435/bebd0e3700337eab381ee640d09e28f812b9e835/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_06.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524435#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_2_06.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_06.cs</a>
            <a href="https://gist.github.com/1524435">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2><span style="color: #0000ff;">After</span></h2>
<div id="gist-1524437" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">private</span> <span class="k">void</span> <span class="nf">Initialize</span><span class="p">()</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Create the IoC container.</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Container</span><span class="p">();</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Initialize the ViewModelFactory with the Funq ViewModelResolver.</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ViewModelFactory</span><span class="p">.</span><span class="n">InitializeResolver</span><span class="p">(</span><span class="k">new</span> <span class="n">FunqViewModelResolver</span><span class="p">(</span><span class="n">container</span><span class="p">));</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Register the dependency composition.</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">IEventAggregator</span><span class="p">&gt;(</span><span class="k">new</span> <span class="n">EventAggregator</span><span class="p">());</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;(</span><span class="k">new</span> <span class="n">Database</span><span class="p">());</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">MainViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">MainViewModel</span><span class="p">(</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IEventAggregator</span><span class="p">&gt;(),</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;()));</span></div><div class='line' id='LC16'><br/></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">BrowserViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">BrowserViewModel</span><span class="p">());</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">SettingsViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">SettingsViewModel</span><span class="p">(</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;()));</span></div><div class='line' id='LC21'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524437/56ac773ad63765b91700216dcdbec7120020825b/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_07.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524437#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_2_07.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_07.cs</a>
            <a href="https://gist.github.com/1524437">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This is how we use the <strong><em>ViewModelFactory</em></strong> in XAML (leaving out the less important markup). This is all created via Blend, no hand written XAML required. A designer can hook this up in a few clicks.<br />
<div id="gist-1524443" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;phone:PhoneApplicationPage</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">xmlns=</span><span class="s">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">xmlns:x=</span><span class="s">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">xmlns:d=</span><span class="s">&quot;http://schemas.microsoft.com/expression/blend/2008&quot;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">xmlns:mc=</span><span class="s">&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">xmlns:vm=</span><span class="s">&quot;clr-namespace:kellafeed.ViewModels&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;phone:PhoneApplicationPage.Resources&gt;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;vm:MainViewModelFactory</span> <span class="na">x:Key=</span><span class="s">&quot;MainViewModelFactgoryDataSource&quot;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">d:IsDataSource=</span><span class="s">&quot;True&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/phone:PhoneApplicationPage.Resources&gt;</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;phone:PhoneApplicationPage.DataContext&gt;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;Binding</span> <span class="na">Mode=</span><span class="s">&quot;OneWay&quot;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">Path=</span><span class="s">&quot;ViewModel&quot;</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">Source=</span><span class="s">&quot;{StaticResource MainViewModelFactgoryDataSource}&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/phone:PhoneApplicationPage.DataContext&gt;</span></div><div class='line' id='LC19'><br/></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;--</span> <span class="err">XAML</span> <span class="err">visuals...</span> <span class="err">--</span><span class="nt">&gt;</span></div><div class='line' id='LC21'><span class="nt">&lt;/phone:PhoneApplicationPage&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524443/6ba961b88c7387ac340149f1ec51192ecfd8c853/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_08.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524443#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_2_08.xml" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_2_08.xml</a>
            <a href="https://gist.github.com/1524443">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>Here’s how that XAML is created by the designer in Blend.</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/08/image.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" src="http://kellabyte.com/wp-content/uploads/2011/08/image_thumb.png" border="0" alt="image" width="354" height="309" /></a></p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/08/image3.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" src="http://kellabyte.com/wp-content/uploads/2011/08/image_thumb3.png" border="0" alt="image" width="276" height="179" /></a><a href="http://kellabyte.com/wp-content/uploads/2011/08/image4.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" src="http://kellabyte.com/wp-content/uploads/2011/08/image_thumb4.png" border="0" alt="image" width="278" height="141" /></a></p>
<p>This is how the view looks with the <strong><em>DataContext</em></strong> set within Expression Blend. Full design time support!</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/08/image5.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" src="http://kellabyte.com/wp-content/uploads/2011/08/image_thumb5.png" border="0" alt="image" width="230" height="378" /></a></p>
<p>After this 2nd iteration of refactoring we have now encapsulated the IoC container and removed it from being globally accessible and abused. This is important because we enforce a clean path rather than opening the container up and allowing people to go in a wrong direction. We do use the IoC container in the <strong><em>ViewModelResolver</em></strong> but it is a specialized implementation to solve a specific problem and does not open the door for abuse.</p>
<p>Our code base is even more maintainable than it was after part 1 which also raises the level of testability. Our view models were already very testable after part 1 but the <strong><em>ViewModelFactory</em></strong> is now testable without requiring an IoC container by mocking the resolvers.</p>
<p>We reduced a lot of coupling and increased testability quite a lot in these 2 iterative refactoring steps!</p>
<p>Stay tuned for more improvements!</p>
<h2>Series</h2>
<ul>
<li><a href="http://kellabyte.com/2011/07/24/refactoring-to-improve-maintainability-and-blendability-using-ioc-part-1-view-models/" target="_blank">Part 1: View Models</a></li>
<li><a href="http://kellabyte.com/2011/07/25/refactoring-to-improve-maintainability-blendability-using-ioc-part-2-reducing-ioc-container-coupling/" target="_blank">Part 2: Reducing IoC container coupling </a></li>
<li>Part 3: Handling special cases like late registrations outside the composition root</li>
<li>Part 4: Unit testing</li>
</ul>
<img src="http://feeds.feedburner.com/~r/kellabyte/~4/ThdFpBS3CIU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kellabyte.com/2011/07/25/refactoring-to-improve-maintainability-blendability-using-ioc-part-2-reducing-ioc-container-coupling/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://kellabyte.com/2011/07/25/refactoring-to-improve-maintainability-blendability-using-ioc-part-2-reducing-ioc-container-coupling/</feedburner:origLink></item>
		<item>
		<title>Refactoring to improve maintainability and blendability using IoC part 1: View Models</title>
		<link>http://feedproxy.google.com/~r/kellabyte/~3/7b6z9BK71Ow/</link>
		<comments>http://kellabyte.com/2011/07/24/refactoring-to-improve-maintainability-and-blendability-using-ioc-part-1-view-models/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 00:26:54 +0000</pubDate>
		<dc:creator>kellabyte</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[inversion of control]]></category>
		<category><![CDATA[ioc]]></category>
		<category><![CDATA[model view viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[view model]]></category>
		<category><![CDATA[viewmodelfactory]]></category>

		<guid isPermaLink="false">http://kellabyte.com/?p=540</guid>
		<description><![CDATA[I’ve been working on a RSS reader Windows Phone 7 application as my 2nd Continuous Client example. I’m not diving into the Continuous Client work in this post so you won’t see any of that included (I will at a later time) but I decided to take this opportunity to improve on my MVVM (Model-View-ViewModel), [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">I</span>’ve been working on a RSS reader Windows Phone 7 application as my 2nd Continuous Client example. I’m not diving into the Continuous Client work in this post so you won’t see any of that included (I will at a later time) but I decided to take this opportunity to improve on my MVVM (Model-View-ViewModel), separation of concerns and writing more maintainable and testable code.</p>
<p>I had a great opportunity to pair program with <a href="http://twitter.com/gblock" target="_blank">Glenn Block</a> and we went through several iterations of refactoring. I’m amazed at his ability to see small parts of a code base and within a couple minutes he has a strategy for decoupling and separating the concerns.</p>
<p>This series of blog posts shouldn’t only be for people interested in MVVM. In general I learned a lot of of values that I’ll take with me in different problem sets and I hope I am successful in translating those into this series.</p>
<p>One thing I’ve struggled with for a long time is how to cut the dependency on the IoC container. I knew that Service Locator code was bad but every time I tried to reduce the coupling I would run into road blocks where I had to expose the container.</p>
<p>We started at the <strong><em>ViewModelFactory&lt;TViewModel, TDesignViewModel&gt;</em></strong> and worked our way out from there.</p>
<h1>What is ViewModelFactory</h1>
<p><strong><em>ViewModelFactory</em></strong> is one of my favourite classes when implementing MVVM. <a href="http://twitter.com/Jredekop" target="_blank">Jeremiah Redekop</a> created <a href="https://github.com/jeremiahredekop/ViewModelFactory/blob/master/WpfApplication1/Support/ViewModelFactory.Shared.cs" target="_blank">this bit of code</a> and I modified it slightly. <strong><em>ViewModelFactory</em></strong> allows us to provide run time view model data and design time view model data. This enables us to have Blendability (ability to see data in Expression Blend as we design our user experiences with a visual tool).</p>
<p>The original version of <strong><em>ViewModelFactory&lt;TViewModel&gt;</em></strong> from Jeremiah provides 2 methods <strong><em>OnLocatedAtRunTime()</em></strong> and <strong><em>OnLocatedAtDesignTime()</em></strong> so that you can separate run time execution (for example querying a REST service) from populating mock data.</p>
<p>I slightly modified <strong><em>ViewModelFactory&lt;TViewModel&gt;</em></strong> to<br />
<strong><em>ViewModelFactory&lt;TViewModel, TDesignViewModel&gt;</em></strong> because I liked the design time implementation to be completely separate from the run time implementation.</p>
<p>The <strong><em>DataContext</em></strong> of the view binds to the <strong><em>ViewModelFactory.ViewModel</em></strong> property, which returns the proper view model based on if it is executing in the designer or runtime.</p>
<h1>What’s wrong with my view models</h1>
<p>Now that we know what <strong><em>ViewModelFactory</em></strong> does, let’s dive into the problems with my implementation and why we started here in the refactoring.</p>
<p>My View models have a bunch of dependencies like Event Aggregator, access to the data store etc. I was using the Service Locator pattern to resolve these dependencies from a Funq container. The view model gets created from the <strong><em>ViewModelFactory</em></strong> which is created in XAML. I didn’t see a way that I could allow constructor injection of these dependencies so this is why I had put in place Service Locator. I knew it was a bad choice, I just didn’t know what to do about it.</p>
<p>To be able to solve this we had to first fix <strong><em>ViewModelFactory</em></strong> since this is where view models get created.</p>
<h1>ViewModelFactory refactor iteration 1</h1>
<p>The fun thing when pairing with Glenn especially with refactoring is he has a very iterative approach which avoids an “OMG the whole thing is busted!” situation. It’s also great for learning experiences because you can see step by step the evolution of the changes that are happening.</p>
<p>The first iteration was to introduce resolving dependencies for view models by the <em><strong>ViewModelFactory</strong></em> since it is currently responsible for creating view models.</p>
<p>This is what the <strong><em>ViewModel</em></strong> property looks like for <strong><em>ViewModelFactory</em></strong> before the first iteration of refactoring. As you can see we are simply serving the run time or design time version of the view model.</p>
<h3><span style="color: #ff0000;">Before</span></h3>
<div id="gist-1524380" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="n">TViewModel</span> <span class="n">ViewModel</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">get</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">TViewModel</span> <span class="n">viewModel</span><span class="p">;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">bool</span> <span class="n">wasCreated</span><span class="p">;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">bool</span> <span class="n">designMode</span> <span class="p">=</span> <span class="n">DetectDesignMode</span><span class="p">();</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">designMode</span><span class="p">)</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TDesignViewModel</span><span class="p">();</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">wasCreated</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">shareInstances</span><span class="p">)</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span> <span class="p">=</span> <span class="p">(</span><span class="n">TViewModel</span><span class="p">)</span><span class="n">sharedInstances</span><span class="p">.</span><span class="n">CreateOrGetValue</span><span class="p">(</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">typeof</span><span class="p">(</span><span class="n">TViewModel</span><span class="p">),</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">()</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">TViewModel</span><span class="p">(),</span> <span class="k">out</span> <span class="n">wasCreated</span><span class="p">);</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TViewModel</span><span class="p">();</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">wasCreated</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC28'><br/></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span><span class="p">.</span><span class="n">OnLocated</span><span class="p">(</span><span class="n">wasCreated</span><span class="p">);</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">viewModel</span><span class="p">;</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC32'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524380/43635097931f296659b6c1ebead478a6465e0048/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_01.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524380#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_1_01.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_01.cs</a>
            <a href="https://gist.github.com/1524380">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The first iteration of refactoring we introduced the IoC container in a Service Locator like fashion so that we could then refactor our view models without breaking everything.</p>
<p>This is how it looked after the changes.</p>
<h3><span style="color: #0000ff;">After</span></h3>
<div id="gist-1524382" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="n">TViewModel</span> <span class="n">ViewModel</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">get</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">TViewModel</span> <span class="n">viewModel</span><span class="p">;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">bool</span> <span class="n">designMode</span> <span class="p">=</span> <span class="n">DetectDesignMode</span><span class="p">();</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">designMode</span><span class="p">)</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// At design time instantiate design time version.</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// This populates mock data, dependencies don&#39;t have to be real.</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TDesignViewModel</span><span class="p">();</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Request IoC container to resolve view model &amp; its deps.</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span> <span class="p">=</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ApplicationHost</span><span class="p">.</span><span class="n">Current</span><span class="p">.</span><span class="n">Container</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">TViewModel</span><span class="p">&gt;();</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">viewModel</span><span class="p">.</span><span class="n">OnLocated</span><span class="p">(</span><span class="n">wasCreated</span><span class="p">);</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">viewModel</span><span class="p">;</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC24'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524382/c7032784530153b74d4d6f9f26ba4341f99d8ff2/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_02.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524382#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_1_02.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_02.cs</a>
            <a href="https://gist.github.com/1524382">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This allowed us to now continue to refactor our view models so that they can support constructor injection for their dependencies. This is how a view model looked before the refactoring.</p>
<h3><span style="color: #ff0000;">Before</span></h3>
<div id="gist-1524391" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">class</span> <span class="nc">MainViewModelFactory</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">:</span> <span class="n">ViewModelFactory</span><span class="p">&lt;</span><span class="n">MainViewModel</span><span class="p">,</span> <span class="n">MainViewModelDesign</span><span class="p">&gt;</span></div><div class='line' id='LC3'><span class="p">{</span></div><div class='line' id='LC4'><span class="p">}</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="c1">/// &lt;summary&gt;</span></div><div class='line' id='LC7'><span class="c1">/// Design time version of the MainViewModel.</span></div><div class='line' id='LC8'><span class="c1">/// &lt;/summary&gt;</span></div><div class='line' id='LC9'><span class="k">public</span> <span class="k">class</span> <span class="nc">MainViewModelDesign</span> <span class="p">:</span> <span class="n">MainViewModel</span></div><div class='line' id='LC10'><span class="p">{</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">OnLocated</span><span class="p">(</span><span class="kt">bool</span> <span class="n">newInstance</span><span class="p">)</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC13'><span class="cp">#if DEBUG</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// This gets called at design time.</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ObservableCollection</span><span class="p">&lt;</span><span class="n">Story</span><span class="p">&gt;();</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">Story</span><span class="p">(</span><span class="s">&quot;The Windows Blog&quot;</span><span class="p">,</span> <span class="s">&quot;Upping our game...&quot;</span><span class="p">));</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">Story</span><span class="p">(</span><span class="s">&quot;Robot Overlords&quot;</span><span class="p">,</span> <span class="s">&quot;Nevada Passes Law...&quot;</span><span class="p">));</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">Story</span><span class="p">(</span><span class="s">&quot;TechCrunch&quot;</span><span class="p">,</span> <span class="s">&quot;eBay Acquired Magento...&quot;</span><span class="p">));</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">Story</span><span class="p">(</span><span class="s">&quot;Inside Facebook&quot;</span><span class="p">,</span> <span class="s">&quot;Facebook Testing...&quot;</span><span class="p">));</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">Story</span><span class="p">(</span><span class="s">&quot;FOSS Patents&quot;</span><span class="p">,</span> <span class="s">&quot;Eastman Kodak v...&quot;</span><span class="p">));</span></div><div class='line' id='LC26'><span class="cp">#endif</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC28'><span class="p">}</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'><span class="k">public</span> <span class="k">class</span> <span class="nc">MainViewModel</span> <span class="p">:</span> <span class="n">BaseViewModel</span></div><div class='line' id='LC31'><span class="p">{</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="n">Database</span> <span class="n">db</span> <span class="p">=</span> <span class="k">null</span><span class="p">;</span></div><div class='line' id='LC33'><br/></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="nf">MainViewModel</span><span class="p">()</span></div><div class='line' id='LC35'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC37'><br/></div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">OnLocated</span><span class="p">(</span><span class="kt">bool</span> <span class="n">newInstance</span><span class="p">)</span></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// This gets called at run time.</span></div><div class='line' id='LC41'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Gah! Bad me. Wish I knew how to fix this.</span></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// I will learn how though!</span></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">db</span> <span class="p">=</span> <span class="n">ApplicationHost</span><span class="p">.</span><span class="n">Current</span><span class="p">.</span><span class="n">Container</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">Database</span><span class="p">&gt;();</span></div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ObservableCollection</span><span class="p">&lt;</span><span class="n">Story</span><span class="p">&gt;();</span></div><div class='line' id='LC45'><br/></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">LoadDatabase</span><span class="p">();</span></div><div class='line' id='LC47'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">GetFeedStories</span><span class="p">();</span></div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC49'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524391/aae5ad51a7c5870b897309cfdca60ab53e563d46/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_03.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524391#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_1_03.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_03.cs</a>
            <a href="https://gist.github.com/1524391">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>As you can see from this view model the Service Locator is used to resolve dependencies. As you can imagine this code is littered through various view models.</p>
<p>Since we moved the Service Locator code to the <strong><em>ViewModelFactory</em></strong>, now we can shed the container dependency from the view models (remember, we are taking an iterative approach, it gets better later, stick with me).</p>
<h3><span style="color: #0000ff;">After</span></h3>
<div id="gist-1524394" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">class</span> <span class="nc">MainViewModelFactory</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">:</span> <span class="n">ViewModelFactory</span><span class="p">&lt;</span><span class="n">MainViewModel</span><span class="p">,</span> <span class="n">MainViewModelDesign</span><span class="p">&gt;</span></div><div class='line' id='LC3'><span class="p">{</span></div><div class='line' id='LC4'><span class="p">}</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="k">public</span> <span class="k">class</span> <span class="nc">MainViewModelDesign</span> <span class="p">:</span> <span class="n">MainViewModel</span></div><div class='line' id='LC7'><span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="nf">MainViewModelDesign</span><span class="p">()</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="k">new</span> <span class="n">EventAggregator</span><span class="p">(),</span> <span class="k">new</span> <span class="n">DesignDatabase</span><span class="p">())</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">OnLocated</span><span class="p">(</span><span class="kt">bool</span> <span class="n">newInstance</span><span class="p">)</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC15'><span class="cp">#if DEBUG</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">Story</span><span class="p">(</span><span class="s">&quot;The Windows Blog&quot;</span><span class="p">,</span> <span class="s">&quot;Upping our game...&quot;</span><span class="p">));</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">Story</span><span class="p">(</span><span class="s">&quot;Robot Overlords&quot;</span><span class="p">,</span> <span class="s">&quot;Nevada Passes Law...&quot;</span><span class="p">));</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">Story</span><span class="p">(</span><span class="s">&quot;TechCrunch&quot;</span><span class="p">,</span> <span class="s">&quot;eBay Acquired Magento...&quot;</span><span class="p">));</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">Story</span><span class="p">(</span><span class="s">&quot;Inside Facebook&quot;</span><span class="p">,</span> <span class="s">&quot;Facebook Testing...&quot;</span><span class="p">));</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">new</span> <span class="nf">Story</span><span class="p">(</span><span class="s">&quot;FOSS Patents&quot;</span><span class="p">,</span> <span class="s">&quot;Eastman Kodak v...&quot;</span><span class="p">));</span></div><div class='line' id='LC26'><span class="cp">#endif</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC28'><span class="p">}</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'><span class="k">public</span> <span class="k">class</span> <span class="nc">MainViewModel</span> <span class="p">:</span> <span class="n">BaseViewModel</span></div><div class='line' id='LC31'><span class="p">{</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="n">IDatabase</span> <span class="n">db</span> <span class="p">=</span> <span class="k">null</span><span class="p">;</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="n">IEventAggregator</span> <span class="n">events</span> <span class="p">=</span> <span class="k">null</span><span class="p">;</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">private</span> <span class="n">Feed</span> <span class="n">feed</span> <span class="p">=</span> <span class="k">null</span><span class="p">;</span></div><div class='line' id='LC35'><br/></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="nf">MainViewModel</span><span class="p">(</span><span class="n">IEventAggregator</span> <span class="n">events</span><span class="p">,</span> <span class="n">IDatabase</span> <span class="n">db</span><span class="p">)</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">events</span> <span class="p">=</span> <span class="n">events</span><span class="p">;</span></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">events</span><span class="p">.</span><span class="n">Subscribe</span><span class="p">(</span><span class="k">this</span><span class="p">);</span></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">db</span> <span class="p">=</span> <span class="n">db</span><span class="p">;</span></div><div class='line' id='LC41'><br/></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">this</span><span class="p">.</span><span class="n">Stories</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ObservableCollection</span><span class="p">&lt;</span><span class="n">Story</span><span class="p">&gt;();</span></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC44'><br/></div><div class='line' id='LC45'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">override</span> <span class="k">void</span> <span class="nf">OnLocated</span><span class="p">(</span><span class="kt">bool</span> <span class="n">newInstance</span><span class="p">)</span></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC47'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">LoadDatabase</span><span class="p">();</span></div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">GetFeedStories</span><span class="p">();</span></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC50'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524394/ea0b56fdae2eec144e7c7bc8026be825272f4c1d/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_04.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524394#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_1_04.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_04.cs</a>
            <a href="https://gist.github.com/1524394">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Interfaces were introduced because at design time we may need to mock some of these dependencies and not use the real types. We created <strong><em>IDatabase</em></strong> and in the generic parameters of the design time view model there is a design version of the database defined. Since we don’t want to be attempting to talk to isolated storage at design time we can implement a mocked version of the interface which does nothing. The mocked database is pretty simple.<br />
<div id="gist-1524398" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">public</span> <span class="k">class</span> <span class="nc">DesignDatabase</span> <span class="p">:</span> <span class="n">IDatabase</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="n">Feed</span> <span class="nf">GetFeed</span><span class="p">()</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="k">new</span> <span class="nf">Feed</span><span class="p">();</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">void</span> <span class="nf">SaveFeed</span><span class="p">(</span><span class="n">Feed</span> <span class="n">feed</span><span class="p">)</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">public</span> <span class="k">void</span> <span class="nf">Reset</span><span class="p">()</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC15'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524398/05bc69ecf1cefebf25d09a0289cc521ee1bed90c/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_05.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524398#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_1_05.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_05.cs</a>
            <a href="https://gist.github.com/1524398">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>The <strong><em>MainViewModel</em></strong> is far more testable now as well in this version. We don’t need to wire up an IoC container to test it’s functionality. We can inject mock dependencies in the constructor and test the view model easily.</p>
<p>If we wanted to we could also place the design time view models and mocks in a separate assembly.</p>
<p>This is what I had in the <strong><em>App.cs</em></strong> to initialize the Funq IoC container.<br />
<div id="gist-1524404" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">private</span> <span class="k">void</span> <span class="nf">InitApplicationHost</span><span class="p">()</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">events</span> <span class="p">=</span> <span class="k">new</span> <span class="n">EventAggregator</span><span class="p">();</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">db</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Database</span><span class="p">();</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ApplicationHost</span><span class="p">.</span><span class="n">Current</span><span class="p">.</span><span class="n">Container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">EventAggregator</span><span class="p">&gt;(</span><span class="n">events</span><span class="p">);</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ApplicationHost</span><span class="p">.</span><span class="n">Current</span><span class="p">.</span><span class="n">Container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">Database</span><span class="p">&gt;(</span><span class="n">db</span><span class="p">);</span></div><div class='line' id='LC8'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524404/37131c2d85af95544dd5cf8665ff6d1ae9756500/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_06.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524404#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_1_06.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_06.cs</a>
            <a href="https://gist.github.com/1524404">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>Since we are now going to be utilizing the IoC container to construct more of the object graph and inject dependencies into the constructors we need to ensure we are registering all the types we need. While we were at it we decided to reduce the amount of calls to <strong><em>ApplicationHost.Container</em></strong> to position ourselves nicely for when we nuke that sucker all together.<br />
<div id="gist-1524408" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">private</span> <span class="k">void</span> <span class="nf">InitApplicationHost</span><span class="p">()</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">var</span> <span class="n">container</span> <span class="p">=</span> <span class="n">ApplicationHost</span><span class="p">.</span><span class="n">Current</span><span class="p">.</span><span class="n">Container</span><span class="p">;</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">db</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Database</span><span class="p">();</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">IEventAggregator</span><span class="p">&gt;(</span><span class="k">new</span> <span class="n">EventAggregator</span><span class="p">());</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;(</span><span class="n">db</span><span class="p">);</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">MainViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">MainViewModel</span><span class="p">(</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IEventAggregator</span><span class="p">&gt;(),</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;()));</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">BrowserViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">BrowserViewModel</span><span class="p">());</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">SettingsViewModel</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">SettingsViewModel</span><span class="p">(</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IDatabase</span><span class="p">&gt;()));</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">container</span><span class="p">.</span><span class="n">Register</span><span class="p">&lt;</span><span class="n">NavigationManager</span><span class="p">&gt;(</span><span class="n">c</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">NavigationManager</span><span class="p">(</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">c</span><span class="p">.</span><span class="n">Resolve</span><span class="p">&lt;</span><span class="n">IEventAggregator</span><span class="p">&gt;()));</span></div><div class='line' id='LC21'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524408/8ca03e18e32afc6f62c17cce0d39a91ea6e1b0e0/Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_07.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524408#file_refactoring_to_improve_maintainability_and_blendability_using_io_c_part_1_07.cs" style="float:right;margin-right:10px;color:#666">Refactoring_to_improve_maintainability_and_blendability_using_IoC_part_1_07.cs</a>
            <a href="https://gist.github.com/1524408">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>We’ve reduced coupling significantly to the IoC container from the view models and as a result as mentioned things are becoming more testable. We still have the service locator code within the <strong><em>ViewModelFactory</em></strong> but in the next part in this series I will discuss how we created <strong><em>ViewModelResolver</em></strong> and a composition root in the form of a bootstrapper to completely eliminate the <strong><em>ApplicationHost.Container</em></strong> property all together.</p>
<p>Things get much cleaner in part 2, stay tuned!</p>
<h2>Series</h2>
<ul>
<li><a href="http://kellabyte.com/2011/07/24/refactoring-to-improve-maintainability-and-blendability-using-ioc-part-1-view-models/" target="_blank">Part 1: View Models</a></li>
<li><a href="http://kellabyte.com/2011/07/25/refactoring-to-improve-maintainability-blendability-using-ioc-part-2-reducing-ioc-container-coupling/" target="_blank">Part 2: Reducing IoC container coupling</a></li>
<li>Part 3: Handling special cases like late registrations outside the  composition root</li>
<li>Part 4: Unit testing</li>
</ul>
<img src="http://feeds.feedburner.com/~r/kellabyte/~4/7b6z9BK71Ow" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kellabyte.com/2011/07/24/refactoring-to-improve-maintainability-and-blendability-using-ioc-part-1-view-models/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://kellabyte.com/2011/07/24/refactoring-to-improve-maintainability-and-blendability-using-ioc-part-1-view-models/</feedburner:origLink></item>
		<item>
		<title>Clarification behind the message that properties are bad. What I learned was: Tell don’t ask</title>
		<link>http://feedproxy.google.com/~r/kellabyte/~3/YIkA7FFUcWU/</link>
		<comments>http://kellabyte.com/2011/07/17/clarification-behind-the-message-that-properties-are-bad-what-i-learned-was-tell-dont-ask/#comments</comments>
		<pubDate>Sun, 17 Jul 2011 17:11:25 +0000</pubDate>
		<dc:creator>kellabyte</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[cqrs]]></category>
		<category><![CDATA[ddd]]></category>
		<category><![CDATA[domain driven design]]></category>

		<guid isPermaLink="false">http://kellabyte.com/?p=533</guid>
		<description><![CDATA[I’ve been groking a lot of DDD/CQRS and as much architecture content as I can as of late and one thing I saw a lot was people that I respect saying that using properties was a bad idea, especially setters. Some people said they could see exceptions when getters were okay but setters should be [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">I</span>’ve been groking a lot of DDD/CQRS and as much architecture content as I can as of late and one thing I saw a lot was people that I respect saying that using properties was a bad idea, especially setters. Some people said they could see exceptions when getters were okay but setters should be avoided.</p>
<p>I understood the message around setters but was confused about the message regarding getters (I’ll get to setters later). I’m not sure if people are repeating messages from others without understanding why or they are simply leaving out the details so I thought this post might have some value for people learning like I am.</p>
<h1>Getters</h1>
<p>The confusion for me around why getters were bad was based on seeing implementations of query methods. This led me to believe that the message people were conveying was getter properties are bad but query methods are okay. This confused me as I see them as the exact same thing.</p>
<p>Today when chatting with <a href="http://twitter.com/yreynhout" target="_blank">Yves Reynhout (@yreynhout)</a> on Twitter I decided to ask “why’ and get to the root of the reasoning. What I learned was my thinking wasn’t incorrect, I misunderstood the message which I think maybe a lot of people do also. Here’s what I learned:</p>
<p>Getter properties containing the same code as the query method comes down to preference. It’s the same thing. Getter properties are not bad.</p>
<p>What people are actually advocating is a tell don’t ask approach to your business logic. They are questioning the use of a query (whether it is a getter or method) to drive your behaviour. This makes a whole lot more sense to me now.</p>
<h1>Setters</h1>
<p>Having for example a collection of items for a shopping cart and simply calling <strong>cart.Items.Add(item)</strong> promotes a CRUD style architecture. When you start writing business logic in the form of setters now your creating an array of getters to support the setters to ensure you can do what you want to do in the setter. You’re also littering your code base with business logic by having to check properties on the cart before adding items to it in the various places the user can add items to their shopping cart.</p>
<p>If your business rules said a user can only purchase 5 items, now you are going to be checking if <strong>cart.Items.Count &gt;=5</strong> throughout your code base. Also your collection isn’t protecting against this rule in any way. On top of this, if you build your business logic with a lot of properties, it becomes an ask then tell style of development. This makes it easy to scatter your business logic and creates a lot of coupling throughout your system as well as reduces your code bases flexibility for change.</p>
<h1>Tell don’t ask</h1>
<p>A tell don’t ask approach to your API and behaviours promotes encapsulation. Your cart object will have queries for displaying information but these won’t be used to drive business logic. You will have command methods to handle this which encapsulate the behaviours that drive your business rules.</p>
<p>Here’s a few quotes I found that describe it well:</p>
<blockquote><p>@lucisferre: @kellabyte @yreynhout the idea is to hand off the responsibility to the classes responsible, rather than query them and decide for them</p></blockquote>
<p>Alec Sharp states:</p>
<blockquote><p>Procedural code gets information then makes decisions. Object-oriented code tells objects to do things.</p></blockquote>
<p>I think Alec’s message makes a whole lot of sense. Writing good object-oriented code allows us to encapsulate the responsibilities of the cart within the cart itself rather than exposing state and allowing everyone else to implement the business rules outside of the cart.</p>
<h1>Conclusion</h1>
<p>I think queries in the form of getters or methods returning a value have a place. Some cases it just makes sense. Some times an outside object <em>does</em> need to know about something from another object to make its decisions.</p>
<p>In most cases by designing your objects around encapsulating behaviour which that object is responsible for you are creating a much cleaner and maintainable code base.</p>
<p>As Yves said on Twitter, when creating a query, think about why.</p>
<p>It may have reason to be there but you should be able to identify the times you are exposing state to create behaviours that belongs to an object outside of it’s scope. In these times you should pull it back into the object’s responsibilities.</p>
<p>I suggest reading Yves blog post on <a href="http://seabites.wordpress.com/2010/10/31/guards-and-queries-in-the-domain-model/" target="_blank">Guards and Queries in the domain model</a>. It has some great code examples. It’s geared to DDD but the same principals apply to any business logic behaviour in my opinion.</p>
<img src="http://feeds.feedburner.com/~r/kellabyte/~4/YIkA7FFUcWU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kellabyte.com/2011/07/17/clarification-behind-the-message-that-properties-are-bad-what-i-learned-was-tell-dont-ask/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://kellabyte.com/2011/07/17/clarification-behind-the-message-that-properties-are-bad-what-i-learned-was-tell-dont-ask/</feedburner:origLink></item>
		<item>
		<title>Continuous Client: My first attempt at multi-device user experience transitions</title>
		<link>http://feedproxy.google.com/~r/kellabyte/~3/nlFakmdCBZI/</link>
		<comments>http://kellabyte.com/2011/06/26/continuous-client-my-first-attempt-at-multi-device-user-experience-transitions/#comments</comments>
		<pubDate>Sun, 26 Jun 2011 18:53:21 +0000</pubDate>
		<dc:creator>kellabyte</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[User Experience]]></category>
		<category><![CDATA[azure]]></category>
		<category><![CDATA[azure service bus]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[continuous client]]></category>
		<category><![CDATA[cqrs]]></category>
		<category><![CDATA[event sourcing]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[service bus]]></category>
		<category><![CDATA[windows phone 7]]></category>

		<guid isPermaLink="false">http://kellabyte.com/?p=502</guid>
		<description><![CDATA[Let’s show you the goods before you read how I achieved it. I present my first attempt at continuous client in the form of a video player that allows user experience transitions to cross between a Windows Phone 7 device, an iPhone and a Silverlight web player. Is that not crazy awesome? I was amazed [...]]]></description>
			<content:encoded><![CDATA[<p><span class="drop">L</span>et’s show you the goods before you read how I achieved it. I present my first attempt at continuous client in the form of a video player that allows user experience transitions to cross between a Windows Phone 7 device, an iPhone and a Silverlight web player.    </p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:1204a4fa-dfec-4dd8-99e8-b83cca4c22cb" class="wlWriterEditableSmartContent">
<div><object width="590" height="331"><param name="movie" value="http://www.youtube.com/v/sOBOXMDG7w4?hl=en&amp;hd=1"></param><embed src="http://www.youtube.com/v/sOBOXMDG7w4?hl=en&amp;hd=1" type="application/x-shockwave-flash" width="590" height="331"></embed></object></div>
</div>
<h3>Is that not crazy awesome? I was amazed at how simple it was. Below is how I did it. I can’t wait to work on a more elaborate demo. Stay tuned! Follow my Twitter and this blog.</h3>
<h1>How I got to this point</h1>
<p>My experiments in continuous client architecture has started to gain a lot of traction since I have been tweeting and creating working code and videos. My user experience rant on the potential of continuous client has spanned almost a calendar year now and for the people just following along recently based on my last video I’d like to recap a bit of history.</p>
<p>In <a href="http://kellabyte.com/2010/07/09/the-cloud-a-perfect-compliment-to-mobile/" target="_blank">July 2010 I posted my thoughts</a> of how I thought the cloud plays a key role in continuous client. The cloud helps bridge the gap between cloud hosted code and on-premise hosted code. On-premise in the case of continuous client is devices such as PC’s, tablets, smart phones and any other device. At the time I wasn’t exactly sure of a way to implement it architecturally without introducing large amounts of complexity and synchronization code.</p>
<p>In the last 5 months I’ve been researching and learning enterprise integration patterns and a pattern called CQRS. Fast forward to a month ago (May 2011) and the stars seemed to align in my head one day. <a href="http://kellabyte.com/2011/05/26/continuous-client-our-multi-device-dream-but-how-do-we-build-it/" target="_blank">I wrote a post</a> about combining some principals of both which helps us build an architecture that allows us to fairly easily implement some continuous client experiences. It dawned on me that the challenge is really an integration issue.</p>
<h1>How did I build it</h1>
<p>Enterprise integration patterns brings the value of integrating multiple apps, services and devices through messaging patterns. CQRS gives us the value of building an event driven architecture centered around our state changes. In CQRS the domain mutations only occur as a result of an event being handled. This allows you to support event sourcing where you can get replay ability. In this case we aren’t so much worried about a domain but the user experience state.</p>
<p>Let’s put these together to enable continuous client:</p>
<ul>
<li>Every user interaction generates an event. The application handles the event and mutates internal state. </li>
<li>The event gets sent as a message to a service bus hosted in the cloud with a copy per device we want the user experience to be capable of transitioning to. </li>
<li>The cloud hosted service bus enqueues these messages in a separate queue per device. This allows for each device to event source what happened on the other devices. </li>
</ul>
<p>It would look something like this:</p>
<p><a href="http://kellabyte.com/wp-content/uploads/2011/06/image2.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://kellabyte.com/wp-content/uploads/2011/06/image_thumb2.png" width="582" height="395" /></a></p>
<p>This gives us a system where event sourcing is essentially automating the user experience and since our application code is developed to handle events natively it operates the same. Another key thing to note is that the cloud infrastructure is holding the queue of events. Devices don’t have to be running the application to take part in the state transitions. Reliable messaging allows us to consume these messages whenever the application starts up.</p>
<p>To prove this out I developed a video player demo as my first attempt at continuous client. The video player has 2 basic functions which are play and pause. I created 2 port’s. One for Windows Phone 7 and Silverlight and my friend <a href="http://twitter.com/#!/pilky" target="_blank">Martin Pilkington</a> helped with the iOS iPhone port (thanks Martin!). </p>
<p>We utilized the new Azure Service Bus cloud platform for the messaging infrastructure. The upcoming Azure Service Bus platform has support for topics. I used a topic per user and separate subscription per device. This gives a queue per device, each having their own copy of the messages pushed into the topic. Filtering based on device id was used so that a device would only get messages generated by other devices and not self-generated messages.</p>
<p>In a weekend I had my first continuous client demo working. Most of that time was spent learning Azure Service Bus and writing ports. The actual application code was very simple and I used the <a href="http://twitter.com/#!/gregyoung" target="_blank">Greg Young’s</a> CQRS sample as a reference point.</p>
<h1>Show us the code</h1>
<p>One thing to keep in mind here in the Windows Phone 7 and Silverlight code is I did not implement this using the MVVM pattern. <em><font color="#ff0000">I’m a fan of MVVM but the purpose is to keep the sample code as simple as possible.</font></em> Implementing this in MVVM doesn’t change a whole lot.</p>
<p>Here is the WP7 and Silverlight web application code. It is exactly the same and shared between both:</p>
<pre class="brush: csharp; auto-links: false;">using System;
using System.Windows;
using System.Windows.Media;
using Microsoft.Phone.Controls;

using VideoDemo.Messaging;

namespace VideoDemo
{
    public partial class MainPage : PhoneApplicationPage,
        IHandle&lt;PlayMessage&gt;,
        IHandle&lt;PauseMessage&gt;
    {
        private EventAggregator events;
        private TimeSpan duration;

        public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            this.buttonPlay.Click +=
                new RoutedEventHandler(buttonPlay_Click);
            this.buttonPause.Click +=
                new RoutedEventHandler(buttonPause_Click);
            media.CurrentStateChanged +=
                new RoutedEventHandler(media_CurrentStateChanged);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            events = App.Container.Resolve&lt;EventAggregator&gt;l;();
            events.Init();
            events.Subscribe(this);

            media.Source = new Uri(&quot;http://someurl/tron.wmv&quot;);

        }

        void media_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            switch (media.CurrentState)
            {
                case MediaElementState.Playing:
                    media.Position = duration;
                    break;
            }
        }

        void buttonPlay_Click(object sender, RoutedEventArgs e)
        {
            // Here we publish the interaction as an event to Azure
            // ServiceBus. We don't do any mutation of state without
            // the event going through our event mechanism. This
            // ensures all our code when event sourced from Azure
            // ServiceBus runs as-is with the native behaviors of the app.
            events.Publish(new PlayMessage(media.Position));
        }

        void buttonPause_Click(object sender, RoutedEventArgs e)
        {
            // Same as above.
            events.Publish(new PauseMessage(media.Position));
        }

        public void Handle(PlayMessage message)
        {
            // Since we are handling the even there we can mutate
            // state. Live interactions or event sourced events from
            // Azure ServiceBus will all go through this path. It is the
            // native behavior of the app.
            duration = message.Duration;
            media.Play();
        }

        public void Handle(PauseMessage message)
        {
            // Same as above.
            duration = message.Duration;
            media.Pause();
        }
    }
}

// Here are the message types.
public class PlayMessage : Message
{
    public TimeSpan Duration { get; set; }

    public PlayMessage()
    {
    }

    public PlayMessage(TimeSpan duration)
    {
        this.Duration = duration;
    }
}

public class PauseMessage : Message
{
    public TimeSpan Duration { get; set; }

    public PauseMessage()
    {
    }

    public PauseMessage(TimeSpan duration)
    {
        this.Duration = duration;
    }
}</pre>
<p>Notice here when the interaction event occurs all we do is fire an event. This event is self handled. The events published to the EventAggregator get sent to Azure Service Bus so that all messages are delivered to the topic which each device subscribes to.</p>
<p>I am receiving messages from Azure Service Bus over HTTP. I serialize messages as JSON and when a new message is received we deserialize the JSON into the message type and pump them through the EventAggregator.</p>
<pre class="brush: csharp; auto-links: false;">subscription.MessageRecieved +=
    new EventHandler&lt;SubscriptionMessageEventArgs&gt;(messageRecieved);

subscription.ReceiveMessage(10);

void messageRecieved(object sender, SubscriptionMessageEventArgs e)
{
    EnvelopeMessage msg =
        JsonConvert.DeserializeObject&lt;EnvelopeMessage&gt;(e.Message);

    Type type = MessageMapper.GetType(msg.MessageId);

    object message = JsonConvert.DeserializeObject(
        msg.MessageContent.ToString(), type);

    Publish(message);
    subscription.ReceiveMessage(10);
}</pre>
<p>Since a user interaction is publishing a message to the EventAggregator and our Azure Service Bus topic subscription is also publishing to the EventAggregator our application code is now architected to be natively event driven and our application code has no concept of the source of the event.</p>
<p>Messages have an id that is canonical so that it is not specific to any platform. I chose to use a URI format such as <font style="background-color: #ffff00"></font><font color="#ff0000">http://kellabyte.com/samples/VideoDemo/Messages/PlayMessage</font><font style="background-color: #ffff00"></font><font style="background-color: #ffff00"></font> <font style="background-color: #ffff00"></font>and we use a MessageMapper to get the app specific type that message relates to for deserialization purposes since we can’t share types across platforms who don’t share languages. This could probably be done cleaner, I didn’t spend much time here since it wasn’t the primary focus of the sample.</p>
<p>I’m not covering all the Azure Service Bus code in this post there is already a lot of content out there for Azure. If you feel I should blog about it I can do a specific post for that, let me know!</p>
<p>It’s really very simple and straight forward. As long as you adhere to the rules your application operates the exact same whether it is interactions as a result of a user action or event sourced from the cloud.</p>
<p>Here&#8217;s Martin’s Objective-C iOS application code for the iPhone: </p>
<pre class="brush: csharp; auto-links: false;">#import &quot;Video_DemoViewController.h&quot;
#import &quot;AWSServiceController.h&quot;

NSString *M3PlaybackTime = @&quot;Duration&quot;;
NSString *M3PlayMessage =
    @&quot;http://kellabyte.com/samples/VideoDemo/Messages/PlayMessage&quot;;
NSString *M3PauseMessage =
    @&quot;http://kellabyte.com/samples/VideoDemo/Messages/PauseMessage&quot;;

@interface Video_DemoViewController () 

- (MPMoviePlayerController *)_moviePlayer;

@end

@implementation Video_DemoViewController 

@synthesize serviceController;
@synthesize contentView;

/***************************
 Set up
 **************************/
- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self addObserver:self
               forKeyPath:@&quot;serviceController&quot;
                  options:NSKeyValueObservingOptionOld
                  context:NULL];
    }
    return self;
}

/***************************
 Update notifications when the service controller changes
 **************************/
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    if ([keyPath isEqualToString:@&quot;serviceController&quot;]) {
        [[SAFE_NULL([change objectForKey:NSKeyValueChangeOldKey])
                    notificationCenter] removeObserver:self];

        id centre = [[self serviceController] notificationCenter];

        [centre addObserver:self
                   selector:@selector(playMovie:)
                       name:M3PlayMessage
                     object:nil];

        [centre addObserver:self
                   selector:@selector(pauseMovie:)
                       name:M3PauseMessage
                     object:nil];
    }
}

/***************************
 Set up the movie player
 **************************/
- (void)viewDidLoad {
    MPMoviePlayerController *player = [self _moviePlayer];
    [[player view] setFrame:[[self contentView] bounds]];
    [[self contentView] addSubview:[player view]];
    [super viewDidLoad];
}

/***************************
 Set up and return the movie player
 **************************/
- (MPMoviePlayerController *)_moviePlayer {
    if (!moviePlayer) {
        NSURL *url = [NSURL URLWithString:
        @&quot;http://someurl/tron.m4v&quot;];
        moviePlayer = [[MPMoviePlayerController alloc]
                                  initWithContentURL:url];

        [moviePlayer setShouldAutoplay:NO];
        [moviePlayer setControlStyle:MPMovieControlStyleNone];
    }
    return moviePlayer;
}

/***************************
 Handle a play notification
 **************************/
- (void)playMovie:(NSNotification *)aNote {
    NSNumber *time = [[aNote userInfo] objectForKey:M3PlaybackTime]
    CGFloat playbackTime = [time floatValue];

    [[self _moviePlayer] setCurrentPlaybackTime:playbackTime];
    [[self _moviePlayer] play];
}

/***************************
 Handle a pause notification
 **************************/
- (void)pauseMovie:(NSNotification *)aNote {
    NSNumber *time = [[aNote userInfo] objectForKey:M3PlaybackTime]
    CGFloat playbackTime = [time floatValue];

    [[self _moviePlayer] setCurrentPlaybackTime:playbackTime];
    [[self _moviePlayer] pause];
}

/***************************
 Post a play notification when the user presses the play button
 **************************/
- (IBAction)play:(id)sender {
    CGFloat time = [[self _moviePlayer] currentPlaybackTime];
    NSNumber *timeObj = [NSNumber numberWithFloat:time];
    id info = [NSDictionary dictionaryWithObject:timeObj
                                          forKey:M3PlaybackTime]

    id centre = [[self serviceController] notificationCenter];
    [centre postNotificationName:M3PlayMessage
                          object:nil
                        userInfo:info];
}

/***************************
 Post a pause notification when the user presses the pause button
 **************************/
- (IBAction)pause:(id)sender {
    CGFloat time = [[self _moviePlayer] currentPlaybackTime];
    NSNumber *timeObj = [NSNumber numberWithFloat:time];
    id info = [NSDictionary dictionaryWithObject:timeObj
                                          forKey:M3PlaybackTime]

    id centre = [[self serviceController] notificationCenter];
    [centre postNotificationName:M3PauseMessage
                          object:nil
                        userInfo:info];
}

/***************************
 Clean up
 **************************/
- (void)dealloc {
    [contentView release];
    [super dealloc];
}

- (void)viewDidUnload {
    [self setContentView:nil];
    [super viewDidUnload];
}
@end</pre>
<p>Keep an eye out because I will be working on a more elaborate continuous client sample! I’ve begun work on a mobile RSS reader so that I can prove that the architecture shown here can carry over to all kinds of applications while enabling all sorts of user experience transitions.</p>
<p>If you would like to help port it to a specific platform and work with me on the continuous client concepts feel free to contact me on Twitter <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://kellabyte.com/wp-content/uploads/2011/06/wlEmoticon-smile.png" /></p>
<p>Shout out to <a href="http://twitter.com/#!/pilky" target="_blank">Martin Pilkington</a> for working on the iOS port in parallel with me and the Azure Service Bus team (<a href="http://twitter.com/#!/clemensv" target="_blank">Clemens Vasters</a> and <a href="http://twitter.com/#!/willpe" target="_blank">Will Perry</a>) for answering my questions on Twitter and logging some bugs that I encountered. I really enjoyed working with Azure Service Bus. It’s very easy and handy to use on multiple platforms.</p>
<p>I’m excited about this progress. Stay tuned for more continuous client!</p>
<img src="http://feeds.feedburner.com/~r/kellabyte/~4/nlFakmdCBZI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kellabyte.com/2011/06/26/continuous-client-my-first-attempt-at-multi-device-user-experience-transitions/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://kellabyte.com/2011/06/26/continuous-client-my-first-attempt-at-multi-device-user-experience-transitions/</feedburner:origLink></item>
	</channel>
</rss>

