<?xml version="1.0" encoding="iso-8859-1"?><!-- generator="b2evolution/4.0.3" -->
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Desktop Developer - Author(s): Eli Weinstock-Herman (tarwn)</title>
		<link>http://blogs.lessthandot.com/index.php/DesktopDev/</link>
		<atom:link rel="self" type="application/rss+xml" href="http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2" />
		<description></description>
		<language>en-GB</language>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<admin:generatorAgent rdf:resource="http://b2evolution.net/?v=4.0.3"/>
		<ttl>60</ttl>
				<item>
			<title>Intellisense for custom XML in Visual Studio</title>
			<link>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/vs2012/intellisense-for-custom-xml-in</link>
			<pubDate>Mon, 06 May 2013 13:44:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Vistual Studio 2012</category>			<guid isPermaLink="false">2191@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;This is one of those things that I only need once every several months and always forget how to do. Working with custom XML in Visual Studio is a whole lot easier with intellisense. Schema errors are highlighted, enumerated values are displayed, and the amount of typing goes from full tag names to just a few characters followed by tab to complete.&lt;/p&gt;

&lt;p&gt;There are a few ways to do this, but if you have a schema (XSD) for the file, then here are the steps to make the magic happen.&lt;/p&gt;

&lt;h2&gt;Sample Files&lt;/h2&gt;
&lt;p&gt;So let&#039;s assume for the moment that you have the following sample files:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;AwesomeFile.xml&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;xml&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;xml&quot; id=&quot;cb40035&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;?xml&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;version&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;encoding&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;AwesomeList&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;AwesomeItem&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;AwesomenessFactor&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;11&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;AwesomeType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Is Awesome&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/AwesomeType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/AwesomeItem&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/AwesomeList&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb13916&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Awesome.xsd&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;xml&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;xml&quot; id=&quot;cb76360&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;?xml&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;version&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;encoding&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:schema&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;MyAwesomeSchema&amp;quot;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000066;&quot;&gt;xmlns:xs&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:simpleType&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeLevel&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:restriction&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;base&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;xs:integer&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:enumeration&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:enumeration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:enumeration&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;5&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:enumeration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:enumeration&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:enumeration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:enumeration&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;11&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:enumeration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:restriction&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:simpleType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:complexType&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeElement&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:sequence&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeType&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;xs:string&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:element&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:sequence&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:attribute&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomenessFactor&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeLevel&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;use&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;required&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:attribute&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:complexType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeList&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:complexType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:sequence&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeItem&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeElement&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;minOccurs&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;maxOccurs&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;unbounded&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:element&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:sequence&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:complexType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:element&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:schema&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb5224&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Visual Studio gives us handy intellisense suggestions and warnings when we&#039;re writing the schema because we have specified a namespace it knows, but how do we get that usefulness when we&#039;re adding more content to our awesome XML file?&lt;/p&gt;

&lt;h2&gt;More Cowbell&lt;/h2&gt;
&lt;p&gt;Turns out, adding this functionality is pretty easy. Visual Studio is smart enough to use know schemas, so all we have to do is provide the information that makes our schema known and relevant for our XML file. &lt;/p&gt;

&lt;p&gt;For this example I am assuming that the two files are in the same folder. I&#039;ve also made the file complex enough that the additions we make should work for far more complex setups also.&lt;/p&gt;

&lt;p&gt;Here&#039;s the steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Define a target namespace on the schema&lt;/li&gt;
&lt;li&gt;Define the empty namespace of the schema as this namespace&lt;/li&gt;
&lt;li&gt;set attributeFormDefault to unqualified so attributes in our XML file won&#039;t require namespace declarations&lt;/li&gt;
&lt;li&gt;Add the namespace declaration to the XML file&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Updating our files (and adding comments to reflect the list above), we have:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;AwesomeFile.xml&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;xml&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;xml&quot; id=&quot;cb58352&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;?xml&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;version&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;encoding&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;AwesomeList&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;xmlns&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;my://awesomeness&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;lt;!-- (4) --&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;AwesomeItem&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;AwesomenessFactor&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;11&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;AwesomeType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Is Awesome&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/AwesomeType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/AwesomeItem&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/AwesomeList&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb96735&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Awesome.xsd&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;xml&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;xml&quot; id=&quot;cb35048&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;?xml&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;version&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;encoding&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:schema&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;MyAwesomeSchema&amp;quot;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000066;&quot;&gt;xmlns:xs&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000066;&quot;&gt;targetNamespace&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;my://awesomeness&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;lt;!-- (1) --&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; xmlns=&amp;quot;my://awesomeness&amp;quot; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;lt;!-- (2) --&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; attributeFormDefault=&amp;quot;unqualified&amp;quot; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;lt;!-- (3) --&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; elementFormDefault=&amp;quot;qualified&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:simpleType&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeLevel&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:restriction&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;base&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;xs:integer&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:enumeration&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:enumeration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:enumeration&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;5&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:enumeration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:enumeration&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:enumeration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:enumeration&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;11&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:enumeration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:restriction&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:simpleType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:complexType&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeElement&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:sequence&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeType&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;xs:string&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:element&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:sequence&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:attribute&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomenessFactor&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeLevel&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;use&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;required&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:attribute&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:complexType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeList&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:complexType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:sequence&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeItem&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;AwesomeElement&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;minOccurs&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;maxOccurs&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;unbounded&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:element&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:sequence&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:complexType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:element&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/xs:schema&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb94736&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And there we have it.&lt;/p&gt;

&lt;h2&gt;Results&lt;/h2&gt;

&lt;p&gt;Now when we start typing in the XML file we will get intellisense suggestions/completion:&lt;/p&gt;

&lt;div style=&quot;text-align:center; margin: .5em 0;&quot;&gt;
   &lt;img src=&quot;http://tiernok.com/LTDBlog/XmlSchemaIntellisense/Intellisense.png&quot; alt=&quot;Intellisense suggestions&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;We also get warnings when we forget a required attribute:&lt;/p&gt;

&lt;div style=&quot;text-align:center; margin: .5em 0;&quot;&gt;
   &lt;img src=&quot;http://tiernok.com/LTDBlog/XmlSchemaIntellisense/SchemaWarning.png&quot; alt=&quot;Intellisense suggestions&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;And when we use the wrong type:&lt;/p&gt;

&lt;div style=&quot;text-align:center; margin: .5em 0;&quot;&gt;
   &lt;img src=&quot;http://tiernok.com/LTDBlog/XmlSchemaIntellisense/WrongTypeWarning.png&quot; alt=&quot;Intellisense suggestions&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;&lt;i&gt;Hopefully the custom color scheme isn&#039;t confusing, was feeling too lazy to switch it and switch it back&lt;/i&gt;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/vs2012/intellisense-for-custom-xml-in&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>This is one of those things that I only need once every several months and always forget how to do. Working with custom XML in Visual Studio is a whole lot easier with intellisense. Schema errors are highlighted, enumerated values are displayed, and the amount of typing goes from full tag names to just a few characters followed by tab to complete.</p>

<p>There are a few ways to do this, but if you have a schema (XSD) for the file, then here are the steps to make the magic happen.</p>

<h2>Sample Files</h2>
<p>So let's assume for the moment that you have the following sample files:</p>

<p><b>AwesomeFile.xml</b></p>
<div class="codebox"><div class="codeheader"><span>xml</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb46747'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb46747','cb48814'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="xml" id="cb46747" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="font-weight: bold; color: black;">?&gt;</span></span></li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;AwesomeList<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;AwesomeItem</span> <span style="color: #000066;">AwesomenessFactor</span>=<span style="color: #ff0000;">&quot;11&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;AwesomeType<span style="font-weight: bold; color: black;">&gt;</span></span></span>Is Awesome<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/AwesomeType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/AwesomeItem<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/AwesomeList<span style="font-weight: bold; color: black;">&gt;</span></span></span></li></ol></div><div id="cb48814" style="display: none; color: red;"></div></div></div>

<p><b>Awesome.xsd</b></p>
<div class="codebox"><div class="codeheader"><span>xml</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb47696'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb47696','cb67629'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="xml" id="cb47696" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="font-weight: bold; color: black;">?&gt;</span></span></li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:schema</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;MyAwesomeSchema&quot;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000066;">xmlns:xs</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:simpleType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomeLevel&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:restriction</span> <span style="color: #000066;">base</span>=<span style="color: #ff0000;">&quot;xs:integer&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:enumeration<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:enumeration<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:enumeration<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;11&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:enumeration<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:restriction<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:simpleType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:complexType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomeElement&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomeType&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xs:string&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:element<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomenessFactor&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;AwesomeLevel&quot;</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;required&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:attribute<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomeList&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomeItem&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;AwesomeElement&quot;</span> <span style="color: #000066;">minOccurs</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">maxOccurs</span>=<span style="color: #ff0000;">&quot;unbounded&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:element<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:element<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:schema<span style="font-weight: bold; color: black;">&gt;</span></span></span></li></ol></div><div id="cb67629" style="display: none; color: red;"></div></div></div>

<p>Visual Studio gives us handy intellisense suggestions and warnings when we're writing the schema because we have specified a namespace it knows, but how do we get that usefulness when we're adding more content to our awesome XML file?</p>

<h2>More Cowbell</h2>
<p>Turns out, adding this functionality is pretty easy. Visual Studio is smart enough to use know schemas, so all we have to do is provide the information that makes our schema known and relevant for our XML file. </p>

<p>For this example I am assuming that the two files are in the same folder. I've also made the file complex enough that the additions we make should work for far more complex setups also.</p>

<p>Here's the steps:</p>
<ol>
<li>Define a target namespace on the schema</li>
<li>Define the empty namespace of the schema as this namespace</li>
<li>set attributeFormDefault to unqualified so attributes in our XML file won't require namespace declarations</li>
<li>Add the namespace declaration to the XML file</li>
</ol>

<p>Updating our files (and adding comments to reflect the list above), we have:</p>

<p><b>AwesomeFile.xml</b></p>
<div class="codebox"><div class="codeheader"><span>xml</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb18577'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb18577','cb27458'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="xml" id="cb18577" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="font-weight: bold; color: black;">?&gt;</span></span></li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;AwesomeList</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;my://awesomeness&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span> <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- (4) --&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;AwesomeItem</span> <span style="color: #000066;">AwesomenessFactor</span>=<span style="color: #ff0000;">&quot;11&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;AwesomeType<span style="font-weight: bold; color: black;">&gt;</span></span></span>Is Awesome<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/AwesomeType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/AwesomeItem<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/AwesomeList<span style="font-weight: bold; color: black;">&gt;</span></span></span></li></ol></div><div id="cb27458" style="display: none; color: red;"></div></div></div>

<p><b>Awesome.xsd</b></p>
<div class="codebox"><div class="codeheader"><span>xml</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb3910'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb3910','cb17786'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="xml" id="cb3910" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="font-weight: bold; color: black;">?&gt;</span></span></li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:schema</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;MyAwesomeSchema&quot;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000066;">xmlns:xs</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">targetNamespace</span>=<span style="color: #ff0000;">&quot;my://awesomeness&quot;</span> <span style="color: #808080; font-style: italic;">&lt;!-- (1) --&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; xmlns=&quot;my://awesomeness&quot; <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- (2) --&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; attributeFormDefault=&quot;unqualified&quot; <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- (3) --&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; elementFormDefault=&quot;qualified&quot;&gt;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:simpleType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomeLevel&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:restriction</span> <span style="color: #000066;">base</span>=<span style="color: #ff0000;">&quot;xs:integer&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:enumeration<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:enumeration<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:enumeration<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:enumeration</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;11&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:enumeration<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:restriction<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:simpleType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:complexType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomeElement&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomeType&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xs:string&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:element<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomenessFactor&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;AwesomeLevel&quot;</span> <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;required&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:attribute<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomeList&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;xs:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;AwesomeItem&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;AwesomeElement&quot;</span> <span style="color: #000066;">minOccurs</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">maxOccurs</span>=<span style="color: #ff0000;">&quot;unbounded&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:element<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:sequence<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:complexType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:element<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/xs:schema<span style="font-weight: bold; color: black;">&gt;</span></span></span></li></ol></div><div id="cb17786" style="display: none; color: red;"></div></div></div>

<p>And there we have it.</p>

<h2>Results</h2>

<p>Now when we start typing in the XML file we will get intellisense suggestions/completion:</p>

<div style="text-align:center; margin: .5em 0;">
   <img src="http://tiernok.com/LTDBlog/XmlSchemaIntellisense/Intellisense.png" alt="Intellisense suggestions" />
</div>

<p>We also get warnings when we forget a required attribute:</p>

<div style="text-align:center; margin: .5em 0;">
   <img src="http://tiernok.com/LTDBlog/XmlSchemaIntellisense/SchemaWarning.png" alt="Intellisense suggestions" />
</div>

<p>And when we use the wrong type:</p>

<div style="text-align:center; margin: .5em 0;">
   <img src="http://tiernok.com/LTDBlog/XmlSchemaIntellisense/WrongTypeWarning.png" alt="Intellisense suggestions" />
</div>

<p><i>Hopefully the custom color scheme isn't confusing, was feeling too lazy to switch it and switch it back</i></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/vs2012/intellisense-for-custom-xml-in">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/vs2012/intellisense-for-custom-xml-in#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2&#38;disp=comments&#38;p=2191</wfw:commentRss>
		</item>
				<item>
			<title>Azure Worker Role - Exiting Safely</title>
			<link>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/azure-worker-role-exiting-safely</link>
			<pubDate>Mon, 11 Feb 2013 18:59:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Microsoft Technologies</category>			<guid isPermaLink="false">2090@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;The basic Azure Worker Role consists of a run method, an endless loop, and a sleep statement. Earlier this week, Magnus Martensson walked through &lt;a href=&quot;http://magnusmartensson.com/howto-wait-in-a-workerrole-using-system-timers-timer-and-system-threading-eventwaithandle-over-system-threading-thread-sleep&quot; title=&quot;HowTo wait in a WorkerRole using Timer and EventWaitHandle over Thread.Sleep&quot;&gt;implementing a more sophisticated wait object&lt;/a&gt; than the generic Thread.Sleep call. Which reminded me of a problem inherent in the basic Microsoft template.&lt;/p&gt;

&lt;p&gt;Every exit is a crash.&lt;/p&gt;

&lt;p&gt;The basic Worker Role is a while(true) statement that alternates between doing work and sleeping for a period of time. When it&#039;s time for Azure to recycle the instance, deploy a new one, scale...what happens to this while(true) statement?&lt;/p&gt;

&lt;p&gt;It&#039;s killed. &lt;/p&gt;

&lt;p&gt;The more critical it was, the higher our chances it was in the work side of the work/sleep loop.&lt;/p&gt;

&lt;p&gt;(Ouch.)&lt;/p&gt;

&lt;h2&gt;The Basic Worker Role&lt;/h2&gt;
&lt;p&gt;Here is the worker class that Visual Studio generates on when creating a new Worker Role project:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb36976&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; WorkerRole : RoleEntryPoint&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; Run&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// This is a sample worker implementation. Replace with your logic.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WorkerRole1 entry point called&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Thread.&lt;span style=&quot;color: #0000FF;&quot;&gt;Sleep&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Working&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;bool&lt;/span&gt; OnStart&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// Set the maximum number of concurrent connections &lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ServicePointManager.&lt;span style=&quot;color: #0000FF;&quot;&gt;DefaultConnectionLimit&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;12&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// For information on handling configuration changes&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// see the MSDN topic at &amp;lt;a href=&amp;quot;http://go.microsoft.com/fwlink/?LinkId=166357&amp;quot;&amp;gt;http://go.microsoft.com/fwlink/?LinkId=166357&amp;lt;/a&amp;gt;.&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;base&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;OnStart&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb66218&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Azure calls the OnStart when it starts, then calls the Run method. This sample will hard crash when Azure scales it out of existence, swaps in new instances, decides it&#039;s Windows patch time, lets us press the Stop button, and so on. &lt;/p&gt;

&lt;p&gt;Let&#039;s see it in action. I&#039;ve added a DoWork() method that sleeps for 10 seconds to simulate important work being done. I&#039;ve also added Trace.WriteLine calls to the existing methods and to an override of the OnStop method, so we can see what&#039;s happening.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb11019&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; Run&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// This is a sample worker implementation. Replace with your logic.&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;BasicWorker - Entry point called&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Thread.&lt;span style=&quot;color: #0000FF;&quot;&gt;Sleep&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;BasicWorker - Starting some work&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DoWork&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;BasicWorker - Finished some work&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; DoWork&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Thread.&lt;span style=&quot;color: #0000FF;&quot;&gt;Sleep&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; OnStop&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;BasicWorker - OnStop&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb11391&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we run this in the emulator and suspend the worker role in the middle of our important work, it exits right on cue, in the middle of the work.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Diagnostic Trace Output&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb34696&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;Information: 000.1s - BasicWorker - Entry point called&lt;br /&gt;Information: 010.1s - BasicWorker - Starting some work&lt;br /&gt;Information: 020.1s - BasicWorker - Finished some work&lt;br /&gt;Information: 030.1s - BasicWorker - Starting some work&lt;br /&gt;Information: 040.1s - BasicWorker - Finished some work&lt;br /&gt;Information: 050.1s - BasicWorker - Starting some work&lt;br /&gt;Information: 056.1s - BasicWorker - OnStop&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb68449&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;i&gt;note: I later added timestamps to the Trace output for readability&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;If this were a real worker role, we could have been doing just about anything in that step when it was killed. Is our system still in a good state?&lt;/p&gt;

&lt;h2&gt;A Cancel-able Worker Role&lt;/h2&gt;

&lt;p&gt;The base class for a WorkerRole is the RoleEntryPoint. As we saw above, it offers an OnStop method that will be called when the instance is suspended. More importantly, though, we are allowed to delay that OnStop method &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx&quot; title=&quot;RoleEntryPoint.OnStop, MSDN&quot;&gt;up to 30 seconds&lt;/a&gt; to finish up what we are working on.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Note: Early last year (2012) this was extended to 5 minutes, though it&#039;s not reflected in the documentation above.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;The first change we want to make is to replace the while(true) construct with a method that we can &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/dd997364.aspx&quot; title=&quot;Cancellation in Managed Threads, MSDN&quot;&gt;cancel&lt;/a&gt;. Using a &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.threading.cancellationtokensource.aspx&quot; title=&quot;CancellationTokenSource, MSDN&quot;&gt;CancellationTokenSource&lt;/a&gt;, we can instead loop while that token is not cancelled.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb44114&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; CancellationTokenSource _cancellationTokenSource;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; Run&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SafeWorker - Entry point called&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; _cancellationTokenSource = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; CancellationTokenSource&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var token = _cancellationTokenSource.&lt;span style=&quot;color: #0000FF;&quot;&gt;Token&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;!token.&lt;span style=&quot;color: #0000FF;&quot;&gt;IsCancellationRequested&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SafeWorker - Starting some work&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DoWork&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SafeWorker - Finished some work&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; token.&lt;span style=&quot;color: #0000FF;&quot;&gt;WaitHandle&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;WaitOne&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// sleep 10s or exit early if cancellation is signalled&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb47050&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Replacing the Thread.Sleep with a &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/cc189907.aspx&quot; title=&quot;WaitOne, MSDN&quot;&gt;WaitOne()&lt;/a&gt; call will allow us to reduce the time to cancel. Unless it receives a signal (cancellation), the token will wait the specified number of milliseconds before continuing. Moving the WaitOne to the end ensures that if a cancellation is signaled, we won&#039;t pick up one last bit of work before exiting.&lt;/p&gt;

&lt;p&gt;The other piece of the equation is making the OnStop wait until the we have safely exited the loop. We can achieve this by creating a &quot;Safe to exit&quot; WaitHandle that is only set after successfully exiting the loop. The OnStop will Cancel via the CancellationToken, then wait for the &quot;Safe to exit&quot; token to be set before returning.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb93916&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; CancellationTokenSource _cancellationTokenSource;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; ManualResetEvent _safeToExitHandle;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; Run&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SafeWorker - Entry point called&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; _cancellationTokenSource = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; CancellationTokenSource&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; _safeToExitHandle = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; ManualResetEvent&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var token = _cancellationTokenSource.&lt;span style=&quot;color: #0000FF;&quot;&gt;Token&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;!token.&lt;span style=&quot;color: #0000FF;&quot;&gt;IsCancellationRequested&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SafeWorker - Starting some work&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DoWork&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SafeWorker - Finished some work&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; token.&lt;span style=&quot;color: #0000FF;&quot;&gt;WaitHandle&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;WaitOne&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// sleep 10s or exit early if cancellation is signalled&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SafeWorker - Ready to exit&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; _safeToExitHandle.&lt;span style=&quot;color: #0000FF;&quot;&gt;Set&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// cleanly exited the main loop&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// ...&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; OnStop&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SafeWorker - OnStop Called&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; _cancellationTokenSource.&lt;span style=&quot;color: #0000FF;&quot;&gt;Cancel&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; _safeToExitHandle.&lt;span style=&quot;color: #0000FF;&quot;&gt;WaitOne&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SafeWorker - OnStop Complete, Exiting Safely&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb96912&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now if we run this like the BasicWorker above, hitting the Suspend button in the middle of the DoWork call, we see the system takes the time to exit out safely:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Diagnostic Trace Output&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;text&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;text&quot; id=&quot;cb82914&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;Information: 000.0s - SafeWorker - Entry point called&lt;br /&gt;Information: 000.1s - SafeWorker - Starting some work&lt;br /&gt;Information: 010.1s - SafeWorker - Finished some work&lt;br /&gt;Information: 020.1s - SafeWorker - Starting some work&lt;br /&gt;Information: 030.1s - SafeWorker - Finished some work&lt;br /&gt;Information: 040.1s - SafeWorker - Starting some work&lt;br /&gt;Information: 046.4s - SafeWorker - OnStop Called&lt;br /&gt;Information: 050.1s - SafeWorker - Finished some work&lt;br /&gt;Information: 050.1s - SafeWorker - Ready to exit&lt;br /&gt;Information: 050.1s - SafeWorker - OnStop Complete, Exiting Safely&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb84151&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;i&gt;note: I later added timestamps to the Trace output for readability&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;We can see the OnStop call come in in the middle of our Starting some Work/Finished some work output, but instead of exiting immediately, the worker calmly finished up it&#039;s work and then announced it was ready to exit (by setting the _safeToExitHandle WaitHandle).&lt;/p&gt;

&lt;h2&gt;Wrapping Up&lt;/h2&gt;
&lt;p&gt;Letting your application die in the middle of an operation is typically not a good idea. &lt;/p&gt;

&lt;p&gt;The example code for this post is available on github at &lt;a href=&quot;https://github.com/tarwn/AzureWorkerRole_Cancellation&quot; title=&quot;tarwn/AzureWorkerRole_Cancellation, GitHub&quot;&gt;tarwn/AzureWorkerRole_Cancellation&lt;/a&gt;. After finishing the code samples above, I went back and add seconds elapsed to the trace output message. I didn&#039;t update the code samples above because it would have only served to distract from the real code.&lt;/p&gt;

&lt;p&gt;I haven&#039;t posted on Azure as much as I probably should have, given how much of my time I spend working with it. Expect to see more posts on this in the upcoming months.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/azure-worker-role-exiting-safely&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>The basic Azure Worker Role consists of a run method, an endless loop, and a sleep statement. Earlier this week, Magnus Martensson walked through <a href="http://magnusmartensson.com/howto-wait-in-a-workerrole-using-system-timers-timer-and-system-threading-eventwaithandle-over-system-threading-thread-sleep" title="HowTo wait in a WorkerRole using Timer and EventWaitHandle over Thread.Sleep">implementing a more sophisticated wait object</a> than the generic Thread.Sleep call. Which reminded me of a problem inherent in the basic Microsoft template.</p>

<p>Every exit is a crash.</p>

