<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Phillip Haydon</title>
	<atom:link href="http://www.philliphaydon.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.philliphaydon.com</link>
	<description>Works on my pc.</description>
	<lastBuildDate>Tue, 10 Apr 2012 14:50:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CodeSchool.com is the most awesome way to learn</title>
		<link>http://www.philliphaydon.com/2012/04/codeschool-com-is-the-most-awesome-way-to-learn/</link>
		<comments>http://www.philliphaydon.com/2012/04/codeschool-com-is-the-most-awesome-way-to-learn/#comments</comments>
		<pubDate>Tue, 10 Apr 2012 14:50:12 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[learning]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/?p=444</guid>
		<description><![CDATA[On the 1st of April, Glenn Block posted a link that was too good to be true. www.codeschool.com I have a habit of checking out most links that don’t look like they link to a porn site or some look at photo of me and signup to see me naked. This particular link interested me [...]]]></description>
			<content:encoded><![CDATA[<p>On the 1st of April, Glenn Block posted a link that was too good to be true. </p>
<p><a href="http://www.codeschool.com" target="_blank">www.codeschool.com</a> </p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/04/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/04/image_thumb.png" width="521" height="86" /></a></p>
<p>I have a habit of checking out most links that don’t look like they link to a porn site or some look at photo of me and signup to see me naked. This particular link interested me because his tweet mentioned Backbone, something I’ve been wanting to learn for a while and that I’m been hoping <a href="https://twitter.com/#!/derickbailey" target="_blank">Derick Bailey</a> would add to his awesome JavaScript video collection: <a href="http://www.watchmecode.net" target="_blank">http://www.watchmecode.net</a> (<em><strong>his JavaScript series is absolutely brilliant and I’ve learnt SO much from him so go support him so he will make more plz kthxbi</strong></em>)</p>
<p>Anyway CodeSchool allows you to do level 1 of most courses for free, and if you like what you try you can purchase the rest of the course. Buying the courses 1 by 1 is a little bit expensive at $55 USD, but it’s well worth the money. </p>
<p>You’re not getting just a video like you would with sites like Plural Sight, Tekpub, or any of the many other site’s popping up lately. </p>
<p>After each lesson in the course, there’s a series of challenge&#8221;s which allow you to utilize your new gained knowledge and put it to the test. The Backbone course challenges you to make a small ToDo list application, starting out really basic and slowly modifying the code to follow best practice, or add new features. </p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/04/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/04/image_thumb1.png" width="563" height="377" /></a></p>
<p>Each challenge builds on top of the previous, it gives you a story with some helpful information on functions to invoke or events to listen to, and you can dive back into the slides to double check things, should you forget.</p>
<p>If you get stuck you can ask for hints. </p>
<p>Each time you use a hint, the number of points you can gain from the challenge drops, yes that’s right, there’s a points counter to entice you to complete the challenges. </p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/04/image2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/04/image_thumb2.png" width="107" height="257" /></a>&#160;&#160;&#160; <a href="http://www.philliphaydon.com/wp-content/uploads/2012/04/image3.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/04/image_thumb3.png" width="216" height="71" /></a></p>
<p>To top it all off you get this nifty little public profile with badges to show what you’ve completed:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/04/image4.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/04/image_thumb4.png" width="656" height="394" /></a></p>
<p>This site is just amazing, it really taking learning to a whole new level. You’re not stuck with boring old videos that you forget after you’ve finished watching an hour of content, you spend up to 20 minutes learning a bunch of new stuff which you can use right away in their predefined challenges and get feedback right away. It goes a long way to help you understand what you’re doing.</p>
<p>I highly recommend anyone wanting to learn Backbone, grab this course:</p>
<p><a href="http://www.codeschool.com/courses/anatomy-of-backbonejs">http://www.codeschool.com/courses/anatomy-of-backbonejs</a></p>
<p>Or checkout some of the other courses available:</p>
<p><a href="http://www.codeschool.com/courses">http://www.codeschool.com/courses</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2012/04/codeschool-com-is-the-most-awesome-way-to-learn/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>My must have (short) list programs/extensions etc</title>
		<link>http://www.philliphaydon.com/2012/03/my-must-have-short-list-programs-and-extensions-etc/</link>
		<comments>http://www.philliphaydon.com/2012/03/my-must-have-short-list-programs-and-extensions-etc/#comments</comments>
		<pubDate>Sat, 24 Mar 2012 06:13:53 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/?p=429</guid>
		<description><![CDATA[We have all seen these lists before, but I’ve decided I’ll make a list of all the software I use or think are a must have, blog it, then this time next year I can do the list again and see how much it’s changed. Visual Studio 2010 What can I say, I’m a .NET [...]]]></description>
			<content:encoded><![CDATA[<p>We have all seen these lists before, but I’ve decided I’ll make a list of all the software I use or think are a must have, blog it, then this time next year I can do the list again and see how much it’s changed.</p>
<h3><font size="3">Visual Studio 2010</font></h3>
<p>What can I say, I’m a .NET Developer <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/wlEmoticon-smile.png" /></p>
<h3><font size="3">ReSharper</font></h3>
<p><a title="http://www.jetbrains.com/resharper/" href="http://www.jetbrains.com/resharper/" target="_blank">http://www.jetbrains.com/resharper/</a></p>
<p>This should just ship with Visual Studio, without ReSharper I feel so unproductive in VS. </p>
<h3><font size="3">MindScape Web Workbench</font></h3>
<p><a title="http://www.mindscapehq.com/products/web-workbench" href="http://www.mindscapehq.com/products/web-workbench" target="_blank">http://www.mindscapehq.com/products/web-workbench</a></p>
<p>This VS extension just gets better and better, I primarily got it to write LESS and CoffeeScript, but recently they added support for minifying and combining JavaScript and CSS files. Really cool stuff and I highly recommend it.</p>
<h3><font size="3">BugAid</font></h3>
<p><a title="http://www.bugaidsoftware.com/" href="http://www.bugaidsoftware.com/" target="_blank">http://www.bugaidsoftware.com/</a></p>
<p>This is a relatively new extension, it extends the debugging tools in Visual Studio and gives more information where it can. It was a little annoying at first, but they are listening to the community and fixing issues, adding features, it’s coming along nicely and I recommend it!</p>
<h3><font size="3">MightyMoose / NCrunch</font></h3>
<p><a href="http://www.continuoustests.com/" target="_blank">http://www.continuoustests.com/</a> / <a href="http://www.ncrunch.net/" target="_blank">http://www.ncrunch.net/</a></p>
<p>These two tools are the same taken from two different approaches with two different goals. As a result I’m really torn between them. I love features from both, and I’ve been using MightyMoose at work and NCrunch at home. NCrunch has code-coverage which Greg Young <a href="http://goodenoughsoftware.net/2012/03/21/code-coverage/" target="_blank">disagrees</a> with. I guess that’s what’s stopping me from adopting <a href="http://goodenoughsoftware.net/2012/03/01/mighty-moose-lolcats/" target="_blank">LOLCats</a> and <a href="http://goodenoughsoftware.net/2012/03/06/gary/" target="_blank">Gary</a>, and other cool features in MM.</p>
<h3><font size="3">PostgreSQL</font></h3>
<p><a href="http://www.postgresql.org/">http://www.postgresql.org/</a></p>
<p>One thing I don’t like about SQL Server, is having to install it and have it run all the time… But I’ve always loved PostgreSQL as well, and hated MySQL. <em>(checkout Tekpubs “The Perils of MySQL” for a glimpse of why I don’t like it).</em> Why I think this is a must have is I recently found out <a href="http://www.postgresonline.com/journal/archives/172-Starting-PostgreSQL-in-windows-without-install.html" target="_blank">you don’t actually need to install it to run</a>. This makes it great for a dev environment where you want to run up an instance on demand when you need it.</p>
<h3><font size="3">RavenDB</font></h3>
<p><a href="http://ravendb.net/" target="_blank">http://ravendb.net/</a></p>
<p>If you’re a .NET developer and not already looking at, or using RavenDB… I don’t know you… RavenDB makes working with a document database, fun, and easy to learn, and constantly blows my mind.</p>
<h3><font size="3">ILSpy</font></h3>
<p><a title="http://wiki.sharpdevelop.net/ilspy.ashx" href="http://wiki.sharpdevelop.net/ilspy.ashx">http://wiki.sharpdevelop.net/ilspy.ashx</a></p>
<p>Of all the reflection tools available I prefer ILSpy. No real reason it just works and is fast.</p>
<h3><font size="3">Sublime Text</font></h3>
<p><a title="http://www.sublimetext.com/" href="http://www.sublimetext.com/" target="_blank">http://www.sublimetext.com/</a></p>
<p>I used to be a massive fan of <a href="http://editplus.com/" target="_blank">EditPlus</a>, infact I’ve used it for over 10 years… But recently found Sublime text due to <a href="http://tutsplus.com/course/30-days-to-learn-jquery/" target="_blank">Tuts+ 30 Days to learn jQuery</a>, and haven’t gone back to EditPlus. It has awesome plugin features.</p>
<h3><font size="3">NHProf</font></h3>
<p><a href="http://nhprof.com/" target="_blank">http://nhprof.com/</a></p>
<p>Since I tend to use NHibernate quite a bit, well not so much now that RavenDB is about, but when using NHibernate I use NHProf to view the generated queries. One really, really, really handy tool.</p>
<hr />There’s a lot of other little tools I use, like Fiddler, <a href="https://chrome.google.com/webstore/detail/chklaanhfefbnpoihckbnefhakgolnmc" target="_blank">JSONView</a>, etc. But I just wanted to list the things I use all the time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2012/03/my-must-have-short-list-programs-and-extensions-etc/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>OrmLite Blobbing done with NHibernate and Serialized JSON&#8230;</title>
		<link>http://www.philliphaydon.com/2012/03/ormlite-blobbing-done-with-nhibernate-and-serialized-json/</link>
		<comments>http://www.philliphaydon.com/2012/03/ormlite-blobbing-done-with-nhibernate-and-serialized-json/#comments</comments>
		<pubDate>Mon, 19 Mar 2012 05:15:00 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[Fluent NHibernate]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[OrmLite]]></category>
		<category><![CDATA[Service Stack]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[UserType]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2012/03/ormlite-blobbing-done-with-nhibernate-and-serialized-json/</guid>
		<description><![CDATA[There seems to be a growing trend now with these Micro ORM’s, at least that is what I see with ServiceStack.OrmLite, which is the ability to persist properties of an object as a JSON, rather than in separate tables. Usually with a Relational approach you would create a ‘Customer’ table, ‘Address’ table, and most likely [...]]]></description>
			<content:encoded><![CDATA[<p>There seems to be a growing trend now with these Micro ORM’s, at least that is what I see with <a href="https://github.com/ServiceStack/ServiceStack.OrmLite" target="_blank">ServiceStack.OrmLite</a>, which is the ability to persist properties of an object as a JSON, rather than in separate tables.</p>
<p>Usually with a Relational approach you would create a ‘<em>Customer</em>’ table, ‘<em>Address</em>’ table, and most likely shove the Phone Numbers under separate columns of the customer for ‘<em>HomePhone</em>’ and ‘<em>Mobile</em>’.</p>
<p>This means we are limited to two types of phone numbers, and require joining or querying for the addresses.</p>
<p>Do we really need separate columns for phone numbers? Do we really need to persist the addresses in another table?</p>
<p>One problem I see with putting addresses into it’s own table, is the temptation to relate them to an Order (assuming this is some sort of eCommerce system) when really there is no relationship between a customer’s address, and the address on an order.</p>
<p>Really, the order should have it’s own address, otherwise you can never delete or update an address on a customer, and you can’t delete the customer. However I digress and this is a topic for another day.</p>
<h4>Example by OrmLite</h4>
<p>This post is about how to do it with NHibernate, but I’m going to start by showing the example in OrmLite, then use the same example for NHibernate.</p>
<p><em><font color="#ff0000">Note: OrmLite by default persists as JSV-format (JSON+CSV) rather than JSON. I’m currently unaware of any way to change it to be JSON.</font></em></p>
<p><font color="#000000">The customer is the root aggregate, and has his own Addresses and Phone Numbers, and is modelled like so:</font></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4975cdd1-bb8c-43d6-b392-c4ac2c4b4b84" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">enum</span><span style="color:#333333"> </span><span style="color:#597845">PhoneType</span><br /> <span style="color:#333333">{</span><br />     Home<span style="color:#333333">,</span><br />     Work<span style="color:#333333">,</span><br />     Mobile<span style="color:#333333">,</span><br /> <span style="color:#333333">}</span></p>
<p> <span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">enum</span><span style="color:#333333"> </span><span style="color:#597845">AddressType</span><br /> <span style="color:#333333">{</span><br />     Home<span style="color:#333333">,</span><br />     Work<span style="color:#333333">,</span><br />     Other<span style="color:#333333">,</span><br /> <span style="color:#333333">}</span></p>
<p> <span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">class</span><span style="color:#333333"> </span><span style="color:#348000">Address</span><br /> <span style="color:#333333">{</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>Line1<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>Line2<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>ZipCode<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>State<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>City<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>Country<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">}</span></p>
<p> <span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">class</span><span style="color:#333333"> </span><span style="color:#348000">Customer</span><br /> <span style="color:#333333">{</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span>Customer<span style="color:#333333">()</span><br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">this</span><span style="color:#808080">.</span>PhoneNumbers<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">Dictionary</span><span style="color:#808080">&lt;</span><span style="color:#597845">PhoneType</span><span style="color:#333333">, </span><span style="color:#1f2c9e">string</span><span style="color:#808080">&gt;</span><span style="color:#333333">();</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">this</span><span style="color:#808080">.</span>Addresses<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">Dictionary</span><span style="color:#808080">&lt;</span><span style="color:#597845">AddressType</span><span style="color:#333333">, </span><span style="color:#348000">Address</span><span style="color:#808080">&gt;</span><span style="color:#333333">();</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    [</span><span style="color:#348000">AutoIncrement</span><span style="color:#333333">] </span><span style="color:#767c36">// Creates Auto primary key</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">virtual</span><span style="color:#333333"> </span><span style="color:#1f2c9e">int</span><span style="color:#333333"> </span>Id<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">               </span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">virtual</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>FirstName<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">virtual</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>LastName<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">               </span><br /> <span style="color:#333333">    [</span><span style="color:#348000">Index</span><span style="color:#333333">(</span>Unique<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#1f2c9e">true</span><span style="color:#333333">)] </span><span style="color:#767c36">// Creates Unique Index</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">virtual</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>Email<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">               </span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">virtual</span><span style="color:#333333"> </span><span style="color:#348000">Dictionary</span><span style="color:#808080">&lt;</span><span style="color:#597845">PhoneType</span><span style="color:#333333">, </span><span style="color:#1f2c9e">string</span><span style="color:#808080">&gt;</span><span style="color:#333333"> </span>PhoneNumbers<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }  </span><span style="color:#767c36">//Blobbed</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">virtual</span><span style="color:#333333"> </span><span style="color:#348000">Dictionary</span><span style="color:#808080">&lt;</span><span style="color:#597845">AddressType</span><span style="color:#333333">, </span><span style="color:#348000">Address</span><span style="color:#808080">&gt;</span><span style="color:#333333"> </span>Addresses<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }  </span><span style="color:#767c36">//Blobbed</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">virtual</span><span style="color:#333333"> </span><span style="color:#348000">DateTime</span><span style="color:#333333"> </span>CreatedAt<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">}</span></div>
</p></div>
</p></div>
<p><em><font color="#ff0000">Note: The attributes are for OrmLite and are not used by NHibernate, and the properties have been made virtual for NHibernate.</font></em></p>
<p><font color="#000000">So using OrmLite if we insert some data like so:</font></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:add193f6-6f66-4c71-829f-dda8fbafe669" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>customer<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">Customer</span><br /> <span style="color:#333333">{</span><br />     FirstName<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;Phillip&quot;</span><span style="color:#333333">,</span><br />     LastName<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;Haydon&quot;</span><span style="color:#333333">,</span><br />     Email<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;test@test.com&quot;</span><br /> <span style="color:#333333">}</span><span style="color:#333333">;</span></p>
<p> customer<span style="color:#808080">.</span>Addresses<span style="color:#808080">.</span>Add<span style="color:#333333">(</span><span style="color:#597845">AddressType</span><span style="color:#808080">.</span>Home<span style="color:#333333">, </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">Address</span><br /> <span style="color:#333333">{</span><br />     Line1<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;Unit 31&quot;</span><span style="color:#333333">,</span><br />     Line2<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;102 Banana Street&quot;</span><span style="color:#333333">,</span><br />     City<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;Sydney&quot;</span><span style="color:#333333">,</span><br />     Country<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;Australia&quot;</span><span style="color:#333333">,</span><br />     State<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;NSW&quot;</span><span style="color:#333333">,</span><br />     ZipCode<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;2009&quot;</span><br /> <span style="color:#333333">}</span><span style="color:#333333">);</span></p>
<p> customer<span style="color:#808080">.</span>PhoneNumbers<span style="color:#808080">.</span>Add<span style="color:#333333">(</span><span style="color:#597845">PhoneType</span><span style="color:#808080">.</span>Mobile<span style="color:#333333">, </span><span style="color:#542985">&quot;+61 411 122 34&quot;</span><span style="color:#333333">);</span><br /> customer<span style="color:#808080">.</span>PhoneNumbers<span style="color:#808080">.</span>Add<span style="color:#333333">(</span><span style="color:#597845">PhoneType</span><span style="color:#808080">.</span>Home<span style="color:#333333">, </span><span style="color:#542985">&quot;+61 256 3234&quot;</span><span style="color:#333333">);</span></p>
<p> cmd<span style="color:#808080">.</span>Insert<span style="color:#333333">(</span>customer<span style="color:#333333">);</span></div>
</p></div>
</p></div>
<p>We can query for that data and we get the following results:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image13.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb13.png" width="1026" height="49" /></a></p>
<p><em>(Click on the image to see it fully)</em></p>
<p>As you can see PhoneNumbers are stored like so:</p>
<blockquote><p>{Mobile:+61 411 122 34,Home:+61 256 3234}</p>
</blockquote>
<p>And Addresses are stored like:</p>
<blockquote><p>{Home:{Line1:Unit 31,Line2:102 Banana Street,ZipCode:2009,State:NSW,City:Sydney,Country:Australia}}</p>
</blockquote>
<p>Now if we query for that data back out:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a230bdf9-7b95-46c1-99d0-9ca6fe553e72" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>customer<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span>cmd<span style="color:#808080">.</span>QuerySingle<span style="color:#808080">&lt;</span><span style="color:#348000">Customer</span><span style="color:#808080">&gt;</span><span style="color:#333333">(</span><span style="color:#542985">1</span><span style="color:#333333">);</span></div>
</p></div>
</p></div>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image14.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb14.png" width="525" height="472" /></a></p>
<p>You can see we get all the information back out again, no problem! This stuff is built into OrmLite which is awesome, but how do we do it in NHibernate?</p>
<h4>Custom NHibernate UserType</h4>
<p>So now we want to do this in NHibernate. This is a UserType I wrote a long time ago, well… I re-wrote it recently but wrote the initial idea a long time ago, and I’ve personally never seen anything similar in NHibernate.</p>
<p>I’ve put this on Gist &#8211; <a title="https://gist.github.com/1936188" href="https://gist.github.com/1936188">https://gist.github.com/1936188</a></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:49579079-f8fe-4f27-bcbc-a9b2b083eed8" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#333333">[</span><span style="color:#348000">Serializable</span><span style="color:#333333">]</span><br /> <span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">class</span><span style="color:#333333"> </span><span style="color:#348000">Blobbed</span><span style="color:#808080">&lt;</span>T<span style="color:#808080">&gt;</span><span style="color:#333333"> : </span><span style="color:#597845">IUserType</span><span style="color:#333333"> </span><span style="color:#1f2c9e">where</span><span style="color:#333333"> T : </span><span style="color:#1f2c9e">class</span><br /> <span style="color:#333333">{</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#1f2c9e">bool</span><span style="color:#333333"> </span>Equals<span style="color:#333333">(</span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>x<span style="color:#333333">, </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>y<span style="color:#333333">)</span><br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">if</span><span style="color:#333333"> (</span>x<span style="color:#333333"> </span><span style="color:#808080">==</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333"> </span><span style="color:#808080">&amp;&amp;</span><span style="color:#333333"> </span>y<span style="color:#333333"> </span><span style="color:#808080">==</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333">)</span><br /> <span style="color:#333333">            </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#1f2c9e">true</span><span style="color:#333333">;</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">if</span><span style="color:#333333"> (</span>x<span style="color:#333333"> </span><span style="color:#808080">==</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333"> </span><span style="color:#808080">||</span><span style="color:#333333"> </span>y<span style="color:#333333"> </span><span style="color:#808080">==</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333">)</span><br /> <span style="color:#333333">            </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#1f2c9e">false</span><span style="color:#333333">;</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>xdocX<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#348000">JsonConvert</span><span style="color:#808080">.</span>SerializeObject<span style="color:#333333">(</span>x<span style="color:#333333">);</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>xdocY<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#348000">JsonConvert</span><span style="color:#808080">.</span>SerializeObject<span style="color:#333333">(</span>y<span style="color:#333333">);</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span>xdocY<span style="color:#333333"> </span><span style="color:#808080">==</span><span style="color:#333333"> </span>xdocX<span style="color:#333333">;</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">int</span><span style="color:#333333"> </span>GetHashCode<span style="color:#333333">(</span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>x<span style="color:#333333">)</span><br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">if</span><span style="color:#333333"> (</span>x<span style="color:#333333"> </span><span style="color:#808080">==</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333">)</span><br /> <span style="color:#333333">            </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#542985">0</span><span style="color:#333333">;</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span>x<span style="color:#808080">.</span>GetHashCode<span style="color:#333333">();</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>NullSafeGet<span style="color:#333333">(</span><span style="color:#597845">IDataReader</span><span style="color:#333333"> </span>rs<span style="color:#333333">, </span><span style="color:#1f2c9e">string</span><span style="color:#333333">[] </span>names<span style="color:#333333">, </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>owner<span style="color:#333333">)</span><br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">if</span><span style="color:#333333"> (</span>names<span style="color:#808080">.</span>Length<span style="color:#333333"> </span><span style="color:#808080">!=</span><span style="color:#333333"> </span><span style="color:#542985">1</span><span style="color:#333333">)</span><br /> <span style="color:#333333">            </span><span style="color:#1f2c9e">throw</span><span style="color:#333333"> </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">InvalidOperationException</span><span style="color:#333333">(</span><span style="color:#542985">&quot;Only expecting one column&#8230;&quot;</span><span style="color:#333333">);</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>val<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span>rs<span style="color:#333333">[</span>names<span style="color:#333333">[</span><span style="color:#542985">0</span><span style="color:#333333">]] </span><span style="color:#1f2c9e">as</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333">;</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">if</span><span style="color:#333333"> (</span>val<span style="color:#333333"> </span><span style="color:#808080">!=</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333"> </span><span style="color:#808080">&amp;&amp;</span><span style="color:#333333"> </span><span style="color:#808080">!</span><span style="color:#1f2c9e">string</span><span style="color:#808080">.</span>IsNullOrWhiteSpace<span style="color:#333333">(</span>val<span style="color:#333333">))</span><br /> <span style="color:#333333">        {</span><br /> <span style="color:#333333">            </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#348000">JsonConvert</span><span style="color:#808080">.</span>DeserializeObject<span style="color:#808080">&lt;</span><span style="color:#333333">T</span><span style="color:#808080">&gt;</span><span style="color:#333333">(</span>val<span style="color:#333333">);</span><br /> <span style="color:#333333">        }</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333">;</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">void</span><span style="color:#333333"> </span>NullSafeSet<span style="color:#333333">(</span><span style="color:#597845">IDbCommand</span><span style="color:#333333"> </span>cmd<span style="color:#333333">, </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>value<span style="color:#333333">, </span><span style="color:#1f2c9e">int</span><span style="color:#333333"> </span>index<span style="color:#333333">)</span><br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>parameter<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> (</span><span style="color:#348000">DbParameter</span><span style="color:#333333">)</span>cmd<span style="color:#808080">.</span>Parameters<span style="color:#333333">[</span>index<span style="color:#333333">];</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">if</span><span style="color:#333333"> (</span>value<span style="color:#333333"> </span><span style="color:#808080">==</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333">)</span><br /> <span style="color:#333333">        {</span><br />             parameter<span style="color:#808080">.</span>Value<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#348000">DBNull</span><span style="color:#808080">.</span>Value<span style="color:#333333">;</span><br /> <span style="color:#333333">        }</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">else</span><br /> <span style="color:#333333">        {</span><br />             parameter<span style="color:#808080">.</span>Value<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#348000">JsonConvert</span><span style="color:#808080">.</span>SerializeObject<span style="color:#333333">(</span>value<span style="color:#333333">);</span><br /> <span style="color:#333333">        }</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>DeepCopy<span style="color:#333333">(</span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>value<span style="color:#333333">)</span><br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">if</span><span style="color:#333333"> (</span>value<span style="color:#333333"> </span><span style="color:#808080">==</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333">)</span><br /> <span style="color:#333333">            </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333">;</span></p>
<p> <span style="color:#333333">        </span><span style="color:#767c36">//Serialized and Deserialized using json.net so that I don&#39;t</span><br /> <span style="color:#333333">        </span><span style="color:#767c36">//have to mark the class as serializable. Most likely slower</span><br /> <span style="color:#333333">        </span><span style="color:#767c36">//but only done for convenience. </span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>serialized<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#348000">JsonConvert</span><span style="color:#808080">.</span>SerializeObject<span style="color:#333333">(</span>value<span style="color:#333333">);</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#348000">JsonConvert</span><span style="color:#808080">.</span>DeserializeObject<span style="color:#808080">&lt;</span><span style="color:#333333">T</span><span style="color:#808080">&gt;</span><span style="color:#333333">(</span>serialized<span style="color:#333333">);</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>Replace<span style="color:#333333">(</span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>original<span style="color:#333333">, </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>target<span style="color:#333333">, </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>owner<span style="color:#333333">)</span><br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span>original<span style="color:#333333">;</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>Assemble<span style="color:#333333">(</span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>cached<span style="color:#333333">, </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>owner<span style="color:#333333">)</span><br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>str<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span>cached<span style="color:#333333"> </span><span style="color:#1f2c9e">as</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333">;</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">if</span><span style="color:#333333"> (</span><span style="color:#1f2c9e">string</span><span style="color:#808080">.</span>IsNullOrWhiteSpace<span style="color:#333333">(</span>str<span style="color:#333333">))</span><br /> <span style="color:#333333">            </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333">;</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#348000">JsonConvert</span><span style="color:#808080">.</span>DeserializeObject<span style="color:#808080">&lt;</span><span style="color:#333333">T</span><span style="color:#808080">&gt;</span><span style="color:#333333">(</span>str<span style="color:#333333">);</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>Disassemble<span style="color:#333333">(</span><span style="color:#1f2c9e">object</span><span style="color:#333333"> </span>value<span style="color:#333333">)</span><br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">if</span><span style="color:#333333"> (</span>value<span style="color:#333333"> </span><span style="color:#808080">==</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333">)</span><br /> <span style="color:#333333">            </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#1f2c9e">null</span><span style="color:#333333">;</span></p>
<p> <span style="color:#333333">        </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#348000">JsonConvert</span><span style="color:#808080">.</span>SerializeObject<span style="color:#333333">(</span>value<span style="color:#333333">);</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#348000">SqlType</span><span style="color:#333333">[] </span>SqlTypes<br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">get</span><br /> <span style="color:#333333">        {</span><br /> <span style="color:#333333">            </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">SqlType</span><span style="color:#333333">[] { </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">StringSqlType</span><span style="color:#333333">() };</span><br /> <span style="color:#333333">        }</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#348000">Type</span><span style="color:#333333"> </span>ReturnedType<br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">get</span><span style="color:#333333"> { </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#1f2c9e">typeof</span><span style="color:#333333">(T); }</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">bool</span><span style="color:#333333"> </span>IsMutable<br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">get</span><span style="color:#333333"> { </span><span style="color:#1f2c9e">return</span><span style="color:#333333"> </span><span style="color:#1f2c9e">true</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">    }</span><br /> <span style="color:#333333">}</span></div>
</p></div>
</p></div>
<p>It’s a generic class so that I can return the type of object back, and uses <a href="http://james.newtonking.com/pages/json-net.aspx" target="_blank">json.net</a> to handle the serialization/deserialization of the object to JSON.</p>
<p>Now when mapping the properties we can specify the CustomType like so:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7929dc9b-459d-403d-b583-a1115e03f33c" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">Map<span style="color:#333333">(</span>x<span style="color:#333333"> </span><span style="color:#808080">=&gt;</span><span style="color:#333333"> </span>x<span style="color:#808080">.</span>Addresses<span style="color:#333333">, </span><span style="color:#542985">&quot;Addresses&quot;</span><span style="color:#333333">)</span><span style="color:#808080">.</span>CustomType<span style="color:#808080">&lt;</span><span style="color:#348000">Blobbed</span><span style="color:#808080">&lt;</span><span style="color:#348000">Dictionary</span><span style="color:#808080">&lt;</span><span style="color:#597845">AddressType</span><span style="color:#333333">, </span><span style="color:#348000">Address</span><span style="color:#808080">&gt;&gt;&gt;</span><span style="color:#333333">();</span><br /> Map<span style="color:#333333">(</span>x<span style="color:#333333"> </span><span style="color:#808080">=&gt;</span><span style="color:#333333"> </span>x<span style="color:#808080">.</span>PhoneNumbers<span style="color:#333333">, </span><span style="color:#542985">&quot;PhoneNumbers&quot;</span><span style="color:#333333">)</span><span style="color:#808080">.</span>CustomType<span style="color:#808080">&lt;</span><span style="color:#348000">Blobbed</span><span style="color:#808080">&lt;</span><span style="color:#348000">Dictionary</span><span style="color:#808080">&lt;</span><span style="color:#597845">PhoneType</span><span style="color:#333333">, </span><span style="color:#1f2c9e">string</span><span style="color:#808080">&gt;&gt;&gt;</span><span style="color:#333333">();</span></div>
</p></div>
</p></div>
<p>The example I’m using has two dictionaries of values. But if you were mapping a single type such as a single ‘Address’, you would just specify the type as above, without the Dictionary, .CustomType&lt;Blobbed&lt;Address&gt;&gt;()</p>
<p>The full mapping for the Customer is:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:98fab804-5474-4f67-a93d-8aa86e69f557" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">class</span><span style="color:#333333"> </span><span style="color:#348000">CustomerMap</span><span style="color:#333333"> : </span><span style="color:#348000">ClassMap</span><span style="color:#808080">&lt;</span><span style="color:#348000">Customer</span><span style="color:#808080">&gt;</span><br /> <span style="color:#333333">{</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span>CustomerMap<span style="color:#333333">()</span><br /> <span style="color:#333333">    {</span><br />         Table<span style="color:#333333">(</span><span style="color:#542985">&quot;Customer&quot;</span><span style="color:#333333">);</span></p>
<p>         Id<span style="color:#333333">(</span>x<span style="color:#333333"> </span><span style="color:#808080">=&gt;</span><span style="color:#333333"> </span>x<span style="color:#808080">.</span>Id<span style="color:#333333">, </span><span style="color:#542985">&quot;Id&quot;</span><span style="color:#333333">)</span><span style="color:#808080">.</span>GeneratedBy<span style="color:#808080">.</span>Identity<span style="color:#333333">();</span></p>
<p>         Map<span style="color:#333333">(</span>x<span style="color:#333333"> </span><span style="color:#808080">=&gt;</span><span style="color:#333333"> </span>x<span style="color:#808080">.</span>FirstName<span style="color:#333333">, </span><span style="color:#542985">&quot;FirstName&quot;</span><span style="color:#333333">);</span><br />         Map<span style="color:#333333">(</span>x<span style="color:#333333"> </span><span style="color:#808080">=&gt;</span><span style="color:#333333"> </span>x<span style="color:#808080">.</span>LastName<span style="color:#333333">, </span><span style="color:#542985">&quot;LastName&quot;</span><span style="color:#333333">);</span><br />         Map<span style="color:#333333">(</span>x<span style="color:#333333"> </span><span style="color:#808080">=&gt;</span><span style="color:#333333"> </span>x<span style="color:#808080">.</span>Email<span style="color:#333333">, </span><span style="color:#542985">&quot;Email&quot;</span><span style="color:#333333">);</span><br />         Map<span style="color:#333333">(</span>x<span style="color:#333333"> </span><span style="color:#808080">=&gt;</span><span style="color:#333333"> </span>x<span style="color:#808080">.</span>CreatedAt<span style="color:#333333">, </span><span style="color:#542985">&quot;CreatedAt&quot;</span><span style="color:#333333">);</span></p>
<p>         Map<span style="color:#333333">(</span>x<span style="color:#333333"> </span><span style="color:#808080">=&gt;</span><span style="color:#333333"> </span>x<span style="color:#808080">.</span>Addresses<span style="color:#333333">, </span><span style="color:#542985">&quot;Addresses&quot;</span><span style="color:#333333">)</span><span style="color:#808080">.</span>CustomType<span style="color:#808080">&lt;</span><span style="color:#348000">Blobbed</span><span style="color:#808080">&lt;</span><span style="color:#348000">Dictionary</span><span style="color:#808080">&lt;</span><span style="color:#597845">AddressType</span><span style="color:#333333">, </span><span style="color:#348000">Address</span><span style="color:#808080">&gt;&gt;&gt;</span><span style="color:#333333">();</span><br />         Map<span style="color:#333333">(</span>x<span style="color:#333333"> </span><span style="color:#808080">=&gt;</span><span style="color:#333333"> </span>x<span style="color:#808080">.</span>PhoneNumbers<span style="color:#333333">, </span><span style="color:#542985">&quot;PhoneNumbers&quot;</span><span style="color:#333333">)</span><span style="color:#808080">.</span>CustomType<span style="color:#808080">&lt;</span><span style="color:#348000">Blobbed</span><span style="color:#808080">&lt;</span><span style="color:#348000">Dictionary</span><span style="color:#808080">&lt;</span><span style="color:#597845">PhoneType</span><span style="color:#333333">, </span><span style="color:#1f2c9e">string</span><span style="color:#808080">&gt;&gt;&gt;</span><span style="color:#333333">();</span><br /> <span style="color:#333333">    }</span><br /> <span style="color:#333333">}</span></div>
</p></div>
</p></div>
<p>Now we can insert some data:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:aa32b7e5-9bab-430c-8d74-c695139e16a4" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">using</span><span style="color:#333333"> (</span><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>tx<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span>session<span style="color:#808080">.</span>BeginTransaction<span style="color:#333333">())</span><br /> <span style="color:#333333">{</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>customer<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">Customer</span><br /> <span style="color:#333333">    {</span><br />         FirstName<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;Prentice&quot;</span><span style="color:#333333">,</span><br />         LastName<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;Porter&quot;</span><span style="color:#333333">,</span><br />         Email<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;banana3@test.com&quot;</span><br /> <span style="color:#333333">    };</span></p>
<p>     customer<span style="color:#808080">.</span>Addresses<span style="color:#808080">.</span>Add<span style="color:#333333">(</span><span style="color:#597845">AddressType</span><span style="color:#808080">.</span>Home<span style="color:#333333">, </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">Address</span><br /> <span style="color:#333333">    {</span><br />         Line1<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;13/187 Jones St&quot;</span><span style="color:#333333">,</span><br />         City<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;Auckland&quot;</span><span style="color:#333333">,</span><br />         Country<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;New Zealand&quot;</span><span style="color:#333333">,</span><br />         ZipCode<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#542985">&quot;0629&quot;</span><br /> <span style="color:#333333">    });</span></p>
<p>     customer<span style="color:#808080">.</span>PhoneNumbers<span style="color:#808080">.</span>Add<span style="color:#333333">(</span><span style="color:#597845">PhoneType</span><span style="color:#808080">.</span>Mobile<span style="color:#333333">, </span><span style="color:#542985">&quot;+64 27 551 443&quot;</span><span style="color:#333333">);</span><br />     customer<span style="color:#808080">.</span>PhoneNumbers<span style="color:#808080">.</span>Add<span style="color:#333333">(</span><span style="color:#597845">PhoneType</span><span style="color:#808080">.</span>Home<span style="color:#333333">, </span><span style="color:#542985">&quot;+64 9445 1982&quot;</span><span style="color:#333333">);</span></p>
<p>     session<span style="color:#808080">.</span>SaveOrUpdate<span style="color:#333333">(</span>customer<span style="color:#333333">);</span></p>
<p>     tx<span style="color:#808080">.</span>Commit<span style="color:#333333">();</span><br /> <span style="color:#333333">}</span></div>
</p></div>
</p></div>
<p>Again, the data is inserted:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image15.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb15.png" width="1026" height="59" /></a></p>
<p><em>(Click on the image to see it fully)</em></p>
<p>Only this data is serialized as JSON rather than JSV-format.</p>
<p>PhoneNumbers:</p>
<blockquote><p>{&quot;Mobile&quot;:&quot;+64 27 551 443&quot;,&quot;Home&quot;:&quot;+64 9445 1982&quot;}</p>
</blockquote>
<p>And Addresses:</p>
<blockquote><p>{&quot;Home&quot;:{&quot;Line1&quot;:&quot;13/187 Jones St&quot;,&quot;Line2&quot;:null,&quot;ZipCode&quot;:&quot;0629&quot;,&quot;State&quot;:null,&quot;City&quot;:&quot;Auckland&quot;,&quot;Country&quot;:&quot;New Zealand&quot;}}</p>
</blockquote>
<p>If we query for the data:</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:91e4b9ae-8a3f-40a1-890b-6c58b0d65581" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">var</span><span style="color:#333333"> </span>customer<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span>session<span style="color:#808080">.</span>Get<span style="color:#808080">&lt;</span><span style="color:#348000">Customer</span><span style="color:#808080">&gt;</span><span style="color:#333333">(</span><span style="color:#542985">2</span><span style="color:#333333">);</span></div>
</p></div>
</p></div>
</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image16.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb16.png" width="503" height="472" /></a></p>
<p>Just like OrmLite we get the object back just the same.</p>
<h4>Things to note:</h4>
<p>The custom user type in it’s current state does not handle inherited objects. If you want to support it, then you can modify it to serialize and deserialize using the type information.</p>
<p>This can be done like so:</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:07d6c9d5-698d-444c-b090-96dd9e07cf23" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#348000">JsonConvert</span><span style="color:#808080">.</span>SerializeObject<span style="color:#333333">(</span>x<span style="color:#333333">, </span><br /> <span style="color:#333333">    </span><span style="color:#597845">Formatting</span><span style="color:#808080">.</span>None<span style="color:#333333">, </span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">JsonSerializerSettings</span><span style="color:#333333"> { </span>TypeNameHandling<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#597845">TypeNameHandling</span><span style="color:#808080">.</span>All<span style="color:#333333"> });</span></div>
</p></div>
</p></div>
</p>
<p>And</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:72dfd94d-187e-4f9b-9082-2566c9589e38" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#348000">JsonConvert</span><span style="color:#808080">.</span>DeserializeObject<span style="color:#808080">&lt;</span><span style="color:#333333">T</span><span style="color:#808080">&gt;</span><span style="color:#333333">(</span>val<span style="color:#333333">, </span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">JsonSerializerSettings</span><span style="color:#333333"> { </span>TypeNameHandling<span style="color:#333333"> </span><span style="color:#808080">=</span><span style="color:#333333"> </span><span style="color:#597845">TypeNameHandling</span><span style="color:#808080">.</span>All<span style="color:#333333"> });</span></div>
</p></div>
</p></div>
<p>What this will do is include the type information on the serialized object:</p>
<blockquote><p>{&quot;$type&quot;:&quot;System.Collections.Generic.Dictionary2[[NHibernateJsonTest.AddressType, NHibernateJsonTest],[NHibernateJsonTest.Address, NHibernateJsonTest]], mscorlib&quot;,&quot;Home&quot;:{&quot;$type&quot;:&quot;NHibernateJsonTest.Address, NHibernateJsonTest&quot;,&quot;Line1&quot;:&quot;13/187 Jones St&quot;,&quot;Line2&quot;:null,&quot;ZipCode&quot;:&quot;0629&quot;,&quot;State&quot;:null,&quot;City&quot;:&quot;Auckland&quot;,&quot;Country&quot;:&quot;New Zealand&quot;}}</p>
</blockquote>
<h4>Why would you want to do this</h4>
<p>To avoid unnecessary tables and mappings. The example above is a perfect example where we can remove the need for a table on data that is never searched against and is never related to anything else.</p>
<p>There’s no need for joins or adding additional columns. We just map the object or collection to a single column and we are done, and our code knows no different.</p>
<p>Also, schema changes in blobs don’t need DDL updates. If your model changes, you add new properties, or remove old properties, the blob will get updated next time you update your data. No more scripting off schema changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2012/03/ormlite-blobbing-done-with-nhibernate-and-serialized-json/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Service Stack Exceptions and Errors</title>
		<link>http://www.philliphaydon.com/2012/03/service-stack-exceptions-and-errors/</link>
		<comments>http://www.philliphaydon.com/2012/03/service-stack-exceptions-and-errors/#comments</comments>
		<pubDate>Fri, 09 Mar 2012 12:54:12 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[Service Stack]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[service stack]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2012/03/service-stack-exceptions-and-errors/</guid>
		<description><![CDATA[Note: This post on ServiceStack is to do with the C# Client. JavaScript posts will be coming in the future. One of the most painful experiences with WCF is exceptions, if using WCF makes you want to slit your wrists, exceptions in WCF will make you want douse yourself in petrol and light yourself on [...]]]></description>
			<content:encoded><![CDATA[<p><em><font color="#ff0000">Note: This post on ServiceStack is to do with the C# Client. JavaScript posts will be coming in the future.</font></em></p>
<p>One of the most painful experiences with WCF is exceptions, if using WCF makes you want to slit your wrists, exceptions in WCF will make you want douse yourself in petrol and light yourself on fire.</p>
<p>Before you even get to your code exceptions, you have to wade yourself through piles of retarded errors to do with Contract Mismatching, Forcefully Disconnected, Binding Issues, Random Faults… The list goes on.</p>
<p>Then, it all works in development, and you put it in production, and it doesn’t work, and to debug it you got to modify the config file, setup the <a href="http://msdn.microsoft.com/en-us/library/ms732023.aspx" target="_blank">diagnostics logging</a>, then view it and trawl through piles of crap.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image7.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb7.png" width="482" height="371" /></a></p>
<p><em>(image taken from: </em><a title="http://weblogs.asp.net/nmarun/archive/2011/06/10/wcf-service-trace-viewer-part-1.aspx" href="http://weblogs.asp.net/nmarun/archive/2011/06/10/wcf-service-trace-viewer-part-1.aspx" target="_blank">http://weblogs.asp.net/nmarun/archive/2011/06/10/wcf-service-trace-viewer-part-1.aspx</a><em></em><em>)</em></p>
<p>And in the end, it was probably a PEBKAC issue where you forgot to put a stupid attribute on a property…</p>
<p>At my previous job, a couple of my friends were up until about 4am, they spent 16 hours debugging a WCF issue, because it’s a pain.</p>
<h4>Enter Service Stack</h4>
<p>One of the things I looked at early on was how it handled errors, then I neglected them because I lost interest.There’s a couple of exceptions that aren’t SS related. The main one you may come across is: </p>
<blockquote><p><strong>WebException</strong>      <br />Unable to connect to remote server</p>
</blockquote>
<p>This is kind of obvious, either the remote server doesn’t exist or you mistyped the URL, or maybe there’s a firewall issue or something, what ever it is, it can’t connect to your service.</p>
<blockquote><p><strong>HttpException       <br /></strong>Maximum request length exceeded.</p>
</blockquote>
<p>You may also get an HttpException if your requests are larger than 4mb, tho that’s pretty large, not sure what you’re sending to get that exception. But you can modify the <a href="http://msdn.microsoft.com/en-us/library/e1f13641(vs.71).aspx" target="_blank">maxRequestLength</a> to get around this one.</p>
<h5><font size="3">MethodNotAllowed</font></h5>
<p>This method is the easiest to fix, it basically means your service hasn’t been implemented, or maybe it was implemented but you didn’t tell the AppHost about it.</p>
<h5><font size="3">General Exceptions</font></h5>
<p>General exceptions as I’ll call, them, are any unhandled exceptions that are thrown, or ones that you explicitly throw yourself. </p>
<p>These exceptions are always thrown as a <a href="https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Common/ServiceClient.Web/ServiceClientBase.cs#L278" target="_blank">WebServiceException</a>. This means we can capture these exceptions in a try-catch like so:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d27f9b61-1a4d-43dc-b89c-ffc81e3f656c" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">try</span><br /> <span style="color:#333333">{</span><br /> <span style="color:#333333">}</span><br /> <span style="color:#1f2c9e">catch</span><span style="color:#333333"> (</span><span style="color:#348000">WebServiceException</span><span style="color:#333333"> </span>webEx<span style="color:#333333">)</span><br /> <span style="color:#333333">{</span><br /> <span style="color:#333333">    </span><span style="color:#767c36">//Handle our Web Service Exception</span><br /> <span style="color:#333333">}</span></div>
</p></div>
</p></div>
<p>There’s a couple of different ways you can throw exceptions, or errors. You can raise your own:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0bb0f15b-4ee4-4b86-9e09-e6b98c31aa1c" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">throw</span><span style="color:#333333"> </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">ApplicationException</span><span style="color:#333333">(</span><span style="color:#542985">&quot;Ahhhh stuff happened :S&quot;</span><span style="color:#333333">);</span></div>
</p></div>
</p></div>
<p>And then you will get this when you call the service.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image8.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb8.png" width="359" height="291" /></a></p>
<p>The second way is to throw an HttpError. There are a few predefined errors:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image9.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb9.png" width="361" height="75" /></a></p>
<p>That allow you to just pass a message:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7a3a7d15-046a-4a37-af14-b92bf2227527" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">throw</span><span style="color:#333333"> </span><span style="color:#348000">HttpError</span><span style="color:#808080">.</span>NotFound<span style="color:#333333">(</span><span style="color:#542985">&quot;Coffee wasn&#39;t found <img src='http://www.philliphaydon.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  sad panda&quot;</span><span style="color:#333333">);</span></div>
</p></div>
</p></div>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image10.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb10.png" width="395" height="248" /></a></p>
<p>Or you can throw your own new one which allows you to define the Http Status Code:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e9ae6678-4311-445d-89df-5fe90c0149f7" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">throw</span><span style="color:#333333"> </span><span style="color:#1f2c9e">new</span><span style="color:#333333"> </span><span style="color:#348000">HttpError</span><span style="color:#333333">(</span><span style="color:#597845">HttpStatusCode</span><span style="color:#808080">.</span>PaymentRequired<span style="color:#333333">, </span><br /> <span style="color:#333333">    </span><span style="color:#542985">&quot;Kneedz monies plz&quot;</span><span style="color:#333333">, </span><span style="color:#542985">&quot;Please deposit monies into my bank account <img src='http://www.philliphaydon.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &quot;</span><span style="color:#333333">);</span></div>
</p></div>
</p></div>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image11.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb11.png" width="535" height="245" /></a></p>
<p>This is great stuff, it gives a LOT of flexibility to be able to give the client informative error messages rather than the infamous:</p>
<blockquote><p>The connection was closed unexpectedly </p>
</blockquote>
<h4>My errors are empty <img src='http://www.philliphaydon.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </h4>
<p>So I threw an error and I didn’t get it on the client:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image12.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb12.png" width="359" height="284" /></a></p>
<p>As you can see, I’ve lost a lot of information <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-sadsmile" alt="Sad smile" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/wlEmoticon-sadsmile1.png" /> This is while throwing the exact same HttpError in the previous example.</p>
<p>One of the little catches with this is the automated error handling shown above requires a naming convention of your service Request/Response objects.</p>
<p>Lets say we have a ‘UserSearch’ request object. </p>
<p>If an exception is thrown, ServiceStack will try and find an object of the same name, with the suffix ‘Response’</p>
<p>This this case it would look for UserSearchResponse.</p>
<p><a href="https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.ServiceInterface/ServiceUtils.cs#L124" target="_blank">https://github.com/ServiceStack/&#8230;/ServiceUtils.cs#L124</a></p>
<p>As you can see, the method GetResponseDtoName takes in what your Request object was, and appends the ResponseDtoSuffix</p>
<p>The second catch is that your Response DTO must have a property on it named “ResponseStatus”:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1e58ef4d-529a-471a-a219-033df5e4855a" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#348000">ResponseStatus</span><span style="color:#333333"> </span>ResponseStatus<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span></div>
</p></div>
</p></div>
<p>Although it’s not required, I personally add the interface “IHasResponseStatus” to my response DTO objects. This ensures that I remember to add the property, and it’s named correctly with the correct type.</p>
<p>Here is an example:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:51127bbd-bef1-450a-8775-73b22a7faf39" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">class</span><span style="color:#333333"> </span><span style="color:#348000">UserSearch</span><br /> <span style="color:#333333">{</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>NameStartsWith<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">}</span></p>
<p> <span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">class</span><span style="color:#333333"> </span><span style="color:#348000">UserSearchResponse</span><span style="color:#333333"> : </span><span style="color:#597845">IHasResponseStatus</span><br /> <span style="color:#333333">{</span><br /> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#597845">IEnumerable</span><span style="color:#808080">&lt;</span><span style="color:#348000">User</span><span style="color:#808080">&gt;</span><span style="color:#333333"> </span>Results<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">class</span><span style="color:#333333"> </span><span style="color:#348000">User</span><br /> <span style="color:#333333">    {</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">int</span><span style="color:#333333"> </span>Id<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">        </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#1f2c9e">string</span><span style="color:#333333"> </span>Name<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">    }</span></p>
<p> <span style="color:#333333">    </span><span style="color:#1f2c9e">public</span><span style="color:#333333"> </span><span style="color:#348000">ResponseStatus</span><span style="color:#333333"> </span>ResponseStatus<span style="color:#333333"> { </span><span style="color:#1f2c9e">get</span><span style="color:#333333">; </span><span style="color:#1f2c9e">set</span><span style="color:#333333">; }</span><br /> <span style="color:#333333">}</span></div>
</p></div>
</p></div>
<p>My ‘UserSearch’ object is the Request, and my response is ‘UserSearchResponse’</p>
<p>I discussed with <a href="https://github.com/mythz" target="_blank">Demis</a>, because I wanted to use the same response object for a few different services. But he’s specifically designed SS this way, one of the reasons for the naming is:</p>
<p>If a use Requests a UserSearch, he can expect a UserSearchResponse. He doesn’t need to know anything about the contracts or the service implementation, just that given a UserSearch request, he will get a UserSearchResponse.</p>
<p>It’s actually a really good point and after thinking about it, I completely agree!</p>
<h4>Conclusion</h4>
<p>This is only the automated error handling you get with SS, there’s more that you can do, dive in and handle it yourself if you like. But in my opinion, the automated stuff covers a lot of scenarios and is very helpful.</p>
<p>For more information on Error handling visit the wiki here: <a title="https://github.com/ServiceStack/ServiceStack/wiki/Validation" href="https://github.com/ServiceStack/ServiceStack/wiki/Validation" target="_blank">https://github.com/ServiceStack/ServiceStack/wiki/Validation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2012/03/service-stack-exceptions-and-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RavenDB &#8211; Flattening Object Graphs and Projections</title>
		<link>http://www.philliphaydon.com/2012/03/ravendb-flattening-object-graphs-and-projections/</link>
		<comments>http://www.philliphaydon.com/2012/03/ravendb-flattening-object-graphs-and-projections/#comments</comments>
		<pubDate>Sun, 04 Mar 2012 11:48:53 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[RavenDB]]></category>
		<category><![CDATA[AsProjection]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[Projection]]></category>
		<category><![CDATA[Query]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2012/03/ravendb-flattening-object-graphs-and-projections/</guid>
		<description><![CDATA[One of the guys in JabbR RavenDB chat room had a pretty interesting problem that took a while to solve. The problem was that he wasn’t trying to return anything to do with the original document, but get a flattened view of some information inside the document. The scenario was a Game Server which a [...]]]></description>
			<content:encoded><![CDATA[<p>One of the guys in <a href="http://jabbr.net/#/rooms/RavenDB" target="_blank">JabbR RavenDB</a> chat room had a pretty interesting problem that took a while to solve. The problem was that he wasn’t trying to return anything to do with the original document, but get a flattened view of some information inside the document.</p>
<p>The scenario was a <strong><em>Game Server</em></strong> which a collection of <strong><em>Connected Users</em></strong>.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:75ab1f9d-0a04-40a8-9962-c8b93d4209bd" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">GameServer</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Id { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> ServerName { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#2b91af">IEnumerable</span>&lt;<span style="color:#2b91af">User</span>&gt; ConnectedUser { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">User</span><br />     {<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> UserId { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Name { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">DateTimeOffset</span> DateConnected { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     }<br /> }</div>
</p></div>
</p></div>
<p>Given say 3 servers with a bunch of users on each server, and searching for a user who’s name begins with ‘b’ the expected result was along the lines of:</p>
<table border="0" cellspacing="0" cellpadding="2" width="600">
<tbody>
<tr>
<td valign="top" width="100"><strong>UserId</strong></td>
<td valign="top" width="100"><strong>Name</strong></td>
<td valign="top" width="144"><strong>DateConnected</strong></td>
<td valign="top" width="254"><strong>ServerName</strong></td>
</tr>
<tr>
<td valign="top" width="100">users/3</td>
<td valign="top" width="100">Bob</td>
<td valign="top" width="144">15/03/2012 12:44</td>
<td valign="top" width="254">iPGN CS #01 Iceworld</td>
</tr>
<tr>
<td valign="top" width="100">users/2</td>
<td valign="top" width="100">Bill</td>
<td valign="top" width="144">15/03/2012 1:23</td>
<td valign="top" width="254">3FL CS #4</td>
</tr>
<tr>
<td valign="top" width="100">users/8</td>
<td valign="top" width="100">Benny</td>
<td valign="top" width="144">15/03/2012 1:18</td>
<td valign="top" width="254">3FL CS #4</td>
</tr>
</tbody>
</table>
<p>So basically for each user you get the server he’s connected to.</p>
<p>We tried everything under the sun, Reduce, Transform, etc. But couldn’t figure out how to get the results. Infact using a Transform we could get the # of results returned, except they were all NULL. <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-sadsmile" alt="Sad smile" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/wlEmoticon-sadsmile.png" /></p>
<p>Turns out it’s rather easy using just a Map and ‘AsProjection&lt;T&gt;’</p>
<h4>Setup Data:</h4>
<p>Test data can be viewed here: <a title="http://pastie.org/3516113" href="http://pastie.org/3516113" target="_blank">http://pastie.org/3516113</a></p>
<h4>Index:</h4>
<p>The index is really easy, it’s basically a Select Many map, but we also need to store the results from the Map. </p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3762437e-2537-4b94-a069-459406340ec5" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">GameServers_ConnectedUsers</span> :<br />     <span style="color:#2b91af">AbstractIndexCreationTask</span>&lt;<span style="color:#2b91af">GameServer</span>, <span style="color:#2b91af">GameServers_ConnectedUsers</span>.<span style="color:#2b91af">IndexResult</span>&gt;<br /> {<br />     <span style="color:#0000ff">public</span> GameServers_ConnectedUsers()<br />     {<br />         Map = servers =&gt; <span style="color:#0000ff">from</span> s <span style="color:#0000ff">in</span> servers<br />                             <span style="color:#0000ff">from</span> y <span style="color:#0000ff">in</span> s.ConnectedUsers<br />                             <span style="color:#0000ff">select</span> <span style="color:#0000ff">new</span><br />                             {<br />                                 ServerName = s.ServerName,<br />                                 UserName = y.Name,<br />                                 DateConnected = y.DateConnected,<br />                                 UserId = y.UserId<br />                             };</p>
<p>         Store(x =&gt; x.ServerName, <span style="color:#2b91af">FieldStorage</span>.Yes);<br />         Store(x =&gt; x.UserName, <span style="color:#2b91af">FieldStorage</span>.Yes);<br />         Store(x =&gt; x.DateConnected, <span style="color:#2b91af">FieldStorage</span>.Yes);<br />         Store(x =&gt; x.UserId, <span style="color:#2b91af">FieldStorage</span>.Yes);<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">IndexResult</span><br />     {<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> ServerName { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> UserName { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">DateTimeOffset</span> DateConnected { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> UserId { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     }<br /> }</div>
</p></div>
</p></div>
<p>The reason for storing the results from the Map is because if we don’t, we don’t actually get any results back. <em>(will show you soon)</em></p>
<h4>Querying:</h4>
<p>So querying is the same as always, only we need to provide the ‘AsProjection&lt;T&gt;’ to the query, like so:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:58668117-c449-4093-aee8-b0763bd20c3f" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> results = <br />     session.Query&lt;<span style="color:#2b91af">GameServers_ConnectedUsers</span>.<span style="color:#2b91af">IndexResult</span>, <span style="color:#2b91af">GameServers_ConnectedUsers</span>&gt;()<br />            .Where(x =&gt; x.UserName.StartsWith(<span style="color:#a31515">&quot;b&quot;</span>))<br />            .AsProjection&lt;<span style="color:#2b91af">GameServers_ConnectedUsers</span>.<span style="color:#2b91af">IndexResult</span>&gt;()<br />            .ToList();</div>
</p></div>
</p></div>
<p>The Projection just needs to be an object that matches the result, otherwise it will attempt to return the original documents.</p>
<p>If we run the query without the ‘AsProjection&lt;T&gt;’ we end up with an exception because the result (the original document) doesn’t match the object we were querying against ‘IndexResult’</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb.png" width="518" height="153" /></a></p>
<p>If we set the ‘AsProjection’ to dynamic, or GameServer, we get the original documents. BUT the funny thing is if you use ‘GameServer’ you end up with as many results as the projection has. In this case 3.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb1.png" width="446" height="119" /></a></p>
<p>If we expand them out, we see we actually get a duplicate.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb2.png" width="501" height="297" /></a></p>
<p>But if we use ‘dynamic’ we get unique documents:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image3.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb3.png" width="484" height="232" /></a></p>
<p>Using the IndexResult (or an object that matches the projection) we get the projected results that we wanted at the start of this post:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image4.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb4.png" width="583" height="126" /></a></p>
<p>Three results, and all the correct data:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image5.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb5.png" width="610" height="336" /></a></p>
<h4>Storing the results:</h4>
<p>I showed in the index that I was storing the results I wanted in the projection. This is because if we don’t, we end up with this:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/03/image6.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/03/image_thumb6.png" width="595" height="260" /></a></p>
<p>Not only do we end up with the incorrect number of results, they are missing data that isn’t found in the original document.</p>
<p>You can see ‘GameServer’ is there, that’s because it’s found on the document. We need to ‘store’ the data so that RavenDB can return it, that means we use more disk space, but without it, RavenDB would have to query and assume the data that you wanted to return. By storing it RavenDB just returns what matches the query, and doesn’t do any extra work.</p>
<p>So if you want to return the projected results, then you need to Store them.</p>
<p>I’ve put a gist here for anyone interested.</p>
<p><a href="https://gist.github.com/1972646" target="_blank">https://gist.github.com/1972646</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2012/03/ravendb-flattening-object-graphs-and-projections/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Service Stack&#8230; I heart you. My conversion from WCF to SS</title>
		<link>http://www.philliphaydon.com/2012/02/service-stack-i-heart-you-my-conversion-from-wcf-to-ss/</link>
		<comments>http://www.philliphaydon.com/2012/02/service-stack-i-heart-you-my-conversion-from-wcf-to-ss/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 12:57:38 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[Service Stack]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[service stack]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2012/02/service-stack-i-heart-you-my-conversion-from-wcf-to-ss/</guid>
		<description><![CDATA[I’ve just spent the weekend ripping out that dreaded WCF abomination and replacing it with Service Stack. http://servicestack.net/ A modern fresh alternative to WCF. Code-first, convention-based, codegen-free. Encourages best-practices high-performance, scalable REST &#38; RPC web services. Over the past couple of months I’ve been fighting with WCF to the point I wanted to slit my [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve just spent the weekend ripping out that dreaded WCF abomination and replacing it with Service Stack.</p>
<p><a title="http://servicestack.net/" href="http://servicestack.net/">http://servicestack.net/</a></p>
<blockquote><p>A modern fresh alternative to WCF. Code-first, convention-based, codegen-free. Encourages best-practices high-performance, scalable REST &amp; RPC web services.</p>
</blockquote>
<p>Over the past couple of months I’ve been fighting with WCF to the point I wanted to slit my wrists.</p>
<p>So I asked <a href="http://jabbr.net" target="_blank">JabbR</a> and Twitter if I should use Web API. Well –no- one recommended Web API and everyone recommended Service Stack.</p>
<h4>It’s a different way of thinking</h4>
<p>The first noticeable difference between WCF and SS (Service Stack) is that I’m no longer writing a single service class with a butt load of methods. Which is most likely a good thing because after a while they just become hard to manage.</p>
<p>So instead of a Contract, Service, Response DTO, and Request DTO, with 9234823 methods defined in the Contract/Service. It’s now <em><strong>1 Request DTO per Service</strong></em>.</p>
<p>What does that mean?</p>
<p>Well before I would have something like:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4221f937-0441-4be8-be4f-09ba94f911c9" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">[<span style="color:#2b91af">ServiceContract</span>]<br /> <span style="color:#0000ff">public</span> <span style="color:#0000ff">interface</span> <span style="color:#2b91af">IMemberQueryService</span><br /> {<br />     [<span style="color:#2b91af">OperationContract</span>]<br />     <span style="color:#2b91af">MemberResponse</span> ById(<span style="color:#0000ff">string</span> id);</p>
<p>     [<span style="color:#2b91af">OperationContract</span>]<br />     <span style="color:#2b91af">MemberResponse</span> ByEmail(<span style="color:#0000ff">string</span> email);</p>
<p>     [<span style="color:#2b91af">OperationContract</span>]<br />     <span style="color:#2b91af">MemberResponse</span> ByOpenId(<span style="color:#0000ff">string</span> openId);<br /> }</div>
</p></div>
</p></div>
<p>Obviously with the actual service implementation and all that jazz.</p>
<h4>Implementation with Service Stack</h4>
<p>Now with Service Stack I would write that as a single service. This means I need a request class. </p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:65723b6f-f482-456f-9922-975e0eb82a6b" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MemberRequest</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Id { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> OpenId { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Email { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</div>
</p></div>
</p></div>
<p><em><strong>Note:</strong> My ‘Id’ is a string because I’m using RavenDB and this is an a real example</em></p>
<p>The next class,the Service itself:</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9e409cbc-2c93-4c73-820d-ba9babaa84e6" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'',Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MemberService</span> : <span style="color:#2b91af">IService</span>&lt;<span style="color:#2b91af">MemberRequest</span>&gt;<br /> {<br />     <span style="color:#0000ff">private</span> <span style="color:#2b91af">IDocumentStore</span> DocumentStore { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</p>
<p>     <span style="color:#0000ff">public</span> MemberService(<span style="color:#2b91af">IDocumentStore</span> documentStore)<br />     {<br />         DocumentStore = documentStore;<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">object</span> Execute(<span style="color:#2b91af">MemberRequest</span> request)<br />     {<br />     }<br /> }</div>
</p></div>
</p></div>
<p>So now I have a Request and a Service. But the request is meant to handle what the WCF service with three methods was doing, so how is this implemented.</p>
<p>Well rather than having three methods, I simply add the results to a collection and return the result.</p>
<p>The full implementation of this service looks like so:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a9183fd4-4503-4972-9722-ba904afedae2" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MemberService</span> : <span style="color:#2b91af">IService</span>&lt;<span style="color:#2b91af">MemberRequest</span>&gt;<br /> {<br />     <span style="color:#0000ff">private</span> <span style="color:#2b91af">IDocumentStore</span> DocumentStore { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</p>
<p>     <span style="color:#0000ff">public</span> MemberService(<span style="color:#2b91af">IDocumentStore</span> documentStore)<br />     {<br />         DocumentStore = documentStore;<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">object</span> Execute(<span style="color:#2b91af">MemberRequest</span> request)<br />     {<br />         <span style="color:#0000ff">var</span> result = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">MemberResponse</span>.<span style="color:#2b91af">Member</span>&gt;();</p>
<p>         <span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> session = DocumentStore.OpenSession())<br />         {<br />             <span style="color:#0000ff">if</span> (!<span style="color:#0000ff">string</span>.IsNullOrWhiteSpace(request.Id))<br />             {<br />                 <span style="color:#0000ff">var</span> member = session.Load&lt;<span style="color:#2b91af">Member</span>&gt;(request.Id);<br />                 <span style="color:#0000ff">if</span> (member != <span style="color:#0000ff">null</span>)<br />                     result.Add(member.TranslateTo&lt;<span style="color:#2b91af">MemberResponse</span>.<span style="color:#2b91af">Member</span>&gt;());<br />             }</p>
<p>             <span style="color:#0000ff">if</span> (!<span style="color:#0000ff">string</span>.IsNullOrWhiteSpace(request.Email))<br />             {<br />                 <span style="color:#0000ff">var</span> member = session.Query&lt;<span style="color:#2b91af">Member</span>, <span style="color:#2b91af">All_Members</span>&gt;()<br />                                     .SingleOrDefault(x =&gt; x.Email == request.Email);</p>
<p>                 <span style="color:#0000ff">if</span> (member != <span style="color:#0000ff">null</span>)<br />                     result.Add(member.TranslateTo&lt;<span style="color:#2b91af">MemberResponse</span>.<span style="color:#2b91af">Member</span>&gt;());<br />             }</p>
<p>             <span style="color:#0000ff">if</span> (!<span style="color:#0000ff">string</span>.IsNullOrWhiteSpace(request.OpenId))<br />             {<br />                 <span style="color:#0000ff">var</span> member = session.Query&lt;<span style="color:#2b91af">Member</span>, <span style="color:#2b91af">All_Members</span>&gt;()<br />                                     .SingleOrDefault(x =&gt; x.OpenId == request.OpenId);</p>
<p>                 <span style="color:#0000ff">if</span> (member != <span style="color:#0000ff">null</span> &amp;&amp; member.OpenId.Equals(request.OpenId, <span style="color:#2b91af">StringComparison</span>.Ordinal))<br />                     result.Add(member.TranslateTo&lt;<span style="color:#2b91af">MemberResponse</span>.<span style="color:#2b91af">Member</span>&gt;());<br />             }<br />         }</p>
<p>         <span style="color:#0000ff">return</span> <span style="color:#0000ff">new</span> <span style="color:#2b91af">MemberResponse</span> { Results = result };<br />     }<br /> }</div>
</p></div>
</p></div>
<p>So if I have any of the information defined on the request object, I simply query for it. </p>
<p>Now if you’re looking at the method you’re probably thinking the same thing I thought when I first looked at something similar. How the fark do I query that? <em>Well I’ll explain that soon. <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://www.philliphaydon.com/wp-content/uploads/2012/02/wlEmoticon-smile.png" /></em></p>
<p>The last thing missing tho is the Response. </p>
<p>There’s a couple of things to note, in the code above I actually translate my Model to a DTO, this is done using the ‘TranslateTo&lt;T&gt;’ method. This maps the object from 1 object to another, providing the two models share similar properties. This is exactly the same as AutoMapper except it doesn’t handle relationships. </p>
<p>It is possible to handle relationships however and I’ll demonstrate that in future posts.</p>
<p>Now one thing that annoyed me with WCF was getting null objects as a response. The approach used here is I have a MemberResponse which has a collection of Results.</p>
<p>The actual DTO looks like so:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:cce7bb85-7388-43ea-8f55-53c95c6833d9" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">MemberResponse</span> : <span style="color:#2b91af">IHasResponseStatus</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#2b91af">IEnumerable</span>&lt;<span style="color:#2b91af">Member</span>&gt; Results { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Member</span><br />     {<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Id { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> OpenId { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> DisplayName { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> FirstName { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> LastName { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Email { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#2b91af">ResponseStatus</span> ResponseStatus { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</div>
</p></div>
</p></div>
<p><strong>Note:</strong> I like to use nested classes for the results because I can name it what it is, and modify it without breaking any other Response classes I make in the future.</p>
<p>The response implements the interface IHasResponseStatus <em>(which I think needs to be renamed to ICanHazResponseStatus)</em> which provides the property ResponseStatus, this allows SS to attach it’s own information about the response such as exception information. </p>
<p>So rather than WCF where it just faults and throws exceptions and falls over and starts a fire, it just returns a response and gives you the information about it. AWESOME!</p>
<p>Also I’ve added the Member as a collection so that I can have multiple results if I need, maybe I want to find a user who has an OpenId of ‘xyz’ and an email of ‘abc’ so I can link them. No need to write yet ANOTHER WCF method. </p>
<h4>Configuration</h4>
<p>Configuration in WCF is always a pain in the ass, specially when dealing with message sizes, buffers, bindings and endpoints, so on and so forth somebody shoot me because WCF configuration is the bane of my existence.</p>
<p>Configuring SS is so easy that I over configured it to begin with. While configuring SS I realised I can remove Autofac, AutoMapper and a bunch of configuration code. The end result was the following:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a2f7cc88-522b-46de-b72d-7150bf911734" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px; white-space: nowrap"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Global</span> : System.Web.<span style="color:#2b91af">HttpApplication</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">QueryServiceAppHost</span> : <span style="color:#2b91af">AppHostBase</span><br />     {<br />         <span style="color:#0000ff">private</span> <span style="color:#0000ff">readonly</span> <span style="color:#2b91af">IContainerAdapter</span> _containerAdapter;</p>
<p>         <span style="color:#0000ff">public</span> QueryServiceAppHost(<span style="color:#2b91af">IDocumentStore</span> documentStore)<br />             : <span style="color:#0000ff">base</span>(<span style="color:#a31515">&quot;ITCompiler Query Services&quot;</span>, <span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">MemberService</span>).Assembly)<br />         {<br />             <span style="color:#0000ff">base</span>.Container.Register&lt;<span style="color:#2b91af">IDocumentStore</span>&gt;(documentStore);</p>
<p>             <span style="color:#0000ff">base</span>.SetConfig(<span style="color:#0000ff">new</span> <span style="color:#2b91af">EndpointHostConfig</span> { DebugMode = <span style="color:#0000ff">true</span> });<br />         }</p>
<p>         <span style="color:#0000ff">public</span> <span style="color:#0000ff">override</span> <span style="color:#0000ff">void</span> Configure(Funq.<span style="color:#2b91af">Container</span> container)<br />         {<br />             container.Adapter = _containerAdapter;<br />         }<br />     }</p>
<p>     <span style="color:#0000ff">private</span> <span style="color:#0000ff">static</span> <span style="color:#2b91af">IDocumentStore</span> DocumentStore { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> Application_Start()<br />     {<br />         DocumentStore = ConfigureRavenDb();</p>
<p>         (<span style="color:#0000ff">new</span> <span style="color:#2b91af">QueryServiceAppHost</span>(DocumentStore)).Init();<br />     }</p>
<p>     <span style="color:#0000ff">private</span> <span style="color:#0000ff">static</span> <span style="color:#2b91af">IDocumentStore</span> ConfigureRavenDb()<br />     {<br />         <span style="color:#0000ff">var</span> documentStore = <span style="color:#0000ff">new</span> <span style="color:#2b91af">DocumentStore</span><br />         {<br />             ConnectionStringName = <span style="color:#a31515">&quot;RavenDB&quot;</span>,<br />             DefaultDatabase = <span style="color:#a31515">&quot;ITCompiler&quot;</span><br />         }.Initialize();</p>
<p>         <span style="color:#2b91af">IndexCreation</span>.CreateIndexes(<span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">All_Members</span>).Assembly, documentStore);</p>
<p>         <span style="color:#0000ff">return</span> documentStore;<br />     }<br /> }</div>
</p></div>
</p></div>
<p>I would show you the original configuration I had for WCF but you would probably freak out and run. </p>
<p>But I’ve cut out Autofac, AutoMapper, and it’s really just ‘Configure RavenDB’ and ‘Initialize SS’</p>
<p>I didn’t touch the .config file, didn’t do anything special to setup SS, simply created a AppHost class and registered my Document Store.</p>
<h4>Querying the Services</h4>
<p>The last piece to the puzzle was querying the newly written services. Usually with WCF I configure the ChannelFactory and then inject a new Channel for every controller that needs specific services. </p>
<p>This caused a lot of configuration since each configured service has it’s own endpoint I ended up with a lot of code.</p>
<p>SS creates a reusable client for querying, and all it needs is the base URL of the service host.</p>
<p>I first started just by creating a new client like so:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:69d85fa5-1b7f-40d7-a8a4-da8506cefb3d" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> client = <span style="color:#0000ff">new</span> <span style="color:#2b91af">JsonServiceClient</span>(<span style="color:#a31515">&quot;http://localhost:9001&quot;</span>);</div>
</p></div>
</p></div>
<p><em><strong>       <br />Note:</strong> I use the JSON service client but there’s a few to choose from, XML, JSV, WCF, SOAP, etc.</em></p>
<p>Now when calling the client I can specify the response and pass in a request, so lets say I wanted to get a use by email address:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c999e2dd-f4b8-4b63-aa3d-7d9f0a309776" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">client.Send&lt;<span style="color:#2b91af">MemberResponse</span>&gt;(<span style="color:#0000ff">new</span> <span style="color:#2b91af">MemberRequest</span><br />                                 {<br />                                     Email = <span style="color:#a31515">&quot;bob@googlelymail.com&quot;</span><br />                                 });</div>
</p></div>
</p></div>
<p>This sends a request, and works out which service to invoke, passes in the request, and returns the result. </p>
<p>It couldn’t be easier. If I wanted to find a user by Id, just pass a request with just the Id.</p>
<p>Now I setup my application with two different projects, one for Queries, and one for Commands. So when I setup my client I just created two really simple classes:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7497351c-9d54-4d83-8096-6eea3501d74d" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">QueryServiceClient</span> : <span style="color:#2b91af">JsonServiceClient</span><br /> {<br />     <span style="color:#0000ff">public</span> QueryServiceClient(<span style="color:#0000ff">string</span> url) : <span style="color:#0000ff">base</span>(url) { }<br /> }</div>
</p></div>
</p></div>
<p>And another for Commands named CommandServiceClient.</p>
<p>Then I registered them in Autofac <em>(on the MVC site I’m still using Autofac)</em></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6f0cc280-4883-4367-b7c9-7a65b626a5c8" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">builder.RegisterType&lt;<span style="color:#2b91af">QueryServiceClient</span>&gt;()<br />         .WithParameter(<span style="color:#0000ff">new</span> <span style="color:#2b91af">NamedParameter</span>(<span style="color:#a31515">&quot;url&quot;</span>, QueryServicesUrl))<br />         .AsSelf().SingleInstance();</p>
<p> builder.RegisterType&lt;<span style="color:#2b91af">CommandServiceClient</span>&gt;()<br />         .WithParameter(<span style="color:#0000ff">new</span> <span style="color:#2b91af">NamedParameter</span>(<span style="color:#a31515">&quot;url&quot;</span>, CommandServicesUrl))<br />         .AsSelf().SingleInstance();</div>
</p></div>
</p></div>
<p>Now I can just inject those two service clients and reuse them over and over.</p>
<h4>Conclusion</h4>
<p>I had to change my way of thinking and to be honest, I threw in the towel pretty early on. But I stuck with it. I was lucky enough to have help from the creator himself, <a href="http://www.twitter.com/demisbellot" target="_blank">@demisbellot</a> in the <a href="http://jabbr.net/#/rooms/servicestack" target="_blank">JabbR ServiceSack</a> room.</p>
<p>He was kind enough to answer all my woes and put me on the right path, regardless of how silly my questions probably were. </p>
<p>After a little perseverance I’m now completely in love with Service Stack and I look forward to learning more of it’s capabilities around Error Handling, REST, and Messaging.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2012/02/service-stack-i-heart-you-my-conversion-from-wcf-to-ss/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>MVC 4 Project Templates are stupid</title>
		<link>http://www.philliphaydon.com/2012/02/mvc-4-project-templates-are-stupid/</link>
		<comments>http://www.philliphaydon.com/2012/02/mvc-4-project-templates-are-stupid/#comments</comments>
		<pubDate>Sun, 19 Feb 2012 11:29:56 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[Rant]]></category>
		<category><![CDATA[rant mvc4]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2012/02/mvc-4-project-templates-are-stupid/</guid>
		<description><![CDATA[&#60;begin rant&#62; Thought I would jump on the MVC 4 Beta bandwagon today, installed, create new project… And this is what I’m greeted with: &#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?&#62; &#60;packages&#62;   &#60;package id=&#34;AspNetMvc&#34; version=&#34;4.0.20126.16343&#34; /&#62;   &#60;package id=&#34;AspNetRazor.Core&#34; version=&#34;2.0.20126.16343&#34; /&#62;   &#60;package id=&#34;AspNetWebApi&#34; version=&#34;4.0.20126.16343&#34; /&#62;   &#60;package id=&#34;AspNetWebApi.Core&#34; version=&#34;4.0.20126.16343&#34; /&#62;   &#60;package id=&#34;AspNetWebPages.Core&#34; version=&#34;2.0.20126.16343&#34; /&#62;   &#60;package id=&#34;EntityFramework&#34; version=&#34;4.1.10331.0&#34; /&#62;   &#60;package id=&#34;jQuery&#34; version=&#34;1.6.2&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>&lt;begin rant&gt;</p>
<p>Thought I would jump on the MVC 4 Beta bandwagon today, installed, create new project… And this is what I’m greeted with:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e4553a32-1afa-4d10-8e08-148869ad54d6" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">&lt;?</span><span style="color:#a31515">xml</span><span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.0</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">encoding</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">utf-8</span>&quot;<span style="color:#0000ff">?&gt;</span><br /> <span style="color:#0000ff">&lt;</span><span style="color:#a31515">packages</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">AspNetMvc</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">AspNetRazor.Core</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">2.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">AspNetWebApi</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">AspNetWebApi.Core</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">AspNetWebPages.Core</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">2.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">EntityFramework</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.1.10331.0</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">jQuery</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.6.2</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">jQuery.Ajax.Unobtrusive</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">2.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">jQuery.UI.Combined</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.8.11</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">jQuery.Validation</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.8.1</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">jQuery.Validation.Unobtrusive</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">2.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">knockoutjs</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">2.0.0.0</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">Microsoft.Web.Infrastructure</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.0.0.0</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">Microsoft.Web.Optimization</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.0.0-beta</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">Modernizr</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">2.0.6</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Json</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Net.Http</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">2.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Net.Http.Formatting</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Web.Http.Common</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Web.Providers</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.1</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Web.Providers.Core</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.0</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">&lt;/</span><span style="color:#a31515">packages</span><span style="color:#0000ff">&gt;</span></div>
</p></div>
</p></div>
<p>This is absolutely stupid…</p>
<p>I select EMPTY project template.</p>
<p><strong><font size="5">EMPTY</font></strong></p>
<p>So I removed the things I don’t need to begin with:</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f2847f08-6e89-4d78-a7f4-4e8893def824" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">&lt;?</span><span style="color:#a31515">xml</span><span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.0</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">encoding</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">utf-8</span>&quot;<span style="color:#0000ff">?&gt;</span><br /> <span style="color:#0000ff">&lt;</span><span style="color:#a31515">packages</span><span style="color:#0000ff">&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">AspNetMvc</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">AspNetRazor.Core</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">2.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">AspNetWebApi</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">AspNetWebApi.Core</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">AspNetWebPages.Core</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">2.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">Microsoft.Web.Infrastructure</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.0.0.0</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">Microsoft.Web.Optimization</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.0.0-beta</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Json</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Net.Http</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">2.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Net.Http.Formatting</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Web.Http.Common</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">4.0.20126.16343</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Web.Providers</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.1</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">  &lt;</span><span style="color:#a31515">package</span><span style="color:#0000ff"> </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">System.Web.Providers.Core</span>&quot;<span style="color:#0000ff"> </span><span style="color:#ff0000">version</span><span style="color:#0000ff">=</span>&quot;<span style="color:#0000ff">1.0</span>&quot;<span style="color:#0000ff"> /&gt;</span><br /> <span style="color:#0000ff">&lt;/</span><span style="color:#a31515">packages</span><span style="color:#0000ff">&gt;</span></div>
</p></div>
</p></div>
</p>
<p>But beyond that I don’t know if the rest can be removed or if it’s required by MVC 4. Time for some trial and error <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-sadsmile" alt="Sad smile" src="http://www.philliphaydon.com/wp-content/uploads/2012/02/wlEmoticon-sadsmile.png" /></p>
<p>I really hope MS decide to create a REAL empty project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2012/02/mvc-4-project-templates-are-stupid/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RavenDB &#8211; Searching across multiple properties</title>
		<link>http://www.philliphaydon.com/2012/01/ravendb-searching-across-multiple-properties/</link>
		<comments>http://www.philliphaydon.com/2012/01/ravendb-searching-across-multiple-properties/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 13:09:57 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[RavenDB]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[Map]]></category>
		<category><![CDATA[Searching]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2012/01/ravendb-searching-across-multiple-properties/</guid>
		<description><![CDATA[Ayende recently posted about Orders Search in RavenDB which got me a little bit excited, since I was pondering how I would do searching in RavenDB without having Full Text Searching from SQL Server. So digging into it I wanted to try it out for myself how to use it. Given the model:     public class [...]]]></description>
			<content:encoded><![CDATA[<p>Ayende recently posted about <a href="http://ayende.com/blog/152833/orders-search-in-ravendb" target="_blank">Orders Search</a> in RavenDB which got me a little bit excited, since I was pondering how I would do searching in RavenDB without having Full Text Searching from SQL Server.</p>
<p>So digging into it I wanted to try it out for myself how to use it. Given the model:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e098d48a-0962-4d48-bd3c-ba9758461f02" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">    <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Post</span><br />     {<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">int</span> Id { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Title { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Description { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">IEnumerable</span>&lt;<span style="color:#0000ff">string</span>&gt; Tags { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">DateTime</span> DatePosted { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     }</div>
</p></div>
</p></div>
<p>I’ve setup 10 posts (<a href="http://pastie.org/3200462" target="_blank">click here for the insert pastie</a>) just with some really basic data. </p>
<p>So I’m going to detail here all the data that I’ve setup.</p>
<h4>Tags</h4>
<table border="0" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td valign="top" width="200"><strong>Tag Name</strong></td>
<td valign="top" width="200"><strong># of Posts Containing Tag</strong></td>
</tr>
<tr>
<td valign="top" width="200">html</td>
<td valign="top" width="200">3</td>
</tr>
<tr>
<td valign="top" width="200">c#</td>
<td valign="top" width="200">6</td>
</tr>
<tr>
<td valign="top" width="200">ravendb</td>
<td valign="top" width="200">4</td>
</tr>
<tr>
<td valign="top" width="200">nhibernate</td>
<td valign="top" width="200">3</td>
</tr>
<tr>
<td valign="top" width="200">javascript</td>
<td valign="top" width="200">1</td>
</tr>
<tr>
<td valign="top" width="200">coffeescript</td>
<td valign="top" width="200">2</td>
</tr>
<tr>
<td valign="top" width="200">less</td>
<td valign="top" width="200">3</td>
</tr>
<tr>
<td valign="top" width="200">search</td>
<td valign="top" width="200">6</td>
</tr>
<tr>
<td valign="top" width="200">closures</td>
<td valign="top" width="200">1</td>
</tr>
<tr>
<td valign="top" width="200">jquery</td>
<td valign="top" width="200">2</td>
</tr>
<tr>
<td valign="top" width="200">css</td>
<td valign="top" width="200">1</td>
</tr>
<tr>
<td valign="top" width="200">queryover</td>
<td valign="top" width="200">2</td>
</tr>
<tr>
<td valign="top" width="200">mapreduce</td>
<td valign="top" width="200">4</td>
</tr>
</tbody>
</table>
<h4>Titles</h4>
<p>Nothing interesting, just ‘Test Post X’ for each one to identify them.</p>
<h4>Description</h4>
<p>Basically for this testing I’ve taken the blog post names of a few things from Google Reader, that some-way relate to the tags above. Take a look at the script mentioned above to see the data.</p>
<h4>Creating the Index</h4>
<p>So the first thing I want to do is create a Map with a Reduce Result, but we aren’t going to add the Reduce to the index, since we don’t need it to store that data or do anything with it. We purely want the Reduce Result that matches the map, so that we can query against it.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c58ee97b-64f7-4a26-bf75-9a98da20e32d" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Post_Search</span> : <span style="color:#2b91af">AbstractIndexCreationTask</span>&lt;<span style="color:#2b91af">Post</span>, <span style="color:#2b91af">Post_Search</span>.<span style="color:#2b91af">ReduceResult</span>&gt;<br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">ReduceResult</span><br />     {<br />         <span style="color:#0000ff">public</span> <span style="color:#0000ff">object</span>[] SearchQuery { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />         <span style="color:#0000ff">public</span> <span style="color:#2b91af">DateTime</span> DatePosted { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     }</p>
<p>     <span style="color:#0000ff">public</span> Post_Search()<br />     {<br />         Map = posts =&gt; <br />             <span style="color:#0000ff">from</span> post <span style="color:#0000ff">in</span> posts<br />             <span style="color:#0000ff">select</span> <span style="color:#0000ff">new</span> <br />             {<br />                 SearchQuery = post.Tags.Concat(<span style="color:#0000ff">new</span>[]<br />                                                 {<br />                                                     post.Description,<br />                                                     post.Title<br />                                                 }),<br />                 DatePosted = post.DatePosted<br />             };<br />     }<br /> }</div>
</p></div>
</p></div>
<p>This index is a little bit funky, and differs from what Ayende showed in his example. I wanted to try something a little different.</p>
<p>In my scenario I have a collection of Tag’s that I wanted to include in the search, this the tags is already a collection, I concatenate the additional array of items I want to add into the map.</p>
<p>The SearchQuery is the property that we will search against, while the DatePosted wont be included in the Search, but is there to provide additional filtering on my search.</p>
<h4>Querying</h4>
<p>Querying threw me off at first, because in order to query against this index, we have to specify the ReduceResult class.</p>
<p>So we end up with the starting of our query looking like this:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:62658a9c-5370-432d-973c-19b07c6fce09" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> result = session.Query&lt;<span style="color:#2b91af">Post_Search</span>.<span style="color:#2b91af">ReduceResult</span>, <span style="color:#2b91af">Post_Search</span>&gt;()</div>
</p></div>
</p></div>
<p>At first I thought “oh, that means we end up with a ReduceResult result type, this is pointless and useless”. But I commented on Ayende’s blog post and it turns out we can call ‘As&lt;T&gt;’ on the query.</p>
<p>Without filtering the results just yet, our query would look like the following:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6351c3b4-0cdb-4d8d-b2e4-52ed10c8bc88" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> result = session.Query&lt;<span style="color:#2b91af">Post_Search</span>.<span style="color:#2b91af">ReduceResult</span>, <span style="color:#2b91af">Post_Search</span>&gt;()<br />                     .As&lt;<span style="color:#2b91af">Post</span>&gt;()<br />                     .ToList();</div>
</p></div>
</p></div>
<p>So if I run this up now, for a quick test, I should get 10 results back of type Post</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/01/image.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/01/image_thumb.png" width="515" height="318" /></a></p>
<p>Great!</p>
<p>So now I need to begin filtering out the results. To begin with I’m doing to use the .Where extension. Since we are looking an object array, we can’t directly compare it to a string, but if we explicitly cast it to an object we can look for:</p>
<p>coffeescript expecting 2 results:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:52f8cdc9-d473-42bb-99e3-669a8f43cb8a" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> result = session.Query&lt;<span style="color:#2b91af">Post_Search</span>.<span style="color:#2b91af">ReduceResult</span>, <span style="color:#2b91af">Post_Search</span>&gt;()<br />                     .Where(x =&gt; x.SearchQuery == (<span style="color:#0000ff">object</span>)<span style="color:#a31515">&quot;coffeescript&quot;</span>)<br />                     .As&lt;<span style="color:#2b91af">Post</span>&gt;()<br />                     .ToList();</div>
</p></div>
</p></div>
<p>&#160;</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/01/image1.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/01/image_thumb1.png" width="531" height="97" /></a></p>
<p>How about javascript expecting 2 (1 via Tag and 1 via the Description)</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/01/image2.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/01/image_thumb2.png" width="503" height="90" /></a></p>
<p>Oh, we didn’t get the desired result… This is because the search is only doing a search on an exact match. Since the search value is an exact match of the tag, the result is returned.</p>
<p>So to fix this we need to make the index analysed. Adding to the index:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f54ff287-c280-4323-8097-b2759334cfbd" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">Index(x =&gt; x.SearchQuery, <span style="color:#2b91af">FieldIndexing</span>.Analyzed);</div>
</p></div>
</p></div>
<p>If we run the exact same query again:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/01/image3.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/01/image_thumb3.png" width="546" height="121" /></a></p>
<p>Now we get 2 results.</p>
<p>Now to try something a little bit different, using ‘Search’, if we wanted to search for something like mvc which happens to only be in the description, rather than using ‘Where’ like shown above, we can use ‘Search’ like so:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9095a5dd-4d84-4615-bc50-e071606feb39" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> result = session.Query&lt;<span style="color:#2b91af">Post_Search</span>.<span style="color:#2b91af">ReduceResult</span>, <span style="color:#2b91af">Post_Search</span>&gt;()<br />                     .Search(x =&gt; x.SearchQuery, <span style="color:#a31515">&quot;mvc&quot;</span>)<br />                     .As&lt;<span style="color:#2b91af">Post</span>&gt;()<br />                     .ToList();</div>
</p></div>
</p></div>
<p>This will give us the same result, except it looks much cleaner</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/01/image4.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/01/image_thumb4.png" width="497" height="91" /></a></p>
<p>Now there’s 1 catch I’ve found with this, which is searching is always an exact match. I’m not sure (no research done into lucene yet) if lucene has the ability to do a wild-card type search similar to SQL like: ‘%mvc%’, but you can get suggestions from this.</p>
<p>For example if I search for ‘coffee’ rather than ‘coffeescript’ I would expect all documents containing ‘coffee’ to be returned. This doesn’t happen. It does give you suggestions though. </p>
<p>Looking at the management studio for ‘coffee’ :</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/01/image5.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/01/image_thumb5.png" width="350" height="181" /></a></p>
<p><em>Side Comment: I think it would be cool if RavenDB provided the ability to have say include suggestions, like:</em></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b1921c36-651e-4bbc-95f9-408db15460bd" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> result = session.Query&lt;<span style="color:#2b91af">Post_Search</span>.<span style="color:#2b91af">ReduceResult</span>, <span style="color:#2b91af">Post_Search</span>&gt;()<br />                     .Search(x =&gt; x.SearchQuery, <span style="color:#a31515">&quot;coffee&quot;</span>)<br />                     .<span style="color:#ff0000">IncludeAllSuggestions</span>()<br />                     .As&lt;<span style="color:#2b91af">Post</span>&gt;()<br />                     .ToList();</div>
</p></div>
</p></div>
<p><em>Or other variations such as:</em></p>
<p><em>.Suggestions.IncludeAll()</em></p>
<p><em>.Suggestions.IncludeTop(3)</em></p>
<p><em></em></p>
<p><em>.Suggestions.IncludeAll(WhenResults.AreEmpty)</em></p>
<p><em></em></p>
<p><em>.Suggestions.IncludeAll(WhenResults.AreLessThan, 10)</em></p>
<p><em>Hopefully you can work out where I’m going with this?</em></p>
<p>Ok continuing on. Why do we need to call ‘As&lt;T&gt;()’ on the query?</p>
<p>Well from my understanding of how RavenDB works is like this, when we create an index, it’s creating a sub-set of data that points to the document in RavenDB.</p>
<p>For example I have all those documents inserted (<a href="http://pastie.org/3200462" target="_blank">link for the lazy</a>), and these are all stored like so:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/01/image6.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/01/image_thumb6.png" width="377" height="294" /></a></p>
<p>When we created the index with the following Map:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ee69f7f8-8e43-473a-ab8e-4771526286f0" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">Map = posts =&gt; <br />     <span style="color:#0000ff">from</span> post <span style="color:#0000ff">in</span> posts<br />     <span style="color:#0000ff">select</span> <span style="color:#0000ff">new</span> <br />     {<br />         SearchQuery = post.Tags.Concat(<span style="color:#0000ff">new</span>[]<br />                                         {<br />                                             post.Description,<br />                                             post.Title<br />                                         }),<br />         DatePosted = post.DatePosted<br />     };</div>
</p></div>
</p></div>
<p>It basically created an index that looks like this, for the data above:</p>
<table style="width: 590px; height: 75px" border="0" cellspacing="0" cellpadding="2" width="591">
<tbody>
<tr>
<td valign="top" rowspan="2" width="50">posts/2</td>
<td valign="top" width="540">SearchQuery: [&quot;c#&quot;, &quot;nhibernate&quot;, &quot;search&quot;, &quot;queryover&quot;, &quot;Benjamin Day slides us into &quot;How to be a C# ninja in 10 easy steps&quot;&quot;, &quot;Test Post 2&quot;</td>
</tr>
<tr>
<td valign="top">DatePosted: &quot;2012-01-02T00:00:00.0000000&quot;</td>
</tr>
</tbody>
</table>
<p>So the index actually points directly to a Document in RavenDB, when we search against the index, if a match is found, the index returns the Id ‘posts/2’ back, and that knows to go to the posts collection and grab the document with Id 2.</p>
<p>The problem with the query is we need to specify an object to query against.</p>
<p>So we introduced the ReduceResult (<em>not sure on this naming but I took it from Ayende’s blog</em>), this allows us to specify the Properties we defined in our index, as search criteria, but now our query is expecting ReduceResult:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/01/image7.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/01/image_thumb7.png" width="633" height="99" /></a></p>
<p>By specifying as we are telling the query that our result is going to be a type of ‘Post’:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/01/image8.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/01/image_thumb8.png" width="601" height="113" /></a></p>
<h4>Conclusion</h4>
<p>This functionality is really cool, it allows us to easily search against multiple different properties without having to create messy conjunctions in our LINQ. If we were to attempt to do this without an index, we would probably end up writing something like:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ebee481a-84e0-4313-9006-bf4914d7b06d" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> result = session.Query&lt;<span style="color:#2b91af">Post</span>&gt;()<br />                     .Where(x =&gt;<br />                             x.Description.Contains(<span style="color:#a31515">&quot;c#&quot;</span>)<br />                             ||<br />                             x.Tags.Any(y =&gt; y == <span style="color:#a31515">&quot;c#&quot;</span>)<br />                             ||<br />                             x.Title.Contains(<span style="color:#a31515">&quot;c#&quot;</span>)<br />                         )<br />                     .ToList();</div>
</p></div>
</p></div>
<p>And really, that’s just nasty… Specially considering we get the same results for writing more readable code:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2012/01/image9.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2012/01/image_thumb9.png" width="442" height="264" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2012/01/ravendb-searching-across-multiple-properties/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RavenDB &#8211; Map Reduce</title>
		<link>http://www.philliphaydon.com/2011/12/ravendb-map-reduce/</link>
		<comments>http://www.philliphaydon.com/2011/12/ravendb-map-reduce/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 13:18:55 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[RavenDB]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2011/12/ravendb-map-reduce/</guid>
		<description><![CDATA[So, learning Map Reduce in RavenDB I decided that to take what I learnt from the index created in my previous post. I think I picked something rather difficult to begin with, but I’ve succeeded Given a document Article which has a collection of Tags. I want to get a Count of each Tag across [...]]]></description>
			<content:encoded><![CDATA[<p>So, learning Map Reduce in RavenDB I decided that to take what I learnt from the index created in my <a href="http://www.philliphaydon.com/2011/12/ravendb-inheritance-revisited/" target="_blank">previous post</a>. I think I picked something rather difficult to begin with, but I’ve succeeded <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://www.philliphaydon.com/wp-content/uploads/2011/12/wlEmoticon-smile1.png" /></p>
<p>Given a document Article which has a collection of Tags.</p>
<p>I want to get a Count of each Tag across all Articles.</p>
<p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:04420726-90d7-44e4-971c-b27806e72f3e" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Content</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">int</span> Id { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Title { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#2b91af">IEnumerable</span>&lt;<span style="color:#2b91af">Tag</span>&gt; Tags { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</p>
<p> <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Tag</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Name { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</div>
</p></div>
</p></div>
</p>
<p><em>Note: Tag is it’s own class because I added additional properties to it. </em></p>
<p>Now I insert some data:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d9ccd028-4730-41ac-bd15-bf927c5ae357" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> session = documentStore.OpenSession())<br /> {<br />     session.Store(<span style="color:#0000ff">new</span> <span style="color:#2b91af">Content</span><br />     {<br />         Title = <span style="color:#a31515">&quot;Test Title for a Video&quot;</span>,<br />         Tags = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">Tag</span>&gt;<br />         {<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;c#&quot;}</span>,<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;autofac&quot;}</span>,<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;asp.net&quot;}</span>,<br />         }<br />     });<br />     session.Store(<span style="color:#0000ff">new</span> <span style="color:#2b91af">Content</span><br />     {<br />         Title = <span style="color:#a31515">&quot;Test Title for an Article&quot;</span>,<br />         Tags = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">Tag</span>&gt;<br />         {<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;c#&quot;}</span>,<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;nhibernate&quot;}</span>,<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;fluent-nhibernate&quot;}</span>,<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;mvc&quot;}</span><br />         }<br />     });<br />     session.Store(<span style="color:#0000ff">new</span> <span style="color:#2b91af">Content</span><br />     {<br />         Title = <span style="color:#a31515">&quot;Test Title for an Article&quot;</span>,<br />         Tags = <span style="color:#0000ff">new</span> <span style="color:#2b91af">List</span>&lt;<span style="color:#2b91af">Tag</span>&gt;<br />         {<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;ravendb&quot;}</span>,<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;asp.net&quot;}</span>,<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;autofac&quot;}</span>,<br />             <span style="color:#0000ff">new</span> <span style="color:#2b91af">Tag</span>() {Name = <span style="color:#a31515">&quot;c#&quot;}</span><br />         }<br />     });</p>
<p>     session.SaveChanges();<br /> }</div>
</p></div>
</p></div>
<p>&#160;</p>
<p>So I’m expecting a count of:</p>
<ul>
<li>3 x c#</li>
<li>2 x autofac</li>
<li>2 x asp.net</li>
<li>1 x ravendb</li>
<li>1 x mvc</li>
<li>1 x nhibernate</li>
<li>1 x fluent-nhibernate</li>
<p>I’m going to pull these out with a defined type rather than dynamic/object, so I’ve created a new class with Count and Name:</ul>
<ul>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:87501ae5-1b35-4beb-a7e4-8d9cc8d6392c" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">TagResult</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">int</span>      Count   { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span>   Name    { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</div>
</p></div>
</p></div>
</ul>
<p>So creating a new Index:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1d6733ce-4692-46d8-b4dc-cfb8b41f153d" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">All_Tags</span> : <span style="color:#2b91af">AbstractMultiMapIndexCreationTask</span>&lt;<span style="color:#2b91af">TagResult</span>&gt;<br /> {<br />     <span style="color:#0000ff">public</span> All_Tags()<br />     {<br />     }<br /> }</div>
</p></div>
</p></div>
<p>The first thing I need to do is map out ONLY the Tag’s, when I select out the Tag’s, I’m also going to include another field called Count, with a default value of 1. This is so I can re-use it to sum the total number of times the tag is used.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ec9d7429-176e-4338-8b07-aa6113df183d" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">AddMap&lt;<span style="color:#2b91af">Content</span>&gt;(contents =&gt; <span style="color:#0000ff">from</span> content <span style="color:#0000ff">in</span> contents<br />                             <span style="color:#0000ff">from</span> tag <span style="color:#0000ff">in</span> content.Tags<br />                             <span style="color:#0000ff">select</span> <span style="color:#0000ff">new</span><br />                             {<br />                                 Name = tag.Name,<br />                                 Count = 1<br />                             });</div>
</p></div>
</p></div>
<p>This would give me a result that contains duplicates for the tags. Along the lines of:</p>
<table border="1" cellspacing="0" cellpadding="2" width="398">
<tbody>
<tr>
<td valign="top" width="203">c#</td>
<td valign="top" width="193">1</td>
</tr>
<tr>
<td valign="top" width="204">c#</td>
<td valign="top" width="192">1</td>
</tr>
<tr>
<td valign="top" width="205">c#</td>
<td valign="top" width="192">1</td>
</tr>
<tr>
<td valign="top" width="205">autofac</td>
<td valign="top" width="192">1</td>
</tr>
<tr>
<td valign="top" width="205">autofac</td>
<td valign="top" width="192">1</td>
</tr>
<tr>
<td valign="top" width="205">asp.net</td>
<td valign="top" width="192">1</td>
</tr>
<tr>
<td valign="top" width="205">asp.net</td>
<td valign="top" width="192">1</td>
</tr>
<tr>
<td valign="top" width="205">ravendb</td>
<td valign="top" width="192">1</td>
</tr>
<tr>
<td valign="top" width="205">mvc</td>
<td valign="top" width="192">1</td>
</tr>
<tr>
<td valign="top" width="205">nhibernate</td>
<td valign="top" width="192">1</td>
</tr>
<tr>
<td valign="top" width="205">fluent-nhibernate</td>
<td valign="top" width="192">1</td>
</tr>
</tbody>
</table>
<p>So what I need to do in the Reduce, is group the tags together by their Name.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f2f0fd2a-3036-4bd4-a39b-7d0ee628b4d0" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;">Reduce = results =&gt; <span style="color:#0000ff">from</span> result <span style="color:#0000ff">in</span> results<br />                     <span style="color:#0000ff">group</span> result <span style="color:#0000ff">by</span> result.Name <span style="color:#0000ff">into</span> tag<br />                     <span style="color:#0000ff">select</span> <span style="color:#0000ff">new</span><br />                     {<br />                         Count = tag.Sum(x =&gt; x.Count),<br />                         Name = tag.Key,<br />                     };</div>
</p></div>
</p></div>
<p>So here, I group all the tags together by their name, but I also sum the ‘count’ value together to get the total number of times the tag is used.</p>
<p>Now run up the app and view the index:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image19.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2011/12/image_thumb19.png" width="593" height="358" /></a></p>
<p>Now if I query the index:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image20.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2011/12/image_thumb20.png" width="553" height="319" /></a></p>
<p>Awesome. Now to query this, I have to use the TagResult class defined previously, and the All_Tags index just created.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a80a8491-8c43-4aaf-bf8e-e013a43b7278" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> session = documentStore.OpenSession())<br /> {<br />     <span style="color:#0000ff">var</span> result = session.Query&lt;<span style="color:#2b91af">TagResult</span>, <span style="color:#2b91af">All_Tags</span>&gt;()<br />                         .ToList();</p>
<p>     <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> tag <span style="color:#0000ff">in</span> result)<br />     {<br />         <span style="color:#2b91af">Console</span>.WriteLine(tag.Count + <span style="color:#a31515">&quot; x &quot;</span> + tag.Name);<br />     }</p>
<p>     session.SaveChanges();<br /> }</div>
</p></div>
</p></div>
<p>Running this I get the following result:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image21.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2011/12/image_thumb21.png" width="196" height="125" /></a></p>
<p>The results I expected previously. </p>
<p>So there you have it. Map Reduce. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2011/12/ravendb-map-reduce/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RavenDB Inheritance&#8211;Revisited</title>
		<link>http://www.philliphaydon.com/2011/12/ravendb-inheritance-revisited/</link>
		<comments>http://www.philliphaydon.com/2011/12/ravendb-inheritance-revisited/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 13:35:21 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[RavenDB]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2011/12/ravendb-inheritancerevisited/</guid>
		<description><![CDATA[So after my initial post on RavenDB Inheritance, and the issue I had with polymorphic queries, and seeking help from the guys in JabbR and the RavenDB Google Group, Ayende ended up doing a screen cast with me where he solved all my problems. One of the things he asked me was what I was [...]]]></description>
			<content:encoded><![CDATA[<p>So after my initial post on <a href="http://www.philliphaydon.com/2011/12/ravendb-inheritance/" target="_blank">RavenDB Inheritance</a>, and the issue I had with polymorphic queries, and seeking help from the guys in <a href="http://jabbr.net" target="_blank">JabbR</a> and the <a href="http://groups.google.com/group/ravendb/browse_thread/thread/c71df8f1cd92e04c" target="_blank">RavenDB Google Group</a>, <a href="http://ayende.com/blog/" target="_blank">Ayende</a> ended up doing a screen cast with me where he solved all my problems.</p>
<p>One of the things he asked me was what I was trying to achieve by having a polymorphic query, which was a very good question, something I hadn’t really thought about.</p>
<p>The problem I was trying to solve was actually displaying search results. </p>
<h3>The Problem</h3>
<p>So I’m working on a personal project, and I need to display a few things which are similar, but different. There’s 3 different types but I’ll use two to keep it simple. I’ve also cut out most of the properties.</p>
<p>So I have an abstract class Content, with two derived classes, Article and Video.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d4143260-3181-45c4-8e29-5100ade2fd35" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">abstract</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Content</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">int</span> Id { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Title { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#2b91af">DateTime</span> DatePublished { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</p>
<p> <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Article</span> : <span style="color:#2b91af">Content</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> HtmlContent { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</p>
<p> <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Video</span> : <span style="color:#2b91af">Content</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> Description { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">string</span> VideoUrl { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</div>
</p></div>
</p></div>
<p>Then I initialize the DocumentStore and store a couple of documents.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6c5cdcff-4ca5-4183-9d41-40590b98d1fc" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> documentStore =<br />     (<span style="color:#0000ff">new</span> <span style="color:#2b91af">DocumentStore</span>()<br />             {<br />                 Url = <span style="color:#a31515">&quot;http://localhost:8080&quot;</span><br />             }).Initialize();</p>
<p> <span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> session = documentStore.OpenSession())<br /> {<br />     session.Store(<span style="color:#0000ff">new</span> <span style="color:#2b91af">Video</span><br />     {<br />         DatePublished = <span style="color:#2b91af">DateTime</span>.Now,<br />         Description = <span style="color:#a31515">&quot;Test Description for a Video&quot;</span>,<br />         Title = <span style="color:#a31515">&quot;Test Title for a Video&quot;</span>,<br />         VideoUrl = <span style="color:#a31515">&quot;http://www.youtube.com/watch?v=PGz9GokDkkg&quot;</span><br />     });</p>
<p>     session.Store(<span style="color:#0000ff">new</span> <span style="color:#2b91af">Article</span><br />     {<br />         DatePublished = <span style="color:#2b91af">DateTime</span>.Now,<br />         Title = <span style="color:#a31515">&quot;Test Title for an Article&quot;</span>,<br />         HtmlContent = <span style="color:#a31515">&quot;Some content for the article&#8230;&quot;</span><br />     });</p>
<p>     session.SaveChanges();<br /> }</div>
</p></div>
</p></div>
<p>This time I’m not using the Convention to store the two documents as ‘Content’, rather I’m allowing it to store them as what they are. This gives me a result in Raven like:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image13.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2011/12/image_thumb13.png" width="569" height="135" /></a></p>
<p>Now if I query for Video:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d47138bf-a045-40c7-a881-158fff07d0a1" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> session = documentStore.OpenSession())<br /> {<br />     <span style="color:#0000ff">var</span> result = session.Query&lt;<span style="color:#2b91af">Video</span>&gt;().ToList();<br />     <br />     <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> content <span style="color:#0000ff">in</span> result)<br />     {<br />         <span style="color:#2b91af">Console</span>.WriteLine(content.Id);<br />         <span style="color:#2b91af">Console</span>.WriteLine(content.Title);<br />     }<br /> }</div>
</p></div>
</p></div>
<p>I get the output of the first Document.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image14.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2011/12/image_thumb14.png" width="194" height="42" /></a></p>
<p>Likewise if I select ‘Article’ I get the Article document that I previously stored. </p>
<p>So how do I get a list of Content?</p>
<h3>The Solution</h3>
<p>So, the solution is really, really easy, it’s an index.</p>
<p>The first thing Ayende showed me was creating the index in RavenDB Management Studio, then he showed me doing it in code. I’m just going to show it done in code.</p>
<p>I created a class called ‘All_Content’ (with an underscore) like so:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:22895285-ad0f-4f8a-aee3-941be029b305" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">All_Content</span> : <span style="color:#2b91af">AbstractMultiMapIndexCreationTask</span><br /> {<br />     <span style="color:#0000ff">public</span> All_Content()<br />     {<br />         AddMap&lt;<span style="color:#2b91af">Article</span>&gt;(articles =&gt; <span style="color:#0000ff">from</span> article <span style="color:#0000ff">in</span> articles<br />                                     <span style="color:#0000ff">select</span> <span style="color:#0000ff">new</span><br />                                                 {<br />                                                     article.Id,<br />                                                     article.Title,<br />                                                     article.DatePublished<br />                                                 });<br />         AddMap&lt;<span style="color:#2b91af">Video</span>&gt;(videos =&gt; <span style="color:#0000ff">from</span> video <span style="color:#0000ff">in</span> videos<br />                                 <span style="color:#0000ff">select</span> <span style="color:#0000ff">new</span><br />                                             {<br />                                                 video.Id,<br />                                                 video.Title,<br />                                                 video.DatePublished<br />                                             });<br />     }<br /> }</div>
</p></div>
</p></div>
<p><em><u>It reminds me of writing a Union View in SQL Server in some ways.</u></em> It basically maps to the Articles and Videos, but only selects the things I need. Those of which would actually be displayed to the screen or that are common between the two document types. </p>
<p>Then I create the index right after I initialize the DocumentStore:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:25a91885-4105-48a6-a61a-122396738e3d" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#2b91af">IndexCreation</span>.CreateIndexes(<span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">All_Content</span>).Assembly, documentStore);</div>
</p></div>
</p></div>
<p>This creates the index in RavenDB for me. </p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image15.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2011/12/image_thumb15.png" width="410" height="148" /></a></p>
<p>As you can see, even tho I specified the class index with an underscore, it converts it to All/Content, that’s a really nice way of presenting it. I think it will go well for being able to create descriptive indexes in the future.</p>
<p>And the index itself:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image16.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2011/12/image_thumb16.png" width="559" height="331" /></a></p>
<p>Now I need to actually query against the index. That’s also really really easy. When I specify the type, I can specify the index with it:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bce100ee-db39-4015-942f-37d0fcee7ad4" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">using</span> (<span style="color:#0000ff">var</span> session = documentStore.OpenSession())<br /> {<br />     <span style="color:#0000ff">var</span> result = session.Query&lt;<span style="color:#2b91af">Content</span>, <span style="color:#2b91af">All_Content</span>&gt;().ToList();<br />     <br />     <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> content <span style="color:#0000ff">in</span> result)<br />     {<br />         <span style="color:#2b91af">Console</span>.WriteLine(content.Id);<br />         <span style="color:#2b91af">Console</span>.WriteLine(content.Title);<br />     }<br /> }</div>
</p></div>
</p></div>
<p>Now when I run this I get the output:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image17.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2011/12/image_thumb17.png" width="225" height="81" /></a></p>
<p>Awesome!</p>
<p>The really interesting thing I found is that if I look at what’s returned:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image18.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.philliphaydon.com/wp-content/uploads/2011/12/image_thumb18.png" width="465" height="133" /></a></p>
<p>Are the correct CLR types that I originally defined. So I haven’t lost all the additional fields by not defining them. I’m still learning but for now I assume it allows those fields to be searchable.</p>
<h3>Extras</h3>
<p>One of the additional things Ayende showed me was that you can include other documents that don’t inherit from the base type. You can include those in the index map, and then rather than returning a concrete type, you can specify object, or dynamic.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:723fa54e-8c1e-414f-ab77-b9c2d667a1c5" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; overflow: auto; padding: 2px 5px;"><span style="color:#0000ff">var</span> result = session.Query&lt;<span style="color:#0000ff">dynamic</span>, <span style="color:#2b91af">All_Content</span>&gt;().ToList();</div>
</p></div>
</p></div>
<p>RavenDB is really powerful. It’s truly amazing, and so much nicer to work with in .NET than other document databases like MongoDB.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2011/12/ravendb-inheritance-revisited/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

