<?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/" rel="self" type="application/rss+xml" />
	<link>http://www.philliphaydon.com</link>
	<description>Works on my pc.</description>
	<lastBuildDate>Tue, 21 Feb 2012 12:57:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<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>4</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>0</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>1</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>0</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>1</slash:comments>
		</item>
		<item>
		<title>RavenDB Inheritance</title>
		<link>http://www.philliphaydon.com/2011/12/ravendb-inheritance/</link>
		<comments>http://www.philliphaydon.com/2011/12/ravendb-inheritance/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 05:20:42 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[RavenDB]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2011/12/ravendb-inheritance/</guid>
		<description><![CDATA[Edit: Updated solution: http://www.philliphaydon.com/2011/12/ravendb-inheritance-revisited/ Continuing my learning of RavenDB, I wanted to see how it handled Inheritance. I found: http://ravendb.net/faq/polymorphic-indexes Which showed what to do allow you to select over all types of ‘Animal’ for the example shown. So I wanted to see what happens before and after using this method. So like the example [...]]]></description>
			<content:encoded><![CDATA[<p><em><font color="#ff0000">Edit: Updated solution: </font></em><a title="http://www.philliphaydon.com/2011/12/ravendb-inheritance-revisited/" href="http://www.philliphaydon.com/2011/12/ravendb-inheritance-revisited/"><em><font color="#ff0000">http://www.philliphaydon.com/2011/12/ravendb-inheritance-revisited/</font></em></a></p>
<p>Continuing my learning of RavenDB, I wanted to see how it handled Inheritance. </p>
<p>I found: <a href="http://ravendb.net/faq/polymorphic-indexes">http://ravendb.net/faq/polymorphic-indexes</a></p>
<p>Which showed what to do allow you to select over all types of ‘Animal’ for the example shown. So I wanted to see what happens before and after using this method.</p>
<p>So like the example shown I’ve created an Animal, with a Dog and Cat.</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:5103027d-87bc-4b5b-8f85-3f22f22a9fa3" 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">Animal</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> Name { <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">Dog</span> : <span style="color:#2b91af">Animal</span> { }<br /> <span style="color:#0000ff">public</span> <span style="color:#0000ff">class</span> <span style="color:#2b91af">Cat</span> : <span style="color:#2b91af">Animal</span> { }</div>
</p></div>
</p></div>
<p>Now if I insert a Dog and Cat:</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:e99f0b1b-6547-47ba-819b-b2b9cc97da45" 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">Dog</span>() { Name = <span style="color:#a31515">&quot;Test Dog&quot;</span> });<br />     session.Store(<span style="color:#0000ff">new</span> <span style="color:#2b91af">Cat</span>() { Name = <span style="color:#a31515">&quot;Test Cat&quot;</span> });</p>
<p>     session.SaveChanges();<br /> }</div>
</p></div>
</p></div>
<p>What’s stored in RavenDB is two separate documents, one for ‘dogs’ and one for ‘cats’.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image7.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_thumb7.png" width="527" height="109" /></a></p>
<p>If I include the Convention.</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:d8d84704-0e7b-4bd2-bdc8-5c0919175343" 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> documentConvention =<br />     <span style="color:#0000ff">new</span> <span style="color:#2b91af">DocumentConvention</span>()<br />         {<br />             FindTypeTagName =<br />                 type =&gt;<br />                     {<br />                         <span style="color:#0000ff">if</span> (<span style="color:#0000ff">typeof</span> (<span style="color:#2b91af">Animal</span>).IsAssignableFrom(type))<br />                             <span style="color:#0000ff">return</span> <span style="color:#a31515">&quot;animals&quot;</span>;<br />                         <span style="color:#0000ff">return</span> <span style="color:#2b91af">DocumentConvention</span>.DefaultTypeTagName(type);<br />                     }<br />         };</div>
</p></div>
</p></div>
<p><em>Note: You can do the conversion when the DocumentStore is initialized, I broke the two up so that it would fit easier into my blog. Otherwise it’s too nested and yucky.</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:96302d95-1d97-4482-a3ec-63bdb1a4739c" 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 />                 Conventions = documentConvention<br />             }).Initialize();</div>
</p></div>
</p></div>
<p>Now when I insert a Dog and Cat I get:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image8.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_thumb8.png" width="401" height="110" /></a></p>
<p>Awesome. If we look at the document however:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image9.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_thumb9.png" width="220" height="177" /></a></p>
<p>There is no information about it being a cat or dog, I thought it would add some sort of discriminator similar to how NHibernate works.</p>
<p>However, if we look at the Metadata tab:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image10.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_thumb10.png" width="412" height="194" /></a></p>
<p>We can see the CLR type is stored in the metadata so RavenDB knows what type to create when we query it.</p>
<p>This means if we query for ‘Animal’ we get a list of Dogs and Cats.</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:82ff679f-4f56-477f-9212-69aade2508b3" 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">Animal</span>&gt;();</p>
<p>     <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> animal <span style="color:#0000ff">in</span> result)<br />     {<br />         <span style="color:#2b91af">Console</span>.WriteLine(animal.Name);<br />     }<br /> }</div>
</p></div>
</p></div>
<p>&#160;</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image11.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_thumb11.png" width="375" height="131" /></a></p>
<p>However, if you wanted to query for just Dogs, 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:51adf224-e32f-45fd-9baf-7670a6407a1a" 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">Dog</span>&gt;().ToList();</div>
</p></div>
</p></div>
<p>It doesn’t seem to work <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/2011/12/wlEmoticon-sadsmile1.png" /></p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/image12.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_thumb12.png" width="437" height="104" /></a></p>
<p>I’m probably just doing something wrong, either way, the more I play with RavenDB. The more I love it. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2011/12/ravendb-inheritance/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Post a collection of ViewModel&#8217;s to a MVC Action with jQuery</title>
		<link>http://www.philliphaydon.com/2011/12/post-a-collection-of-viewmodels-to-a-mvc-action-with-jquery/</link>
		<comments>http://www.philliphaydon.com/2011/12/post-a-collection-of-viewmodels-to-a-mvc-action-with-jquery/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 12:13:37 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2011/12/post-a-collection-of-viewmodels-to-a-mvc-action-with-jquery/</guid>
		<description><![CDATA[Maybe I searched for the wrong thing, but I couldn’t find what I was looking for My Bing and Google fu failed me. Basically I wanted to post a collection of ViewModels to an MCV action. Turns out it’s rather simple. Lets say I have a bunch of Products, and Products are managed in a [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe I searched for the wrong thing, but I couldn’t find what I was looking for <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/2011/12/wlEmoticon-sadsmile.png" /> My Bing and Google fu failed me.</p>
<p>Basically I wanted to post a collection of ViewModels to an MCV action. Turns out it’s rather simple.</p>
<p>Lets say I have a bunch of Products, and Products are managed in a WarehouseLocation. A product doesn’t have a warehouse location, since it could exist in multiple locations. </p>
<p>If I’m currently working in Location A, I want to post a collection of Products to an action, as well as the WarehouseLocationId.</p>
<p>So given a simple ViewModel, and an Action:</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:30a8a5ec-35f3-40a1-897f-f008a1de693d" 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">ProductViewModel</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> Name { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">decimal</span> Price { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</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:52efada5-5114-4d42-9003-bf9798eeb7ec" 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:#2b91af">JsonResult</span> Update(<span style="color:#0000ff">int</span> warehouseLocationId, <span style="color:#2b91af">IEnumerable</span>&lt;<span style="color:#2b91af">ProductViewModel</span>&gt; products)<br /> {<br />     <span style="color:#008000">//Do something with the products&#8230;</span></p>
<p>     <span style="color:#0000ff">return</span> Json(<span style="color:#0000ff">new</span> {Staus = <span style="color:#a31515">&quot;success&quot;}</span>);<br /> }</div>
</p></div>
</p></div>
<p>I think usually when someone sends data from jQuery it’s usually a single parameter,so the JSON would look something like:</p>
<p><em>var data = { id: 1,name: ‘test name’, price: 15.95 };</em></p>
<p>This would populate an action that looked like:</p>
<p><em>public JsonResult Update(ProductViewModel product)</em></p>
<p>But what I was faced with was passing in two parameters, one of which is a collection…</p>
<p>MVC seems to pair up the posted result with the parameter name, in the same way they it does for Route parameters. So to the JSON needs to look 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:d1908b0a-71d9-41b9-bd6a-22cbd3cdaed5" 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> data = {<br />     warehouseLocationId: 12,<br />     products: [{<br />         Id: 1,<br />         Name: <span style="color:#800000">&quot;Product 1&quot;</span>,<br />         Price: 15.95<br />     }, {<br />         Id: 3,<br />         Name: <span style="color:#800000">&quot;Product 2&quot;</span>,<br />         Price: 12.50<br />     }]<br /> };</div>
</p></div>
</p></div>
<p>As you can see the key’s on the first level match the parameter names, while the array on products, match the ViewModel.</p>
<p>Taking this exact data and posting it to the action:</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:4e484c30-a717-44de-98ac-b4aaceab8df8" 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;">$.ajax({<br />         type: &#39;POST&#39;,<br />         url: &#39;<span style="background:#ffff00">@</span>Url.Action(<span style="color:#a31515">&quot;Update&quot;</span>, <span style="color:#a31515">&quot;Home&quot;</span>)&#39;,<br />         data: JSON.stringify(data),<br />         contentType: &#39;application/json&#39;,<br />         success: function(result) {<br />             //Do something with result&#8230;<br />         },<br />         dataType: &#39;json&#39;<br />     });</div>
</p></div>
</p></div>
<p>&#160;</p>
<p>I submit that, and with a breakpoint on my action I see:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/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/2011/12/image_thumb.png" width="343" height="43" /></a></p>
<p>^ The warehouseLocationId…</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/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/2011/12/image_thumb1.png" width="640" height="99" /></a></p>
<p>^ 2 products with the values:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/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/2011/12/image_thumb2.png" width="387" height="115" /></a></p>
<p>And:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/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/2011/12/image_thumb3.png" width="373" height="137" /></a></p>
<p>The exact same data we defined in our JavaScript.</p>
<p>The really cool thing about this, is you can have nested collections also. Using the same scenario, but extending Product to have a collection of Categories 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:d86ddfe3-e941-4ba7-8f46-bc1f090b7f0d" 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">ProductViewModel</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> Name { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">decimal</span> Price { <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">Category</span>&gt; Categories { <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">Category</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> Name { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</div>
</p></div>
</p></div>
<p>I can update the JSON object to include Category information on 1 of the two products:</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:887c08e4-93ff-463b-b0e2-b562b5cc6263" 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;">var data = {<br />     warehouseLocationId: 12,<br />     products: [{<br />         Id: 1,<br />         Name: &quot;Product 1&quot;,<br />         Price: 15.95<br />     }, {<br />         Id: 3,<br />         Name: &quot;Product 2&quot;,<br />         Price: 12.50,<br />         Categories: [{<br />             Id: 1,<br />             Name: &quot;Category 1&quot;<br />         }, {<br />             Id: 1,<br />             Name: &quot;Category 2&quot;<br />         }]<br />     }]<br /> };</div>
</p></div>
</p></div>
<p>And submit that in the same way as before, capturing the results in the Action we get the first product with null for the categories, since we didn’t define it as an empty array:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/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/2011/12/image_thumb4.png" width="380" height="135" /></a></p>
<p>While the second Product has 2 items, the first item is Category 1, and the second item is Category 2.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/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/2011/12/image_thumb5.png" width="404" height="156" /></a></p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/12/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/2011/12/image_thumb6.png" width="381" height="172" /></a></p>
<p>And that’s it, easy peasy, sending a collection of ViewModels from jQuery to an MVC Action. </p>
<p>I &lt;3 MVC <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-smile.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2011/12/post-a-collection-of-viewmodels-to-a-mvc-action-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Split Windows + Web Workbench = Win</title>
		<link>http://www.philliphaydon.com/2011/11/split-windows-web-workbench-win/</link>
		<comments>http://www.philliphaydon.com/2011/11/split-windows-web-workbench-win/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 02:49:17 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[LESS]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Web Workbench]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2011/11/split-windows-web-workbench-win/</guid>
		<description><![CDATA[One of the cool features of the Web Workbench from Mindscape is the ability to generate the output files. Prior to using the Web Workbench I was using .LESS{} which uses an HTTP Handler to generate the output files for LESS. The problem with this is sometimes I would write some CSS and not realise [...]]]></description>
			<content:encoded><![CDATA[<p>One of the cool features of the <a href="http://www.mindscapehq.com/products/web-workbench/">Web Workbench</a> from Mindscape is the ability to generate the output files. Prior to using the Web Workbench I was using <a href="http://www.dotlesscss.org/">.LESS{}</a> which uses an HTTP Handler to generate the output files for LESS.</p>
<p>The problem with this is sometimes I would write some CSS and not realise I missed something only to find my site doesn’t display anything, then I have to figure out what I did wrong.</p>
<p>With the Web Workbench, and it’s ability to generate the files every time I save, makes me less error prone.</p>
<p>But the most efficient way of working with it I found (when writing a lot of CSS in 1 sitting) is to split my windows so the first window has where I’m writing, and the second window has the generated CSS file.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/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/2011/11/image_thumb7.png" width="585" height="394" /></a></p>
<p>If I make an error on the left side:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/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/2011/11/image_thumb8.png" width="585" height="394" /></a></p>
<p>I see the error on the right. </p>
<p>If I make a change to the left, (added a background colour) i see the change on the right.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/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/2011/11/image_thumb9.png" width="585" height="394" /></a></p>
<p>This makes it really handy when applying nested rules. Such as nesting a hover rule to an anchor tag. Sometimes I forget to include the colon.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/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/2011/11/image_thumb10.png" width="585" height="394" /></a></p>
<p>Maybe in the next version of the Web Bench, Mindscape can include side-by-side windows that scroll with each other <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-openmouthedsmile" alt="Open-mouthed smile" src="http://www.philliphaydon.com/wp-content/uploads/2011/11/wlEmoticon-openmouthedsmile.png" /> Tho it would be difficult depending on how much CSS was generated. Would still be a nice feature. </p>
<p>Either way, the Web Workbench is a tool that can’t be missed. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2011/11/split-windows-web-workbench-win/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optional Parameters with AttributeRouting</title>
		<link>http://www.philliphaydon.com/2011/11/optional-parameters-with-attributerouting/</link>
		<comments>http://www.philliphaydon.com/2011/11/optional-parameters-with-attributerouting/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 16:38:03 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[AttributeRouting]]></category>
		<category><![CDATA[Optional Parameter]]></category>
		<category><![CDATA[Routing]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2011/11/optional-parameters-with-attributerouting/</guid>
		<description><![CDATA[I found a little trick with using Optional Parameters with AttributeRouting, by using standard optional parameters in the action. The documentation says you can add an attributes to specify the defaults, or add ‘=value’ to the parameter name, and I guess that’s a more correct way to generate routes, but you can achieve the same [...]]]></description>
			<content:encoded><![CDATA[<p>I found a little trick with using Optional Parameters with <a href="https://github.com/mccalltd/AttributeRouting/wiki/2.-Usage">AttributeRouting</a>, by using standard optional parameters in the action.</p>
<p>The documentation says you can add an attributes to specify the defaults, or add ‘=value’ to the parameter name, and I guess that’s a more correct way to generate routes, but you can achieve the same affect by making the parameter optional. 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:af51052c-bccb-4d06-b0bc-9b3bacf140b7" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: ''Courier New'', Courier, Monospace; font-size: 10pt">
<div style="background-color: #ffffff; max-height: 300px; overflow: auto; padding: 2px 5px; white-space: nowrap">[<span style="color:#2b91af">GET</span>(<span style="color:#a31515">&quot;videos/{?page}&quot;</span>)]<br /> <span style="color:#0000ff">public</span> <span style="color:#2b91af">ActionResult</span> Videos(<span style="color:#0000ff">int</span> page = 1)<br /> {<br />     <span style="color:#0000ff">return</span> View(<span style="color:#a31515">&quot;Result&quot;</span>);<br /> }</div>
</p></div>
</p></div>
</p>
<p>If I browse to the URL: </p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/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/2011/11/image_thumb3.png" width="214" height="33" /></a></p>
<p>It uses the default value of 1. </p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/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/2011/11/image_thumb4.png" width="380" height="89" /></a></p>
<p>Now when appending a number to the end of the URL:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/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/2011/11/image_thumb5.png" width="219" height="35" /></a></p>
<p>It captures the correct value:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/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/2011/11/image_thumb6.png" width="390" height="90" /></a></p>
<p>I prefer this method, the optional parameter on the action is more quickly identified, than looking at the attribute to see the defaults. (in my opinion)</p>
<p>Ahh AttributeRouting, how I love you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2011/11/optional-parameters-with-attributerouting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Unit of Work with WCF and Autofac</title>
		<link>http://www.philliphaydon.com/2011/11/unit-of-work-with-wcf-and-autofac/</link>
		<comments>http://www.philliphaydon.com/2011/11/unit-of-work-with-wcf-and-autofac/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 15:10:22 +0000</pubDate>
		<dc:creator>Phillip</dc:creator>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[Autofac]]></category>
		<category><![CDATA[Unit Of Work]]></category>

		<guid isPermaLink="false">http://www.philliphaydon.com/2011/11/unit-of-work-with-wcf-and-autofac/</guid>
		<description><![CDATA[I’ve just spent the last few days trying to find a way to have a Unit of Work with WCF, but it seems no one has a nice clean easy solution. The first, and the only decent solution I found was here: http://ianfnelson.com/archives/2010/04/09/wcf-nhibernate-unit-of-work-endpoint-behavior The problem I found with this solution is that the ICallContextInitializer as [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve just spent the last few days trying to find a way to have a Unit of Work with WCF, but it seems no one has a nice clean easy solution.</p>
<p>The first, and the only decent solution I found was here:</p>
<p><a title="http://ianfnelson.com/archives/2010/04/09/wcf-nhibernate-unit-of-work-endpoint-behavior" href="http://ianfnelson.com/archives/2010/04/09/wcf-nhibernate-unit-of-work-endpoint-behavior" target="_blank">http://ianfnelson.com/archives/2010/04/09/wcf-nhibernate-unit-of-work-endpoint-behavior</a></p>
<p>The problem I found with this solution is that the <em>ICallContextInitializer </em>as well as the <em>EndpointBehavior</em> is only created once. So it would seem all calls to a service would share the same Unit of Work instance. </p>
<p>Maybe Castle Windsor does something fancy and injects a brand new <em>EndpointBehavior</em> every request to a service, but for me, it seemed the <em>ServiceBehavior</em>, <em>EndpointBehavior</em>, and <em>ICallContextInitializer</em> were all created once.</p>
<p>This caused my service to resolve a different instance of IUnitOfWork to what was in the <em>ICallContextInitializer.</em></p>
<h3>Interim Solution 1</h3>
<p>The first solution I came up with was to use Autofac to call Commit on release:</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:14ea6e8e-a34f-470a-a40e-e139ea3477d9" 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(<span style="color:#0000ff">typeof</span> (<span style="color:#2b91af">UnitOfWork</span>))<br />         .As(<span style="color:#0000ff">typeof</span> (<span style="color:#2b91af">IUnitOfWork</span>))<br />         .InstancePerLifetimeScope()<br />         .OnRelease(x =&gt;<br />                         {<br />                             ((<span style="color:#2b91af">IUnitOfWork</span>) x).Commit();<br />                         });</div>
</p></div>
</p></div>
<p>It works… <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/11/wlEmoticon-smile.png" /> but it seemed like a real hack, so I kept digging.</p>
<p>I posted on <a href="http://stackoverflow.com/questions/7989918/using-a-custom-endpoint-behavior-with-wcf-and-autofac" target="_blank">StackOverflow</a> &amp; <a href="http://groups.google.com/group/autofac/browse_thread/thread/7310498aea634abd" target="_blank">Autofac Google Group</a>, but so far I haven’t had anyone suggest a good solution.</p>
<h3>Solution 2</h3>
<p>The next took a while to come up with. </p>
<p><strong>Note:</strong> <font color="#ff0000"><em>I don’t know if this is an appropriate solution for utilizing a Unit of Work with WCF. It works for me but if there is a better solution I would like to hear it.</em></font></p>
<p>I started by giving all my classes an interface called <em>IService</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:d930f4da-f034-4d4b-b1e1-8ed83e9afa5b" 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">interface</span> <span style="color:#2b91af">IService</span><br /> {<br />     <span style="color:#2b91af">IUnitOfWork</span> UnitOfWork { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br /> }</div>
</p></div>
</p></div>
<p>My Service implements this interface:</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:d4fd9486-a127-4996-88e7-ea88aef1114f" 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">WasteInventoryQueryService</span> : <span style="color:#2b91af">IWasteInventoryQueryService</span>, <span style="color:#2b91af">IService</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#2b91af">IUnitOfWork</span> UnitOfWork { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }<br />     <span style="color:#0000ff">public</span> <span style="color:#2b91af">IWasteStockRepository</span> WasteStockRepository { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</p>
<p>     <span style="color:#0000ff">public</span> WasteInventoryQueryService(<span style="color:#2b91af">IUnitOfWork</span> unitOfWork, <span style="color:#2b91af">IWasteStockRepository</span> wasteStockRepository)<br />     {<br />         UnitOfWork = unitOfWork;<br />         WasteStockRepository = wasteStockRepository;<br />     }</p>
<p>     &#8230;<br /> }</div>
</p></div>
</p></div>
</p>
<p>Next I created some behaviours similar to the linked article.</p>
<p>Since finding that the ServiceBehavior, EndpointBehavior and ICallContextInitializer all are created once, I started at the ServiceBehavior. </p>
<p>I created a class called EndpointResolverServiceBehavior, it’s purpose is to inject all the endpoint behaviours I create.</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:b0780a55-e236-46f5-8932-bc97bff59a8a" 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">EndpointResolverServiceBehavior</span> : <span style="color:#2b91af">IServiceBehavior</span><br /> {<br />     <span style="color:#0000ff">protected</span> <span style="color:#2b91af">IEnumerable</span>&lt;<span style="color:#2b91af">IEndpointBehavior</span>&gt; EndpointBehaviors { <span style="color:#0000ff">get</span>; <span style="color:#0000ff">set</span>; }</p>
<p>     <span style="color:#0000ff">public</span> EndpointResolverServiceBehavior(<span style="color:#2b91af">IEnumerable</span>&lt;<span style="color:#2b91af">IEndpointBehavior</span>&gt; endpointBehaviors)<br />     {<br />         EndpointBehaviors = endpointBehaviors;<br />     }</p>
<p> <span style="color:#0000ff">    #region</span> Implementation of IServiceBehavior</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> Validate(<span style="color:#2b91af">ServiceDescription</span> serviceDescription, <span style="color:#2b91af">ServiceHostBase</span> serviceHostBase)<br />     {<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> AddBindingParameters(<span style="color:#2b91af">ServiceDescription</span> serviceDescription, <br />         <span style="color:#2b91af">ServiceHostBase</span> serviceHostBase, <br />         <span style="color:#2b91af">Collection</span>&lt;<span style="color:#2b91af">ServiceEndpoint</span>&gt; endpoints, <br />         <span style="color:#2b91af">BindingParameterCollection</span> bindingParameters)<br />     {<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyDispatchBehavior(<span style="color:#2b91af">ServiceDescription</span> serviceDescription, <span style="color:#2b91af">ServiceHostBase</span> serviceHostBase)<br />     {<br />         <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> endpoint <span style="color:#0000ff">in</span> serviceDescription.Endpoints)<br />         {<br />             <span style="color:#0000ff">foreach</span> (<span style="color:#0000ff">var</span> endpointBehavior <span style="color:#0000ff">in</span> EndpointBehaviors)<br />             {<br />                 endpoint.Behaviors.Add(endpointBehavior);<br />             }<br />         }<br />     }</p>
<p> <span style="color:#0000ff">    #endregion</span><br /> }</div>
</p></div>
</p></div>
<p>Next I created an EndpointBehavior called UnitOfWorkEndpointBehavior, it’s purpose is to add the ICallContextInitializer instance.</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:4ab965c3-1746-40d4-b005-01a72f717606" 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">UnitOfWorkEndpointBehavior</span> : <span style="color:#2b91af">IEndpointBehavior</span><br /> {<br />     <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> AddBindingParameters(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">BindingParameterCollection</span> bindingParameters)<br />     {<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyClientBehavior(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">ClientRuntime</span> clientRuntime)<br />     {<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> ApplyDispatchBehavior(<span style="color:#2b91af">ServiceEndpoint</span> endpoint, <span style="color:#2b91af">EndpointDispatcher</span> endpointDispatcher)<br />     {<br />         <span style="color:#0000ff">foreach</span> (<span style="color:#2b91af">DispatchOperation</span> operation <span style="color:#0000ff">in</span> endpointDispatcher.DispatchRuntime.Operations)<br />         {<br />             operation.CallContextInitializers.Add(<span style="color:#0000ff">new</span> <span style="color:#2b91af">UnitOfWorkCallContextInitializer</span>());<br />         }<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> Validate(<span style="color:#2b91af">ServiceEndpoint</span> endpoint)<br />     {<br />     }<br /> }</div>
</p></div>
</p></div>
<p>And then is the ICallContextInitializer. </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:d63640b6-7c52-43b4-b244-dd2f1b4399ab" 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">UnitOfWorkCallContextInitializer</span> : <span style="color:#2b91af">ICallContextInitializer</span><br /> {<br />     <span style="color:#0000ff">private</span> <span style="color:#2b91af">PropertyInfo</span> _userObjectInfo;<br />     <span style="color:#0000ff">private</span> <span style="color:#0000ff">readonly</span> <span style="color:#2b91af">BindingFlags</span> _flags = <span style="color:#2b91af">BindingFlags</span>.NonPublic | <span style="color:#2b91af">BindingFlags</span>.Instance;</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">object</span> BeforeInvoke(<span style="color:#2b91af">InstanceContext</span> instanceContext, <span style="color:#2b91af">IClientChannel</span> channel, <span style="color:#2b91af">Message</span> message)<br />     {<br />         <span style="color:#0000ff">if</span> (_userObjectInfo == <span style="color:#0000ff">null</span>)<br />             _userObjectInfo = instanceContext.GetType()<br />                                                 .GetProperty(<span style="color:#a31515">&quot;UserObject&quot;</span>, _flags);<br />             <br />         <span style="color:#0000ff">return</span> _userObjectInfo.GetValue(instanceContext, <span style="color:#0000ff">null</span>) <span style="color:#0000ff">as</span> <span style="color:#2b91af">IService</span>;<br />     }</p>
<p>     <span style="color:#0000ff">public</span> <span style="color:#0000ff">void</span> AfterInvoke(<span style="color:#0000ff">object</span> correlationState)<br />     {<br />         <span style="color:#0000ff">var</span> uow = correlationState <span style="color:#0000ff">as</span> <span style="color:#2b91af">IService</span>;</p>
<p>         <span style="color:#0000ff">if</span> (uow != <span style="color:#0000ff">null</span>)<br />             uow.UnitOfWork.Commit();<br />     }<br /> }</div>
</p></div>
</p></div>
<p>While debugging I found out that the InstanceContext has a private property which has the current service attached to it:</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/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/2011/11/image_thumb.png" width="639" height="32" /></a></p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/image1.png" target="_blank"><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/11/image_thumb1.png" width="242" height="127" /></a></p>
<p><em>(click the image for a larger view)</em></p>
<p>So I reflected the property and casted it to an IService, and return the result. When the service has been invoked, the result passed out of the ‘BeforeInvoke’ method, is passed into the ‘AfterInvoke’.</p>
<p><a href="http://www.philliphaydon.com/wp-content/uploads/2011/11/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/2011/11/image_thumb2.png" width="626" height="210" /></a></p>
<p>So basically I return the result from the BeforeInvoke which is passed into the AfterInvoke. Then I attempt to cast it to an IService again.</p>
<p>If the cast is successful, then I can call Commit on my UnitOfWork.</p>
<p>The last piece is to wire up the Behavior with Autofac.</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:d0ce48a9-f6af-478d-a1b7-7ceb8be6b2c0" 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(<span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">UnitOfWorkEndpointBehavior</span>)).As(<span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">IEndpointBehavior</span>));<br /> builder.RegisterType(<span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">EndpointResolverServiceBehavior</span>)).As(<span style="color:#0000ff">typeof</span>(<span style="color:#2b91af">IServiceBehavior</span>));</p>
<p> Container = builder.Build();</p>
<p> <span style="color:#2b91af">AutofacHostFactory</span>.Container = Container;<br /> <span style="color:#2b91af">AutofacHostFactory</span>.HostConfigurationAction =<br />     host =&gt;<br />     {<br />         host.Description.Behaviors.Add(Container.Resolve&lt;<span style="color:#2b91af">IServiceBehavior</span>&gt;());<br />     };</div>
</p></div>
</p></div>
<p>And it’s done. I have a working Unit of Work, that is injected into my Service and Committed after the service has been invoked.</p>
<p>The only down-side I see to all of this, is that if an exception is thrown that I don’t capture, then the UoW will still be committed regardless. </p>
<p>If anyone has any better solutions, let me know! <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/11/wlEmoticon-smile.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.philliphaydon.com/2011/11/unit-of-work-with-wcf-and-autofac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