<p>The basic Worker Role is a while(true) statement that alternates between doing work and sleeping for a period of time. When it's time for Azure to recycle the instance, deploy a new one, scale...what happens to this while(true) statement?</p>

<p>It's killed. </p>

<p>The more critical it was, the higher our chances it was in the work side of the work/sleep loop.</p>

<p>(Ouch.)</p>

<h2>The Basic Worker Role</h2>
<p>Here is the worker class that Visual Studio generates on when creating a new Worker Role project:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb53031'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb53031','cb65004'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb53031" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> WorkerRole : RoleEntryPoint</li><li style="" class="li2"><span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// This is a sample worker implementation. Replace with your logic.</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;WorkerRole1 entry point called&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10000</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Working&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> OnStart<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Set the maximum number of concurrent connections </span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; ServicePointManager.<span style="color: #0000FF;">DefaultConnectionLimit</span> = <span style="color: #FF0000;">12</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// For information on handling configuration changes</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// see the MSDN topic at &lt;a href=&quot;http://go.microsoft.com/fwlink/?LinkId=166357&quot;&gt;http://go.microsoft.com/fwlink/?LinkId=166357&lt;/a&gt;.</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnStart</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb65004" style="display: none; color: red;"></div></div></div>

<p>Azure calls the OnStart when it starts, then calls the Run method. This sample will hard crash when Azure scales it out of existence, swaps in new instances, decides it's Windows patch time, lets us press the Stop button, and so on. </p>

<p>Let's see it in action. I've added a DoWork() method that sleeps for 10 seconds to simulate important work being done. I've also added Trace.WriteLine calls to the existing methods and to an override of the OnStop method, so we can see what's happening.</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb22549'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb22549','cb4132'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb22549" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2"><span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// This is a sample worker implementation. Replace with your logic.</span></li><li style="" class="li2">&nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;BasicWorker - Entry point called&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10000</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;BasicWorker - Starting some work&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; DoWork<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;BasicWorker - Finished some work&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> DoWork<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2"><span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10000</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnStop<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1"><span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;BasicWorker - OnStop&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb4132" style="display: none; color: red;"></div></div></div>

<p>If we run this in the emulator and suspend the worker role in the middle of our important work, it exits right on cue, in the middle of the work.</p>

<p><b>Diagnostic Trace Output</b></p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb20529'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb20529','cb77055'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb20529" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">Information: 000.1s - BasicWorker - Entry point called</li><li style="" class="li2">Information: 010.1s - BasicWorker - Starting some work</li><li style="" class="li1">Information: 020.1s - BasicWorker - Finished some work</li><li style="" class="li2">Information: 030.1s - BasicWorker - Starting some work</li><li style="" class="li1">Information: 040.1s - BasicWorker - Finished some work</li><li style="" class="li2">Information: 050.1s - BasicWorker - Starting some work</li><li style="" class="li1">Information: 056.1s - BasicWorker - OnStop</li></ol></div><div id="cb77055" style="display: none; color: red;"></div></div></div>

<p><i>note: I later added timestamps to the Trace output for readability</i></p>

<p>If this were a real worker role, we could have been doing just about anything in that step when it was killed. Is our system still in a good state?</p>

<h2>A Cancel-able Worker Role</h2>

<p>The base class for a WorkerRole is the RoleEntryPoint. As we saw above, it offers an OnStop method that will be called when the instance is suspended. More importantly, though, we are allowed to delay that OnStop method <a href="http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx" title="RoleEntryPoint.OnStop, MSDN">up to 30 seconds</a> to finish up what we are working on.</p>

<p><i>Note: Early last year (2012) this was extended to 5 minutes, though it's not reflected in the documentation above.</i></p>

<p>The first change we want to make is to replace the while(true) construct with a method that we can <a href="http://msdn.microsoft.com/en-us/library/dd997364.aspx" title="Cancellation in Managed Threads, MSDN">cancel</a>. Using a <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtokensource.aspx" title="CancellationTokenSource, MSDN">CancellationTokenSource</a>, we can instead loop while that token is not cancelled.</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb40760'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb40760','cb41804'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb40760" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">private</span> CancellationTokenSource _cancellationTokenSource;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2"><span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SafeWorker - Entry point called&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; _cancellationTokenSource = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> CancellationTokenSource<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; var token = _cancellationTokenSource.<span style="color: #0000FF;">Token</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>!token.<span style="color: #0000FF;">IsCancellationRequested</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SafeWorker - Starting some work&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; DoWork<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SafeWorker - Finished some work&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; token.<span style="color: #0000FF;">WaitHandle</span>.<span style="color: #0000FF;">WaitOne</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10000</span><span style="color: #000000;">&#41;</span>;&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// sleep 10s or exit early if cancellation is signalled</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// ...</span></li></ol></div><div id="cb41804" style="display: none; color: red;"></div></div></div>

<p>Replacing the Thread.Sleep with a <a href="http://msdn.microsoft.com/en-us/library/cc189907.aspx" title="WaitOne, MSDN">WaitOne()</a> call will allow us to reduce the time to cancel. Unless it receives a signal (cancellation), the token will wait the specified number of milliseconds before continuing. Moving the WaitOne to the end ensures that if a cancellation is signaled, we won't pick up one last bit of work before exiting.</p>

<p>The other piece of the equation is making the OnStop wait until the we have safely exited the loop. We can achieve this by creating a "Safe to exit" WaitHandle that is only set after successfully exiting the loop. The OnStop will Cancel via the CancellationToken, then wait for the "Safe to exit" token to be set before returning.</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb61262'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb61262','cb9940'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb61262" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">private</span> CancellationTokenSource _cancellationTokenSource;</li><li style="" class="li2"><span style="color: #0600FF;">private</span> ManualResetEvent _safeToExitHandle;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1"><span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SafeWorker - Entry point called&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; _cancellationTokenSource = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> CancellationTokenSource<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; _safeToExitHandle = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ManualResetEvent<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; var token = _cancellationTokenSource.<span style="color: #0000FF;">Token</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>!token.<span style="color: #0000FF;">IsCancellationRequested</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SafeWorker - Starting some work&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; DoWork<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SafeWorker - Finished some work&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; token.<span style="color: #0000FF;">WaitHandle</span>.<span style="color: #0000FF;">WaitOne</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">10000</span><span style="color: #000000;">&#41;</span>;&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// sleep 10s or exit early if cancellation is signalled</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SafeWorker - Ready to exit&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; _safeToExitHandle.<span style="color: #0000FF;">Set</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// cleanly exited the main loop</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #008080; font-style: italic;">// ...</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnStop<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2"><span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SafeWorker - OnStop Called&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; _cancellationTokenSource.<span style="color: #0000FF;">Cancel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; _safeToExitHandle.<span style="color: #0000FF;">WaitOne</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SafeWorker - OnStop Complete, Exiting Safely&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb9940" style="display: none; color: red;"></div></div></div>

<p>Now if we run this like the BasicWorker above, hitting the Suspend button in the middle of the DoWork call, we see the system takes the time to exit out safely:</p>

<p><b>Diagnostic Trace Output</b></p>
<div class="codebox"><div class="codeheader"><span>text</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb24427'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb24427','cb35511'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="text" id="cb24427" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">Information: 000.0s - SafeWorker - Entry point called</li><li style="" class="li2">Information: 000.1s - SafeWorker - Starting some work</li><li style="" class="li1">Information: 010.1s - SafeWorker - Finished some work</li><li style="" class="li2">Information: 020.1s - SafeWorker - Starting some work</li><li style="" class="li1">Information: 030.1s - SafeWorker - Finished some work</li><li style="" class="li2">Information: 040.1s - SafeWorker - Starting some work</li><li style="" class="li1">Information: 046.4s - SafeWorker - OnStop Called</li><li style="" class="li2">Information: 050.1s - SafeWorker - Finished some work</li><li style="" class="li1">Information: 050.1s - SafeWorker - Ready to exit</li><li style="" class="li2">Information: 050.1s - SafeWorker - OnStop Complete, Exiting Safely</li></ol></div><div id="cb35511" style="display: none; color: red;"></div></div></div>

<p><i>note: I later added timestamps to the Trace output for readability</i></p>

<p>We can see the OnStop call come in in the middle of our Starting some Work/Finished some work output, but instead of exiting immediately, the worker calmly finished up it's work and then announced it was ready to exit (by setting the _safeToExitHandle WaitHandle).</p>

<h2>Wrapping Up</h2>
<p>Letting your application die in the middle of an operation is typically not a good idea. </p>

<p>The example code for this post is available on github at <a href="https://github.com/tarwn/AzureWorkerRole_Cancellation" title="tarwn/AzureWorkerRole_Cancellation, GitHub">tarwn/AzureWorkerRole_Cancellation</a>. After finishing the code samples above, I went back and add seconds elapsed to the trace output message. I didn't update the code samples above because it would have only served to distract from the real code.</p>

<p>I haven't posted on Azure as much as I probably should have, given how much of my time I spend working with it. Expect to see more posts on this in the upcoming months.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/azure-worker-role-exiting-safely">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/azure-worker-role-exiting-safely#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2&#38;disp=comments&#38;p=2090</wfw:commentRss>
		</item>
				<item>
			<title>Testing the Not-So-Testable HttpWebResponse</title>
			<link>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/testing-httpwebresponse</link>
			<pubDate>Mon, 10 Dec 2012 14:56:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="alt">Microsoft Technologies</category>
<category domain="main">C#</category>			<guid isPermaLink="false">1932@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Recently I was working on a library to consume a REST API without exposing any of the specifics to the rest of the application. Implementing a common interface and set of custom exceptions was easy enough, but exercising the internal logic was going to be tough. &lt;/p&gt;

&lt;p&gt;While I could use the live REST API to verify the general logic worked, I had limited options when it came to the full range of HTTP responses and communication failures. Add in the impact those live API calls would have on my build process performance, the occasional failures of my build due the imperfections of talking to a live service, and the overhead of maintaining separation between my test and live data in that service...what I really had on my hands was the beginning of years of random, painful maintenance.&lt;/p&gt;

&lt;p&gt;If only I could mock WebRequest and have it return carefully crafted responses to test my code with, all without ever touching the real network.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx&quot; title=&quot;HttpWebResponse on MSDN&quot;&gt;MSDN&lt;/a&gt;: You should never directly create an instance of the HttpWebResponse class.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Hmm, ok, maybe not.&lt;/p&gt;

&lt;p&gt;Despite having done this search before, this time around I uncovered a couple posts that helped me find a solution to this whole mess. Although not before I left some helpful feedback on the MSDN page about the difference between opinion and documentation.&lt;/p&gt;

&lt;p&gt;Yep, making friends.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;The source code for this post is located on github, with the sample service implementation and test projects: &lt;a href=&quot;https://github.com/tarwn/TestableHttpWebResponse&quot; title=&quot;tarwn/TestableHttpWebResponse on github&quot;&gt;tarwn/TestableHttpWebResponse&lt;/a&gt;&lt;/i&gt;&lt;/p&gt;

&lt;h2&gt;Let&#039;s Start with the Tests&lt;/h2&gt;
&lt;p&gt;I&#039;ve created a sample service implementation with two API call implementations. Each one builds a WebRequest, executes it, and analyzes the response. A retry policy wraps around the request execution, evaluating exceptions to determine whether to retry or map them to a local exception type to be rethrown.&lt;/p&gt;

&lt;p&gt;Here is an example of one of those calls and the synchronous method it uses internally:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;TestableHttpWebResponse.Sample/SampleService.cs&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb45764&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; SampleService&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// ...&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; ServiceResponse ListRemoteStuff&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var uri = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Uri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;_baseUri, operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var request = WebRequest.&lt;span style=&quot;color: #0000FF;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;uri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; request.&lt;span style=&quot;color: #0000FF;&quot;&gt;Headers&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;version&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;123-awesome&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; SendRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;request&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// ...&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; ServiceResponse SendRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;WebRequest request&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; _retryPolicy.&lt;span style=&quot;color: #0000FF;&quot;&gt;ExecuteAction&lt;/span&gt;&amp;lt;ServiceResponse&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; =&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;try&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var response = &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;HttpWebResponse&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;request.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetResponse&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var reader = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; StreamReader&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;response.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetResponseStream&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var message = reader.&lt;span style=&quot;color: #0000FF;&quot;&gt;ReadToEnd&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; ServiceResponse&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; IsSuccess = &lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;, Message = message &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;WebException we&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;throw&lt;/span&gt; MappedException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;we&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb63785&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Testing a method like this typically requires an integration test against the live service. With the provided TestableHttpWebResponse and TestableWebRequest, however, we can set up an expected request and response and verify the service reacts appropriately.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;1: Register the TestableWebRequestCreateFactory&lt;/b&gt;&lt;br /&gt;
WebRequest.Create(&lt;i&gt;uri&lt;/i&gt;) uses a factory to produce the relevant WebRequest instance of a Uri, based on the prefix. So first things first, lets register a new prefix and a factory to serve up requests:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb43433&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;TestFixtureSetUp&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; TestFixtureSetup&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; WebRequest.&lt;span style=&quot;color: #0000FF;&quot;&gt;RegisterPrefix&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;test&amp;quot;&lt;/span&gt;, TestableWebRequestCreateFactory.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetFactory&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb43029&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;TestableWebRequestCreateFactory.GetFactory() exposes a singleton that can be referenced from any of the tests in this class. When the WebRequest object receives a Uri starting with &quot;test://&quot;, it will call the associated factory, giving us the opportunity to respond with a Request object of our choosing.&lt;/p&gt;

&lt;p&gt;A common base URI will prove helpful as we write the tests:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb4308&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; Uri BaseUri &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Uri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;test://mydomain.com/api/&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb33793&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;2: Building a Test&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;The easiest test to start with is one that will test the &quot;happy path&quot; where our API call receives a 200 Success response. &lt;/p&gt;

&lt;p&gt;First we need to set up the request:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb14280&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;var operation = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;ListOfStuff&amp;quot;&lt;/span&gt;;&lt;br /&gt;var expectedRequest = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; TestableWebRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Uri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;BaseUri, operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb65172&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next we need to set up the response the request will return when it is executed:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb35401&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;expectedRequest.&lt;span style=&quot;color: #0000FF;&quot;&gt;EnqueueResponse&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;HttpStatusCode.&lt;span style=&quot;color: #0000FF;&quot;&gt;OK&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Success&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Even More Success&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #0600FF;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb86999&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And then add it to the Factory so it will be available when WebRequest calls it:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb1292&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;TestableWebRequestCreateFactory.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetFactory&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;AddRequest&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;expectedRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb6724&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Put all of this together and we have:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;TestableHttpWebResponse.Sample.Tests/SampleServiceTests.cs&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb76977&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;Test&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; ListRemoteStuff_ValidRequest_ReturnsSuccessfulResponse&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var operation = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;ListOfStuff&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var expectedRequest = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; TestableWebRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Uri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;BaseUri, operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; expectedRequest.&lt;span style=&quot;color: #0000FF;&quot;&gt;EnqueueResponse&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;HttpStatusCode.&lt;span style=&quot;color: #0000FF;&quot;&gt;OK&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Success&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Even More Success&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #0600FF;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; TestableWebRequestCreateFactory.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetFactory&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;AddRequest&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;expectedRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var service = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; SampleService&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;BaseUri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var response = service.&lt;span style=&quot;color: #0000FF;&quot;&gt;ListRemoteStuff&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Assert.&lt;span style=&quot;color: #0000FF;&quot;&gt;IsTrue&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;response.&lt;span style=&quot;color: #0000FF;&quot;&gt;IsSuccess&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb52692&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This exercises the entire successful path of the operation without any additional abstractions in our API code or reliance on external communications and services.&lt;/p&gt;

&lt;h2&gt;Testing Http Status Codes&lt;/h2&gt;

&lt;p&gt;Another tricky part of testing a service is figuring out how to test HTTP codes other then the success case. &lt;/p&gt;

&lt;p&gt;The sample service maps received Protocol Errors (401, 404, etc) to a local exception so the code consuming this library doesn&#039;t have to know how to parse WebExceptions. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;TestableHttpWebResponse.Sample/SampleService.cs&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb84562&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; Exception MappedException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;WebException we&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// map to custom exceptions&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;we.&lt;span style=&quot;color: #0000FF;&quot;&gt;Status&lt;/span&gt; == WebExceptionStatus.&lt;span style=&quot;color: #0000FF;&quot;&gt;ProtocolError&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var reader = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; StreamReader&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;we.&lt;span style=&quot;color: #0000FF;&quot;&gt;Response&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetResponseStream&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var message = reader.&lt;span style=&quot;color: #0000FF;&quot;&gt;ReadToEnd&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var httpResponse = &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;HttpWebResponse&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;we.&lt;span style=&quot;color: #0000FF;&quot;&gt;Response&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;switch&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;httpResponse.&lt;span style=&quot;color: #0000FF;&quot;&gt;StatusCode&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;case&lt;/span&gt; HttpStatusCode.&lt;span style=&quot;color: #0000FF;&quot;&gt;NotFound&lt;/span&gt;:&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;httpResponse.&lt;span style=&quot;color: #0000FF;&quot;&gt;StatusDescription&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Contains&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Dohicky&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; DohickyNotFoundException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;message, we&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;else&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; GenericNotFoundException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;message, we&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;default&lt;/span&gt;:&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; ExampleOfAnotherUsefulException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;message, we&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;else&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; SampleServiceOutageException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;we&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb75143&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Exercising the mapping logic is going to require the WebRequest to receive a WebException. Let&#039;s make that happen.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Yes, I know a HEAD request will break this, that&#039;s why it&#039;s called &quot;sample&quot; code &lt;img src=&quot;http://blogs.lessthandot.com/rsc/smilies/icon_smile.gif&quot; title=&quot;:)&quot; alt=&quot;:)&quot; class=&quot;middle&quot; width=&quot;15&quot; height=&quot;15&quot; /&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;In the first test, we used the EnqueueResponse method of the TestableWebRequest to set up a 200 Success response. It&#039;s just as simple to return a 404 Http code with the expected message and request body:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;TestableHttpWebResponse.Sample.Tests/SampleServiceTests.cs&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb67708&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;expectedRequest.&lt;span style=&quot;color: #0000FF;&quot;&gt;EnqueueResponse&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;HttpStatusCode.&lt;span style=&quot;color: #0000FF;&quot;&gt;NotFound&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Dohicky not found&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;I couldn&#039;t find your dohicky because I don&#039;t like you&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb3632&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which allows us to create the ExpectedException test:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb42108&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;Test&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;ExpectedException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=typeof+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;typeof&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;DohickyNotFoundException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; ListRemoteStuff_404DohickeyNotFound_ThrowsDohickeyNotFoundException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var operation = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;ListOfStuff&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var expectedRequest = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; TestableWebRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Uri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;BaseUri, operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; expectedRequest.&lt;span style=&quot;color: #0000FF;&quot;&gt;EnqueueResponse&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;HttpStatusCode.&lt;span style=&quot;color: #0000FF;&quot;&gt;NotFound&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Dohicky not found&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;I couldn&#039;t find your dohicky because I don&#039;t like you&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; TestableWebRequestCreateFactory.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetFactory&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;AddRequest&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;expectedRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var service = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; SampleService&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;BaseUri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var response = service.&lt;span style=&quot;color: #0000FF;&quot;&gt;ListRemoteStuff&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// expect exception&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb91598&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;Testing Other WebExceptions&lt;/h2&gt;

&lt;p&gt;What about connection failures? Well there is another version of the EnqueueResponse method that allows us to queue up an exception to be returned from the Request, like so:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb16253&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;expectedRequest.&lt;span style=&quot;color: #0000FF;&quot;&gt;EnqueueResponse&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; WebException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;I&#039;m broke!&amp;quot;&lt;/span&gt;, WebExceptionStatus.&lt;span style=&quot;color: #0000FF;&quot;&gt;ConnectFailure&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb26360&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Just like the previous test, we can use that response to put together a full test&lt;/p&gt;

&lt;p&gt;&lt;b&gt;TestableHttpWebResponse.Sample.Tests/SampleServiceTests.cs&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb60260&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;Test&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;ExpectedException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=typeof+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;typeof&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;SampleServiceOutageException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; ListRemoteStuff_ServiceOutage_ThrowsSampleServiceOutage&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var operation = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;ListOfStuff&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var expectedRequest = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; TestableWebRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Uri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;BaseUri, operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; expectedRequest.&lt;span style=&quot;color: #0000FF;&quot;&gt;EnqueueResponse&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; WebException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;I&#039;m broke!&amp;quot;&lt;/span&gt;, WebExceptionStatus.&lt;span style=&quot;color: #0000FF;&quot;&gt;ConnectFailure&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; TestableWebRequestCreateFactory.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetFactory&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;AddRequest&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;expectedRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var service = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; SampleService&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;BaseUri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var response = service.&lt;span style=&quot;color: #0000FF;&quot;&gt;ListRemoteStuff&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// expect exception&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb51637&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;Testing the Retry Policy&lt;/h2&gt;

&lt;p&gt;Retry policies are trickier, in that they need to be able to execute a Request multiple times and receive new responses. By enqueueing (sp?) multiple responses on the request, we can exercise the retry policy:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;TestableHttpWebResponse.Sample.Tests/SampleServiceTests.cs&lt;/b&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb24544&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;Test&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; ListRemoteStuff_TimeoutOccurs_TruesASecondTime&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var operation = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;ListOfStuff&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var expectedRequest = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; TestableWebRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Uri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;BaseUri, operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; expectedRequest.&lt;span style=&quot;color: #0000FF;&quot;&gt;EnqueueResponse&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; TimeoutException&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;took too long, so sorry&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;span style=&quot;color: #0000FF;&quot;&gt;EnqueueResponse&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;HttpStatusCode.&lt;span style=&quot;color: #0000FF;&quot;&gt;OK&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;All Good&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Nothing to see, please move along&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #0600FF;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; TestableWebRequestCreateFactory.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetFactory&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;AddRequest&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;expectedRequest&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var service = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; SampleService&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;BaseUri&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var response = service.&lt;span style=&quot;color: #0000FF;&quot;&gt;ListRemoteStuff&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;operation&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; Assert.&lt;span style=&quot;color: #0000FF;&quot;&gt;AreEqual&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Nothing to see, please move along&amp;quot;&lt;/span&gt;, response.&lt;span style=&quot;color: #0000FF;&quot;&gt;Message&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb98922&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And there we have it, all the various flavors of an HttpWebRequest.&lt;/p&gt;

&lt;h2&gt;The Testable Classes&lt;/h2&gt;
&lt;p&gt;The Testable classes are still under construction. At the time of this post they support the functionality above as well as the ability to set and verify Request Headers and write and verify the Request stream contents (upload). Currently the asynchronous methods (BeginGetResponse/EndGetResponse) are not implemented, but I&#039;ll be adding those soon along with SampleService calls that exercise those via TPL and async/await logic. I&#039;ll also be looking through WebRequest for other properties or methods I haven&#039;t imlpemented yet to see what&#039;s useful.&lt;/p&gt;

&lt;p&gt;Hopefully others will find this useful as well.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/testing-httpwebresponse&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Recently I was working on a library to consume a REST API without exposing any of the specifics to the rest of the application. Implementing a common interface and set of custom exceptions was easy enough, but exercising the internal logic was going to be tough. </p>

<p>While I could use the live REST API to verify the general logic worked, I had limited options when it came to the full range of HTTP responses and communication failures. Add in the impact those live API calls would have on my build process performance, the occasional failures of my build due the imperfections of talking to a live service, and the overhead of maintaining separation between my test and live data in that service...what I really had on my hands was the beginning of years of random, painful maintenance.</p>

<p>If only I could mock WebRequest and have it return carefully crafted responses to test my code with, all without ever touching the real network.</p>

<blockquote><p><a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx" title="HttpWebResponse on MSDN">MSDN</a>: You should never directly create an instance of the HttpWebResponse class.</p></blockquote>

<p>Hmm, ok, maybe not.</p>

<p>Despite having done this search before, this time around I uncovered a couple posts that helped me find a solution to this whole mess. Although not before I left some helpful feedback on the MSDN page about the difference between opinion and documentation.</p>

<p>Yep, making friends.</p>

<p><i>The source code for this post is located on github, with the sample service implementation and test projects: <a href="https://github.com/tarwn/TestableHttpWebResponse" title="tarwn/TestableHttpWebResponse on github">tarwn/TestableHttpWebResponse</a></i></p>

<h2>Let's Start with the Tests</h2>
<p>I've created a sample service implementation with two API call implementations. Each one builds a WebRequest, executes it, and analyzes the response. A retry policy wraps around the request execution, evaluating exceptions to determine whether to retry or map them to a local exception type to be rethrown.</p>

<p>Here is an example of one of those calls and the synchronous method it uses internally:</p>

<p><b>TestableHttpWebResponse.Sample/SampleService.cs</b></p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb90360'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb90360','cb71250'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb90360" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SampleService</li><li style="" class="li2"><span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// ...</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> ServiceResponse ListRemoteStuff<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> operation<span style="color: #000000;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var uri = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Uri<span style="color: #000000;">&#40;</span>_baseUri, operation<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; var request = WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>uri<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; request.<span style="color: #0000FF;">Headers</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;version&quot;</span>, <span style="color: #808080;">&quot;123-awesome&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> SendRequest<span style="color: #000000;">&#40;</span>request<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; </li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// ...</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">private</span> ServiceResponse SendRequest<span style="color: #000000;">&#40;</span>WebRequest request<span style="color: #000000;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> _retryPolicy.<span style="color: #0000FF;">ExecuteAction</span>&lt;ServiceResponse&gt;<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> =&gt;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">try</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var response = <span style="color: #000000;">&#40;</span>HttpWebResponse<span style="color: #000000;">&#41;</span>request.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var reader = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamReader<span style="color: #000000;">&#40;</span>response.<span style="color: #0000FF;">GetResponseStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var message = reader.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ServiceResponse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> IsSuccess = <span style="color: #0600FF;">true</span>, Message = message <span style="color: #000000;">&#125;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>WebException we<span style="color: #000000;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">throw</span> MappedException<span style="color: #000000;">&#40;</span>we<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// ...</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb71250" style="display: none; color: red;"></div></div></div>

<p>Testing a method like this typically requires an integration test against the live service. With the provided TestableHttpWebResponse and TestableWebRequest, however, we can set up an expected request and response and verify the service reacts appropriately.</p>

<p><b>1: Register the TestableWebRequestCreateFactory</b><br />
WebRequest.Create(<i>uri</i>) uses a factory to produce the relevant WebRequest instance of a Uri, based on the prefix. So first things first, lets register a new prefix and a factory to serve up requests:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb60864'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb60864','cb1607'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb60864" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #000000;">&#91;</span>TestFixtureSetUp<span style="color: #000000;">&#93;</span></li><li style="" class="li2"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> TestFixtureSetup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1"><span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; WebRequest.<span style="color: #0000FF;">RegisterPrefix</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;test&quot;</span>, TestableWebRequestCreateFactory.<span style="color: #0000FF;">GetFactory</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb1607" style="display: none; color: red;"></div></div></div>

<p>TestableWebRequestCreateFactory.GetFactory() exposes a singleton that can be referenced from any of the tests in this class. When the WebRequest object receives a Uri starting with "test://", it will call the associated factory, giving us the opportunity to respond with a Request object of our choosing.</p>

<p>A common base URI will prove helpful as we write the tests:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb72718'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb72718','cb36119'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb72718" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> Uri BaseUri <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Uri<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;test://mydomain.com/api/&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span></li></ol></div><div id="cb36119" style="display: none; color: red;"></div></div></div>

<p><b>2: Building a Test</b></p>

<p>The easiest test to start with is one that will test the "happy path" where our API call receives a 200 Success response. </p>

<p>First we need to set up the request:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb71323'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb71323','cb89978'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb71323" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">var operation = <span style="color: #808080;">&quot;ListOfStuff&quot;</span>;</li><li style="" class="li2">var expectedRequest = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TestableWebRequest<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Uri<span style="color: #000000;">&#40;</span>BaseUri, operation<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb89978" style="display: none; color: red;"></div></div></div>

<p>Next we need to set up the response the request will return when it is executed:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb51400'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb51400','cb77838'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb51400" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">expectedRequest.<span style="color: #0000FF;">EnqueueResponse</span><span style="color: #000000;">&#40;</span>HttpStatusCode.<span style="color: #0000FF;">OK</span>, <span style="color: #808080;">&quot;Success&quot;</span>, <span style="color: #808080;">&quot;Even More Success&quot;</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb77838" style="display: none; color: red;"></div></div></div>

<p>And then add it to the Factory so it will be available when WebRequest calls it:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb98165'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb98165','cb15015'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb98165" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">TestableWebRequestCreateFactory.<span style="color: #0000FF;">GetFactory</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddRequest</span><span style="color: #000000;">&#40;</span>expectedRequest<span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb15015" style="display: none; color: red;"></div></div></div>

<p>Put all of this together and we have:</p>

<p><b>TestableHttpWebResponse.Sample.Tests/SampleServiceTests.cs</b></p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb19069'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb19069','cb66965'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb19069" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span></li><li style="" class="li2"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ListRemoteStuff_ValidRequest_ReturnsSuccessfulResponse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1"><span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; var operation = <span style="color: #808080;">&quot;ListOfStuff&quot;</span>;</li><li style="" class="li1">&nbsp; &nbsp; var expectedRequest = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TestableWebRequest<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Uri<span style="color: #000000;">&#40;</span>BaseUri, operation<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; expectedRequest.<span style="color: #0000FF;">EnqueueResponse</span><span style="color: #000000;">&#40;</span>HttpStatusCode.<span style="color: #0000FF;">OK</span>, <span style="color: #808080;">&quot;Success&quot;</span>, <span style="color: #808080;">&quot;Even More Success&quot;</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; TestableWebRequestCreateFactory.<span style="color: #0000FF;">GetFactory</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddRequest</span><span style="color: #000000;">&#40;</span>expectedRequest<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; var service = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SampleService<span style="color: #000000;">&#40;</span>BaseUri<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; var response = service.<span style="color: #0000FF;">ListRemoteStuff</span><span style="color: #000000;">&#40;</span>operation<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; Assert.<span style="color: #0000FF;">IsTrue</span><span style="color: #000000;">&#40;</span>response.<span style="color: #0000FF;">IsSuccess</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb66965" style="display: none; color: red;"></div></div></div>

<p>This exercises the entire successful path of the operation without any additional abstractions in our API code or reliance on external communications and services.</p>

<h2>Testing Http Status Codes</h2>

<p>Another tricky part of testing a service is figuring out how to test HTTP codes other then the success case. </p>

<p>The sample service maps received Protocol Errors (401, 404, etc) to a local exception so the code consuming this library doesn't have to know how to parse WebExceptions. </p>

<p><b>TestableHttpWebResponse.Sample/SampleService.cs</b></p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb23889'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb23889','cb12621'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb23889" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">private</span> Exception MappedException<span style="color: #000000;">&#40;</span>WebException we<span style="color: #000000;">&#41;</span></li><li style="" class="li2"><span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// map to custom exceptions</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>we.<span style="color: #0000FF;">Status</span> == WebExceptionStatus.<span style="color: #0000FF;">ProtocolError</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; var reader = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamReader<span style="color: #000000;">&#40;</span>we.<span style="color: #0000FF;">Response</span>.<span style="color: #0000FF;">GetResponseStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var message = reader.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; var httpResponse = <span style="color: #000000;">&#40;</span>HttpWebResponse<span style="color: #000000;">&#41;</span>we.<span style="color: #0000FF;">Response</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span>httpResponse.<span style="color: #0000FF;">StatusCode</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">case</span> HttpStatusCode.<span style="color: #0000FF;">NotFound</span>:</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>httpResponse.<span style="color: #0000FF;">StatusDescription</span>.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Dohicky&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DohickyNotFoundException<span style="color: #000000;">&#40;</span>message, we<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">else</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> GenericNotFoundException<span style="color: #000000;">&#40;</span>message, we<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">default</span>:</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ExampleOfAnotherUsefulException<span style="color: #000000;">&#40;</span>message, we<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">else</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SampleServiceOutageException<span style="color: #000000;">&#40;</span>we<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb12621" style="display: none; color: red;"></div></div></div>

<p>Exercising the mapping logic is going to require the WebRequest to receive a WebException. Let's make that happen.</p>

<p><i>Yes, I know a HEAD request will break this, that's why it's called "sample" code <img src="http://blogs.lessthandot.com/rsc/smilies/icon_smile.gif" title=":)" alt=":)" class="middle" width="15" height="15" /></i></p>

<p>In the first test, we used the EnqueueResponse method of the TestableWebRequest to set up a 200 Success response. It's just as simple to return a 404 Http code with the expected message and request body:</p>

<p><b>TestableHttpWebResponse.Sample.Tests/SampleServiceTests.cs</b></p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb84252'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb84252','cb68662'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb84252" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">expectedRequest.<span style="color: #0000FF;">EnqueueResponse</span><span style="color: #000000;">&#40;</span>HttpStatusCode.<span style="color: #0000FF;">NotFound</span>, <span style="color: #808080;">&quot;Dohicky not found&quot;</span>, <span style="color: #808080;">&quot;I couldn't find your dohicky because I don't like you&quot;</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb68662" style="display: none; color: red;"></div></div></div>

<p>Which allows us to create the ExpectedException test:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb35384'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb35384','cb98183'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb35384" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span></li><li style="" class="li2"><span style="color: #000000;">&#91;</span>ExpectedException<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>DohickyNotFoundException<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ListRemoteStuff_404DohickeyNotFound_ThrowsDohickeyNotFoundException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2"><span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; var operation = <span style="color: #808080;">&quot;ListOfStuff&quot;</span>;</li><li style="" class="li2">&nbsp; &nbsp; var expectedRequest = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TestableWebRequest<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Uri<span style="color: #000000;">&#40;</span>BaseUri, operation<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; expectedRequest.<span style="color: #0000FF;">EnqueueResponse</span><span style="color: #000000;">&#40;</span>HttpStatusCode.<span style="color: #0000FF;">NotFound</span>, <span style="color: #808080;">&quot;Dohicky not found&quot;</span>, <span style="color: #808080;">&quot;I couldn't find your dohicky because I don't like you&quot;</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; TestableWebRequestCreateFactory.<span style="color: #0000FF;">GetFactory</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddRequest</span><span style="color: #000000;">&#40;</span>expectedRequest<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; var service = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SampleService<span style="color: #000000;">&#40;</span>BaseUri<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; var response = service.<span style="color: #0000FF;">ListRemoteStuff</span><span style="color: #000000;">&#40;</span>operation<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// expect exception</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb98183" style="display: none; color: red;"></div></div></div>

<h2>Testing Other WebExceptions</h2>

<p>What about connection failures? Well there is another version of the EnqueueResponse method that allows us to queue up an exception to be returned from the Request, like so:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb38662'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb38662','cb60800'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb38662" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">expectedRequest.<span style="color: #0000FF;">EnqueueResponse</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> WebException<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;I'm broke!&quot;</span>, WebExceptionStatus.<span style="color: #0000FF;">ConnectFailure</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb60800" style="display: none; color: red;"></div></div></div>

<p>Just like the previous test, we can use that response to put together a full test</p>

<p><b>TestableHttpWebResponse.Sample.Tests/SampleServiceTests.cs</b></p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb31790'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb31790','cb1800'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb31790" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span></li><li style="" class="li2"><span style="color: #000000;">&#91;</span>ExpectedException<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>SampleServiceOutageException<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ListRemoteStuff_ServiceOutage_ThrowsSampleServiceOutage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2"><span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; var operation = <span style="color: #808080;">&quot;ListOfStuff&quot;</span>;</li><li style="" class="li2">&nbsp; &nbsp; var expectedRequest = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TestableWebRequest<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Uri<span style="color: #000000;">&#40;</span>BaseUri, operation<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; expectedRequest.<span style="color: #0000FF;">EnqueueResponse</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> WebException<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;I'm broke!&quot;</span>, WebExceptionStatus.<span style="color: #0000FF;">ConnectFailure</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; TestableWebRequestCreateFactory.<span style="color: #0000FF;">GetFactory</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddRequest</span><span style="color: #000000;">&#40;</span>expectedRequest<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; var service = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SampleService<span style="color: #000000;">&#40;</span>BaseUri<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; var response = service.<span style="color: #0000FF;">ListRemoteStuff</span><span style="color: #000000;">&#40;</span>operation<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// expect exception</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb1800" style="display: none; color: red;"></div></div></div>

<h2>Testing the Retry Policy</h2>

<p>Retry policies are trickier, in that they need to be able to execute a Request multiple times and receive new responses. By enqueueing (sp?) multiple responses on the request, we can exercise the retry policy:</p>

<p><b>TestableHttpWebResponse.Sample.Tests/SampleServiceTests.cs</b></p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb89517'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb89517','cb91997'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb89517" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span></li><li style="" class="li2"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ListRemoteStuff_TimeoutOccurs_TruesASecondTime<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1"><span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; var operation = <span style="color: #808080;">&quot;ListOfStuff&quot;</span>;</li><li style="" class="li1">&nbsp; &nbsp; var expectedRequest = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TestableWebRequest<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Uri<span style="color: #000000;">&#40;</span>BaseUri, operation<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; expectedRequest.<span style="color: #0000FF;">EnqueueResponse</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeoutException<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;took too long, so sorry&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span style="color: #0000FF;">EnqueueResponse</span><span style="color: #000000;">&#40;</span>HttpStatusCode.<span style="color: #0000FF;">OK</span>, <span style="color: #808080;">&quot;All Good&quot;</span>, <span style="color: #808080;">&quot;Nothing to see, please move along&quot;</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; TestableWebRequestCreateFactory.<span style="color: #0000FF;">GetFactory</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddRequest</span><span style="color: #000000;">&#40;</span>expectedRequest<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; var service = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SampleService<span style="color: #000000;">&#40;</span>BaseUri<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; var response = service.<span style="color: #0000FF;">ListRemoteStuff</span><span style="color: #000000;">&#40;</span>operation<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Nothing to see, please move along&quot;</span>, response.<span style="color: #0000FF;">Message</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb91997" style="display: none; color: red;"></div></div></div>

<p>And there we have it, all the various flavors of an HttpWebRequest.</p>

<h2>The Testable Classes</h2>
<p>The Testable classes are still under construction. At the time of this post they support the functionality above as well as the ability to set and verify Request Headers and write and verify the Request stream contents (upload). Currently the asynchronous methods (BeginGetResponse/EndGetResponse) are not implemented, but I'll be adding those soon along with SampleService calls that exercise those via TPL and async/await logic. I'll also be looking through WebRequest for other properties or methods I haven't imlpemented yet to see what's useful.</p>

<p>Hopefully others will find this useful as well.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/testing-httpwebresponse">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/testing-httpwebresponse#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2&#38;disp=comments&#38;p=1932</wfw:commentRss>
		</item>
				<item>
			<title>Converting a .Net Console Project to an Azure Worker Role Project</title>
			<link>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/converting-a-net-console-project</link>
			<pubDate>Wed, 07 Nov 2012 11:28:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Microsoft Technologies</category>			<guid isPermaLink="false">1886@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;So lets say you&#039;re building a worker role for Azure, but started it out as a Console app for faster local debugging. It comes time to deploy it to Azure for the first time, but there&#039;s no option to convert a Console application to a Worker Role project. We could copy all the code into a new project, but then we not only lose our faster debugging but also the history of commits we have made against the project. &lt;/p&gt;

&lt;p&gt;Luckily converting the existing Console project to be deployable as an Azure Worker Role project is fairly easy.&lt;/p&gt;

&lt;p&gt;For the purposes of this post, I have created a solution with an Existing Console project, an Azure Cloud Project, and an Existing Azure Worker Role project (we have to have at least one to create the Azure Cloud Project, it can be deleted later).&lt;/p&gt;

&lt;div class=&quot;font-size: 85%; color: #666666; text-align: center&quot;&gt;
&lt;img src=&quot;http://tiernok.com/LTDBlog/AzureWorkerRole/awr_01.png&quot; alt=&quot;Solution Explorer&quot; /&gt;&lt;br /&gt;
Solution Explorer view of the Sample Solution
&lt;/div&gt;

&lt;p&gt;If we right click the Azure &quot;Roles&quot; folder, the option to use an existing Worker Role project is not available. Visual Studio enables or disables this option automatically, based on whether we have any assignable projects in the solution that aren&#039;t already associated with the Azure project.&lt;/p&gt;

&lt;div class=&quot;font-size: 85%; color: #666666; text-align: center&quot;&gt;
&lt;img src=&quot;http://tiernok.com/LTDBlog/AzureWorkerRole/awr_02.png&quot; alt=&quot;Add Role menu options&quot; /&gt;&lt;br /&gt;
&quot;Worker Role Project in Solution&quot; is Disabled
&lt;/div&gt;

&lt;p&gt;The first thing we want to do is add a RoleEntryPoint to the console application. To keep this simple, I&#039;m going to convert the Program class to serve as the RoleEntryPoint also.&lt;/p&gt;

&lt;p&gt;To add the RoleEntryPoint class, I need to first add some azure references. I&#039;m using Azure 1.8, so this means I&#039;ll need the following 4 references:&lt;/p&gt;

&lt;div class=&quot;font-size: 85%; color: #666666; text-align: center&quot;&gt;
&lt;img src=&quot;http://tiernok.com/LTDBlog/AzureWorkerRole/awr_03.png&quot; alt=&quot;Assembly References Dialog&quot; /&gt;&lt;br /&gt;
Adding WindowsAzure References
&lt;/div&gt;

&lt;p&gt;&lt;i&gt;Note: The Storage SDK version number for Azure SDK 1.8 is 2.0. I suspect they chose to do this because they made a number of significant changes in the storage library that were not backwards compatible with 1.7.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Note #2: I noticed that if you add a new worker role through Visual Studio, it uses StorageClient 1.7 still despite this being SDK 1.8. This may be a requirement from the Azure Diagnostics assembly, Diagnostics uses StorageClient and may still be a version behind. I would test this theory, but getting Diagnostics working properly tends to be somewhat painful.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;After adding the references, we can then add RoleEntryPoint inheritance to the Program. New Worker Roles have two methods they override from RoleEntryPoint, but the important one is Run (and why this isn&#039;t abstract, I couldn&#039;t tell you). &lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb86376&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;namespace&lt;/span&gt; ExistingConsoleApplication&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; Program : RoleEntryPoint&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; Main&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt; args&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;/* Console entry point */&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var ibl = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; InterestingBusinessLogic&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ibl.&lt;span style=&quot;color: #0000FF;&quot;&gt;DoStuff&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;Read&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; Run&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;/* Worker Role entry point */&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var ibl = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; InterestingBusinessLogic&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ibl.&lt;span style=&quot;color: #0000FF;&quot;&gt;DoStuff&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb68727&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The last thing we need to do is add some information to the project file. I can open the file in Visual Studio by right clicking and selecting &quot;Unload Project&quot;, then right clicking again and selecting &quot;Edit &lt;i&gt;project name&lt;/i&gt;&quot;. In the Property Group I need to add a single property named &quot;RoleType&quot; to identify this as a Worker (see the last entry):&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;xml&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;xml&quot; id=&quot;cb87974&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;PropertyGroup&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Configuration&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;Condition&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot; &#039;$(Configuration)&#039; == &#039;&#039; &amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Debug&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Configuration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Platform&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;Condition&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot; &#039;$(Platform)&#039; == &#039;&#039; &amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;AnyCPU&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Platform&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;ProjectGuid&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;{4B7C48CB-9899-47D0-95AB-2FD3C3B739CF}&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/ProjectGuid&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;OutputType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Exe&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/OutputType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;AppDesignerFolder&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Properties&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/AppDesignerFolder&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;RootNamespace&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;ExistingConsoleApplication&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/RootNamespace&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;AssemblyName&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;ExistingConsoleApplication&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/AssemblyName&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;TargetFrameworkVersion&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;v4.5&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/TargetFrameworkVersion&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;FileAlignment&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;512&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/FileAlignment&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;RoleType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Worker&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/RoleType&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/PropertyGroup&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb37519&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Save and reload the project, and now when we right click on the Azure Cloud Project Roles, the option to associate and existing project is available.&lt;/p&gt;

&lt;div class=&quot;font-size: 85%; color: #666666; text-align: center&quot;&gt;
&lt;img src=&quot;http://tiernok.com/LTDBlog/AzureWorkerRole/awr_04.png&quot; alt=&quot;Add Role menu options&quot; /&gt;&lt;br /&gt;
&quot;Worker Role Project in Solution&quot; is Available Now
&lt;/div&gt;

&lt;p&gt;After adding the project, we can now run it either as a local console application or in the computer emulator as an emulated worker role.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Note: Don&#039;t forget that the compute emulator must run in elevated administrator mode.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;And there you have it, a Console project promoted to Azure Worker Role, preserving any history of commits that were made against the project and still runnable as either a console application or as a worker role.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/converting-a-net-console-project&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>So lets say you're building a worker role for Azure, but started it out as a Console app for faster local debugging. It comes time to deploy it to Azure for the first time, but there's no option to convert a Console application to a Worker Role project. We could copy all the code into a new project, but then we not only lose our faster debugging but also the history of commits we have made against the project. </p>

<p>Luckily converting the existing Console project to be deployable as an Azure Worker Role project is fairly easy.</p>

<p>For the purposes of this post, I have created a solution with an Existing Console project, an Azure Cloud Project, and an Existing Azure Worker Role project (we have to have at least one to create the Azure Cloud Project, it can be deleted later).</p>

<div class="font-size: 85%; color: #666666; text-align: center">
<img src="http://tiernok.com/LTDBlog/AzureWorkerRole/awr_01.png" alt="Solution Explorer" /><br />
Solution Explorer view of the Sample Solution
</div>

<p>If we right click the Azure "Roles" folder, the option to use an existing Worker Role project is not available. Visual Studio enables or disables this option automatically, based on whether we have any assignable projects in the solution that aren't already associated with the Azure project.</p>

<div class="font-size: 85%; color: #666666; text-align: center">
<img src="http://tiernok.com/LTDBlog/AzureWorkerRole/awr_02.png" alt="Add Role menu options" /><br />
"Worker Role Project in Solution" is Disabled
</div>

<p>The first thing we want to do is add a RoleEntryPoint to the console application. To keep this simple, I'm going to convert the Program class to serve as the RoleEntryPoint also.</p>

<p>To add the RoleEntryPoint class, I need to first add some azure references. I'm using Azure 1.8, so this means I'll need the following 4 references:</p>

<div class="font-size: 85%; color: #666666; text-align: center">
<img src="http://tiernok.com/LTDBlog/AzureWorkerRole/awr_03.png" alt="Assembly References Dialog" /><br />
Adding WindowsAzure References
</div>

<p><i>Note: The Storage SDK version number for Azure SDK 1.8 is 2.0. I suspect they chose to do this because they made a number of significant changes in the storage library that were not backwards compatible with 1.7.</i></p>

<p><i>Note #2: I noticed that if you add a new worker role through Visual Studio, it uses StorageClient 1.7 still despite this being SDK 1.8. This may be a requirement from the Azure Diagnostics assembly, Diagnostics uses StorageClient and may still be a version behind. I would test this theory, but getting Diagnostics working properly tends to be somewhat painful.</i></p>

<p>After adding the references, we can then add RoleEntryPoint inheritance to the Program. New Worker Roles have two methods they override from RoleEntryPoint, but the important one is Run (and why this isn't abstract, I couldn't tell you). </p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb48632'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb48632','cb30531'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb48632" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">namespace</span> ExistingConsoleApplication</li><li style="" class="li2"><span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Program : RoleEntryPoint</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>&nbsp;<span style="color: #008080; font-style: italic;">/* Console entry point */</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var ibl = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> InterestingBusinessLogic<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ibl.<span style="color: #0000FF;">DoStuff</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>&nbsp; <span style="color: #008080; font-style: italic;">/* Worker Role entry point */</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var ibl = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> InterestingBusinessLogic<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ibl.<span style="color: #0000FF;">DoStuff</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb30531" style="display: none; color: red;"></div></div></div>

<p>The last thing we need to do is add some information to the project file. I can open the file in Visual Studio by right clicking and selecting "Unload Project", then right clicking again and selecting "Edit <i>project name</i>". In the Property Group I need to add a single property named "RoleType" to identify this as a Worker (see the last entry):</p>

<div class="codebox"><div class="codeheader"><span>xml</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb36788'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb36788','cb50246'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="xml" id="cb36788" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;PropertyGroup<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Configuration</span> <span style="color: #000066;">Condition</span>=<span style="color: #ff0000;">&quot; '$(Configuration)' == '' &quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>Debug<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Configuration<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Platform</span> <span style="color: #000066;">Condition</span>=<span style="color: #ff0000;">&quot; '$(Platform)' == '' &quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>AnyCPU<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Platform<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ProjectGuid<span style="font-weight: bold; color: black;">&gt;</span></span></span>{4B7C48CB-9899-47D0-95AB-2FD3C3B739CF}<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ProjectGuid<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;OutputType<span style="font-weight: bold; color: black;">&gt;</span></span></span>Exe<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/OutputType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;AppDesignerFolder<span style="font-weight: bold; color: black;">&gt;</span></span></span>Properties<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/AppDesignerFolder<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;RootNamespace<span style="font-weight: bold; color: black;">&gt;</span></span></span>ExistingConsoleApplication<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/RootNamespace<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;AssemblyName<span style="font-weight: bold; color: black;">&gt;</span></span></span>ExistingConsoleApplication<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/AssemblyName<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;TargetFrameworkVersion<span style="font-weight: bold; color: black;">&gt;</span></span></span>v4.5<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/TargetFrameworkVersion<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;FileAlignment<span style="font-weight: bold; color: black;">&gt;</span></span></span>512<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/FileAlignment<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;RoleType<span style="font-weight: bold; color: black;">&gt;</span></span></span>Worker<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/RoleType<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/PropertyGroup<span style="font-weight: bold; color: black;">&gt;</span></span></span></li></ol></div><div id="cb50246" style="display: none; color: red;"></div></div></div>

<p>Save and reload the project, and now when we right click on the Azure Cloud Project Roles, the option to associate and existing project is available.</p>

<div class="font-size: 85%; color: #666666; text-align: center">
<img src="http://tiernok.com/LTDBlog/AzureWorkerRole/awr_04.png" alt="Add Role menu options" /><br />
"Worker Role Project in Solution" is Available Now
</div>

<p>After adding the project, we can now run it either as a local console application or in the computer emulator as an emulated worker role.</p>

<p><i>Note: Don't forget that the compute emulator must run in elevated administrator mode.</i></p>

<p>And there you have it, a Console project promoted to Azure Worker Role, preserving any history of commits that were made against the project and still runnable as either a console application or as a worker role.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/converting-a-net-console-project">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/converting-a-net-console-project#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2&#38;disp=comments&#38;p=1886</wfw:commentRss>
		</item>
				<item>
			<title>Pick the Right Storage: All SQL is Not Equal</title>
			<link>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/pick-the-right-storage-all</link>
			<pubDate>Tue, 26 Jun 2012 10:38:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">C#</category>			<guid isPermaLink="false">1761@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;As part of a long series of posts, I implemented a version of the MVC Music Store tutorial application on top of a pair of SQL Server CE databases. SQL Server CE is great for small apps, being a portable file-based database that can easily be moved to a full SQL Server instance. Last week I migrated my application to use full SQL Server instances instead of the SDF file and picked up a 3x performance improvement. It was interesting enough that I decided to share &lt;img src=&quot;http://blogs.lessthandot.com/rsc/smilies/icon_smile.gif&quot; title=&quot;:)&quot; alt=&quot;:)&quot; class=&quot;middle&quot; width=&quot;15&quot; height=&quot;15&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Note: I wasn&#039;t purposefully performance tuning my sample site. This move was a necessary step to prep for doing some other SQL Server related things with the project and the performance improvement was just a neat side effect.&lt;/i&gt;&lt;/p&gt;

&lt;h2&gt;The Results&lt;/h2&gt;
&lt;p&gt;The graph below shows the results of the load test stage of my build over several builds. &lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/ContinuousDelivery/SqlServerLoadTestRates.png&quot; alt=&quot;Graph of Requests/Second and Transactions/Second&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We start with the Rate graph. On the left we see a rate of about 30 requests/second from when the system was still on the SQL Server CE file and the build and beta VMs resided on their original home (i7 920, VMWare, using Hyper-V equivalent of 2 logical CPUs). In the middle we have the move to their new home (i7 3770K, Hyper-V 2012) still on the SQL Server CE file and switching from one to two virtual CPUs assigned to the beta web server. The last third is the results of moving the primary database to a SQL Server VM, which improved the performance by roughly 3x and appears to have found a new performance constraint.&lt;/p&gt;

&lt;p&gt;The transaction rate has gone from about 1.8 to about 4.8 per second.&lt;/p&gt;


&lt;p&gt;&lt;img src=&quot;http://tiernok.com/LTDBlog/ContinuousDelivery/SqlServerLoadTestResponses.png&quot; alt=&quot;Graph of time to last response for minimum, average, 95%, 99%, and maximum&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We can see the response times have been fairly stable, up to the 99% mark, with the max being a little wonky. The 95% and average marks have gone from averaging 650ms and 250ms on the original server with CE to 450ms and 80ms respectively on SQL Server.&lt;/p&gt;

&lt;h2&gt;The Details&lt;/h2&gt;
&lt;p&gt;The data for these results are generated automatically as part of the &lt;a href=&quot;http://wiki.lessthandot.com/index.php/Eli%27s_Continuous_Delivery_Project&quot; title=&quot;See more information in the Continuous Delivery wiki entry&quot;&gt;build process&lt;/a&gt; for my application. I have 3 dedicated VMs for the build server, a web server, and a database server and they are all currently housed on a 2012 Hyper-V server. Each is assigned about 2GB of RAM and 2, 1, 1 CPUs for the web, build, and DB server respectively. The host has nothing else running on it and is an i7 3770K w/ 16GB of high speed RAM behind it and mirrored standard drives on SATA III.&lt;/p&gt;

&lt;p&gt;The data was collected as part of the &lt;a href=&quot;http://blogs.lessthandot.com/index.php/EnterpriseDev/application-lifecycle-management/continuous-delivery-adding-the-load&quot; title=&quot;Implementing the Automated Load test blog post&quot;&gt;automated Load Test&lt;/a&gt; step of the build process. During this step, the build server deploys a copy of the application to a website on the web VM and then spins up WCAT to &lt;a href=&quot;http://blogs.lessthandot.com/index.php/EnterpriseDev/application-lifecycle-management/implementing-wcat-for-load-testing&quot; title=&quot;Implementing WCAT to test a website&quot;&gt;load test the site&lt;/a&gt;. There is an incremental spin up period that warms up the site and connection pool, followed by a 2.5 minute sustained test where WCAT runs as fast as it can against the site. &lt;/p&gt;

&lt;p&gt;The load test is a full transaction that makes the same requests a real world browser makes as it selects an album, adds it to a cart, and then checks out, including static content and dynamic content. Each transaction is in a unique session and reports the appropriate headers with each HTTP request. The increase from 1.8 to 4.8 transactions per second can be restated as an increase from 1.8 to 4.8 individual purchases per second.&lt;/p&gt;

&lt;p&gt;The site is still on SQL CE for the membership database, which is used during the checkout, and it is using Entity Framework 4 for data access to SQL Server (so more headroom for improvement if I was actually tuning for performance).&lt;/p&gt;

&lt;p&gt;After seeing the increase I thought it would be interesting to share the numbers as well as how useful it is to have a short load test as part of the delivery pipeline.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/pick-the-right-storage-all&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>As part of a long series of posts, I implemented a version of the MVC Music Store tutorial application on top of a pair of SQL Server CE databases. SQL Server CE is great for small apps, being a portable file-based database that can easily be moved to a full SQL Server instance. Last week I migrated my application to use full SQL Server instances instead of the SDF file and picked up a 3x performance improvement. It was interesting enough that I decided to share <img src="http://blogs.lessthandot.com/rsc/smilies/icon_smile.gif" title=":)" alt=":)" class="middle" width="15" height="15" /></p>

<p><i>Note: I wasn't purposefully performance tuning my sample site. This move was a necessary step to prep for doing some other SQL Server related things with the project and the performance improvement was just a neat side effect.</i></p>

<h2>The Results</h2>
<p>The graph below shows the results of the load test stage of my build over several builds. </p>

<p><img src="http://tiernok.com/LTDBlog/ContinuousDelivery/SqlServerLoadTestRates.png" alt="Graph of Requests/Second and Transactions/Second" /></p>

<p>We start with the Rate graph. On the left we see a rate of about 30 requests/second from when the system was still on the SQL Server CE file and the build and beta VMs resided on their original home (i7 920, VMWare, using Hyper-V equivalent of 2 logical CPUs). In the middle we have the move to their new home (i7 3770K, Hyper-V 2012) still on the SQL Server CE file and switching from one to two virtual CPUs assigned to the beta web server. The last third is the results of moving the primary database to a SQL Server VM, which improved the performance by roughly 3x and appears to have found a new performance constraint.</p>

<p>The transaction rate has gone from about 1.8 to about 4.8 per second.</p>


<p><img src="http://tiernok.com/LTDBlog/ContinuousDelivery/SqlServerLoadTestResponses.png" alt="Graph of time to last response for minimum, average, 95%, 99%, and maximum" /></p>

<p>We can see the response times have been fairly stable, up to the 99% mark, with the max being a little wonky. The 95% and average marks have gone from averaging 650ms and 250ms on the original server with CE to 450ms and 80ms respectively on SQL Server.</p>

<h2>The Details</h2>
<p>The data for these results are generated automatically as part of the <a href="http://wiki.lessthandot.com/index.php/Eli%27s_Continuous_Delivery_Project" title="See more information in the Continuous Delivery wiki entry">build process</a> for my application. I have 3 dedicated VMs for the build server, a web server, and a database server and they are all currently housed on a 2012 Hyper-V server. Each is assigned about 2GB of RAM and 2, 1, 1 CPUs for the web, build, and DB server respectively. The host has nothing else running on it and is an i7 3770K w/ 16GB of high speed RAM behind it and mirrored standard drives on SATA III.</p>

<p>The data was collected as part of the <a href="http://blogs.lessthandot.com/index.php/EnterpriseDev/application-lifecycle-management/continuous-delivery-adding-the-load" title="Implementing the Automated Load test blog post">automated Load Test</a> step of the build process. During this step, the build server deploys a copy of the application to a website on the web VM and then spins up WCAT to <a href="http://blogs.lessthandot.com/index.php/EnterpriseDev/application-lifecycle-management/implementing-wcat-for-load-testing" title="Implementing WCAT to test a website">load test the site</a>. There is an incremental spin up period that warms up the site and connection pool, followed by a 2.5 minute sustained test where WCAT runs as fast as it can against the site. </p>

<p>The load test is a full transaction that makes the same requests a real world browser makes as it selects an album, adds it to a cart, and then checks out, including static content and dynamic content. Each transaction is in a unique session and reports the appropriate headers with each HTTP request. The increase from 1.8 to 4.8 transactions per second can be restated as an increase from 1.8 to 4.8 individual purchases per second.</p>

<p>The site is still on SQL CE for the membership database, which is used during the checkout, and it is using Entity Framework 4 for data access to SQL Server (so more headroom for improvement if I was actually tuning for performance).</p>

<p>After seeing the increase I thought it would be interesting to share the numbers as well as how useful it is to have a short load test as part of the delivery pipeline.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/pick-the-right-storage-all">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/pick-the-right-storage-all#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2&#38;disp=comments&#38;p=1761</wfw:commentRss>
		</item>
				<item>
			<title>The Cloud as a Make-It-Finish-Sooner Dial</title>
			<link>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/the-cloud-as-a-make</link>
			<pubDate>Wed, 23 May 2012 10:10:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">Microsoft Technologies</category>			<guid isPermaLink="false">1731@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;There&#039;s been a lot of buzz about the cloud over the past years, with a lot of that attention going to IaaS and SaaS platforms, but there&#039;s a revolution (or re-revolution) that is of even more importance, and that&#039;s PaaS. What PaaS brings us is the ability to scale horizontally and treat CPU, memory, and storage as pools of resources that are as deep as our checkbooks allow. &lt;/p&gt;

&lt;p&gt;Forget about virtual servers. Remember that 60 hour job with a 24 hour deadline? Built on a PaaS platform and equipped with a couple hundred dollars, you won&#039;t even be staying late today.&lt;/p&gt;

&lt;p&gt;Today&#039;s post is going to share highlights from a basic file processing application. Something you would find (hopefully better written) in any random enterprise IT shop or SaaS company. It offers a web page that lets you upload files, a button to process files, a basic and poorly written list of the processed and unprocessed files, and an unattended worker. The trick is that this application was written on top of Windows Azure, so I can play tricks with time just by twisting the dial from one file processor to twenty.&lt;/p&gt;

&lt;h2&gt;The Basic File Processor&lt;/h2&gt;
&lt;p&gt;The file processing in the sample application is intended to be a sample workload. It consists of reading files completely into memory and passing them around, spinning through them one character at a time, replacing each character in the line with it&#039;s upper case variant. Very critical stuff, very performant.&lt;/p&gt;

&lt;p&gt;In addition to running the process via the website, I also need an unattended application that will can run the same processing function. If I owned the server, this would be a scheduled task or service. As an Azure Worker the code will be remarkably similar.&lt;/p&gt;

&lt;div style=&quot;text-align: center; color: #666666; font-size: 90%&quot;&gt;
   &lt;img src=&quot;http://tiernok.com/LTDBlog/CloudFileProcessor.png&quot; alt=&quot;Cloud Processor Architecture&quot; style=&quot;margin-bottom: 5px&quot; /&gt;&lt;br /&gt;
   Architecture of the Processor
&lt;/div&gt;

&lt;p&gt;The two front-ends access common logic in the Core library, which is responsible for both the processing logic and interacting with storage resources. This being sample code, it is certified as working on my machine and is definitely not production ready. That being said, I did write this in a few evenings, so writing a production-ready service doesn&#039;t have to take that long in normal workdays.&lt;/p&gt;

&lt;h3&gt;The Web Site&lt;/h3&gt;
&lt;p&gt;The website has a single MVC controller with 3 actions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;~/Home/Index: Displays the list of processed and unprocessed items and buttons for upload and processing&lt;/li&gt;
&lt;li&gt;~/Home/AddFile: The post address for file uploads&lt;/li&gt;
&lt;li&gt;~/Home/ProcessNextItem: An action to process the next queued file&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb52013&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; HomeController : Controller &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; IStorageLocator _storageLocator;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; HomeController&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _storageLocator = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; StorageManager&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; ActionResult Index&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var store = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; ItemStore&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;_storageLocator&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var model = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; StatusViewModel&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ProcessedItems = store.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetProcessedList&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; UnprocessedItems = store.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetUnprocessedList&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ViewData&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;file&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt; = TempData&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;file&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; View&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;model&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;HttpPost&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; ActionResult AddFile&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;HttpPostedFileBase file&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;file != &lt;span style=&quot;color: #0600FF;&quot;&gt;null&lt;/span&gt; &amp;amp;&amp;amp; file.&lt;span style=&quot;color: #0000FF;&quot;&gt;ContentLength&lt;/span&gt; &amp;gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var item = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; FullItem&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ResourceId = Guid.&lt;span style=&quot;color: #0000FF;&quot;&gt;NewGuid&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Received = DateTime.&lt;span style=&quot;color: #0000FF;&quot;&gt;Now&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;ToUniversalTime&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IsProcessed = &lt;span style=&quot;color: #0600FF;&quot;&gt;false&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FileName = file.&lt;span style=&quot;color: #0000FF;&quot;&gt;FileName&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; item.&lt;span style=&quot;color: #0000FF;&quot;&gt;ReadFileFromStream&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;file.&lt;span style=&quot;color: #0000FF;&quot;&gt;InputStream&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; ItemStore&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;_storageLocator&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;AddNewItem&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;item&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TempData&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;file&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt; = file.&lt;span style=&quot;color: #0000FF;&quot;&gt;FileName&lt;/span&gt; + &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot; uploaded and queued for processing.&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TempData&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;file&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt; = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Processor ignores empty files, sorry.&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; RedirectToAction&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;HttpGet&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; ActionResult ProcessNextItem&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var store = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; ItemStore&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;_storageLocator&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; ItemProcessor&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;ProcessNextItem&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;store&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; RedirectToAction&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Index&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb37356&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The key to all of these methods is the ItemStore and ItemProcessor class, all of the rest of the logic is basic presentation layer logic.&lt;/p&gt;

&lt;h3&gt;The Worker Role&lt;/h3&gt;
&lt;p&gt;The worker role consists of a roughly 6 line while(true) statement that asks the ItemStore to process the next item in the queue, then sleeps for 1 second.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb1244&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; Run&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var storageLocator = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; StorageManager&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var store = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; ItemStore&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;storageLocator&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; ItemProcessor&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;ProcessNextItem&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;store&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Thread.&lt;span style=&quot;color: #0000FF;&quot;&gt;Sleep&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Trace.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Working&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Information&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb3745&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once again, the magic happens in the ItemStore and ItemProcessor instances.&lt;/p&gt;

&lt;h3&gt;The ItemStore&lt;/h3&gt;
&lt;p&gt;The ItemStore class exposes the basic methods we need execute our process:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb2895&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; AddNewItem&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;FullItem item&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;/* More Code */&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; FullItem RetrieveForProcessing&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;/* More Code */&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; StoreFinishedItem&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;FullItem item&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;/* More Code */&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb93278&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;


&lt;p&gt;And a pair of methods for visibility:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb63192&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; IEnumerable&amp;lt;ItemBase&amp;gt; GetUnprocessedList&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;/* More Code */&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; IEnumerable&amp;lt;ItemBase&amp;gt; GetProcessedList&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;/* More Code */&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb75347&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Windows Azure offers a number of storage options, each with their own benefits and constraints. For this process I decided to use &lt;a href=&quot;http://www.windowsazure.com/en-us/develop/net/how-to-guides/table-services/&quot; title=&quot;Table storage on WindowsAzure.com&quot;&gt;table storage&lt;/a&gt; to track the summary level information about each file processing job, &lt;a href=&quot;http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/&quot; title=&quot;Blob storage on WindowsAzure.com&quot;&gt;blob storage&lt;/a&gt; to store the actual file, and the &lt;a href=&quot;http://www.windowsazure.com/en-us/develop/net/how-to-guides/queue-service/&quot; title=&quot;Queue service on WindowsAzure.com&quot;&gt;queue service&lt;/a&gt; for managing task execution. &lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb61479&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; ItemStore &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; RAW_BLOB_NAME = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;RawItems&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; FINISHED_BLOB_NAME = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;FinishedItems&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; QUEUE_NAME = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;ToBeProcessed&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; TABLE_NAME = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Items&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; ITableStore _table;&lt;br /&gt;&amp;nbsp; &amp;nbsp; IBlobStore _rawBlob, _finishedBlob;&lt;br /&gt;&amp;nbsp; &amp;nbsp; IQueueStore _queue;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; ItemStore&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;IStorageLocator storageLocator&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _table = storageLocator.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetTable&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;TABLE_NAME&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _rawBlob = storageLocator.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetBlob&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;RAW_BLOB_NAME&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _finishedBlob = storageLocator.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetBlob&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;FINISHED_BLOB_NAME&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _queue = storageLocator.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetQueue&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;QUEUE_NAME&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; AddNewItem&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;FullItem item&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _rawBlob.&lt;span style=&quot;color: #0000FF;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;item.&lt;span style=&quot;color: #0000FF;&quot;&gt;ResourceId&lt;/span&gt;, item.&lt;span style=&quot;color: #0000FF;&quot;&gt;File&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _queue.&lt;span style=&quot;color: #0000FF;&quot;&gt;Enqueue&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;item.&lt;span style=&quot;color: #0000FF;&quot;&gt;AsSummary&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _table.&lt;span style=&quot;color: #0000FF;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;item.&lt;span style=&quot;color: #0000FF;&quot;&gt;AsSummary&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; IEnumerable&amp;lt;ItemBase&amp;gt; GetUnprocessedList&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; _table.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetUnprocessedItems&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;ToList&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; IEnumerable&amp;lt;ItemBase&amp;gt; GetProcessedList&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// ?&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; _table.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetProcessedItems&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;ToList&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; FullItem RetrieveForProcessing&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FullItem rawItem = &lt;span style=&quot;color: #0600FF;&quot;&gt;null&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var item = _queue.&lt;span style=&quot;color: #0000FF;&quot;&gt;Dequeue&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;item != &lt;span style=&quot;color: #0600FF;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; rawItem = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; FullItem&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;item&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; rawItem.&lt;span style=&quot;color: #0000FF;&quot;&gt;File&lt;/span&gt; = _rawBlob.&lt;span style=&quot;color: #0000FF;&quot;&gt;Retrieve&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;item.&lt;span style=&quot;color: #0000FF;&quot;&gt;ResourceId&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; rawItem;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; StoreFinishedItem&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;FullItem item&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _finishedBlob.&lt;span style=&quot;color: #0000FF;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;item.&lt;span style=&quot;color: #0000FF;&quot;&gt;ResourceId&lt;/span&gt;, item.&lt;span style=&quot;color: #0000FF;&quot;&gt;File&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _rawBlob.&lt;span style=&quot;color: #0000FF;&quot;&gt;Delete&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;item.&lt;span style=&quot;color: #0000FF;&quot;&gt;ResourceId&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; _table.&lt;span style=&quot;color: #0000FF;&quot;&gt;Update&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;item.&lt;span style=&quot;color: #0000FF;&quot;&gt;AsSummary&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb57034&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The ItemStore class is built to interact with interfaces for each of these resources, using a single IStorageLocator interface to get instances of those resource interfaces. The class (and application) was driven by the small set of unit tests that helped me define how i wanted the process to work and interact with the resources above.&lt;/p&gt;

&lt;h3&gt;Configurations&lt;/h3&gt;
&lt;p&gt;With all of the pieces defined, we use a pair of configurations to tell Azure how we want to deploy everything. &lt;/p&gt;

&lt;p&gt;The first configuration defines the services we intend to package and deploy as well as the instance size and any endpoints:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;xml&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;xml&quot; id=&quot;cb55416&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;ServiceDefinition&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;CloudFileProcessorService&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;xmlns&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;WebRole&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Processor_WebRole&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;vmsize&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Small&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Sites&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Site&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Web&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Bindings&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Binding&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Endpoint1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;endpointName&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Endpoint1&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Bindings&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Site&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Sites&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Endpoints&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;InputEndpoint&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Endpoint1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;protocol&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;port&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;80&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Endpoints&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Imports&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Import&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;moduleName&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Diagnostics&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Imports&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/WebRole&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;WorkerRole&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Processor_WorkerRole&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;vmsize&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Small&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Imports&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Import&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;moduleName&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Diagnostics&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Imports&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/WorkerRole&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/ServiceDefinition&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb32811&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The second configuration is applied when we deploy the instances above and tells Azure that I want to deploy 1 Processor_WebRole instance and 2 Processor_WorkerRole instances:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;xml&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;xml&quot; id=&quot;cb76858&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;ServiceConfiguration&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;serviceName&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;CloudFileProcessorService&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;xmlns&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;osFamily&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;osVersion&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;*&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Role&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Processor_WebRole&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Instances&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;count&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;ConfigurationSettings&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Setting&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;UseDevelopmentStorage=true&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/ConfigurationSettings&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Role&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Role&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Processor_WorkerRole&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Instances&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;count&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;2&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;ConfigurationSettings&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Setting&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;UseDevelopmentStorage=true&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/ConfigurationSettings&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Role&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/ServiceConfiguration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb48430&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that I&#039;m telling it to use the local development storage, which is supported by a local storage emulator. In a production configuration I would enter the service location and a generated token.&lt;/p&gt;

&lt;h2&gt;So Where&#039;s the Magic?&lt;/h2&gt;
&lt;p&gt;So where&#039;s the magic that makes this a distribute application instead of 3 days of overtime? It&#039;s sprinkled throughout the system.&lt;/p&gt;

&lt;p&gt;The architecture of this system would work just as well outside of Azure, provided I offered it stand-ins for the 3 storage resources and deployed the instances and any necessary settings accordingly. Instead of worrying about how to manage deployments and what to use for centralized queueing and storage, I can focus on building an application that simply assumes those resources are available. Is there headroom for performance improvements? Sure, but I can also choose to throw another $15/month server at it, push data to CDNs and blob storage, add caching, or even a SQL Azure instance. &lt;/p&gt;

&lt;p&gt;This application may be fairly basic, but nothing stops us from following this same pattern for much larger applications. PaaS has removed some of the constraints we take for granted. Even applications that have to run in-house in order to standardize against a database can now consider uploading a subset of that lookup data to a table store, performing most of the heavy lifting in the cloud, then produce a few files to import back into the on-premise system. The total execution time would be longer, but being able to scale part of the job across numerous parallel instances means the actual elapsed time can actually be much shorter.&lt;/p&gt;

&lt;p&gt;And it means when we have a 60 hour task that&#039;s due in 24, it&#039;s no longer an impossible situation.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;The source code is available &lt;a href=&quot;https://github.com/tarwn/AzureFileProcessorSample&quot; title=&quot;AzureFileProcessorSample on github&quot;&gt;on github&lt;/a&gt; along with requirements and links for setting up the emulators locally.&lt;/i&gt;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/the-cloud-as-a-make&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>There's been a lot of buzz about the cloud over the past years, with a lot of that attention going to IaaS and SaaS platforms, but there's a revolution (or re-revolution) that is of even more importance, and that's PaaS. What PaaS brings us is the ability to scale horizontally and treat CPU, memory, and storage as pools of resources that are as deep as our checkbooks allow. </p>

<p>Forget about virtual servers. Remember that 60 hour job with a 24 hour deadline? Built on a PaaS platform and equipped with a couple hundred dollars, you won't even be staying late today.</p>

<p>Today's post is going to share highlights from a basic file processing application. Something you would find (hopefully better written) in any random enterprise IT shop or SaaS company. It offers a web page that lets you upload files, a button to process files, a basic and poorly written list of the processed and unprocessed files, and an unattended worker. The trick is that this application was written on top of Windows Azure, so I can play tricks with time just by twisting the dial from one file processor to twenty.</p>

<h2>The Basic File Processor</h2>
<p>The file processing in the sample application is intended to be a sample workload. It consists of reading files completely into memory and passing them around, spinning through them one character at a time, replacing each character in the line with it's upper case variant. Very critical stuff, very performant.</p>

<p>In addition to running the process via the website, I also need an unattended application that will can run the same processing function. If I owned the server, this would be a scheduled task or service. As an Azure Worker the code will be remarkably similar.</p>

<div style="text-align: center; color: #666666; font-size: 90%">
   <img src="http://tiernok.com/LTDBlog/CloudFileProcessor.png" alt="Cloud Processor Architecture" style="margin-bottom: 5px" /><br />
   Architecture of the Processor
</div>

<p>The two front-ends access common logic in the Core library, which is responsible for both the processing logic and interacting with storage resources. This being sample code, it is certified as working on my machine and is definitely not production ready. That being said, I did write this in a few evenings, so writing a production-ready service doesn't have to take that long in normal workdays.</p>

<h3>The Web Site</h3>
<p>The website has a single MVC controller with 3 actions:</p>

<ul>
<li>~/Home/Index: Displays the list of processed and unprocessed items and buttons for upload and processing</li>
<li>~/Home/AddFile: The post address for file uploads</li>
<li>~/Home/ProcessNextItem: An action to process the next queued file</li>
</ul>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb52189'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb52189','cb96361'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb52189" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HomeController : Controller <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; IStorageLocator _storageLocator;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> HomeController<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; _storageLocator = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StorageManager<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> ActionResult Index<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; var store = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ItemStore<span style="color: #000000;">&#40;</span>_storageLocator<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var model = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StatusViewModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProcessedItems = store.<span style="color: #0000FF;">GetProcessedList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UnprocessedItems = store.<span style="color: #0000FF;">GetUnprocessedList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; ViewData<span style="color: #000000;">&#91;</span><span style="color: #808080;">&quot;file&quot;</span><span style="color: #000000;">&#93;</span> = TempData<span style="color: #000000;">&#91;</span><span style="color: #808080;">&quot;file&quot;</span><span style="color: #000000;">&#93;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>model<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#91;</span>HttpPost<span style="color: #000000;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> ActionResult AddFile<span style="color: #000000;">&#40;</span>HttpPostedFileBase file<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>file != <span style="color: #0600FF;">null</span> &amp;&amp; file.<span style="color: #0000FF;">ContentLength</span> &gt; <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var item = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> FullItem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResourceId = Guid.<span style="color: #0000FF;">NewGuid</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Received = DateTime.<span style="color: #0000FF;">Now</span>.<span style="color: #0000FF;">ToUniversalTime</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsProcessed = <span style="color: #0600FF;">false</span>,</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileName = file.<span style="color: #0000FF;">FileName</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.<span style="color: #0000FF;">ReadFileFromStream</span><span style="color: #000000;">&#40;</span>file.<span style="color: #0000FF;">InputStream</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ItemStore<span style="color: #000000;">&#40;</span>_storageLocator<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AddNewItem</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TempData<span style="color: #000000;">&#91;</span><span style="color: #808080;">&quot;file&quot;</span><span style="color: #000000;">&#93;</span> = file.<span style="color: #0000FF;">FileName</span> + <span style="color: #808080;">&quot; uploaded and queued for processing.&quot;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TempData<span style="color: #000000;">&#91;</span><span style="color: #808080;">&quot;file&quot;</span><span style="color: #000000;">&#93;</span> = <span style="color: #808080;">&quot;Processor ignores empty files, sorry.&quot;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> RedirectToAction<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Index&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#91;</span>HttpGet<span style="color: #000000;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> ActionResult ProcessNextItem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; var store = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ItemStore<span style="color: #000000;">&#40;</span>_storageLocator<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ItemProcessor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ProcessNextItem</span><span style="color: #000000;">&#40;</span>store<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> RedirectToAction<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Index&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb96361" style="display: none; color: red;"></div></div></div>

<p>The key to all of these methods is the ItemStore and ItemProcessor class, all of the rest of the logic is basic presentation layer logic.</p>

<h3>The Worker Role</h3>
<p>The worker role consists of a roughly 6 line while(true) statement that asks the ItemStore to process the next item in the queue, then sleeps for 1 second.</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb6148'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb6148','cb44248'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb6148" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var storageLocator = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StorageManager<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; var store = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ItemStore<span style="color: #000000;">&#40;</span>storageLocator<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ItemProcessor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ProcessNextItem</span><span style="color: #000000;">&#40;</span>store<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1000</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Working&quot;</span>, <span style="color: #808080;">&quot;Information&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb44248" style="display: none; color: red;"></div></div></div>

<p>Once again, the magic happens in the ItemStore and ItemProcessor instances.</p>

<h3>The ItemStore</h3>
<p>The ItemStore class exposes the basic methods we need execute our process:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb12375'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb12375','cb99161'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb12375" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddNewItem<span style="color: #000000;">&#40;</span>FullItem item<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">/* More Code */</span> <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0600FF;">public</span> FullItem RetrieveForProcessing<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">/* More Code */</span> <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> StoreFinishedItem<span style="color: #000000;">&#40;</span>FullItem item<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">/* More Code */</span> <span style="color: #000000;">&#125;</span></li></ol></div><div id="cb99161" style="display: none; color: red;"></div></div></div>


<p>And a pair of methods for visibility:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb22602'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb22602','cb41533'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb22602" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> IEnumerable&lt;ItemBase&gt; GetUnprocessedList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">/* More Code */</span> <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0600FF;">public</span> IEnumerable&lt;ItemBase&gt; GetProcessedList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">/* More Code */</span> <span style="color: #000000;">&#125;</span></li></ol></div><div id="cb41533" style="display: none; color: red;"></div></div></div>

<p>Windows Azure offers a number of storage options, each with their own benefits and constraints. For this process I decided to use <a href="http://www.windowsazure.com/en-us/develop/net/how-to-guides/table-services/" title="Table storage on WindowsAzure.com">table storage</a> to track the summary level information about each file processing job, <a href="http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/" title="Blob storage on WindowsAzure.com">blob storage</a> to store the actual file, and the <a href="http://www.windowsazure.com/en-us/develop/net/how-to-guides/queue-service/" title="Queue service on WindowsAzure.com">queue service</a> for managing task execution. </p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb46806'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb46806','cb5837'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb46806" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ItemStore <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> RAW_BLOB_NAME = <span style="color: #808080;">&quot;RawItems&quot;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> FINISHED_BLOB_NAME = <span style="color: #808080;">&quot;FinishedItems&quot;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> QUEUE_NAME = <span style="color: #808080;">&quot;ToBeProcessed&quot;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> TABLE_NAME = <span style="color: #808080;">&quot;Items&quot;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; ITableStore _table;</li><li style="" class="li1">&nbsp; &nbsp; IBlobStore _rawBlob, _finishedBlob;</li><li style="" class="li2">&nbsp; &nbsp; IQueueStore _queue;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> ItemStore<span style="color: #000000;">&#40;</span>IStorageLocator storageLocator<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; _table = storageLocator.<span style="color: #0000FF;">GetTable</span><span style="color: #000000;">&#40;</span>TABLE_NAME<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; _rawBlob = storageLocator.<span style="color: #0000FF;">GetBlob</span><span style="color: #000000;">&#40;</span>RAW_BLOB_NAME<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; _finishedBlob = storageLocator.<span style="color: #0000FF;">GetBlob</span><span style="color: #000000;">&#40;</span>FINISHED_BLOB_NAME<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; _queue = storageLocator.<span style="color: #0000FF;">GetQueue</span><span style="color: #000000;">&#40;</span>QUEUE_NAME<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddNewItem<span style="color: #000000;">&#40;</span>FullItem item<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; _rawBlob.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">ResourceId</span>, item.<span style="color: #0000FF;">File</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; _queue.<span style="color: #0000FF;">Enqueue</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">AsSummary</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; _table.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">AsSummary</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> IEnumerable&lt;ItemBase&gt; GetUnprocessedList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> _table.<span style="color: #0000FF;">GetUnprocessedItems</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> IEnumerable&lt;ItemBase&gt; GetProcessedList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// ?</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> _table.<span style="color: #0000FF;">GetProcessedItems</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> FullItem RetrieveForProcessing<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; FullItem rawItem = <span style="color: #0600FF;">null</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var item = _queue.<span style="color: #0000FF;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>item != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rawItem = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> FullItem<span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rawItem.<span style="color: #0000FF;">File</span> = _rawBlob.<span style="color: #0000FF;">Retrieve</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">ResourceId</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> rawItem;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> StoreFinishedItem<span style="color: #000000;">&#40;</span>FullItem item<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; _finishedBlob.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">ResourceId</span>, item.<span style="color: #0000FF;">File</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; _rawBlob.<span style="color: #0000FF;">Delete</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">ResourceId</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; _table.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">AsSummary</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb5837" style="display: none; color: red;"></div></div></div>

<p>The ItemStore class is built to interact with interfaces for each of these resources, using a single IStorageLocator interface to get instances of those resource interfaces. The class (and application) was driven by the small set of unit tests that helped me define how i wanted the process to work and interact with the resources above.</p>

<h3>Configurations</h3>
<p>With all of the pieces defined, we use a pair of configurations to tell Azure how we want to deploy everything. </p>

<p>The first configuration defines the services we intend to package and deploy as well as the instance size and any endpoints:</p>
<div class="codebox"><div class="codeheader"><span>xml</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb74940'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb74940','cb56131'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="xml" id="cb74940" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ServiceDefinition</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;CloudFileProcessorService&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li2">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;WebRole</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Processor_WebRole&quot;</span> <span style="color: #000066;">vmsize</span>=<span style="color: #ff0000;">&quot;Small&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Sites<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Site</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Web&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Bindings<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Endpoint1&quot;</span> <span style="color: #000066;">endpointName</span>=<span style="color: #ff0000;">&quot;Endpoint1&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Bindings<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Site<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Sites<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Endpoints<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;InputEndpoint</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Endpoint1&quot;</span> <span style="color: #000066;">protocol</span>=<span style="color: #ff0000;">&quot;http&quot;</span> <span style="color: #000066;">port</span>=<span style="color: #ff0000;">&quot;80&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Endpoints<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Imports<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Import</span> <span style="color: #000066;">moduleName</span>=<span style="color: #ff0000;">&quot;Diagnostics&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Imports<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/WebRole<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;WorkerRole</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Processor_WorkerRole&quot;</span> <span style="color: #000066;">vmsize</span>=<span style="color: #ff0000;">&quot;Small&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Imports<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Import</span> <span style="color: #000066;">moduleName</span>=<span style="color: #ff0000;">&quot;Diagnostics&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Imports<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/WorkerRole<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ServiceDefinition<span style="font-weight: bold; color: black;">&gt;</span></span></span></li></ol></div><div id="cb56131" style="display: none; color: red;"></div></div></div>

<p>The second configuration is applied when we deploy the instances above and tells Azure that I want to deploy 1 Processor_WebRole instance and 2 Processor_WorkerRole instances:</p>
<div class="codebox"><div class="codeheader"><span>xml</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb4981'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb4981','cb41586'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="xml" id="cb4981" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ServiceConfiguration</span> <span style="color: #000066;">serviceName</span>=<span style="color: #ff0000;">&quot;CloudFileProcessorService&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration&quot;</span> <span style="color: #000066;">osFamily</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">osVersion</span>=<span style="color: #ff0000;">&quot;*&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li2">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Role</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Processor_WebRole&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Instances</span> <span style="color: #000066;">count</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ConfigurationSettings<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Setting</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;UseDevelopmentStorage=true&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ConfigurationSettings<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Role<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Role</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Processor_WorkerRole&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Instances</span> <span style="color: #000066;">count</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ConfigurationSettings<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Setting</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;UseDevelopmentStorage=true&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ConfigurationSettings<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Role<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ServiceConfiguration<span style="font-weight: bold; color: black;">&gt;</span></span></span></li></ol></div><div id="cb41586" style="display: none; color: red;"></div></div></div>

<p>Note that I'm telling it to use the local development storage, which is supported by a local storage emulator. In a production configuration I would enter the service location and a generated token.</p>

<h2>So Where's the Magic?</h2>
<p>So where's the magic that makes this a distribute application instead of 3 days of overtime? It's sprinkled throughout the system.</p>

<p>The architecture of this system would work just as well outside of Azure, provided I offered it stand-ins for the 3 storage resources and deployed the instances and any necessary settings accordingly. Instead of worrying about how to manage deployments and what to use for centralized queueing and storage, I can focus on building an application that simply assumes those resources are available. Is there headroom for performance improvements? Sure, but I can also choose to throw another $15/month server at it, push data to CDNs and blob storage, add caching, or even a SQL Azure instance. </p>

<p>This application may be fairly basic, but nothing stops us from following this same pattern for much larger applications. PaaS has removed some of the constraints we take for granted. Even applications that have to run in-house in order to standardize against a database can now consider uploading a subset of that lookup data to a table store, performing most of the heavy lifting in the cloud, then produce a few files to import back into the on-premise system. The total execution time would be longer, but being able to scale part of the job across numerous parallel instances means the actual elapsed time can actually be much shorter.</p>

<p>And it means when we have a 60 hour task that's due in 24, it's no longer an impossible situation.</p>

<p><i>The source code is available <a href="https://github.com/tarwn/AzureFileProcessorSample" title="AzureFileProcessorSample on github">on github</a> along with requirements and links for setting up the emulators locally.</i></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/the-cloud-as-a-make">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/the-cloud-as-a-make#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2&#38;disp=comments&#38;p=1731</wfw:commentRss>
		</item>
				<item>
			<title>PetaPoco: Mapping related objects</title>
			<link>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/petapoco-mapping-related-objects</link>
			<pubDate>Mon, 07 May 2012 10:16:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">C#</category>			<guid isPermaLink="false">1725@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;In the &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/more-petapoco-id-s-and&quot; title=&quot;More PetaPoco: Id&#039;s and Multi-POCO queries&quot;&gt;prior PetaPoco post&lt;/a&gt;, I started to dig into many-to-one relationships a little. Chrissie followed up with yet more mapping behavior in his &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/simple-data-and-complex-types&quot; title=&quot;Simple.Data and complex types: many to one&quot;&gt;latest Simple.Data post&lt;/a&gt;, so I thought I would cover it in a bit more detail.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Note: Chrissie has also covered &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/simple-data-and-complex-types-1&quot; title=&quot;Simple.Data and complex types: one to many&quot;&gt;one-to-many&lt;/a&gt; since I wrote this post the other night and has at least one more post following that&lt;/i&gt;&lt;/p&gt;

&lt;h2&gt;Many flavors of Mapping Related Objects&lt;/h2&gt;
&lt;p&gt;PetaPoco doesn&#039;t offer the instrumentation for lazy loading, though it wouldn&#039;t be too hard to add it to the T4 template that is provided to automatically generate POCOs from the database. Of course the POCOs would stop being POCOs at this point and I&#039;d be showing off my ability to write lazy loading rather than the library at hand, so lets stick to what PetaPoco does out of the box.&lt;/p&gt;

&lt;p&gt;That said, we still have a number of ways to map data from multi-table queries into objects.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Note: I am using the same tables and insert statements I used in the prior post to create the Person and Address table, so I&#039;ve left those out of the examples below to reduce the noise a bit&lt;/i&gt;&lt;/p&gt;

&lt;h3&gt;Automatic Mapping w/ Decorated Objects&lt;/h3&gt;
&lt;p&gt;As we saw in the prior post, using a pair of decorated objects makes it pretty easy to map a JOIN to objects:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb59332&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; SelectUsingDecoratedClasses&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = GetDatabase&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;DecoratedPerson, DecoratedAddress&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;@&quot;SELECT Person.*, Address.* &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FROM Person &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; INNER JOIN Address ON Person.AddressId = Address.Id &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHERE Person.lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var person &lt;span style=&quot;color: #0600FF;&quot;&gt;in&lt;/span&gt; results&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Person: {0} {1}&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;LastName&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;FirstName&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Address: {0} {1}&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Street&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;HouseNumber&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;TableName&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Person&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;PrimaryKey&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;, autoIncrement = &lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; DecoratedPerson &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;int&lt;/span&gt; Id &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; LastName &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; FirstName &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;int&lt;/span&gt; AddressId &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;Ignore&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; DecoratedAddress Address &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; ToString&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;String&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Format&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;{0}: {1}, {2}&amp;quot;&lt;/span&gt;, Id, LastName, FirstName&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;TableName&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;PrimaryKey&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;, autoIncrement = &lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; DecoratedAddress &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;int&lt;/span&gt; Id &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; Street &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; HouseNumber &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb72448&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3&gt;Defining Mappings&lt;/h3&gt;
&lt;p&gt;While the previous example handled the mapping automatically and assigned the Address instance to the appropriate attribute in the Person, we also have the ability to define the mapping manually. &lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb50885&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; SelectUsingMappingAndPOCO&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = GetDatabase&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;Person, Address, Person&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;p, a&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; p.&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&lt;/span&gt; = a; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; p; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;@&quot;SELECT Person.*, Address.* &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FROM Person &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; INNER JOIN Address ON Person.AddressId = Address.Id &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHERE Person.lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var person &lt;span style=&quot;color: #0600FF;&quot;&gt;in&lt;/span&gt; results&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Person: {0} {1}&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;LastName&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;FirstName&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Address: {0} {1}&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Street&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;HouseNumber&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb3808&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While this example achieves the same outcome as the prior one, the ability to provide our own mapping gives us some flexibility to add more complex logic during the mapping process, such as calculating additional field values or adding change tracking.&lt;/p&gt;

&lt;h3&gt;Dynamics&lt;/h3&gt;
&lt;p&gt;Of course PetaPoco also handles dynamics, however this is limited to outputting a single object to represent the results. This works well if we wanted to present a report view of the data and didn&#039;t have any column names that repeat:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb60935&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; SelectWithDynamics&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = GetDatabase&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;dynamic&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;@&quot;SELECT Person.*, Address.Street, Address.HouseNumber &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FROM Person &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; INNER JOIN Address ON Person.AddressId = Address.Id &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHERE Person.lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var person &lt;span style=&quot;color: #0600FF;&quot;&gt;in&lt;/span&gt; results&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Person: {0} {1}&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;LastName&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;FirstName&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Address: {0} {1}&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;Street&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;HouseNumber&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb27432&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Instead of a dynamic, we could just as easily create a POCO for this report view, which would then be easy to offer as a service DTO or serializable object. &lt;/p&gt;

&lt;h3&gt;One-to-Many&lt;/h3&gt;
&lt;p&gt;Switching directions for a moment, let&#039;s instead query for an address and all of it&#039;s associated persons. First we&#039;ll need an updated POCO:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb23052&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; AddressWithPeople : Address &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; List&amp;lt;Person&amp;gt; Persons &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb60236&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then with a slightly more complex mapping method, we can map a one-to-many to our new AddressWithPeople and existing Person POCOs:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb69969&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; SelectOneToMany&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = GetDatabase&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;AddressWithPeople, Person, AddressWithPeople&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; AddressToPersonRelator&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;MapIt&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;@&quot;SELECT Address.*, Person.*&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FROM Person &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; INNER JOIN Address ON Person.AddressId = Address.Id &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHERE Address.Id=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var address &lt;span style=&quot;color: #0600FF;&quot;&gt;in&lt;/span&gt; results&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Address: {0} {1}&amp;quot;&lt;/span&gt;, address.&lt;span style=&quot;color: #0000FF;&quot;&gt;Street&lt;/span&gt;, address.&lt;span style=&quot;color: #0000FF;&quot;&gt;HouseNumber&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;foreach&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var person &lt;span style=&quot;color: #0600FF;&quot;&gt;in&lt;/span&gt; address.&lt;span style=&quot;color: #0000FF;&quot;&gt;Persons&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\t&lt;/span&gt;Person: {0} {1}&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;LastName&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;FirstName&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb64528&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course, the magic in this case is the tricky part. In order to map the objects from the right side of the result set to the columns from my address on the left, I had to write a custom mapper that would keep track of the Address and add Person records to it while it remained the same. &lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb11458&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; AddressToPersonRelator &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; AddressWithPeople current;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; AddressWithPeople MapIt&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;AddressWithPeople a, Person p&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;a == &lt;span style=&quot;color: #0600FF;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; current;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;a != &lt;span style=&quot;color: #0600FF;&quot;&gt;null&lt;/span&gt; &amp;amp;&amp;amp; current != &lt;span style=&quot;color: #0600FF;&quot;&gt;null&lt;/span&gt; &amp;amp;&amp;amp; a.&lt;span style=&quot;color: #0000FF;&quot;&gt;Id&lt;/span&gt; == current.&lt;span style=&quot;color: #0000FF;&quot;&gt;Id&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; current.&lt;span style=&quot;color: #0000FF;&quot;&gt;Persons&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;p&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;null&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var prev = current;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; current = a;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; current.&lt;span style=&quot;color: #0000FF;&quot;&gt;Persons&lt;/span&gt; = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; List&amp;lt;Person&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; p &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; prev;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb990&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Even though I mostly copied this code from the official blogs, it was still more work than I would have liked just to map the records. However it wouldn&#039;t be hard to convert this method to use generics and accept arguments for ID comparison and adding child record to the parent&#039;s collection. Another option would be to use the &lt;a href=&quot;http://nuget.org/packages/PetaPoco.RelationExtensions&quot; title=&quot;PetaPoco.RelationExtensions package&quot;&gt;PetaPoco.RelationExtensions nuget&lt;/a&gt; package, which offers simplified methods for one-to-many and many-to-one mappings.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Although Chrissie and I have been posting in parallel, I think we&#039;ve reached the point where the feature sets diverge. Simple.Data obviously offers a simpler looking syntax for more complex JOINs (especially if we start looking at one-to-many) and offers fluent, LINQ-based syntax. PetaPoco offers a very clean, very simple way to continue to use SQL to get our data and map it into POCOs or dynamics, with the flexibility to do it for us automatically, with help from decorators, or via specified mapping functions (not to mention the blazing performance). Hopefully seeing us play back and forth a bit will have inspired you to play with one or both of these in the future, and don&#039;t be surprised if you see them reused in some of my future projects as well.&lt;/p&gt;

&lt;p&gt;The examples above and several related ones are available in the &lt;a href=&quot;https://github.com/tarwn/PetaPocoSample&quot; title=&quot;Source on Github&quot;&gt;source on GitHub&lt;/a&gt; as well as examples of the methods for updates and the upserting Save() method.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/petapoco-mapping-related-objects&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>In the <a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/more-petapoco-id-s-and" title="More PetaPoco: Id's and Multi-POCO queries">prior PetaPoco post</a>, I started to dig into many-to-one relationships a little. Chrissie followed up with yet more mapping behavior in his <a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/simple-data-and-complex-types" title="Simple.Data and complex types: many to one">latest Simple.Data post</a>, so I thought I would cover it in a bit more detail.</p>

<p><i>Note: Chrissie has also covered <a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/simple-data-and-complex-types-1" title="Simple.Data and complex types: one to many">one-to-many</a> since I wrote this post the other night and has at least one more post following that</i></p>

<h2>Many flavors of Mapping Related Objects</h2>
<p>PetaPoco doesn't offer the instrumentation for lazy loading, though it wouldn't be too hard to add it to the T4 template that is provided to automatically generate POCOs from the database. Of course the POCOs would stop being POCOs at this point and I'd be showing off my ability to write lazy loading rather than the library at hand, so lets stick to what PetaPoco does out of the box.</p>

<p>That said, we still have a number of ways to map data from multi-table queries into objects.</p>

<p><i>Note: I am using the same tables and insert statements I used in the prior post to create the Person and Address table, so I've left those out of the examples below to reduce the noise a bit</i></p>

<h3>Automatic Mapping w/ Decorated Objects</h3>
<p>As we saw in the prior post, using a pair of decorated objects makes it pretty easy to map a JOIN to objects:</p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb96823'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb96823','cb17234'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb96823" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SelectUsingDecoratedClasses<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = GetDatabase<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var results = db.<span style="color: #0000FF;">Query</span>&lt;DecoratedPerson, DecoratedAddress&gt;<span style="color: #000000;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">@"SELECT Person.*, Address.* </span></li><li style="" class="li1"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM Person </span></li><li style="" class="li2"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN Address ON Person.AddressId = Address.Id </span></li><li style="" class="li1"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE Person.lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var person <span style="color: #0600FF;">in</span> results<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Person: {0} {1}&quot;</span>, person.<span style="color: #0000FF;">LastName</span>, person.<span style="color: #0000FF;">FirstName</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Address: {0} {1}&quot;</span>, person.<span style="color: #0000FF;">Address</span>.<span style="color: #0000FF;">Street</span>, person.<span style="color: #0000FF;">Address</span>.<span style="color: #0000FF;">HouseNumber</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000;">&#91;</span>TableName<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Person&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li1"><span style="color: #000000;">&#91;</span>PrimaryKey<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Id&quot;</span>, autoIncrement = <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li2"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DecoratedPerson <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> LastName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FirstName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> AddressId <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#91;</span>Ignore<span style="color: #000000;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> DecoratedAddress Address <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;{0}: {1}, {2}&quot;</span>, Id, LastName, FirstName<span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000;">&#91;</span>TableName<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li1"><span style="color: #000000;">&#91;</span>PrimaryKey<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Id&quot;</span>, autoIncrement = <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li2"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DecoratedAddress <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Street <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> HouseNumber <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb17234" style="display: none; color: red;"></div></div></div>

<h3>Defining Mappings</h3>
<p>While the previous example handled the mapping automatically and assigned the Address instance to the appropriate attribute in the Person, we also have the ability to define the mapping manually. </p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb35049'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb35049','cb19426'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb35049" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SelectUsingMappingAndPOCO<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = GetDatabase<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var results = db.<span style="color: #0000FF;">Query</span>&lt;Person, Address, Person&gt;<span style="color: #000000;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#40;</span>p, a<span style="color: #000000;">&#41;</span> =&gt; <span style="color: #000000;">&#123;</span> p.<span style="color: #0000FF;">Address</span> = a; <span style="color: #0600FF;">return</span> p; <span style="color: #000000;">&#125;</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">@"SELECT Person.*, Address.* </span></li><li style="" class="li2"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM Person </span></li><li style="" class="li1"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN Address ON Person.AddressId = Address.Id </span></li><li style="" class="li2"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE Person.lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var person <span style="color: #0600FF;">in</span> results<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Person: {0} {1}&quot;</span>, person.<span style="color: #0000FF;">LastName</span>, person.<span style="color: #0000FF;">FirstName</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Address: {0} {1}&quot;</span>, person.<span style="color: #0000FF;">Address</span>.<span style="color: #0000FF;">Street</span>, person.<span style="color: #0000FF;">Address</span>.<span style="color: #0000FF;">HouseNumber</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb19426" style="display: none; color: red;"></div></div></div>

<p>While this example achieves the same outcome as the prior one, the ability to provide our own mapping gives us some flexibility to add more complex logic during the mapping process, such as calculating additional field values or adding change tracking.</p>

<h3>Dynamics</h3>
<p>Of course PetaPoco also handles dynamics, however this is limited to outputting a single object to represent the results. This works well if we wanted to present a report view of the data and didn't have any column names that repeat:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb58767'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb58767','cb81856'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb58767" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SelectWithDynamics<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = GetDatabase<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var results = db.<span style="color: #0000FF;">Query</span>&lt;dynamic&gt;<span style="color: #000000;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">@"SELECT Person.*, Address.Street, Address.HouseNumber </span></li><li style="" class="li1"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM Person </span></li><li style="" class="li2"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN Address ON Person.AddressId = Address.Id </span></li><li style="" class="li1"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE Person.lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var person <span style="color: #0600FF;">in</span> results<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Person: {0} {1}&quot;</span>, person.<span style="color: #0000FF;">LastName</span>, person.<span style="color: #0000FF;">FirstName</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Address: {0} {1}&quot;</span>, person.<span style="color: #0000FF;">Street</span>, person.<span style="color: #0000FF;">HouseNumber</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb81856" style="display: none; color: red;"></div></div></div>

<p>Instead of a dynamic, we could just as easily create a POCO for this report view, which would then be easy to offer as a service DTO or serializable object. </p>

<h3>One-to-Many</h3>
<p>Switching directions for a moment, let's instead query for an address and all of it's associated persons. First we'll need an updated POCO:</p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb25264'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb25264','cb33708'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb25264" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> AddressWithPeople : Address <span style="color: #000000;">&#123;</span> </li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> List&lt;Person&gt; Persons <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb33708" style="display: none; color: red;"></div></div></div>

<p>Then with a slightly more complex mapping method, we can map a one-to-many to our new AddressWithPeople and existing Person POCOs:</p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb37987'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb37987','cb30246'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb37987" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SelectOneToMany<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = GetDatabase<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var results = db.<span style="color: #0000FF;">Query</span>&lt;AddressWithPeople, Person, AddressWithPeople&gt;<span style="color: #000000;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> AddressToPersonRelator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">MapIt</span>,</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">@"SELECT Address.*, Person.*</span></li><li style="" class="li2"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM Person </span></li><li style="" class="li1"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN Address ON Person.AddressId = Address.Id </span></li><li style="" class="li2"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE Address.Id=@0&quot;</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var address <span style="color: #0600FF;">in</span> results<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Address: {0} {1}&quot;</span>, address.<span style="color: #0000FF;">Street</span>, address.<span style="color: #0000FF;">HouseNumber</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var person <span style="color: #0600FF;">in</span> address.<span style="color: #0000FF;">Persons</span><span style="color: #000000;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;<span style="color: #008080; font-weight: bold;">\t</span>Person: {0} {1}&quot;</span>, person.<span style="color: #0000FF;">LastName</span>, person.<span style="color: #0000FF;">FirstName</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb30246" style="display: none; color: red;"></div></div></div>

<p>Of course, the magic in this case is the tricky part. In order to map the objects from the right side of the result set to the columns from my address on the left, I had to write a custom mapper that would keep track of the Address and add Person records to it while it remained the same. </p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb75294'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb75294','cb73685'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb75294" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> AddressToPersonRelator <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> AddressWithPeople current;</li><li style="" class="li1">&nbsp; &nbsp; </li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> AddressWithPeople MapIt<span style="color: #000000;">&#40;</span>AddressWithPeople a, Person p<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>a == <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> current;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>a != <span style="color: #0600FF;">null</span> &amp;&amp; current != <span style="color: #0600FF;">null</span> &amp;&amp; a.<span style="color: #0000FF;">Id</span> == current.<span style="color: #0000FF;">Id</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current.<span style="color: #0000FF;">Persons</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>p<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">null</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var prev = current;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; current = a;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; current.<span style="color: #0000FF;">Persons</span> = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List&lt;Person&gt;<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> p <span style="color: #000000;">&#125;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> prev;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb73685" style="display: none; color: red;"></div></div></div>

<p>Even though I mostly copied this code from the official blogs, it was still more work than I would have liked just to map the records. However it wouldn't be hard to convert this method to use generics and accept arguments for ID comparison and adding child record to the parent's collection. Another option would be to use the <a href="http://nuget.org/packages/PetaPoco.RelationExtensions" title="PetaPoco.RelationExtensions package">PetaPoco.RelationExtensions nuget</a> package, which offers simplified methods for one-to-many and many-to-one mappings.</p>

<h2>Conclusion</h2>
<p>Although Chrissie and I have been posting in parallel, I think we've reached the point where the feature sets diverge. Simple.Data obviously offers a simpler looking syntax for more complex JOINs (especially if we start looking at one-to-many) and offers fluent, LINQ-based syntax. PetaPoco offers a very clean, very simple way to continue to use SQL to get our data and map it into POCOs or dynamics, with the flexibility to do it for us automatically, with help from decorators, or via specified mapping functions (not to mention the blazing performance). Hopefully seeing us play back and forth a bit will have inspired you to play with one or both of these in the future, and don't be surprised if you see them reused in some of my future projects as well.</p>

<p>The examples above and several related ones are available in the <a href="https://github.com/tarwn/PetaPocoSample" title="Source on Github">source on GitHub</a> as well as examples of the methods for updates and the upserting Save() method.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/petapoco-mapping-related-objects">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/petapoco-mapping-related-objects#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2&#38;disp=comments&#38;p=1725</wfw:commentRss>
		</item>
				<item>
			<title>More PetaPoco: Id's and Multi-POCO queries</title>
			<link>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/more-petapoco-id-s-and</link>
			<pubDate>Wed, 02 May 2012 08:23:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">C#</category>			<guid isPermaLink="false">1717@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;So yesterday Chrissie and I did posts on &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/simple-data-and-vb-net&quot; title=&quot;Simple.Data and VB.Net the beginning&quot;&gt;Simple.Data&lt;/a&gt; and &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/playing-with-petapoco&quot; title=&quot;Playing with PetaPoco&quot;&gt;PetaPoco&lt;/a&gt;. Today he &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/more-simple-data-with-vb&quot; title=&quot;More Simple.Data with VB.Net: adding fields and tables&quot;&gt;followed up&lt;/a&gt; with more complex examples, including keys and multiple table queries.&lt;/p&gt;

&lt;p&gt;PetaPoco is built specifically with primary keys as a first class citizen, so it will be interesting to see how it compares.&lt;/p&gt;

&lt;h2&gt;Adding a column&lt;/h2&gt;
&lt;p&gt;Like Simple.Data, adding a column to our database table is no problem at all. First lets add the column to our database, then we&#039;ll look at how that affects both our existing code and an updated POCO with a matching field.&lt;/p&gt;

&lt;p&gt;Like before, I&#039;ll use the more concise syntax of PetaPoco to add the column instead of the standard ADO logic:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb22361&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; CreateTable&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Database&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;DataSource=&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;test.sdf&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;; Password=&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;chrissiespassword&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;System.Data.SqlServerCe.4.0&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Execute&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;CREATE TABLE Person (Id int IDENTITY(1,1) PRIMARY KEY, LastName nvarchar (40) NOT NULL, FirstName nvarchar (40));&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb37526&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After adding this additional column, we can still use the original POCO as PetaPoco will map the columns that are present in the POCO without complaining about leftovers. In the case where we allowed PetaPoco to build the query for us, we get more concise SQL that only queries for the columns  with matching properties in that smaller POCO class:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb48048&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// SQL = SELECT [Person].[LastName], [Person].[FirstName] FROM [Person] WHERE lastname=@0&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; SelectDecoratedRecords&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = GetDatabase&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;DecoratedPerson&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb67550&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now let&#039;s add the new column to both our raw POCO and the decorated POCO:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb99518&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; Person &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;int&lt;/span&gt; Id &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; LastName &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; FirstName &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; ToString&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;String&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Format&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;{0}: {1}, {2}&amp;quot;&lt;/span&gt;, Id, LastName, FirstName&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;TableName&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Person&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;PrimaryKey&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;,autoIncrement=&lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; DecoratedPerson : Person &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb47629&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Without changing of the logic, the function from above will populate the extra column in our non-decorated POCO and the SQL generated by the short query version above will now include the Id column:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb39162&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;//SQL: SELECT * FROM Person WHERE lastname=@0&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; SelectRecords&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = GetDatabase&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;Person&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SELECT * FROM Person WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;//SQL: SELECT [Person].[Id], [Person].[LastName], [Person].[FirstName] FROM [Person] WHERE lastname=@0&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; SelectDecoratedRecords&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = GetDatabase&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;DecoratedPerson&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb65159&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Yesterdays third insert option, using an undecorated object and counting on reflection to match up columns to properties, will fail now because it will attempt to insert a value into that ID field, but using it in a select would still work. The first option, where we supplied the name of the table, will have to be updated to also supply the name of the ID and a boolean to indicate that it is autoincrementing. The second option, inserting the decorated object, requires no changes at all. &lt;/p&gt;

&lt;h2&gt;Adding a Table&lt;/h2&gt;
&lt;p&gt;Following Chrissie&#039;s lead, lets add an address table and an undeclared foreign key relationship from the Person table (I have to tease him about something).&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb6541&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; CreateTables&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Database&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;DataSource=&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;test.sdf&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;; Password=&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;chrissiespassword&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;System.Data.SqlServerCe.4.0&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Execute&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;CREATE TABLE Person (Id int IDENTITY(1,1) PRIMARY KEY, LastName nvarchar (40) NOT NULL, FirstName nvarchar (40), AddressId int NOT NULL);&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Execute&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;CREATE TABLE Address (Id int IDENTITY(1,1) PRIMARY KEY, Street nvarchar (40) NOT NULL, HouseNumber nvarchar (10));&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb83222&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And I will add an additional Person POCO to reflect the new column, as well as an Address POCO to reflect the new table. The simpler query logic is addictive, so I&#039;ve decorated both POCOs (I&#039;ll explain the ResultColumn later):&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb81685&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;TableName&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Person&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;PrimaryKey&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;, autoIncrement = &lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; Person &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;int&lt;/span&gt; Id &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; LastName &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; FirstName &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;int&lt;/span&gt; AddressId &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;ResultColumn&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; Address Address &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;override&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; ToString&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;String&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Format&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;{0}: {1}, {2}&amp;quot;&lt;/span&gt;, Id, LastName, FirstName&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;TableName&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Address&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;PrimaryKey&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;, autoIncrement = &lt;span style=&quot;color: #0600FF;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; Address &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;int&lt;/span&gt; Id &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; Street &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; HouseNumber &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb69262&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Following Chrissie&#039;s lead, I&#039;ll query for the related records separately first:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb88218&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; QuerySeperately&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// already called CreateDatabase()&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// already called CreateTables()&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = GetDatabase&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Address&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; Street = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;street1&amp;quot;&lt;/span&gt;, HouseNumber = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Person&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; LastName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;, FirstName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;firstname1&amp;quot;&lt;/span&gt;, AddressId = &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Person&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; LastName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname2&amp;quot;&lt;/span&gt;, FirstName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;firstname2&amp;quot;&lt;/span&gt;, AddressId = &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;Person&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WHERE LastName=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var person &lt;span style=&quot;color: #0600FF;&quot;&gt;in&lt;/span&gt; results&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Person: {0} {1} {2}&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;Id&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;LastName&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;FirstName&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var address = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Single&lt;/span&gt;&amp;lt;Address&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Where Id=@0&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;AddressId&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Address: {0} {1}&amp;quot;&lt;/span&gt;, address.&lt;span style=&quot;color: #0000FF;&quot;&gt;Street&lt;/span&gt;, address.&lt;span style=&quot;color: #0000FF;&quot;&gt;HouseNumber&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;int&lt;/span&gt; count = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;ExecuteScalar&lt;/span&gt;&amp;lt;int&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SELECT COUNT(*) FROM Person WHERE LastName=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Count: &amp;quot;&lt;/span&gt; + count.&lt;span style=&quot;color: #0000FF;&quot;&gt;ToString&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb25644&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As he pointed out, this method doesn&#039;t perform well. We can replace this with a single query using the &lt;a href=&quot;http://www.toptensoftware.com/Articles/111/PetaPoco-Experimental-Multi-Poco-Queries&quot; title=&quot;Read more about this at TopTen Software&quot;&gt;Multi-POCO&lt;/a&gt; support.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb55036&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; QueryMultiStyle&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// already called CreateDatabase()&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// already called CreateTables()&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = GetDatabase&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Address&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; Street = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;street1&amp;quot;&lt;/span&gt;, HouseNumber = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Person&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; LastName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;, FirstName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;firstname1&amp;quot;&lt;/span&gt;, AddressId = &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Person&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; LastName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;, FirstName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;firstname2&amp;quot;&lt;/span&gt;, AddressId = &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;Person, Address&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;@&quot;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SELECT Person.*, Address.* &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FROM Person &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; INNER JOIN Address ON Person.AddressId = Address.Id &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHERE Person.lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;foreach&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var person &lt;span style=&quot;color: #0600FF;&quot;&gt;in&lt;/span&gt; results&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Person: {0} {1}&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;LastName&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;FirstName&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Address: {0} {1}&amp;quot;&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Street&lt;/span&gt;, person.&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;HouseNumber&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb53450&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;PetaPoco has the ability to map the results of JOINs to several objects, but it&#039;s kind of tricky. The simplest method is to return the fields in the same order as the generic object list. What PetaPoco then does is attempt to process each column in the result from left to right, moving to the next object in line when it reaches a column that doesn&#039;t exist in the first or has already been populated. So in this case, because both of the tables and POCOs have an &quot;Id&quot;, when the result set reaches the second id it makes the logical conclusion that it is time to start mapping the Address object. PetaPoco uses type detection in the Person object to locate a property to assign the Address instance to.&lt;/p&gt;

&lt;p&gt;There is also more extensive capabilities available to use lambdas to manage the multi-POCO mapping on our own, and if we wanted to we could easily define a single POCO object that had all the necessary fields for both tables. Logic for &lt;a href=&quot;http://www.toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships&quot; title=&quot;PetaPoco - Mapping One-to-Many and Many-to-One Relationships&quot;&gt;One-to-many joins&lt;/a&gt; is more complex and I haven&#039;t had time to dig fully into the intricacies yet.&lt;/p&gt;

&lt;p&gt;The last trick was the ResultColumn attribute I used above. By default PetaPoco assumes that all of the properties in our POCO are going to be inserted into the database. ResultColumn properties are ignored for inserts and updates, but can still be selected into. In this case I&#039;m using it to have PetaPoco ignore the column, but the real purpose would be to allow me to return an additional calculated column, aggregate, or other value that wouldn&#039;t have meaning in an INSERT or UPDATE. &lt;/p&gt;

&lt;p&gt;&lt;i&gt;Note: There is an Ignore attribute that would have worked just as well and been a better fit, but then I wouldn&#039;t have had a chance to talk about the ResultColumn &lt;img src=&quot;http://blogs.lessthandot.com/rsc/smilies/icon_smile.gif&quot; title=&quot;:)&quot; alt=&quot;:)&quot; class=&quot;middle&quot; width=&quot;15&quot; height=&quot;15&quot; /&gt;&lt;/i&gt;&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;So there we go. We can add auto-incrementing IDs very easily, PetaPoco is smart enough to map partial objects, and there is some really neat stuff available for multi-POCO joins. I&#039;ve continued to update the &lt;a href=&quot;https://github.com/tarwn/PetaPocoSample&quot; title=&quot;Sample code on github&quot;&gt;github repository&lt;/a&gt;, so feel free to grab a copy of the code and play around with yourself.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/more-petapoco-id-s-and&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>So yesterday Chrissie and I did posts on <a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/simple-data-and-vb-net" title="Simple.Data and VB.Net the beginning">Simple.Data</a> and <a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/playing-with-petapoco" title="Playing with PetaPoco">PetaPoco</a>. Today he <a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/more-simple-data-with-vb" title="More Simple.Data with VB.Net: adding fields and tables">followed up</a> with more complex examples, including keys and multiple table queries.</p>

<p>PetaPoco is built specifically with primary keys as a first class citizen, so it will be interesting to see how it compares.</p>

<h2>Adding a column</h2>
<p>Like Simple.Data, adding a column to our database table is no problem at all. First lets add the column to our database, then we'll look at how that affects both our existing code and an updated POCO with a matching field.</p>

<p>Like before, I'll use the more concise syntax of PetaPoco to add the column instead of the standard ADO logic:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb51921'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb51921','cb88483'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb51921" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> CreateTable<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Database<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;DataSource=<span style="color: #008080; font-weight: bold;">\&quot;</span>test.sdf<span style="color: #008080; font-weight: bold;">\&quot;</span>; Password=<span style="color: #008080; font-weight: bold;">\&quot;</span>chrissiespassword<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, <span style="color: #808080;">&quot;System.Data.SqlServerCe.4.0&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #0000FF;">Execute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;CREATE TABLE Person (Id int IDENTITY(1,1) PRIMARY KEY, LastName nvarchar (40) NOT NULL, FirstName nvarchar (40));&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb88483" style="display: none; color: red;"></div></div></div>

<p>After adding this additional column, we can still use the original POCO as PetaPoco will map the columns that are present in the POCO without complaining about leftovers. In the case where we allowed PetaPoco to build the query for us, we get more concise SQL that only queries for the columns  with matching properties in that smaller POCO class:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb70080'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb70080','cb1526'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb70080" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #008080; font-style: italic;">// SQL = SELECT [Person].[LastName], [Person].[FirstName] FROM [Person] WHERE lastname=@0</span></li><li style="" class="li2"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> SelectDecoratedRecords<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = GetDatabase<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; var results = db.<span style="color: #0000FF;">Query</span>&lt;DecoratedPerson&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb1526" style="display: none; color: red;"></div></div></div>

<p>Now let's add the new column to both our raw POCO and the decorated POCO:</p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb40717'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb40717','cb70505'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb40717" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Person <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> LastName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FirstName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;{0}: {1}, {2}&quot;</span>, Id, LastName, FirstName<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#91;</span>TableName<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Person&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#91;</span>PrimaryKey<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Id&quot;</span>,autoIncrement=<span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DecoratedPerson : Person <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span></li></ol></div><div id="cb70505" style="display: none; color: red;"></div></div></div>

<p>Without changing of the logic, the function from above will populate the extra column in our non-decorated POCO and the SQL generated by the short query version above will now include the Id column:</p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb55990'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb55990','cb28838'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb55990" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #008080; font-style: italic;">//SQL: SELECT * FROM Person WHERE lastname=@0</span></li><li style="" class="li2"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> SelectRecords<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = GetDatabase<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; var results = db.<span style="color: #0000FF;">Query</span>&lt;Person&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SELECT * FROM Person WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #008080; font-style: italic;">//SQL: SELECT [Person].[Id], [Person].[LastName], [Person].[FirstName] FROM [Person] WHERE lastname=@0</span></li><li style="" class="li1"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> SelectDecoratedRecords<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = GetDatabase<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var results = db.<span style="color: #0000FF;">Query</span>&lt;DecoratedPerson&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb28838" style="display: none; color: red;"></div></div></div>

<p>Yesterdays third insert option, using an undecorated object and counting on reflection to match up columns to properties, will fail now because it will attempt to insert a value into that ID field, but using it in a select would still work. The first option, where we supplied the name of the table, will have to be updated to also supply the name of the ID and a boolean to indicate that it is autoincrementing. The second option, inserting the decorated object, requires no changes at all. </p>

<h2>Adding a Table</h2>
<p>Following Chrissie's lead, lets add an address table and an undeclared foreign key relationship from the Person table (I have to tease him about something).</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb67753'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb67753','cb27689'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb67753" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> CreateTables<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Database<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;DataSource=<span style="color: #008080; font-weight: bold;">\&quot;</span>test.sdf<span style="color: #008080; font-weight: bold;">\&quot;</span>; Password=<span style="color: #008080; font-weight: bold;">\&quot;</span>chrissiespassword<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, <span style="color: #808080;">&quot;System.Data.SqlServerCe.4.0&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #0000FF;">Execute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;CREATE TABLE Person (Id int IDENTITY(1,1) PRIMARY KEY, LastName nvarchar (40) NOT NULL, FirstName nvarchar (40), AddressId int NOT NULL);&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #0000FF;">Execute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;CREATE TABLE Address (Id int IDENTITY(1,1) PRIMARY KEY, Street nvarchar (40) NOT NULL, HouseNumber nvarchar (10));&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb27689" style="display: none; color: red;"></div></div></div>

<p>And I will add an additional Person POCO to reflect the new column, as well as an Address POCO to reflect the new table. The simpler query logic is addictive, so I've decorated both POCOs (I'll explain the ResultColumn later):</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb52009'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb52009','cb84429'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb52009" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #000000;">&#91;</span>TableName<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Person&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li2"><span style="color: #000000;">&#91;</span>PrimaryKey<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Id&quot;</span>, autoIncrement = <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Person <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> LastName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FirstName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> AddressId <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#91;</span>ResultColumn<span style="color: #000000;">&#93;</span> <span style="color: #0600FF;">public</span> Address Address <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;{0}: {1}, {2}&quot;</span>, Id, LastName, FirstName<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #000000;">&#91;</span>TableName<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li2"><span style="color: #000000;">&#91;</span>PrimaryKey<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Id&quot;</span>, autoIncrement = <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Address <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Id <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Street <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> HouseNumber <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb84429" style="display: none; color: red;"></div></div></div>

<p>Following Chrissie's lead, I'll query for the related records separately first:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb58154'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb58154','cb57036'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb58154" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> QuerySeperately<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// already called CreateDatabase()</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// already called CreateTables()</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = GetDatabase<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Address<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> Street = <span style="color: #808080;">&quot;street1&quot;</span>, HouseNumber = <span style="color: #808080;">&quot;1&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> LastName = <span style="color: #808080;">&quot;lastname1&quot;</span>, FirstName = <span style="color: #808080;">&quot;firstname1&quot;</span>, AddressId = <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> LastName = <span style="color: #808080;">&quot;lastname2&quot;</span>, FirstName = <span style="color: #808080;">&quot;firstname2&quot;</span>, AddressId = <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var results = db.<span style="color: #0000FF;">Query</span>&lt;Person&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;WHERE LastName=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var person <span style="color: #0600FF;">in</span> results<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Person: {0} {1} {2}&quot;</span>, person.<span style="color: #0000FF;">Id</span>, person.<span style="color: #0000FF;">LastName</span>, person.<span style="color: #0000FF;">FirstName</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var address = db.<span style="color: #0000FF;">Single</span>&lt;Address&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Where Id=@0&quot;</span>, person.<span style="color: #0000FF;">AddressId</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Address: {0} {1}&quot;</span>, address.<span style="color: #0000FF;">Street</span>, address.<span style="color: #0000FF;">HouseNumber</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">int</span> count = db.<span style="color: #0000FF;">ExecuteScalar</span>&lt;int&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SELECT COUNT(*) FROM Person WHERE LastName=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Count: &quot;</span> + count.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb57036" style="display: none; color: red;"></div></div></div>

<p>As he pointed out, this method doesn't perform well. We can replace this with a single query using the <a href="http://www.toptensoftware.com/Articles/111/PetaPoco-Experimental-Multi-Poco-Queries" title="Read more about this at TopTen Software">Multi-POCO</a> support.</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb26369'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb26369','cb22328'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb26369" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> QueryMultiStyle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> </li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// already called CreateDatabase()</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// already called CreateTables()</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = GetDatabase<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Address<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> Street = <span style="color: #808080;">&quot;street1&quot;</span>, HouseNumber = <span style="color: #808080;">&quot;1&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> LastName = <span style="color: #808080;">&quot;lastname1&quot;</span>, FirstName = <span style="color: #808080;">&quot;firstname1&quot;</span>, AddressId = <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> LastName = <span style="color: #808080;">&quot;lastname1&quot;</span>, FirstName = <span style="color: #808080;">&quot;firstname2&quot;</span>, AddressId = <span style="color: #FF0000;">1</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; var results = db.<span style="color: #0000FF;">Query</span>&lt;Person, Address&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">@"</span></li><li style="" class="li2"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SELECT Person.*, Address.* </span></li><li style="" class="li1"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM Person </span></li><li style="" class="li2"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INNER JOIN Address ON Person.AddressId = Address.Id </span></li><li style="" class="li1"><span style="color: #808080;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHERE Person.lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var person <span style="color: #0600FF;">in</span> results<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Person: {0} {1}&quot;</span>, person.<span style="color: #0000FF;">LastName</span>, person.<span style="color: #0000FF;">FirstName</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Address: {0} {1}&quot;</span>, person.<span style="color: #0000FF;">Address</span>.<span style="color: #0000FF;">Street</span>, person.<span style="color: #0000FF;">Address</span>.<span style="color: #0000FF;">HouseNumber</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb22328" style="display: none; color: red;"></div></div></div>

<p>PetaPoco has the ability to map the results of JOINs to several objects, but it's kind of tricky. The simplest method is to return the fields in the same order as the generic object list. What PetaPoco then does is attempt to process each column in the result from left to right, moving to the next object in line when it reaches a column that doesn't exist in the first or has already been populated. So in this case, because both of the tables and POCOs have an "Id", when the result set reaches the second id it makes the logical conclusion that it is time to start mapping the Address object. PetaPoco uses type detection in the Person object to locate a property to assign the Address instance to.</p>

<p>There is also more extensive capabilities available to use lambdas to manage the multi-POCO mapping on our own, and if we wanted to we could easily define a single POCO object that had all the necessary fields for both tables. Logic for <a href="http://www.toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships" title="PetaPoco - Mapping One-to-Many and Many-to-One Relationships">One-to-many joins</a> is more complex and I haven't had time to dig fully into the intricacies yet.</p>

<p>The last trick was the ResultColumn attribute I used above. By default PetaPoco assumes that all of the properties in our POCO are going to be inserted into the database. ResultColumn properties are ignored for inserts and updates, but can still be selected into. In this case I'm using it to have PetaPoco ignore the column, but the real purpose would be to allow me to return an additional calculated column, aggregate, or other value that wouldn't have meaning in an INSERT or UPDATE. </p>

<p><i>Note: There is an Ignore attribute that would have worked just as well and been a better fit, but then I wouldn't have had a chance to talk about the ResultColumn <img src="http://blogs.lessthandot.com/rsc/smilies/icon_smile.gif" title=":)" alt=":)" class="middle" width="15" height="15" /></i></p>

<h2>Conclusion</h2>
<p>So there we go. We can add auto-incrementing IDs very easily, PetaPoco is smart enough to map partial objects, and there is some really neat stuff available for multi-POCO joins. I've continued to update the <a href="https://github.com/tarwn/PetaPocoSample" title="Sample code on github">github repository</a>, so feel free to grab a copy of the code and play around with yourself.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/more-petapoco-id-s-and">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/more-petapoco-id-s-and#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2&#38;disp=comments&#38;p=1717</wfw:commentRss>
		</item>
				<item>
			<title>Playing with PetaPoco</title>
			<link>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/playing-with-petapoco</link>
			<pubDate>Sun, 29 Apr 2012 12:40:00 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">C#</category>			<guid isPermaLink="false">1715@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Since Chrissie is playing around with &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/simple-data-and-vb-net&quot; title=&quot;Read Chrissie&#039;s post&quot;&gt;Simple.Data&lt;/a&gt; today, I found some time to play with &lt;a href=&quot;http://www.toptensoftware.com/petapoco/&quot; title=&quot;Main Site for PetaPoco&quot;&gt;PetaPoco&lt;/a&gt;. PetaPoco is a single file micro ORM that uses MSIL generation to do it&#039;s magic. As the name suggests, it works with concrete POCOs, though support for dynamics is also being tested. It is designed to be fast and doesn&#039;t try to reimplement SQL, so double win in my book. PetaPoco was created by Brad Robinson (&lt;a href=&quot;http://www.toptensoftware.com/blog/&quot; title=&quot;topten software blog&quot;&gt;b&lt;/a&gt; | &lt;a href=&quot;http://twitter.com/toptensoftware&quot; title=&quot;TopTenSoftware on twitter&quot;&gt;t&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;It has been on my list to try for a while, so today seemed like a good day for it.&lt;/p&gt;

&lt;h2&gt;Setting it up&lt;/h2&gt;
&lt;p&gt;Setup is simple using Nuget:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Install-Package &lt;a href=&quot;http://nuget.org/List/Packages/PetaPoco&quot; title=&quot;PetaPoco on nuget&quot;&gt;PetaPoco&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Install-Package &lt;a href=&quot;http://nuget.org/packages/SqlServerCompact&quot;
 title=&quot;SqlServerCompact on nuget&quot;&gt;SqlServerCompact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PetaPoco comes with a T4 template to generate POCOs from a database, all you have to do is add a connection string to your config and run the transform. This is a neat feature, but I didn&#039;t plan to use it for this post so I excluded the files from my project.&lt;/p&gt;

&lt;p&gt;PetaPoco supports SQL Server, SQL Server CE, MySQL, PostgreSQL and Oracle, and works with .Net 3.5 and Mono 2.6 forward. Documentation is available via the &lt;a href=&quot;http://www.toptensoftware.com/petapoco/&quot; title=&quot;PetaPoco main page&quot;&gt;Main Page&lt;/a&gt; and &lt;a href=&quot;http://www.toptensoftware.com/Categories/PetaPoco&quot; title=&quot;PetaPoco blog posts&quot;&gt;blog posts&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As Chrissie did in his post, we&#039;re going to first create a Compact SQL database, except we&#039;ll be doing it in C#.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb24893&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; SqlCeEngine CreateDatabase&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;File.&lt;span style=&quot;color: #0000FF;&quot;&gt;Exists&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;test.sdf&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; File.&lt;span style=&quot;color: #0000FF;&quot;&gt;Delete&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;test.sdf&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; connectionString = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;DataSource=&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;test.sdf&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;; Password=&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;chrissiespassword&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;&amp;quot;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; var en = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; SqlCeEngine&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;connectionString&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; en.&lt;span style=&quot;color: #0000FF;&quot;&gt;CreateDatabase&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;return&lt;/span&gt; en;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb92500&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now that we have Chrissie&#039;s database, lets add his table.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb72332&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0600FF;&quot;&gt;void&lt;/span&gt; CreateTable&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;var db = &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Database&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;DataSource=&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;test.sdf&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;; Password=&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;chrissiespassword&lt;span style=&quot;color: #008080; font-weight: bold;&quot;&gt;\&amp;quot;&lt;/span&gt;&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;System.Data.SqlServerCe.4.0&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Execute&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;CREATE TABLE Person (LastName nvarchar (40) NOT NULL, FirstName nvarchar (40))&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb8857&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This statement was quite a bit shorter using PetaPoco then it was with Simple.Data. The Database object takes care of the connection and command work for us, leaving us just the bits that are specific to our individual scenario. We still have the option of providing an IDbConnection if we want, which would be handy if we were using something like Sam Saffron&#039;s &lt;a href=&quot;http://miniprofiler.com/&quot; title=&quot;MiniProfiler&quot;&gt;MiniProfiler&lt;/a&gt; and wanted to pass in a profiled connection object.&lt;/p&gt;

&lt;p&gt;Because we want to play with concrete POCOs, I&#039;m going to add a very plain POCO and a very basic decorated POCO:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb61059&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; Person &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; LastName &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; FirstName &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; get; set; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#91;&lt;/span&gt;PetaPoco.&lt;span style=&quot;color: #0000FF;&quot;&gt;TableName&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Person&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0600FF;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; DecoratedPerson : Person &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb98100&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are also attributes for the primary key* and the ability to explicitly define columns so PetaPoco will know which columns should be included in queries and which should not.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;I have to say I was surprised Chrissie didn&#039;t include a Primary Key given how many DBAs and DB Developers also blog here, brave man &lt;img src=&quot;http://blogs.lessthandot.com/rsc/smilies/icon_wink.gif&quot; title=&quot;;)&quot; alt=&quot;;)&quot; class=&quot;middle&quot; width=&quot;15&quot; height=&quot;15&quot; /&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;So as chrissie pointed out in his post, we&#039;ve got the basic setup behind us and can now move forward to interacting with our new database.&lt;/p&gt;

&lt;h2&gt;PetaPoco&lt;/h2&gt;
&lt;p&gt;With a database created, lets go ahead and add some data to play with.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb32029&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;Person&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #0600FF;&quot;&gt;null&lt;/span&gt;, &lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Person&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; LastName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;, FirstName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;firstname1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb91850&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first overload of the Insert() method takes a table name, primary key name, and the POCO instance to insert. if we don&#039;t mind add some decoration to our Person object, we can decorate the POCO with the table name and shrink it to this:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb13089&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; DecoratedPerson&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; LastName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname2&amp;quot;&lt;/span&gt;, FirstName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;firstname2&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb71360&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This method uses the table name attribute to generate the insert.&lt;/p&gt;

&lt;p&gt;And last, if we want to keep our table and object names in sync, we can let reflection magically figure it out:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb34509&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Insert&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; Person&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#123;&lt;/span&gt; LastName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname3&amp;quot;&lt;/span&gt;, FirstName = &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;firstname3&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb74397&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now that we have some data in our database, lets look at a few ways to get it out.&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb79733&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// select statement&lt;/span&gt;&lt;br /&gt;var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;Person&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SELECT * FROM Person WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// let PetaPoco generate the SELECT portion &lt;/span&gt;&lt;br /&gt;var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;DecoratedPerson&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb55869&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can execute a parameterized SQL statement fairly easily by using numbered parameters that will line up with the additional arguments we provide. In the second case we&#039;re actually letting PetaPoco generate the SELECT portion of the statement for us, which will resolve to: &lt;code class=&quot;codespan&quot;&gt;SELECT [Person].[LastName], [Person].[FirstName] FROM [Person] WHERE lastname=@0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Executing specifically for a single record instead of querying for a collection is similarly straight forward:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb3852&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// select statement&lt;/span&gt;&lt;br /&gt;var result = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Single&lt;/span&gt;&amp;lt;Person&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;SELECT * FROM Person WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;String&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Format&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;{0}: {1}&amp;quot;&lt;/span&gt;, result.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetType&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;, result&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// let PetaPoco generate the SELECT portion &lt;/span&gt;&lt;br /&gt;var result = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Single&lt;/span&gt;&amp;lt;DecoratedPerson&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;String&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Format&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;{0}: {1}&amp;quot;&lt;/span&gt;, result.&lt;span style=&quot;color: #0000FF;&quot;&gt;GetType&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;, result&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb33284&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And if we examine the output we&#039;ll see they are concrete instances of our POCOs, not dynamics or proxies:&lt;br /&gt;
&lt;monospace&gt;
PetaPocoSample.Person: lastname1, firstname1
PetaPocoSample.DecoratedPerson: lastname1, firstname1
&lt;/monospace&gt;&lt;/p&gt;

&lt;p&gt;If we then follow Chrissie&#039;s lead and add two records into the database that will match this criteria, we&#039;ll receive an exception, as we would expect from a Single call. PetaPoco also offers a &lt;code class=&quot;codespan&quot;&gt;First&amp;lt;T&amp;gt;&lt;/code&gt; implementation we could use in this situation, a &lt;code class=&quot;codespan&quot;&gt;SkipTake&amp;lt;T&amp;gt;&lt;/code&gt; we could use to get the 2nd record, and a number of different ways to query multiple records out of the database:&lt;/p&gt;

&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;csharp&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;csharp&quot; id=&quot;cb9297&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// T&lt;/span&gt;&lt;br /&gt;var result = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;First&lt;/span&gt;&amp;lt;DecoratedPerson&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;// List&amp;lt;T&amp;gt;&lt;/span&gt;&lt;br /&gt;var results = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;SkipTake&lt;/span&gt;&amp;lt;DecoratedPerson&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;//IEnumerable&amp;lt;T&amp;gt;&lt;/span&gt;&lt;br /&gt;var results2 = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Query&lt;/span&gt;&amp;lt;DecoratedPerson&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;//List&amp;lt;T&amp;gt;&lt;/span&gt;&lt;br /&gt;var results3 = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Fetch&lt;/span&gt;&amp;lt;DecoratedPerson&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;//Page&amp;lt;T&amp;gt; - page #2 and page size of 1&lt;/span&gt;&lt;br /&gt;var results4 = db.&lt;span style=&quot;color: #0000FF;&quot;&gt;Page&lt;/span&gt;&amp;lt;DecoratedPerson&amp;gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;WHERE lastname=@0&amp;quot;&lt;/span&gt;, &lt;span style=&quot;color: #808080;&quot;&gt;&amp;quot;lastname1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb13453&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I haven&#039;t done much with PetaPoco yet, but just from playing with these basic queries I can tell I want to spend some more time with it. The syntax is clean and focuses on simplifying the bits that are repeated in so many projects (connection and command wrangling, mapping) while leaving me the full power of SQL and not injecting an additional layer of abstraction to try to work through. On top of that, it performs very closely to the speed of hand-coded SqlDataReader statements (results available on &lt;a href=&quot;http://code.google.com/p/dapper-dot-net/#Performance_of_SELECT_mapping_over_500_iterations_-_POCO_seriali&quot; title=&quot;dapper-dot-net&quot;&gt;the dapper-dot-net&lt;/a&gt; page).&lt;/p&gt;

&lt;p&gt;My sample code is up on &lt;a href=&quot;https://github.com/tarwn/PetaPocoSample&quot; title=&quot;Sample code on github&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/playing-with-petapoco&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Since Chrissie is playing around with <a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/simple-data-and-vb-net" title="Read Chrissie's post">Simple.Data</a> today, I found some time to play with <a href="http://www.toptensoftware.com/petapoco/" title="Main Site for PetaPoco">PetaPoco</a>. PetaPoco is a single file micro ORM that uses MSIL generation to do it's magic. As the name suggests, it works with concrete POCOs, though support for dynamics is also being tested. It is designed to be fast and doesn't try to reimplement SQL, so double win in my book. PetaPoco was created by Brad Robinson (<a href="http://www.toptensoftware.com/blog/" title="topten software blog">b</a> | <a href="http://twitter.com/toptensoftware" title="TopTenSoftware on twitter">t</a>).</p>

<p>It has been on my list to try for a while, so today seemed like a good day for it.</p>

<h2>Setting it up</h2>
<p>Setup is simple using Nuget:</p>
<ul>
<li>Install-Package <a href="http://nuget.org/List/Packages/PetaPoco" title="PetaPoco on nuget">PetaPoco</a></li>
<li>Install-Package <a href="http://nuget.org/packages/SqlServerCompact"
 title="SqlServerCompact on nuget">SqlServerCompact</a></li>
</ul>

<p>PetaPoco comes with a T4 template to generate POCOs from a database, all you have to do is add a connection string to your config and run the transform. This is a neat feature, but I didn't plan to use it for this post so I excluded the files from my project.</p>

<p>PetaPoco supports SQL Server, SQL Server CE, MySQL, PostgreSQL and Oracle, and works with .Net 3.5 and Mono 2.6 forward. Documentation is available via the <a href="http://www.toptensoftware.com/petapoco/" title="PetaPoco main page">Main Page</a> and <a href="http://www.toptensoftware.com/Categories/PetaPoco" title="PetaPoco blog posts">blog posts</a>.</p>

<p>As Chrissie did in his post, we're going to first create a Compact SQL database, except we'll be doing it in C#.</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb76399'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb76399','cb94858'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb76399" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">private</span> SqlCeEngine CreateDatabase<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;test.sdf&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> File.<span style="color: #0000FF;">Delete</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;test.sdf&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #FF0000;">string</span> connectionString = <span style="color: #808080;">&quot;DataSource=<span style="color: #008080; font-weight: bold;">\&quot;</span>test.sdf<span style="color: #008080; font-weight: bold;">\&quot;</span>; Password=<span style="color: #008080; font-weight: bold;">\&quot;</span>chrissiespassword<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>;</li><li style="" class="li1">&nbsp; &nbsp; var en = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> SqlCeEngine<span style="color: #000000;">&#40;</span>connectionString<span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; en.<span style="color: #0000FF;">CreateDatabase</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">return</span> en;</li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb94858" style="display: none; color: red;"></div></div></div>

<p>Now that we have Chrissie's database, lets add his table.</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb58001'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb58001','cb84922'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb58001" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> CreateTable<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var db = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Database<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;DataSource=<span style="color: #008080; font-weight: bold;">\&quot;</span>test.sdf<span style="color: #008080; font-weight: bold;">\&quot;</span>; Password=<span style="color: #008080; font-weight: bold;">\&quot;</span>chrissiespassword<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span>, <span style="color: #808080;">&quot;System.Data.SqlServerCe.4.0&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: #0000FF;">Execute</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;CREATE TABLE Person (LastName nvarchar (40) NOT NULL, FirstName nvarchar (40))&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></li><li style="" class="li1"><span style="color: #000000;">&#125;</span></li></ol></div><div id="cb84922" style="display: none; color: red;"></div></div></div>

<p>This statement was quite a bit shorter using PetaPoco then it was with Simple.Data. The Database object takes care of the connection and command work for us, leaving us just the bits that are specific to our individual scenario. We still have the option of providing an IDbConnection if we want, which would be handy if we were using something like Sam Saffron's <a href="http://miniprofiler.com/" title="MiniProfiler">MiniProfiler</a> and wanted to pass in a profiled connection object.</p>

<p>Because we want to play with concrete POCOs, I'm going to add a very plain POCO and a very basic decorated POCO:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb37627'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb37627','cb53845'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb37627" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Person <span style="color: #000000;">&#123;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> LastName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FirstName <span style="color: #000000;">&#123;</span> get; set; <span style="color: #000000;">&#125;</span></li><li style="" class="li2"><span style="color: #000000;">&#125;</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #000000;">&#91;</span>PetaPoco.<span style="color: #0000FF;">TableName</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Person&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></li><li style="" class="li1"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> DecoratedPerson : Person <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span></li></ol></div><div id="cb53845" style="display: none; color: red;"></div></div></div>

<p>There are also attributes for the primary key* and the ability to explicitly define columns so PetaPoco will know which columns should be included in queries and which should not.</p>

<p><i>I have to say I was surprised Chrissie didn't include a Primary Key given how many DBAs and DB Developers also blog here, brave man <img src="http://blogs.lessthandot.com/rsc/smilies/icon_wink.gif" title=";)" alt=";)" class="middle" width="15" height="15" /></i></p>

<p>So as chrissie pointed out in his post, we've got the basic setup behind us and can now move forward to interacting with our new database.</p>

<h2>PetaPoco</h2>
<p>With a database created, lets go ahead and add some data to play with.</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb83170'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb83170','cb36483'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb83170" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">db.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Person&quot;</span>, <span style="color: #0600FF;">null</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> LastName = <span style="color: #808080;">&quot;lastname1&quot;</span>, FirstName = <span style="color: #808080;">&quot;firstname1&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb36483" style="display: none; color: red;"></div></div></div>

<p>The first overload of the Insert() method takes a table name, primary key name, and the POCO instance to insert. if we don't mind add some decoration to our Person object, we can decorate the POCO with the table name and shrink it to this:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb96292'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb96292','cb5631'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb96292" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">db.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DecoratedPerson<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> LastName = <span style="color: #808080;">&quot;lastname2&quot;</span>, FirstName = <span style="color: #808080;">&quot;firstname2&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb5631" style="display: none; color: red;"></div></div></div>

<p>This method uses the table name attribute to generate the insert.</p>

<p>And last, if we want to keep our table and object names in sync, we can let reflection magically figure it out:</p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb39502'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb39502','cb83205'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb39502" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">db.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Person<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> LastName = <span style="color: #808080;">&quot;lastname3&quot;</span>, FirstName = <span style="color: #808080;">&quot;firstname3&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb83205" style="display: none; color: red;"></div></div></div>

<p>Now that we have some data in our database, lets look at a few ways to get it out.</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb16953'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb16953','cb64396'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb16953" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #008080; font-style: italic;">// select statement</span></li><li style="" class="li2">var results = db.<span style="color: #0000FF;">Query</span>&lt;Person&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SELECT * FROM Person WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1"><span style="color: #008080; font-style: italic;">// let PetaPoco generate the SELECT portion </span></li><li style="" class="li2">var results = db.<span style="color: #0000FF;">Query</span>&lt;DecoratedPerson&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb64396" style="display: none; color: red;"></div></div></div>

<p>We can execute a parameterized SQL statement fairly easily by using numbered parameters that will line up with the additional arguments we provide. In the second case we're actually letting PetaPoco generate the SELECT portion of the statement for us, which will resolve to: <code class="codespan">SELECT [Person].[LastName], [Person].[FirstName] FROM [Person] WHERE lastname=@0</code>.</p>

<p>Executing specifically for a single record instead of querying for a collection is similarly straight forward:</p>
<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb75705'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb75705','cb89286'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb75705" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #008080; font-style: italic;">// select statement</span></li><li style="" class="li2">var result = db.<span style="color: #0000FF;">Single</span>&lt;Person&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;SELECT * FROM Person WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;{0}: {1}&quot;</span>, result.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, result<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #008080; font-style: italic;">// let PetaPoco generate the SELECT portion </span></li><li style="" class="li2">var result = db.<span style="color: #0000FF;">Single</span>&lt;DecoratedPerson&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1">Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;{0}: {1}&quot;</span>, result.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, result<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb89286" style="display: none; color: red;"></div></div></div>

<p>And if we examine the output we'll see they are concrete instances of our POCOs, not dynamics or proxies:<br />
<monospace>
PetaPocoSample.Person: lastname1, firstname1
PetaPocoSample.DecoratedPerson: lastname1, firstname1
</monospace></p>

<p>If we then follow Chrissie's lead and add two records into the database that will match this criteria, we'll receive an exception, as we would expect from a Single call. PetaPoco also offers a <code class="codespan">First&lt;T&gt;</code> implementation we could use in this situation, a <code class="codespan">SkipTake&lt;T&gt;</code> we could use to get the 2nd record, and a number of different ways to query multiple records out of the database:</p>

<div class="codebox"><div class="codeheader"><span>csharp</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb73254'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb73254','cb36764'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="csharp" id="cb73254" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #008080; font-style: italic;">// T</span></li><li style="" class="li2">var result = db.<span style="color: #0000FF;">First</span>&lt;DecoratedPerson&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1"><span style="color: #008080; font-style: italic;">// List&lt;T&gt;</span></li><li style="" class="li2">var results = db.<span style="color: #0000FF;">SkipTake</span>&lt;DecoratedPerson&gt;<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">1</span>, <span style="color: #808080;">&quot;WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1"><span style="color: #008080; font-style: italic;">//IEnumerable&lt;T&gt;</span></li><li style="" class="li2">var results2 = db.<span style="color: #0000FF;">Query</span>&lt;DecoratedPerson&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1"><span style="color: #008080; font-style: italic;">//List&lt;T&gt;</span></li><li style="" class="li2">var results3 = db.<span style="color: #0000FF;">Fetch</span>&lt;DecoratedPerson&gt;<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li><li style="" class="li1"><span style="color: #008080; font-style: italic;">//Page&lt;T&gt; - page #2 and page size of 1</span></li><li style="" class="li2">var results4 = db.<span style="color: #0000FF;">Page</span>&lt;DecoratedPerson&gt;<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">1</span>, <span style="color: #808080;">&quot;WHERE lastname=@0&quot;</span>, <span style="color: #808080;">&quot;lastname1&quot;</span><span style="color: #000000;">&#41;</span>;</li></ol></div><div id="cb36764" style="display: none; color: red;"></div></div></div>

<h2>Conclusion</h2>
<p>I haven't done much with PetaPoco yet, but just from playing with these basic queries I can tell I want to spend some more time with it. The syntax is clean and focuses on simplifying the bits that are repeated in so many projects (connection and command wrangling, mapping) while leaving me the full power of SQL and not injecting an additional layer of abstraction to try to work through. On top of that, it performs very closely to the speed of hand-coded SqlDataReader statements (results available on <a href="http://code.google.com/p/dapper-dot-net/#Performance_of_SELECT_mapping_over_500_iterations_-_POCO_seriali" title="dapper-dot-net">the dapper-dot-net</a> page).</p>

<p>My sample code is up on <a href="https://github.com/tarwn/PetaPocoSample" title="Sample code on github">GitHub</a>.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/playing-with-petapoco">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/CSharp/playing-with-petapoco#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2&#38;disp=comments&#38;p=1715</wfw:commentRss>
		</item>
				<item>
			<title>Unit Testing Costs Too Much - Twice The Code = Value?</title>
			<link>http://blogs.lessthandot.com/index.php/DesktopDev/GeneralPurposeLanguages/unit-testing-costs-too-much-twice-the-co</link>
			<pubDate>Mon, 15 Nov 2010 15:56:09 +0000</pubDate>			<dc:creator>Eli Weinstock-Herman (tarwn)</dc:creator>
			<category domain="main">General Purpose Languages</category>			<guid isPermaLink="false">1012@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Last week I had the pleasure of presenting at a &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/raleigh-code-camp-followup&quot; title=&quot;Read my quick CodeCamp review&quot;&gt;.Net Code Camp&lt;/a&gt; on the topic of Unit Testing. A key theme of the session was barriers to adoption and the values we can achieve using this tool.&lt;/p&gt;

&lt;p&gt;The main barrier to my adoption of Unit Testing was the idea that writing twice as much code would increase the value of my work. Twice as much code, as was pointed out in a response to &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/GeneralPurposeLanguages/unit-testing-costs-too-much&quot; title=&quot;Read the &#039;Unit testing Costs Too Much&#039; post&quot;&gt;my post two week&#039;s ago&lt;/a&gt;, sounds like higher maintenance costs, higher initial development costs, and a greater opportunity for bugs.&lt;/p&gt;

&lt;h2&gt;How Does Adding Code Increase Value?&lt;/h2&gt;
&lt;p&gt;People have published numerous lists of benefits they have received from Unit Testing, but there is still this seemingly illogical starting point of creating more code to deliver the same end product. And before evaluating any other benefits we could achieve from Unit testing, we have to start by resolving that seeming contradiction.&lt;/p&gt;

&lt;h3&gt;Current Process&lt;/h3&gt;
&lt;p&gt;Consider a software process that includes developers, a QA department, a customer review, and a production deployment. &lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Each developer writes code to fulfill assigned features or tasks&lt;/li&gt;
  &lt;li&gt;Once enough pieces have been written, the developer can wire them together and debug the pieces they have written&lt;/li&gt;
  &lt;li&gt;Completed pieces are handed off to QA&lt;/li&gt;
  &lt;li&gt;QA tests those pieces against the requirements&lt;/li&gt;
  &lt;li&gt;Bugs (code defects or missing requirements) are returned to developers&lt;/li&gt;
  &lt;li&gt;Once QA is satisfied, the pieces are handed off to the customer for review&lt;/li&gt;
  &lt;li&gt;The customer reviews the pieces, possibly sending back instructions for refinement or corrections&lt;/li&gt;
  &lt;li&gt;Once the customer is satisfied, the pieces are deployed to production&lt;/li&gt;
  &lt;li&gt;End users use the pieces in production and interact with production data&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This won&#039;t match everyone&#039;s environments but it should be simple enough to be representative of the work that happens in our environments.&lt;/p&gt;

&lt;h3&gt;Unit Testing is Testing at the Source&lt;/h3&gt;
&lt;p&gt;In Lean Manufacturing there is the concept of moving quality to the source of production. Tests are created that are quick to execute and provide the operator with a quick detection of defects instead of waiting for a Quality Control at the end of the process.&lt;/p&gt;

&lt;p&gt;Moving testing closer to the source reduces the number of defective parts that are stored, transported, and used in later production steps. It allows for faster reaction time to correct the problem and reduces the number of interruptions from having to stop a run and re-produce product for a previous order.&lt;/p&gt;

&lt;p&gt;All of these costs have well-defined values in manufacturing environments as well as parallels in our own environments.&lt;/p&gt;

&lt;h3&gt;Lean Development&lt;/h3&gt;
&lt;p&gt;Each step in our sample development process above increases the base cost of producing a working feature. The further a bug makes it without being detected the greater costs it has accrued and the more cost it will take to correct it.&lt;/p&gt;

&lt;div style=&quot;text-align: center; color: #666666;&quot;&gt;
&lt;img src=&quot;http://tiernok.com/LTDBlog/unittesting/UnitTestingGraphs.png&quot; alt=&quot;Effort and cost of bugs increase dramatically as time passes&quot; /&gt;&lt;br /&gt;
Effort and cost from bugs increase dramatically as time passes
&lt;/div&gt;

&lt;p&gt;Costs:&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;Bug while writing one unit: Cheapest cost&lt;/li&gt;
   &lt;li&gt;Several pieces wired together: changes to the unit and surrounding units&lt;/li&gt;
   &lt;li&gt;During Handoff: Wasted meeting/handoff time for devs and QA personnel, potential schedule change, additional debugging time and cost changes (per prior step)&lt;/li&gt;
   &lt;li&gt;Bugs found in QA: (at minimum) Handoff to Dev, switch from new task to work on bug, investigation, development, debugging changes, handoff to QA, QA repeats some level of testing (high potential for new bug, developers tend to test direct changes only)&lt;/li&gt;
   &lt;li&gt;Bugs found in handoff to customer: (at a minimum) Prior QA steps,time for QA to duplicate problem, task-switching time for QA, cost of customer time and others involved, re-handoff to customer, timeline impact, potential impact to customer trust (depends on frequency - costs include increased micro-management, increased meetings, etc)&lt;/li&gt;
   &lt;li&gt;Bugs found by customer: Similar to last step but any testing to this point by customer is likely to be duplicated, customer schedules impacted, cost of extar customer time, timeline impacted&lt;/li&gt;
   &lt;li&gt;Bugs found in production: Prior step + developers are pulled off other tasks to detect and cleanup bad data, schedules are impacted due to developer time, potentially wasted end user time, potential to retrain + monitor users to work around problem and then retrain back afterward&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Besides people-based costs, there are also hidden costs to returning to development and quality stages. The more time that passes between writing the code and returning to it, the more familiarity will be lost and the higher the potential that other pieces will have been added on top of the bug. Other details, such as the specifics of the requirements that code was fulfilling and how it was initially tested prior to QA hand-off, will rely on the memory of the developer and will have to take into account any changes that have occurred since initial development. Perhaps of greater concern is that QA will not have the same level of familiarity with the tests they initially performed and the customer is more likely to add even more &#039;refinements&#039; when it comes time to re-review the changes.&lt;/p&gt;

&lt;p&gt;Unit Testing is our tool for doing testing at the source, to catch defects earlier before they propagate costs throughout the process.&lt;/p&gt;

&lt;h2&gt;The Value of Quality at the Source&lt;/h2&gt;
&lt;p&gt;Unit Testing will not prevent all bugs but it will catch bugs earlier, reducing the costs above. Unit Testing starts earlier, allowing us to test each unit we build instead of waiting until we have 10-20 built and wired to an interface. For the situations where a bug still makes it past development, we will have a solid set of tests to run after making corrections so we can not only test changes but also the rest of the codebase, heavily reducing the risk of creating new bugs out of bug fixes.&lt;/p&gt;

&lt;p&gt;The value we gain from Unit Testing is value to the team and the business, not to the individual developer (though there are values to be gained there as well).&lt;/p&gt;

&lt;h2&gt;Balancing Cost and Value&lt;/h2&gt;
&lt;p&gt;There is a cost to writing Unit Tests that we have to balance with the potential savings and risk reduction. I don&#039;t believe there is a universal answer as to how much Unit Testing is appropriate for a given environment, project, and team. Many teams take it on faith that 100% coverage is their best bet, but I am not convinced. &lt;/p&gt;

&lt;p&gt;What I can tell you is that all of the benefits of this tool are out of your reach if you don&amp;#8217;t give it a test drive. &lt;/p&gt;

&lt;p&gt;As several people saw in my CodeCamp presentation, it doesn&amp;#8217;t take long to add a Unit Testing project to an existing application and build a few tests. Pick a particularly complex business function or an existing bug in need of correction. If you decide to start evaluating it for greater implementation, the list of costs above should provide some pointers on where you can create some measurements to determine the level of value you are receiving.&lt;/p&gt;


&lt;p&gt;Other posts in this unplanned series:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Initial &quot;Unit Testing Costs Too Much&quot; post: &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/GeneralPurposeLanguages/unit-testing-costs-too-much&quot; title=&quot;Check out the first post&quot;&gt;Unit Testing Costs Too Much&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Code camp review and links for slides: &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?p=999&quot; title=&quot;Code Camp review&quot;&gt;Raleigh Code Camp Followup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;2x Code Followup: &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/GeneralPurposeLanguages/unit-testing-costs-too-much-twice-the-co&quot; title=&quot;Read more on the 2x Code topic&quot;&gt;Unit Testing Costs Too Much - Twice The Code = Value?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Too Many Things to Learn: &lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/unit-testing-costs-too-much-too-many-thi&quot; title=&quot;Read more on the Unit Test Cost topic&quot;&gt;Unit Testing Costs Too Much - Too Many Things to Learn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;&lt;style&gt;
ul{ margin: 1em; list-style-position: outside; }
ul li{ margin: .25em;}
&lt;/style&gt;&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DesktopDev/GeneralPurposeLanguages/unit-testing-costs-too-much-twice-the-co&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Last week I had the pleasure of presenting at a <a href="http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/raleigh-code-camp-followup" title="Read my quick CodeCamp review">.Net Code Camp</a> on the topic of Unit Testing. A key theme of the session was barriers to adoption and the values we can achieve using this tool.</p>

<p>The main barrier to my adoption of Unit Testing was the idea that writing twice as much code would increase the value of my work. Twice as much code, as was pointed out in a response to <a href="http://blogs.lessthandot.com/index.php/DesktopDev/GeneralPurposeLanguages/unit-testing-costs-too-much" title="Read the 'Unit testing Costs Too Much' post">my post two week's ago</a>, sounds like higher maintenance costs, higher initial development costs, and a greater opportunity for bugs.</p>

<h2>How Does Adding Code Increase Value?</h2>
<p>People have published numerous lists of benefits they have received from Unit Testing, but there is still this seemingly illogical starting point of creating more code to deliver the same end product. And before evaluating any other benefits we could achieve from Unit testing, we have to start by resolving that seeming contradiction.</p>

<h3>Current Process</h3>
<p>Consider a software process that includes developers, a QA department, a customer review, and a production deployment. </p>
<ol>
  <li>Each developer writes code to fulfill assigned features or tasks</li>
  <li>Once enough pieces have been written, the developer can wire them together and debug the pieces they have written</li>
  <li>Completed pieces are handed off to QA</li>
  <li>QA tests those pieces against the requirements</li>
  <li>Bugs (code defects or missing requirements) are returned to developers</li>
  <li>Once QA is satisfied, the pieces are handed off to the customer for review</li>
  <li>The customer reviews the pieces, possibly sending back instructions for refinement or corrections</li>
  <li>Once the customer is satisfied, the pieces are deployed to production</li>
  <li>End users use the pieces in production and interact with production data</li>
</ol>

<p>This won't match everyone's environments but it should be simple enough to be representative of the work that happens in our environments.</p>

<h3>Unit Testing is Testing at the Source</h3>
<p>In Lean Manufacturing there is the concept of moving quality to the source of production. Tests are created that are quick to execute and provide the operator with a quick detection of defects instead of waiting for a Quality Control at the end of the process.</p>

<p>Moving testing closer to the source reduces the number of defective parts that are stored, transported, and used in later production steps. It allows for faster reaction time to correct the problem and reduces the number of interruptions from having to stop a run and re-produce product for a previous order.</p>

<p>All of these costs have well-defined values in manufacturing environments as well as parallels in our own environments.</p>

<h3>Lean Development</h3>
<p>Each step in our sample development process above increases the base cost of producing a working feature. The further a bug makes it without being detected the greater costs it has accrued and the more cost it will take to correct it.</p>

<div style="text-align: center; color: #666666;">
<img src="http://tiernok.com/LTDBlog/unittesting/UnitTestingGraphs.png" alt="Effort and cost of bugs increase dramatically as time passes" /><br />
Effort and cost from bugs increase dramatically as time passes
</div>

<p>Costs:</p>
<ul>
   <li>Bug while writing one unit: Cheapest cost</li>
   <li>Several pieces wired together: changes to the unit and surrounding units</li>
   <li>During Handoff: Wasted meeting/handoff time for devs and QA personnel, potential schedule change, additional debugging time and cost changes (per prior step)</li>
   <li>Bugs found in QA: (at minimum) Handoff to Dev, switch from new task to work on bug, investigation, development, debugging changes, handoff to QA, QA repeats some level of testing (high potential for new bug, developers tend to test direct changes only)</li>
   <li>Bugs found in handoff to customer: (at a minimum) Prior QA steps,time for QA to duplicate problem, task-switching time for QA, cost of customer time and others involved, re-handoff to customer, timeline impact, potential impact to customer trust (depends on frequency - costs include increased micro-management, increased meetings, etc)</li>
   <li>Bugs found by customer: Similar to last step but any testing to this point by customer is likely to be duplicated, customer schedules impacted, cost of extar customer time, timeline impacted</li>
   <li>Bugs found in production: Prior step + developers are pulled off other tasks to detect and cleanup bad data, schedules are impacted due to developer time, potentially wasted end user time, potential to retrain + monitor users to work around problem and then retrain back afterward</li>
</ul>

<p>Besides people-based costs, there are also hidden costs to returning to development and quality stages. The more time that passes between writing the code and returning to it, the more familiarity will be lost and the higher the potential that other pieces will have been added on top of the bug. Other details, such as the specifics of the requirements that code was fulfilling and how it was initially tested prior to QA hand-off, will rely on the memory of the developer and will have to take into account any changes that have occurred since initial development. Perhaps of greater concern is that QA will not have the same level of familiarity with the tests they initially performed and the customer is more likely to add even more 'refinements' when it comes time to re-review the changes.</p>

<p>Unit Testing is our tool for doing testing at the source, to catch defects earlier before they propagate costs throughout the process.</p>

<h2>The Value of Quality at the Source</h2>
<p>Unit Testing will not prevent all bugs but it will catch bugs earlier, reducing the costs above. Unit Testing starts earlier, allowing us to test each unit we build instead of waiting until we have 10-20 built and wired to an interface. For the situations where a bug still makes it past development, we will have a solid set of tests to run after making corrections so we can not only test changes but also the rest of the codebase, heavily reducing the risk of creating new bugs out of bug fixes.</p>

<p>The value we gain from Unit Testing is value to the team and the business, not to the individual developer (though there are values to be gained there as well).</p>

<h2>Balancing Cost and Value</h2>
<p>There is a cost to writing Unit Tests that we have to balance with the potential savings and risk reduction. I don't believe there is a universal answer as to how much Unit Testing is appropriate for a given environment, project, and team. Many teams take it on faith that 100% coverage is their best bet, but I am not convinced. </p>

<p>What I can tell you is that all of the benefits of this tool are out of your reach if you don&#8217;t give it a test drive. </p>

<p>As several people saw in my CodeCamp presentation, it doesn&#8217;t take long to add a Unit Testing project to an existing application and build a few tests. Pick a particularly complex business function or an existing bug in need of correction. If you decide to start evaluating it for greater implementation, the list of costs above should provide some pointers on where you can create some measurements to determine the level of value you are receiving.</p>


<p>Other posts in this unplanned series:</p>
<ul>
<li>Initial "Unit Testing Costs Too Much" post: <a href="http://blogs.lessthandot.com/index.php/DesktopDev/GeneralPurposeLanguages/unit-testing-costs-too-much" title="Check out the first post">Unit Testing Costs Too Much</a></li>
<li>Code camp review and links for slides: <a href="http://blogs.lessthandot.com/index.php/All/?p=999" title="Code Camp review">Raleigh Code Camp Followup</a></li>
<li>2x Code Followup: <a href="http://blogs.lessthandot.com/index.php/DesktopDev/GeneralPurposeLanguages/unit-testing-costs-too-much-twice-the-co" title="Read more on the 2x Code topic">Unit Testing Costs Too Much - Twice The Code = Value?</a></li>
<li>Too Many Things to Learn: <a href="http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/unit-testing-costs-too-much-too-many-thi" title="Read more on the Unit Test Cost topic">Unit Testing Costs Too Much - Too Many Things to Learn</a></li>
</ul>


<p><style>
ul{ margin: 1em; list-style-position: outside; }
ul li{ margin: .25em;}
</style></p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DesktopDev/GeneralPurposeLanguages/unit-testing-costs-too-much-twice-the-co">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DesktopDev/GeneralPurposeLanguages/unit-testing-costs-too-much-twice-the-co#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DesktopDev/?tempskin=_rss2&#38;disp=comments&#38;p=1012</wfw:commentRss>
		</item>
			</channel>
</rss>
