<?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>The Inquisitive Coder - Davy Brion&#039;s Blog &#187; agatha</title>
	<atom:link href="http://davybrion.com/blog/category/agatha/feed/" rel="self" type="application/rss+xml" />
	<link>http://davybrion.com/blog</link>
	<description>Trying to walk that thin line between intelligence and ignorance</description>
	<lastBuildDate>Thu, 29 Jul 2010 20:51:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using Agatha&#8217;s Server-Side Caching</title>
		<link>http://davybrion.com/blog/2010/06/using-agathas-server-side-caching/</link>
		<comments>http://davybrion.com/blog/2010/06/using-agathas-server-side-caching/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 16:00:46 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/06/using-agathas-server-side-caching/</guid>
		<description><![CDATA[One of the most important new additions in the Agatha 1.1 release is the ability to have the service layer cache responses for requests that are eligible for caching.&#160; Obviously, this doesn’t happen automatically and you need to configure this yourself.&#160; Unfortunately, i’ve never really written a post to describe how to do this and [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most important new additions in the <a href="http://code.google.com/p/agatha-rrsl/" target="_blank">Agatha</a> 1.1 release is the ability to have the service layer cache responses for requests that are eligible for caching.&#160; Obviously, this doesn’t happen automatically and you need to configure this yourself.&#160; Unfortunately, i’ve never really written a post to describe how to do this and what you need to keep in mind.&#160; Hopefully, all of this will be clear after this post.</p>
<p>There are only two things you need to do to use Agatha’s server-side caching feature:</p>
<p><strong>1. Use the EnableResponseCaching attribute</strong></p>
<p>If you want certain Request-derived types to be eligible for caching, you need to put the EnableResponseCaching attribute on top of them.&#160; This attribute enables you to set the logical region (more on that later) where the Response for this Request needs to stored in the cache, and it requires you to set an expiration.&#160; Here’s a simple example:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #00008b">EnableResponseCaching</span>(<span style="color: purple">Minutes</span> = 10, <span style="color: purple">Region</span> = <span style="color: #a31515">&quot;Issues&quot;</span>)]</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #00008b">GetUnassignedIssuesForProjectRequest</span> : <span style="color: #00008b">Request</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #00008b">Guid</span> <span style="color: purple">ProjectId</span> { <span style="color: #008b8b">get</span>; <span style="color: #008b8b">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>For this particular request type, responses should be cached for a maximum duration of 10 minutes, and the cached responses will be stored in the Issues region.&#160; A region is pretty much just a section within the cache.&#160; If you don’t specify a region, each response will be placed in the default region.&#160; If you do specify one, each cached response for that region is placed within that section.&#160; This gives you the ability to clear an entire region (and thus, all the cached responses that are stored in that region) without impacting any of the other regions (including the default one).</p>
<p>The expiration can be configured by providing a number of hours, minutes or seconds (or a combination of those three) to the attribute.</p>
<p>Now obviously, Agatha needs a way to differentiate between multiple instances of the GetUnassignedIssuesForProjectRequest class.&#160; More specifically, Agatha needs to know when a request can be considered equal to a previous request for which a response has already been cached.&#160; So that brings us to the next thing you need to do:</p>
<p><strong>2. Override the Equals and GetHashCode methods</strong></p>
<p>The response for a GetUnassignedIssuesForProjectRequest instance with ProjectId set to e35c60f7-c35e-43db-9988-0dab3f39c61b will obviously contain a different set of unassigned issues than one for a GetUnassignedIssuesForProjectRequest with ProjectId set to 5d47161a-f334-4cce-9cc4-9606a9d294a6.&#160; To make sure that Agatha knows which response can be returned for a given request, your request needs to override the Equals and GetHashCode methods so you can tell Agatha when an instance of a certain request can be considered equal to one for which a cached response already exists.&#160; In the case of our example, requests of type GetUnassignedIssuesForProjectRequest can be considered equal if they both return the same value through their ProjectId property.&#160; So in this case, our request class needs to look like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #00008b">EnableResponseCaching</span>(<span style="color: purple">Minutes</span> = 10, <span style="color: purple">Region</span> = <span style="color: #a31515">&quot;Issues&quot;</span>)]</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #00008b">GetUnassignedIssuesForProjectRequest</span> : <span style="color: #00008b">Request</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #00008b">Guid</span> <span style="color: purple">ProjectId</span> { <span style="color: #008b8b">get</span>; <span style="color: #008b8b">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">bool</span> <span style="color: #008b8b">Equals</span>(<span style="color: #00008b">GetUnassignedIssuesForProjectRequest</span> other)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (<span style="color: #008b8b">ReferenceEquals</span>(<span style="color: blue">null</span>, other)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (<span style="color: #008b8b">ReferenceEquals</span>(<span style="color: blue">this</span>, other)) <span style="color: blue">return</span> <span style="color: blue">true</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> other.<span style="color: purple">ProjectId</span>.<span style="color: #008b8b">Equals</span>(<span style="color: purple">ProjectId</span>);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">bool</span> <span style="color: #008b8b">Equals</span>(<span style="color: blue">object</span> obj)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (<span style="color: #008b8b">ReferenceEquals</span>(<span style="color: blue">null</span>, obj)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (<span style="color: #008b8b">ReferenceEquals</span>(<span style="color: blue">this</span>, obj)) <span style="color: blue">return</span> <span style="color: blue">true</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (obj.<span style="color: #008b8b">GetType</span>() <span style="color: #008b8b">!=</span> <span style="color: blue">typeof</span>(<span style="color: #00008b">GetUnassignedIssuesForProjectRequest</span>)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: #008b8b">Equals</span>((<span style="color: #00008b">GetUnassignedIssuesForProjectRequest</span>)obj);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">int</span> <span style="color: #008b8b">GetHashCode</span>()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: purple">ProjectId</span>.<span style="color: #008b8b">GetHashCode</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>Now that we have an Equals and a GetHashCode method which only looks at the value of the ProjectId property, Agatha can differentiate between different requests on a value basis instead of a reference basis.&#160; Note that this doesn’t mean that your request types need to be value objects in the truest sense of the term.&#160; They just need to be able to perform an equality check based on the values that make the difference between actually handling the request, or returning the response that has previously been cached for the <em>set of values</em> that you’re using to determine equality.&#160; Simply overriding the Equals method is not enough, since Agatha will use these instances of requests as keys in a dictionary so you need to provide a proper GetHashCode implementation as well (which is recommended anyway if you’re overriding the Equals method).</p>
<p>It’s very important to really consider which properties you want to include in the equality check and the hashcode calculation.&#160; If your request type inherits from some base request (typically one that contains user credentials and stuff like that), then you typically <em>don’t </em>want to include those inherited property values in your equality check, unless you really want to cache different responses based on one of those inherited properties.&#160; I’d recommend writing enough tests to verify that your equality checks and hashcode calculations indeed behave the way you <em>want</em> them to because if they don’t, you will either get suboptimal results from Agatha’s caching or even incorrect ones which would lead to bugs that will be very hard to debug. </p>
<p>A question that came up recently in the <a href="http://groups.google.com/group/agatha-rrsl" target="_blank">Agatha discussion group</a> was how to implement the Equals and GetHashCode method for request types for which each instance should really be considered equal.&#160; For instance, a request type like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #00008b">EnableResponseCaching</span>(<span style="color: purple">Hours</span> = 2, <span style="color: purple">Region</span> = <span style="color: #a31515">&quot;ReferenceData&quot;</span>)]</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #00008b">GetAllCountriesRequest</span> : <span style="color: #00008b">Request</span> {}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>In this case, the default Equals and GetHashCode implementations will be reference-based and not value based.&#160; But as you can see, there are no values to differentiate between requests.&#160; In this case, there is only one way to retrieve the known Countries in this system, so how do we implement the Equals and GetHashCode methods so this request type can be used correctly with Agatha’s caching layer?&#160; Well, the solution isn’t very nice but it is pretty simple.&#160; You can just introduce a dummy field with a fixed value:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #00008b">EnableResponseCaching</span>(<span style="color: purple">Hours</span> = 2, <span style="color: purple">Region</span> = <span style="color: #a31515">&quot;ReferenceData&quot;</span>)]</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #00008b">GetAllCountriesRequest</span> : <span style="color: #00008b">Request</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">string</span> <span style="color: purple">dummyValue</span> = <span style="color: blue">typeof</span>(<span style="color: #00008b">GetAllCountriesRequest</span>).<span style="color: purple">FullName</span>;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">bool</span> <span style="color: #008b8b">Equals</span>(<span style="color: #00008b">GetAllCountriesRequest</span> other)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (<span style="color: #008b8b">ReferenceEquals</span>(<span style="color: blue">null</span>, other)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (<span style="color: #008b8b">ReferenceEquals</span>(<span style="color: blue">this</span>, other)) <span style="color: blue">return</span> <span style="color: blue">true</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: #008b8b">Equals</span>(other.<span style="color: purple">dummyValue</span>, <span style="color: purple">dummyValue</span>);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">bool</span> <span style="color: #008b8b">Equals</span>(<span style="color: blue">object</span> obj)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (<span style="color: #008b8b">ReferenceEquals</span>(<span style="color: blue">null</span>, obj)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (<span style="color: #008b8b">ReferenceEquals</span>(<span style="color: blue">this</span>, obj)) <span style="color: blue">return</span> <span style="color: blue">true</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (obj.<span style="color: #008b8b">GetType</span>() <span style="color: #008b8b">!=</span> <span style="color: blue">typeof</span>(<span style="color: #00008b">GetAllCountriesRequest</span>)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: #008b8b">Equals</span>((<span style="color: #00008b">GetAllCountriesRequest</span>)obj);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">int</span> <span style="color: #008b8b">GetHashCode</span>()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: purple">dummyValue</span>.<span style="color: #008b8b">GetHashCode</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>Now, every instance of the GetAllCountriesRequest will be considered equal to each other and they’ll all return the same hashcode as well.&#160; So every incoming instance of this request will return the same cached response (once it’s been cached that is).</p>
<p>That’s pretty much it.&#160; In itself, the caching layer of Agatha is very easy to use, but you definitely need to make sure that your Equals and GetHashCode implementations are correct.&#160; That’s pretty much the only tricky part (and downside) to how Agatha’s caching works, but i was unfortunately unable to come up with something that was easier to use. </p>
<p>One final word on the usage of regions.&#160; If you’re caching responses, then you typically want a way to remove stale responses from the cache.&#160; If some of the data that you’re caching is changed before the cached responses expire, you can clear the region in which those cached responses are being stored.&#160; Just add an ICacheManager constructor parameter to your handler (or any other class for that matter) and call the Clear method which takes a region name as a parameter.</p>
<p>As always with caching: be careful in how you use it, and make sure you think it through <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/06/using-agathas-server-side-caching/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Consuming An Agatha Service From A Non-Agatha-Aware Client</title>
		<link>http://davybrion.com/blog/2010/05/consuming-an-agatha-service-from-a-non-agatha-aware-client/</link>
		<comments>http://davybrion.com/blog/2010/05/consuming-an-agatha-service-from-a-non-agatha-aware-client/#comments</comments>
		<pubDate>Fri, 14 May 2010 13:49:35 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/05/consuming-an-agatha-service-from-a-non-agatha-aware-client/</guid>
		<description><![CDATA[A question that also comes up occasionally is how you can use an Agatha service from a client which isn’t aware of Agatha? Or more specifically: can an Agatha service be used from a client which has generated a proxy based on the WSDL of the Agatha service? The answer is yes First of all, [...]]]></description>
			<content:encoded><![CDATA[<p>A question that also comes up occasionally is how you can use an Agatha service from a client which isn’t aware of Agatha? Or more specifically: can an Agatha service be used from a client which has generated a proxy based on the WSDL of the Agatha service? The answer is yes <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>First of all, make sure your service exposes its metadata.&#160; You do this in the usual WCF fashion:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">behaviors</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">serviceBehaviors</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">behavior</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">RequestProcessorBehavior</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">serviceMetadata</span><span style="color: blue"> </span><span style="color: red">httpGetEnabled</span><span style="color: blue">=</span>&quot;<span style="color: blue">true</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">dataContractSerializer</span><span style="color: blue"> </span><span style="color: red">maxItemsInObjectGraph</span><span style="color: blue">=</span>&quot;<span style="color: blue">2147483647</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">serviceThrottling</span><span style="color: blue"> </span><span style="color: red">maxConcurrentCalls</span><span style="color: blue">=</span>&quot;<span style="color: blue">500</span>&quot;<span style="color: blue"> </span><span style="color: red">maxConcurrentInstances</span><span style="color: blue">=</span>&quot;<span style="color: blue">500</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">behavior</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">serviceBehaviors</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">behaviors</span><span style="color: blue">&gt;</span></p>
</p></div>
<p>&#160;</p>
<p>The serviceMetadata element with the httpGetEnabled=”true” attribute is the important one in the snippet above.</p>
<p>After that, you can simply generate a service proxy through visual studio or svcutil or whatever:</p>
<p><a href="http://davybrion.com/pictures/ConsumingAnAgathaServiceFromANonAgathaAw_DF1A/add_service_reference.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="add_service_reference" border="0" alt="add_service_reference" src="http://davybrion.com/pictures/ConsumingAnAgathaServiceFromANonAgathaAw_DF1A/add_service_reference_thumb.png" width="635" height="510" /></a> </p>
<p>Now you can write the following code to communicate with your Agatha Service Layer:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">using</span> System;</p>
<p style="margin: 0px"><span style="color: blue">using</span> Sample.NonAgathaAwareClient.MyAgathaService;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">namespace</span> Sample.NonAgathaAwareClient</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">class</span> <span style="color: #2b91af">Program</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">static</span> <span style="color: blue">void</span> Main(<span style="color: blue">string</span>[] args)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">using</span> (<span style="color: blue">var</span> proxy = <span style="color: blue">new</span> MyAgathaService.<span style="color: #2b91af">WcfRequestProcessorClient</span>())</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> responses = proxy.ProcessRequests(<span style="color: blue">new</span>[] { <span style="color: blue">new</span> <span style="color: #2b91af">HelloWorldRequest</span>() });</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> helloWorldResponse = (<span style="color: #2b91af">HelloWorldResponse</span>)responses[0];</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.WriteLine(helloWorldResponse.Message);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Console</span>.Read();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p style="margin: 0px">Notice that there are no Agatha-related using statements, nor is there any reference to Agatha or the assembly which contains the Request/Response types. All of the required data can be found within the WSDL and you can generate proxies for it just as you could with any other WCF service.&#160; The client-side usage model is of course as bad as it always is with standard WCF (for more information, be sure to read: <a href="http://davybrion.com/blog/2009/07/why-i-dislike-classic-or-typical-wcf-usage/" target="_blank">Why I Dislike Classic Or Typical WCF Usage</a>) but if you’re willing to put up with it, then at least you can <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">This also means that you should be able to generate service proxies for other platforms as well, as long as they support SOAP services.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/05/consuming-an-agatha-service-from-a-non-agatha-aware-client/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Agatha 1.1 Is Out</title>
		<link>http://davybrion.com/blog/2010/05/agatha-1-1-is-out/</link>
		<comments>http://davybrion.com/blog/2010/05/agatha-1-1-is-out/#comments</comments>
		<pubDate>Sun, 09 May 2010 12:07:51 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/05/agatha-1-1-is-out/</guid>
		<description><![CDATA[I just released the 1.1 version of Agatha.&#160; It’s a bit sooner than i had originally expected it to be, but the reason why i’m releasing the current trunk is because i want to move the current trunk to .NET 4 and Silverlight 4.&#160; So, the 1.1 version is still targeting .NET 3.5 and Silverlight [...]]]></description>
			<content:encoded><![CDATA[<p>I just released the 1.1 version of Agatha.&#160; It’s a bit sooner than i had originally expected it to be, but the reason why i’m releasing the current trunk is because i want to move the current trunk to .NET 4 and Silverlight 4.&#160; So, the 1.1 version is still targeting .NET 3.5 and Silverlight 3 but apart from possible bugfix releases (1.1.x), there will be no more version targeting .NET 3.5 and Silverlight 3.</p>
<p>These are the changes between the 1.0.1 and the 1.1 release:</p>
<ul>
<li>Support for the NInject container (patch contributed by Ian Davis). </li>
<li>First version of the <a href="http://davybrion.com/blog/2009/12/agathas-caching-layer-implementation-first-draft/" target="_blank">server side caching layer</a>.&#160; Consider this implementation to be experimental and there are most likely plenty of issues with it.&#160; Issues that pop up will obviously be fixed in further 1.1.x releases and the trunk as well.&#160; </li>
<li>AsyncRequestDispatcher and RequestDispatcher now have a virtual AfterSendingRequests virtual method where you can add some custom code if you need to. </li>
<li>Both Request/Response types and RequestHandler can now be located in multiple assemblies (patch contributed by Jason Diamond) </li>
<li>Castle Windsor binaries have been updated to version 2.1 </li>
<li>Castle support for Silverlight has been added </li>
<li>The Process method of the IWcfRequestProcessor service contract now allows transaction flowing </li>
<li>All configuration classes have been made a bit more fluently (thanks to Mark Meyers) </li>
<li>There is now an Agatha.Servicelayer.Silverlight assembly which contains a lightweight version of the service layer to run within your Silverlight application if you want/need this </li>
<li>Support for the StructureMap container (patch contributed by Andrew Rea) </li>
<li>The IWcfRequestProcessor service contract now <a href="http://weblogs.asp.net/andrewrea/archive/2010/03/09/compiling-examples-for-consuming-the-rest-endpoints-for-wcf-service-using-agatha.aspx" target="_blank">supports REST (JSON + XML)</a> (patch contributed by Andrew Rea) </li>
<li>RequestDispatcher contains a BeforeReturningResponses method where you can put some additional code to modify responses before they are returned to the consumer of the dispatcher. </li>
<li>The serverside RequestProcessor contains a few extra virtual methods which enables you to customize how exceptions are logged (patch contributed by Emil Ingerslev) </li>
<li>Fixed NullReferenceException in MessageInspector when using one-way requests in combination with INFO logging </li>
</ul>
<p>So basically, everyone that’s been running on the trunk is probably better off using the 1.1 version, unless you’re willing to move to .NET 4.</p>
<p>You can download the 1.1 version <a href="http://code.google.com/p/agatha-rrsl/downloads/list" target="_blank">here</a>.</p>
<p>The goals for the 1.2 release are to take as much advantage as possible from new .NET 4 and WCF 4 improvements, and also to make the caching layer as good as it can be.&#160; And yes, that will include some form of client-side caching <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/05/agatha-1-1-is-out/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Consuming Multiple Agatha Services</title>
		<link>http://davybrion.com/blog/2010/05/consuming-multiple-agatha-services/</link>
		<comments>http://davybrion.com/blog/2010/05/consuming-multiple-agatha-services/#comments</comments>
		<pubDate>Sat, 08 May 2010 17:21:35 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/05/consuming-multiple-agatha-services/</guid>
		<description><![CDATA[One question that occasionally comes up is how a client can use multiple Agatha services.&#160; While it’s not possible with Agatha’s out-of-the-box configuration and usage patterns, it can be done if you’re willing to write a little bit of glue code. So, let’s go through the steps to make it work. I’m gonna use one [...]]]></description>
			<content:encoded><![CDATA[<p>One question that occasionally comes up is how a client can use multiple Agatha services.&#160; While it’s not possible with Agatha’s out-of-the-box configuration and usage patterns, it can be done if you’re willing to write a little bit of glue code. So, let’s go through the steps to make it work. I’m gonna use one of our Silverlight applications for this example, but it works just the same for regular .NET clients.</p>
<p>First of all, the Silverlight client’s WCF config has 2 defined endpoints:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">client</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">endpoint</span><span style="color: blue"> </span><span style="color: red">binding</span><span style="color: blue">=</span>&quot;<span style="color: blue">basicHttpBinding</span>&quot;<span style="color: blue"> </span><span style="color: red">bindingConfiguration</span><span style="color: blue">=</span>&quot;<span style="color: blue">firstServiceBinding</span>&quot;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: red">contract</span><span style="color: blue">=</span>&quot;<span style="color: blue">Agatha.Common.WCF.IWcfRequestProcessor</span>&quot;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">firstService</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">endpoint</span><span style="color: blue"> </span><span style="color: red">binding</span><span style="color: blue">=</span>&quot;<span style="color: blue">basicHttpBinding</span>&quot;<span style="color: blue"> </span><span style="color: red">bindingConfiguration</span><span style="color: blue">=</span>&quot;<span style="color: blue">secondServiceBinding</span>&quot;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: red">contract</span><span style="color: blue">=</span>&quot;<span style="color: blue">Agatha.Common.WCF.IWcfRequestProcessor</span>&quot;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">secondService</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">client</span><span style="color: blue">&gt;</span></p>
</p></div>
<p>&#160;</p>
<p>Notice that each endpoint has a defined name. </p>
<p>Now you can create 2 new service proxies which inherit from Agatha’s proxy:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">FirstServiceProxy</span> : Agatha.Common.WCF.<span style="color: #2b91af">AsyncRequestProcessorProxy</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> FirstServiceProxy()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: blue">base</span>(<span style="color: #a31515">&quot;firstService&quot;</span>, GetServiceUrl()) {}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">string</span> GetServiceUrl()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// return the correct url from somewhere&#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">SecondServiceProxy</span> : Agatha.Common.WCF.<span style="color: #2b91af">AsyncRequestProcessorProxy</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> SecondServiceProxy()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: blue">base</span>(<span style="color: #a31515">&quot;secondService&quot;</span>, GetServiceUrl()) {}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">string</span> GetServiceUrl()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// return the correct url from somewhere&#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Once you have those proxies, you can create new request dispatchers, which also inherit from Agatha’s:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IFirstDispatcher</span> : Agatha.Common.<span style="color: #2b91af">IAsyncRequestDispatcher</span> {}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">FirstDispatcher</span> : Agatha.Common.<span style="color: #2b91af">AsyncRequestDispatcher</span>, <span style="color: #2b91af">IFirstDispatcher</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> FirstDispatcher(<span style="color: #2b91af">FirstServiceProxy</span> requestProcessor) : <span style="color: blue">base</span>(requestProcessor) { }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">ISecondDispatcher</span> : Agatha.Common.<span style="color: #2b91af">IAsyncRequestDispatcher</span> {}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">SecondDispatcher</span> : Agatha.Common.<span style="color: #2b91af">AsyncRequestDispatcher</span>, <span style="color: #2b91af">ISecondDispatcher</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> SecondDispatcher(<span style="color: #2b91af">SecondServiceProxy</span> requestProcessor) : <span style="color: blue">base</span>(requestProcessor) { }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>If you register those types with your container (make sure it’s the same container instance that Agatha uses) and you can have the correct dispatchers injected for whichever service you want to communicate with.&#160; But in the case of asynchronous request dispatchers, you typically use a factory to get them, so you probably want to add something like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IFirstDispatcherFactory</span> : Agatha.Common.<span style="color: #2b91af">IAsyncRequestDispatcherFactory</span> {}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">FirstDispatcherFactory</span> : <span style="color: #2b91af">IFirstDispatcherFactory</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">IAsyncRequestDispatcher</span> CreateAsyncRequestDispatcher()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: #2b91af">IoC</span>.Container.Resolve&lt;<span style="color: #2b91af">IFirstDispatcher</span>&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">ISecondDispatcherFactory</span> : Agatha.Common.<span style="color: #2b91af">IAsyncRequestDispatcherFactory</span> {}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">SecondDispatcherFactory</span> : <span style="color: #2b91af">ISecondDispatcherFactory</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">IAsyncRequestDispatcher</span> CreateAsyncRequestDispatcher()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: #2b91af">IoC</span>.Container.Resolve&lt;<span style="color: #2b91af">ISecondDispatcher</span>&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>And there you go… inject the proper factory (or both of them) and just use whichever one you need to get a dispatcher for the service you want. </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/05/consuming-multiple-agatha-services/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Looking For Agatha Testimonials</title>
		<link>http://davybrion.com/blog/2010/05/looking-for-agatha-testimonials/</link>
		<comments>http://davybrion.com/blog/2010/05/looking-for-agatha-testimonials/#comments</comments>
		<pubDate>Wed, 05 May 2010 15:02:51 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/05/looking-for-agatha-testimonials/</guid>
		<description><![CDATA[I’ve noticed recently that i’m gradually getting more visitors to this blog through Agatha’s project site.&#160; Pretty glad to see that, though the numbers are still very low.&#160; Nevertheless, once you notice that something you’ve created is actually being used by people, you love to hear more about what their experiences are.&#160; Which is kinda [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve noticed recently that i’m gradually getting more visitors to this blog through <a href="http://code.google.com/p/agatha-rrsl" target="_blank">Agatha’s project site</a>.&#160; Pretty glad to see that, though the numbers are still very low.&#160; Nevertheless, once you notice that something you’ve created is actually being used by people, you love to hear more about what their experiences are.&#160; Which is kinda hard in this case since i picked one of the worst possible names for a project if you ever want to be able to search google or twitter to read what people are saying about it.&#160;&#160; </p>
<p>So, i figured i’d just ask you guys.&#160; Have you used Agatha? Did you have positive experiences with it? If so, i’d really appreciate any testimonials you can send me (email address is ralinx at the domain name of this blog).&#160; Not only do i get to read about your experiences, i can add them to the project site as well which does kinda boost the credibility of the project for people who never heard about it before.&#160; If you’re willing to write such a testimonial (which only needs to be one or two sentences, though something longer than that is more than welcome as well), please include your name, your jobtitle and the name of your company in the email.</p>
<p>Also, if you have tried it and were not happy with it or ran into problems with it, i’d definitely like to hear from you as well so i can look into possible solutions or improvements.</p>
<p>Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/05/looking-for-agatha-testimonials/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Securing Your Agatha Service Layer</title>
		<link>http://davybrion.com/blog/2010/01/securing-your-agatha-service-layer/</link>
		<comments>http://davybrion.com/blog/2010/01/securing-your-agatha-service-layer/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 20:05:11 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/securing-your-agatha-service-layer/</guid>
		<description><![CDATA[The question of how to implement security for an Agatha Service Layer is one that comes up frequently.&#160; First of all, you need to remember that if you’re using Agatha with WCF you can use any of the WCF features that you’d normally use to secure your WCF service.&#160; There’s already plenty of information available [...]]]></description>
			<content:encoded><![CDATA[<p>The question of how to implement security for an Agatha Service Layer is one that comes up frequently.&#160; First of all, you need to remember that if you’re using Agatha with WCF you can use any of the WCF features that you’d normally use to secure your WCF service.&#160; There’s already plenty of information available online or in books on implementing security for WCF services so i’m not going to spend time on covering those options.&#160; What i am going to cover is the approach that we typically use for our Agatha service layers.</p>
<p>I don’t like to tie myself to WCF-specific features, so i always plug in custom authentication into either a custom Request Processor, or a base Request Handler class that all other Request Handlers must inherit from.&#160;&#160; But first, how do we get the authentication-related data from the client to the service?</p>
<p>In each project i use Agatha in, i always have a MyProjectRequest and MyProjectResponse base class:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectRequest</span> : <span style="color: #2b91af">Request</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> UserName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">byte</span>[] PasswordHash { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectResponse</span> : <span style="color: #2b91af">Response</span> {}</p>
</p></div>
<p>&#160;</p>
<p>Each request in the project inherits from this base request, and each response inherits from the base response.&#160; The base response class is often empty, though this does make it very easy if you ever need to send something back with every response.</p>
<p>Now obviously, if every single request that is sent to your service layer needs values for the UserName and PasswordHash properties you want this to be done in only one place.&#160; I do this by using a custom request dispatcher:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectAsyncRequestDispatcher</span> : <span style="color: #2b91af">AsyncRequestDispatcher</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IUserContext</span> userContext;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> MyProjectAsyncRequestDispatcher(<span style="color: #2b91af">IAsyncRequestProcessor</span> requestProcessor, <span style="color: #2b91af">IUserContext</span> userContext) : <span style="color: blue">base</span>(requestProcessor)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.userContext = userContext;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> BeforeSendingRequests(<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Request</span>&gt; requestsToProcess)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">base</span>.BeforeSendingRequests(requestsToProcess);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (<span style="color: blue">var</span> myProjectRequest <span style="color: blue">in</span> requestsToProcess.OfType&lt;<span style="color: #2b91af">MyProjectRequest</span>&gt;())</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; myProjectRequest.UserName = userContext.UserName;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; myProjectRequest.PasswordHash = userContext.PasswordHash;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The IUserContext dependency is just a component that is registered in my IOC container and will be injected automatically whenever you get an instance of IAsyncRequestDispatcher.&#160;&#160; Now, in this example you can see that i add the authentication data to <em>every request </em>in a batch, even though the batch will be sent in one roundtrip.&#160; If you want, you can add the authentication data only to the first request and then only use the first request to do the authentication within your service layer.&#160; I prefer to add the authentication data to each request and then authenticate every single request (even subsequent requests in a batch) within the service layer.&#160; I’ll get back to this point later on.</p>
<p>Now, the only thing we need to do to make sure that your requests will always have the authentication data contained within them is to instruct Agatha to always use instances of your MyProjectAsyncRequestDispatcher class whenever an IAsyncRequestDispatcher is needed.&#160; This is easily done during Agatha’s client-side configuration:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ClientConfiguration</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">MyProjectRequest</span>).Assembly, <span style="color: blue">new</span> Agatha.Castle.<span style="color: #2b91af">Container</span>(myContainerWrapper))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AsyncRequestDispatcherImplementation = <span style="color: blue">typeof</span>(<span style="color: #2b91af">MyProjectAsyncRequestDispatcher</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Initialize();</p>
</p></div>
<p>&#160;</p>
<p>Keep in mind that you still have to register your IUserContext with the container on your own though.&#160; </p>
<p>With that in place, we are sure that each request that comes from <em>our clients</em> always contains the proper authentication data.&#160; Now we need to make sure that we actually authenticate these requests within the service layer.&#160; You basically have 2 options: either implement your own Request Processor which adds authentication checks, or create a base Request Handler that your other Request Handlers inherit from.</p>
<p>We’ll first cover the option of using a custom Request Processor.&#160;&#160; You could create one like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectRequestProcessor</span> : <span style="color: #2b91af">RequestProcessor</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IAuthenticator</span> authenticator;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> MyProjectRequestProcessor(<span style="color: #2b91af">IAuthenticator</span> authenticator, <span style="color: #2b91af">ServiceLayerConfiguration</span> serviceLayerConfiguration, <span style="color: #2b91af">ICacheManager</span> cacheManager) </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: blue">base</span>(serviceLayerConfiguration, cacheManager)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.authenticator = authenticator;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> BeforeHandle(<span style="color: #2b91af">Request</span> request)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> myProjectRequest = request <span style="color: blue">as</span> <span style="color: #2b91af">MyProjectRequest</span>;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (myProjectRequest != <span style="color: blue">null</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!authenticator.AreValidCredentials(myProjectRequest.UserName, myProjectRequest.PasswordHash))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">MySecurityException</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The BeforeHandle virtual method is called right before the request is passed to its Request Handler to be handled.&#160; Note that this Request Processor would authenticate <em>every </em>request.&#160; If you want a Request Processor that only authenticates the first request, you could do so like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectRequestProcessor</span> : <span style="color: #2b91af">RequestProcessor</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IAuthenticator</span> authenticator;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> MyProjectRequestProcessor(<span style="color: #2b91af">IAuthenticator</span> authenticator, <span style="color: #2b91af">ServiceLayerConfiguration</span> serviceLayerConfiguration, <span style="color: #2b91af">ICacheManager</span> cacheManager) </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: blue">base</span>(serviceLayerConfiguration, cacheManager)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.authenticator = authenticator;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> BeforeProcessing(<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Request</span>&gt; requests)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> myProjectRequest = (<span style="color: #2b91af">MyProjectRequest</span>)requests.ElementAt(0);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!authenticator.AreValidCredentials(myProjectRequest.UserName, myProjectRequest.PasswordHash))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">MySecurityException</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The BeforeProcessing virtual method is called before any of the requests are handled, so you could authenticate only the first request in a batch and then proceed with regular processing if the first one is authenticated.&#160; Now, the big problem that i have with this approach is that you aren’t really in control of what is sent to your service layer.&#160; Yes, you can guarantee that each request coming from <em>your clients</em> contains the proper authentication data.&#160; What you simply can’t guarantee however is what other people or custom clients can send to your service layer.&#160; If they send you a batch of 2 requests, the first containing valid credentials of a normal user for a benign request, it will pass the authentication just fine.&#160; If the second request in that batch contains invalid credentials (to trick your authorization into believing it’s from a user with higher privileges for instance) for a request that could cause some damage (deleting important information or triggering certain tasks or whatever), then you can’t really do anything to prevent that.&#160; Unless of course, you reject this approach and authenticate every single request.</p>
<p>As for the MySecurityException type, that’s up to you as well.&#160; When you configure your Agatha service layer, you can set the SecurityExceptionType property to the type of exception that should be considered as a security exception.&#160; When the Request Processor catches an exception of that type, it will set the ExceptionType property of the response to ExceptionType.Security so you can check for that specific situation in your client.</p>
<p>To configure Agatha to use your custom Request Processor, your configuration code would look like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ServiceLayerConfiguration</span>(<span style="color: #2b91af">Assembly</span>.GetExecutingAssembly(), <span style="color: blue">typeof</span>(<span style="color: #2b91af">MyProjectRequest</span>).Assembly,</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Agatha.Castle.<span style="color: #2b91af">Container</span>(containerWrapper))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RequestProcessorImplementation = <span style="color: blue">typeof</span>(<span style="color: #2b91af">MyProjectRequestProcessor</span>),</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SecurityExceptionType = <span style="color: blue">typeof</span>(<span style="color: #2b91af">MySecurityException</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Initialize();</p>
</p></div>
<p>&#160;</p>
<p>Another alternative is to create a base Request Handler for your project and to have each Request Handler inherit from it.&#160; Something like this, for instance:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">abstract</span> <span style="color: blue">class</span> <span style="color: #2b91af">MyProjectRequestHandler</span>&lt;TRequest, TResponse&gt; : <span style="color: #2b91af">RequestHandler</span>&lt;TRequest, TResponse&gt; </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">where</span> TRequest : <span style="color: #2b91af">MyProjectRequest</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">IAuthenticator</span> Authenticator { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">void</span> BeforeHandle(TRequest request)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">base</span>.BeforeHandle(request);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!Authenticator.AreValidCredentials(request.UserName, request.PasswordHash))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">MySecurityException</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Authorize(request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> Authorize(TRequest request) {}</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>In case you’re wondering why i’m using Setter-injection here instead of Constructor-injection, read <a href="http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/" target="_blank">this</a>.</p>
<p>I typically prefer the custom Request Handler approach for authentication.&#160; In most applications that we write, authentication is not enough and we need custom authorization checks for many requests.&#160; So i’m going to need a base Request Handler which introduces the virtual Authorize method anyway.&#160; So i might as well do my authentication check right before it. </p>
<p>With the custom Request Handler approach, you probably still want to configure Agatha to use your custom security exception type:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ServiceLayerConfiguration</span>(<span style="color: #2b91af">Assembly</span>.GetExecutingAssembly(), <span style="color: blue">typeof</span>(<span style="color: #2b91af">MyProjectRequest</span>).Assembly,</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> Agatha.Castle.<span style="color: #2b91af">Container</span>(containerWrapper))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SecurityExceptionType = <span style="color: blue">typeof</span>(<span style="color: #2b91af">MySecurityException</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Initialize();</p>
</p></div>
<p>&#160;</p>
<p>And then you just need to let your own Request Handlers inherit from your MyProjectRequestHandler.&#160; Authentication will be performed for each request by default, and you can easily add specific authorization logic for every request.&#160; </p>
<p>And those are pretty much the options you have to secure your Agatha Service Layer.&#160;&#160; Oh, and be sure to only expose your Service Layer through SSL <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/securing-your-agatha-service-layer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Agatha Vs NServiceBus?</title>
		<link>http://davybrion.com/blog/2010/01/agatha-vs-nservicebus/</link>
		<comments>http://davybrion.com/blog/2010/01/agatha-vs-nservicebus/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 20:50:51 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>
		<category><![CDATA[nservicebus]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2010/01/agatha-vs-nservicebus/</guid>
		<description><![CDATA[Ever since i open sourced Agatha, i’ve gotten questions from people who are wondering whether they should use Agatha or NServiceBus.&#160; I’ve also gotten questions about things that people wanted to do with Agatha but that aren’t really supported.&#160; And i’ve also noticed that people were coming to my site with search keywords like “agatha [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since i open sourced <a href="http://code.google.com/p/agatha-rrsl/" target="_blank">Agatha</a>, i’ve gotten questions from people who are wondering whether they should use Agatha or <a href="http://nservicebus.com/" target="_blank">NServiceBus</a>.&#160; I’ve also gotten questions about things that people wanted to do with Agatha but that aren’t really supported.&#160; And i’ve also noticed that people were coming to my site with search keywords like “agatha vs nservicebus”.&#160; The thing is, they are hardly comparable.</p>
<p>Agatha is a Request/Response Service Layer framework.&#160; It basically supports synchronous and asynchronous Request/Response style communication and tries to make it as easy as possible to write a service layer for that type of communication.&#160; Apart from being easy to use, it also encourages a clean implementation of your service layer and a way to minimize the repetitiveness of cross-cutting concerns.&#160; It also enables you to get better performance than with typical Remote Procedure Call or Remote Method Invocation style service layers because it will try to minimize roundtrips by batching requests and responses together.&#160; In the next release, it will also offer a caching layer so certain requests (depending on how you set it up) don’t need to be processed and cached responses can simply be returned.&#160; There’s also support for one-way requests (or fire-and-forget requests) but it has the same downsides as one-way requests with typical WCF services have. That’s pretty much all it does.&#160; That’s probably all it’ll ever do.&#160; In short, it’s just an upgrade over your typical WCF services.</p>
<p>NServiceBus on the other hand also does Request/Response, but that’s just one of the things it can do.&#160; Again, Agatha is essentially an RPC or RMI framework whereas NServiceBus is built on top of one-way messaging technology.&#160; This allows for much more possibilities when it comes to communicating between different applications or different parts or processes within the same application boundary.&#160; A great overview of those possibilities can be found <a href="http://nservicebus.com/ArchitecturalPrinciples.aspx" target="_blank">here</a>.&#160; Because it is based on messaging, there are a lot of benefits that you can get from NServiceBus that you’ll probably never get from Agatha.&#160; For one, the ‘Store &amp; Forward’ messaging model from NServiceBus might seem similar to Agatha’s one-way requests on first sight, but there are some very important differences.&#160; If you send a fire-and-forget request with Agatha, and the service is currently down, then that request is essentially lost.&#160; With NServiceBus, the message is stored in a message queue and it will be processed when the target of the fire-and-forget message comes up again.&#160; From a reliability point of view, that’s obviously a tremendous improvement over what Agatha can offer you.&#160; Another area where NServiceBus truly shines (IMO) is in its <a href="http://en.wikipedia.org/wiki/Publish_subscribe" target="_blank">Publish/Subscribe</a> implementation.&#160; Some people have asked me if i’ll ever provide something like that in Agatha and the answer has always been ‘no’.&#160;&#160; For one, it doesn’t fit into what Agatha tries to do and i don’t see the point in implementing it if a better implementation is available already in another project.&#160;&#160; There’s plenty more interesting things in NServiceBus to deal with more advanced scenarios, which you’ll have to find out about on your own <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>So which one is more suitable for you or your applications? As with every technology choice, it depends.&#160; Agatha is great for most typical business applications.&#160; If you need to communicate between one or more different clients (with different i don’t mean multiple instances but different types of clients like a web app, a silverlight client and/or a WPF client) and<em> one </em>service (you can obviously use it with more than one service as well if you want to) on an application server which encapsulates your business layer then it is a very attractive solution, as long as you don’t need the superior reliability that NServiceBus can offer you.&#160; With superior reliability i particularly mean the ability to still process received messages once the service layer comes back up after having been unavailable.&#160; In my experience, most business applications don’t really need that, since most of those applications are entirely unusable if the service they depend on is down.&#160; In short, if all the possible downsides of using a WCF service layer are not an issue for your project, then Agatha will be a great fit.</p>
<p>If however, you need to maximize reliability, performance and general robustness of a critical enterprise application, then NServiceBus will definitely be a much better choice.&#160; If you’re dealing with many distributed parts, NServiceBus will also make things much easier for you than Agatha (or any other WCF service layer for that matter) will.&#160;&#160; And obviously, if you want to integrate multiple applications while reducing coupling between applications as much as possible, NServiceBus will also be a much better fit than Agatha.&#160; With NServiceBus, you’d only need to share an assembly containing the types of the messages.&#160; With Agatha, you either need to share an assembly with shared request/response types or use proxies for them <em>and</em> you would also have to <em>know</em> about the other applications since you’d need to access their services directly.&#160; This can get quite ugly pretty fast.</p>
<p>And in some cases, you can just use both of them at the same time.&#160; At work we have two projects that use Agatha for all of their internal communication within their own application boundary, yet they use NServiceBus to notify each other of certain events.&#160; Neither of the applications knows about the existence of the other… the only thing they share is one assembly with some shared message types.&#160; We’ve also started working on a new platform where each application in the platform will use Agatha for all of the communication within their own application boundary (since they’re all typical silverlight clients backed by a WCF service style applications) but they will use NServiceBus for every kind of communication that goes outside of the application boundary.</p>
<p>As with many things, it’s just a matter of choosing the right tool for the right job.&#160; Hopefully, this post will help some of you make that decision should you need to make it in the future <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2010/01/agatha-vs-nservicebus/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Testing Agatha&#8217;s Caching Functionality With QuickNet</title>
		<link>http://davybrion.com/blog/2009/12/testing-agathas-caching-functionality-with-quicknet/</link>
		<comments>http://davybrion.com/blog/2009/12/testing-agathas-caching-functionality-with-quicknet/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 00:12:16 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[agatha]]></category>
		<category><![CDATA[quicknet]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2126</guid>
		<description><![CDATA[In this post i’m going to give you a very detailed explanation of a QuickNet test that i wrote for Agatha’s caching layer.&#160; If i do my job well, you’ll have a much better view on what QuickNet does, how it works, and how it can help you.&#160; I do want to ask you to [...]]]></description>
			<content:encoded><![CDATA[<p>In this post i’m going to give you a very detailed explanation of a <a href="http://code.google.com/p/quicknet/" target="_blank">QuickNet</a> test that i wrote for Agatha’s caching layer.&#160; If i do my job well, you’ll have a much better view on what QuickNet does, how it works, and how it can help you.&#160; I do want to ask you to keep a very open mind and to forget pretty much everything that you know about automated testing, including your opinions on what should or should not be done in an automated test.&#160; If you have problems with that, you might be interested in more <a href="http://osherove.com/" target="_blank">conservative reading on automated testing</a>.&#160; Still here? Alright, let’s get started.&#160; Please stay focused throughout, because you will need it :p</p>
<p>First of all, requests whose response is eligible for caching must override the Equals method and the GetHashCode method.&#160; I’ll use the following two request/response combinations for this test:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #2b91af">EnableResponseCaching</span>(Seconds = 1)]</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">FirstCachedRequest</span> : <span style="color: #2b91af">Request</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> String { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">bool</span> Equals(<span style="color: #2b91af">FirstCachedRequest</span> other)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (ReferenceEquals(<span style="color: blue">null</span>, other)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (ReferenceEquals(<span style="color: blue">this</span>, other)) <span style="color: blue">return</span> <span style="color: blue">true</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> Equals(other.String, String);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">bool</span> Equals(<span style="color: blue">object</span> obj)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (ReferenceEquals(<span style="color: blue">null</span>, obj)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (ReferenceEquals(<span style="color: blue">this</span>, obj)) <span style="color: blue">return</span> <span style="color: blue">true</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (obj.GetType() != <span style="color: blue">typeof</span>(<span style="color: #2b91af">FirstCachedRequest</span>)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> Equals((<span style="color: #2b91af">FirstCachedRequest</span>)obj);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">int</span> GetHashCode()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> (String != <span style="color: blue">null</span> ? String.GetHashCode() : 0);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">FirstCachedResponse</span> : <span style="color: #2b91af">Response</span> { }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #2b91af">EnableResponseCaching</span>(Seconds = 2)]</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">SecondCachedRequest</span> : <span style="color: #2b91af">Request</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">int</span> Integer { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">bool</span> Equals(<span style="color: #2b91af">SecondCachedRequest</span> other)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (ReferenceEquals(<span style="color: blue">null</span>, other)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (ReferenceEquals(<span style="color: blue">this</span>, other)) <span style="color: blue">return</span> <span style="color: blue">true</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> other.Integer == Integer;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">bool</span> Equals(<span style="color: blue">object</span> obj)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (ReferenceEquals(<span style="color: blue">null</span>, obj)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (ReferenceEquals(<span style="color: blue">this</span>, obj)) <span style="color: blue">return</span> <span style="color: blue">true</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (obj.GetType() != <span style="color: blue">typeof</span>(<span style="color: #2b91af">SecondCachedRequest</span>)) <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> Equals((<span style="color: #2b91af">SecondCachedRequest</span>)obj);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">int</span> GetHashCode()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> Integer;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">SecondCachedResponse</span> : <span style="color: #2b91af">Response</span> {}</p>
</p></div>
<p>&#160;</p>
<p>Agatha’s Request Processor should cache responses for requests that are eligible for caching, and when subsequent requests need to be processed which are <em>equal</em> (in their values) to previous requests whose response has been cached already, the cached response needs to be returned instead of handling the request again.</p>
<p>In order to inspect the usage of the caching layer during tests, i wrote the following CacheManagerSpy class which inherits from my normal CacheManager class (which is used by Agatha):</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">CacheManagerSpy</span> : <span style="color: #2b91af">CacheManager</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">CacheEntry</span>&gt; cacheEntries;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Response</span>&gt; returnedCachedResponses;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">CacheEntry</span>&gt; CacheEntries</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">get</span> { <span style="color: blue">return</span> cacheEntries; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Response</span>&gt; ReturnedCachedResponses</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">get</span> { <span style="color: blue">return</span> returnedCachedResponses; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> CacheManagerSpy(<span style="color: #2b91af">CacheConfiguration</span> configuration, <span style="color: #2b91af">ICacheProvider</span> cacheProvider) : <span style="color: blue">base</span>(configuration, cacheProvider)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Clear();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Clear() </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cacheEntries = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">CacheEntry</span>&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; returnedCachedResponses = <span style="color: blue">new</span> <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Response</span>&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: #2b91af">Response</span> GetCachedResponseFor(<span style="color: #2b91af">Request</span> request, <span style="color: blue">string</span> region)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> cachedResponse = <span style="color: blue">base</span>.GetCachedResponseFor(request, region);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; returnedCachedResponses.Add(cachedResponse);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> cachedResponse;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> StoreInCache(<span style="color: #2b91af">Request</span> request, <span style="color: #2b91af">Response</span> response, <span style="color: #2b91af">TimeSpan</span> expiration, <span style="color: blue">string</span> region)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cacheEntries.Add(<span style="color: blue">new</span> <span style="color: #2b91af">CacheEntry</span>(request, response, expiration, region));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">base</span>.StoreInCache(request, response, expiration, region);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">CacheEntry</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Request</span> Request { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Response</span> Response { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">TimeSpan</span> Expiration { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Region { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> CacheEntry(<span style="color: #2b91af">Request</span> request, <span style="color: #2b91af">Response</span> response, <span style="color: #2b91af">TimeSpan</span> expiration, <span style="color: blue">string</span> region)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Request = request;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Response = response;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Expiration = expiration;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Region = region;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>This Spy basically gives me the ability to see which entries are stored in the cache, and which responses have been returned.</p>
<p>Now i can create a QuickNet Acid test.&#160; I’m going to go over the code step by step instead of listing it all at once because each step probably needs quite a bit of explanation if you don’t understand QuickNet yet.</p>
<p>First of all, here’s the definition of the test:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">RequestProcessorCachingSpecs</span> : <span style="color: #2b91af">AcidTest</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> RequestProcessorCachingSpecs() : <span style="color: blue">base</span>(10, 2000) { }</p>
</p></div>
<p>&#160;</p>
<p>The 10 means that QuickNet will perform 10 testruns.&#160; The 2000 means that each testrun will contain 2000 executions of the defined transitions.&#160; Don’t worry yet about what a transition is, i’ll get to that later on in the post.</p>
<p>We’ll also need some fields to access some of the classes that will be used in this test:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">static</span> <span style="color: #2b91af">IRequestHandler</span>&lt;<span style="color: #2b91af">FirstCachedRequest</span>&gt; firstCachedRequestHandler;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">static</span> <span style="color: #2b91af">IRequestHandler</span>&lt;<span style="color: #2b91af">SecondCachedRequest</span>&gt; secondCachedRequestHandler;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">static</span> <span style="color: #2b91af">IRequestProcessor</span> requestProcessor;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">static</span> <span style="color: #2b91af">CacheManagerSpy</span> cacheManager;</p>
</p></div>
<p>&#160;</p>
<p>Yeah, i know what you’re thinking.&#160; Static is bad right?&#160; Well, not in this case since we need to be able to use these instances in some inner classes that we’ll go over soon enough and these references will hold the same instance for each testrun (which, as mentioned consists of 2000 transition executions).&#160; Anyway, forget about the static keyword for a second and keep reading.</p>
<p>Now we can implement the SetUp of our testrun:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">void</span> SetUp()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IoC</span>.Container = <span style="color: blue">new</span> Agatha.Castle.<span style="color: #2b91af">Container</span>();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> serviceLayerConfiguration = <span style="color: blue">new</span> <span style="color: #2b91af">ServiceLayerConfiguration</span>(<span style="color: #2b91af">Assembly</span>.GetExecutingAssembly(), </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Assembly</span>.GetExecutingAssembly(), <span style="color: #2b91af">IoC</span>.Container)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; BusinessExceptionType = <span style="color: blue">typeof</span>(<span style="color: #2b91af">BusinessException</span>),</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SecurityExceptionType = <span style="color: blue">typeof</span>(<span style="color: #2b91af">SecurityException</span>),</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CacheManagerImplementation = <span style="color: blue">typeof</span>(<span style="color: #2b91af">CacheManagerSpy</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; serviceLayerConfiguration.Initialize();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// i want to take advantage of the automatic initialization, so i&#8217;m just resolving the requestprocessor instead of creating it</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; requestProcessor = <span style="color: #2b91af">IoC</span>.Container.Resolve&lt;<span style="color: #2b91af">IRequestProcessor</span>&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// the cache manager is a singleton so i can just resolve it and it&#8217;ll be the same one the request processor uses</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cacheManager = (<span style="color: #2b91af">CacheManagerSpy</span>)<span style="color: #2b91af">IoC</span>.Container.Resolve&lt;<span style="color: #2b91af">ICacheManager</span>&gt;();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; firstCachedRequestHandler = <span style="color: #2b91af">MockRepository</span>.GenerateMock&lt;<span style="color: #2b91af">IRequestHandler</span>&lt;<span style="color: #2b91af">FirstCachedRequest</span>&gt;&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; secondCachedRequestHandler = <span style="color: #2b91af">MockRepository</span>.GenerateMock&lt;<span style="color: #2b91af">IRequestHandler</span>&lt;<span style="color: #2b91af">SecondCachedRequest</span>&gt;&gt;();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IoC</span>.Container.RegisterInstance(firstCachedRequestHandler);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IoC</span>.Container.RegisterInstance(secondCachedRequestHandler);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>This setup code will be executed for each testrun.&#160; So QuickNet will execute this 10 times, and each time it will run 2000 transition executions on the classes that we set up for the current testrun.&#160; </p>
<p>So what exactly is a transition? It’s just a piece of code that you want to execute.&#160; That piece of code can receive input and it can return output.&#160; It’s basically the code that you’re testing.&#160; Our transition will simply consist of calling Agatha’s Request Processor and instructing it to process 2 requests.&#160; Those requests would be the two request types shown earlier which are eligible for caching.&#160; After QuickNet executes the transition, it will also verify that our defined specifications for that particular transition are indeed correct.</p>
<p>Let’s define the input that our transition will receive.&#160; In this particular case, we need a Request instance, an instance of a Request Handler for that Request, and the Response that the Request Handler needs to return if there’s no valid Response available in the cache.&#160; So we’ll use the following helper class to define this input:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ProcessInputElement</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">IRequestHandler</span> RequestHandler { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Request</span> Request { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Response</span> Response { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">bool</span> RequestHandlerWasExecuted { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> ProcessInputElement(<span style="color: #2b91af">IRequestHandler</span> requestHandler, <span style="color: #2b91af">Request</span> request, <span style="color: #2b91af">Response</span> response)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RequestHandler = requestHandler;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Request = request;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Response = response;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> StubHandler()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RequestHandler</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Stub(r =&gt; r.Handle(<span style="color: #2b91af">Arg</span>&lt;<span style="color: #2b91af">Request</span>&gt;.Is.Same(Request))) <span style="color: green">// WHY: reference check iso equality check</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Return(Response)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .WhenCalled(a =&gt; RequestHandlerWasExecuted = <span style="color: blue">true</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Repeat.Once();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>An instance of this class basically combines the Request, its Request Handler and the Response that needs to be returned.&#160; It also contains a helper method so we can easily instruct the mocked Request Handler to return the given Response when it is asked to handle the given Request.&#160; And we’ll also set a simple flag to true if the Request Handler has indeed been called.</p>
<p>As i mentioned a few times already, QuickNet will execute our transition (which i’ll show later on in the post) 2000 times for each testrun.&#160; Obviously, we can’t be expected to create these instances manually all the time, so we need to define some generators that QuickNet will be able to use to randomly generate the input that it can pass to the transition.&#160; We first need two generators which are capable of generating random instances of the FirstCachedRequest and SecondCachedRequest types:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">FirstRequestGenerator</span> : <span style="color: #2b91af">BaseGenerator</span>&lt;<span style="color: #2b91af">FirstCachedRequest</span>&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> FirstRequestGenerator()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AddGeneratorForProperty(r =&gt; r.String, <span style="color: blue">new</span> <span style="color: #2b91af">StringGenerator</span>(1, 1)); <span style="color: green">// generates a random string of 1 character</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">SecondRequestGenerator</span> : <span style="color: #2b91af">BaseGenerator</span>&lt;<span style="color: #2b91af">SecondCachedRequest</span>&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> SecondRequestGenerator()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AddGeneratorForProperty(r =&gt; r.Integer, <span style="color: blue">new</span> <span style="color: #2b91af">IntGenerator</span>(0, 5));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The FirstRequestGenerator class is capable of returning a new instance of FirstCachedRequest whenever it is asked to do so, and it will populate the FirstCachedRequest instance’s String property with a random string consisting of one character.&#160;&#160; Some of the generated instances will be considered equals of each other if the generated string value is equal, and some will not be considered as equals if the generated string value is not equal.&#160; The SecondRequestGenerator basically does the same thing, except that it will populate the SecondCachedRequest instances’ Integer property with a random int value between 0 and 5.&#160; So again, some of the generated instances will be considered as equals, and some won’t.&#160;&#160; The important part to remember is that QuickNet will generate random instances for us and that we don’t have to worry about it.</p>
<p>Now that we have the generators for the request instances, we can write a generator which can generate random ProcessInputElement instances:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ProcessInputTupleGenerator</span> : <span style="color: #2b91af">BaseGenerator</span>&lt;<span style="color: #2b91af">Tuple</span>&lt;<span style="color: #2b91af">ProcessInputElement</span>, <span style="color: #2b91af">ProcessInputElement</span>&gt;&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">FirstRequestGenerator</span> firstRequestGenerator = <span style="color: blue">new</span> <span style="color: #2b91af">FirstRequestGenerator</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">SecondRequestGenerator</span> secondRequestGenerator = <span style="color: blue">new</span> <span style="color: #2b91af">SecondRequestGenerator</span>();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: #2b91af">Tuple</span>&lt;<span style="color: #2b91af">ProcessInputElement</span>, <span style="color: #2b91af">ProcessInputElement</span>&gt; GetDefaultInstance()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> firstElement = <span style="color: blue">new</span> <span style="color: #2b91af">ProcessInputElement</span>(firstCachedRequestHandler, firstRequestGenerator.GetRandomValue(),</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">FirstCachedResponse</span>());</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> secondElement = <span style="color: blue">new</span> <span style="color: #2b91af">ProcessInputElement</span>(secondCachedRequestHandler, secondRequestGenerator.GetRandomValue(),</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">SecondCachedResponse</span>());</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: #2b91af">Tuple</span>.New(firstElement, secondElement);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Actually, this generator doesn’t just generate one instance of ProcessInputElement, it generates a tuple consisting of two ProcessInputElement instances.&#160; The first value in the tuple will always contain a random instance of FirstCachedRequest, its Request Handler and a FirstCachedResponse instance that needs to be returned by the Request Handler if there’s no cached response yet.&#160; The same goes for the second value in the tuple, except that it has a random instance of SecondCachedRequest, its Request Handler and a SecondCachedResponse instance.</p>
<p>Again, every time QuickNet will execute our transition, it will ask the ProcessInputTupleGenerator to generate a new tuple, and that tuple will contain the random request instances.&#160; The generated tuple is then passed into the transition and the transition will be executed.</p>
<p>Now i can finally show you the transition that we’ll define:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">ProcessRequestsTransition</span> : <span style="color: #2b91af">MetaTransition</span>&lt;<span style="color: #2b91af">Tuple</span>&lt;<span style="color: #2b91af">ProcessInputElement</span>, <span style="color: #2b91af">ProcessInputElement</span>&gt;, <span style="color: #2b91af">Response</span>[]&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> ProcessRequestsTransition()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Generator = <span style="color: blue">new</span> <span style="color: #2b91af">ProcessInputTupleGenerator</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Execute =</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; input =&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cacheManager.Clear(); <span style="color: green">// clears the spy for every execution of this transition</span></p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; input.First.StubHandler();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; input.Second.StubHandler();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> requestProcessor.Process(<span style="color: blue">new</span>[] {input.First.Request, input.Second.Request});</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>As you can see, the ProcessRequestsTransition inherits from the MetaTransition&lt;TInput, TOutput&gt; class.&#160; So we basically just defined that the input of the transition is a Tuple&lt;ProcessInputElement, ProcessInputElement&gt; and the output is an array of Response objects.&#160; In the constructor of the transition, we define that the generator to be used to generate the input values is the ProcessInputTupleGenerator.&#160; And we also define what actually needs to be executed by the transition.&#160; In this case, the piece of code that will always be executed will first clear the CacheManagerSpy instance (so we can safely inspect it during our specification verification), it will then prepare both Request Handler mocks and then it simply calls the Process method of the Request Processor.&#160; It’s output (a Response array) will be the output of the transition since that is our return value.</p>
<p>After all that work, we can finally define our specifications for this transition.&#160; Let’s start with the first one:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">SpecFor</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">ProcessRequestsTransition</span>))]</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Spec</span> ResponsesAreCachedIfTheyArentInTheCacheYet(<span style="color: #2b91af">Tuple</span>&lt;<span style="color: #2b91af">ProcessInputElement</span>, <span style="color: #2b91af">ProcessInputElement</span>&gt; input, <span style="color: #2b91af">Response</span>[] output)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">Spec</span>(() =&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">ProcessInputElement</span>&gt; verify =</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; element =&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Ensure</span>.Equal(element.Response, cacheManager.CacheEntries.First(e =&gt; e.Request.Equals(element.Request)).Response);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Ensure</span>.True(output.Contains(element.Response));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Ensure</span>.True(element.RequestHandlerWasExecuted);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (cacheManager.CacheEntries.Any(e =&gt; e.Request.Equals(input.First.Request)))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; verify(input.First);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (cacheManager.CacheEntries.Any(e =&gt; e.Request.Equals(input.Second.Request)))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; verify(input.Second);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; })</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .IfAfter(() =&gt; cacheManager.ReturnedCachedResponses.Any(r =&gt; r == <span style="color: blue">null</span>));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The SpecFor attribute makes it possible to define which Transition this Spec belongs to.&#160; The method has 2 parameters, which correspond with the input that will be passed into the transition, and the output that the transition returned.&#160; It’s very important to realize that <em>this method</em> is executed before the transition is executed (yet after the input has been generated), but the block of code that we pass into the Spec instance will only be executed after the transition has been executed.&#160; Actually, that block of code will only be executed if the Spec’s precondition and postcondition have been satisfied.&#160; The precondition (which can be defined with the Spec class’ If method) will be evaluated <em>before </em>the transition is executed.&#160; If it evaluates to false, the spec will be ignored for the <em>current</em> execution of the transition.&#160; The postcondition (which can be defined with the Spec class’ IfAfter method) is evaulated <em>after </em>the transition is executed.&#160; If it evaulates to false, the spec will be ignored.&#160; If it evaluates to true, the spec will be verified using the input that was passed into the transition and the output that was returned from the transition.</p>
<p>For this particular spec, we define a postcondition that the spec only needs to be verified if the CacheManager returned one or more null values instead of cached responses.&#160; If the CacheManager returns a null, it means that there was no cached response for the request it was given.&#160; And that is when we need to verify the following things:</p>
<ol>
<li>The request was handled by the Request Handler </li>
<li>The response that was returned by the Request Handler has been put in the cache </li>
<li>The response array that was returned by the Request Processor contains the expected response </li>
</ol>
<p>We perform this check for both requests, or only one of them, depending on which one was not in the cache yet.&#160; Remember that the transition will be executed 2000 times using <em>the same request processor and the same cache manager.</em>&#160; Which means that cached responses from <em>previous</em> transition executions might still be in the cache.&#160; Also keep in mind that one of the request types has a defined expiration of 1 second, and the other an expiration of 2 seconds which means that there will be plenty of transition executions where they are either both in the cache, only one of them, or none of them.&#160; All of those cases are now covered with the code in this spec.</p>
<p>Here’s another spec:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">SpecFor</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">ProcessRequestsTransition</span>))]</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Spec</span> CachedResponsesAreReturnedWhenAvailableInsteadOfCallingTheHandler(<span style="color: #2b91af">Tuple</span>&lt;<span style="color: #2b91af">ProcessInputElement</span>, <span style="color: #2b91af">ProcessInputElement</span>&gt; input, <span style="color: #2b91af">Response</span>[] output)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">Spec</span>(() =&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">ProcessInputElement</span>&gt; verify =</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; element =&gt;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Ensure</span>.False(element.RequestHandlerWasExecuted);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Ensure</span>.True(output.Contains(cacheManager.ReturnedCachedResponses.First(r =&gt; r != <span style="color: blue">null</span> &amp;&amp; r.GetType() == element.Response.GetType())));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; };</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (cacheManager.ReturnedCachedResponses.Any(r =&gt; r != <span style="color: blue">null</span> &amp;&amp;&#160; r.GetType() == input.First.Response.GetType()))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; verify(input.First);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (cacheManager.ReturnedCachedResponses.Any(r =&gt; r != <span style="color: blue">null</span> &amp;&amp; r.GetType() == input.Second.Response.GetType()))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; verify(input.Second);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; })</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .IfAfter(() =&gt; cacheManager.ReturnedCachedResponses.Any(r =&gt; r != <span style="color: blue">null</span>));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Here we verify that when a cached response was returned by the CacheManager:</p>
<ol>
<li>The Request Handler was <em>not</em> executed </li>
<li>The response array returned by the Request Processor contains the cached response </li>
</ol>
<p>We again perform this check for both requests, or only one of them depending on which one had a cached response.</p>
<p>You might think that this was a lot of work, but was it really?&#160; It’s about 160 lines of code, and i’m quite sure that the functionality that i’m testing is covered <em>much more thoroughly </em>than it would’ve been with 160 lines of code for classical unit tests.&#160;&#160; In fact, i challenge each one of you to come up with a more thorough test for this in less than 160 lines of code.&#160; Also, i can keep adding specs quite easily and those really don’t contain that much code.&#160; But my <em>functional coverage</em> would increase dramatically, much more than it would with classical unit tests.</p>
<p>Granted, the learning curve is steep as i mentioned already in a previous post.&#160; But once you start to get it, you become pretty productive with it and you’ll catch <em>a lot more bugs</em> before you deliver your software because of it.</p>
<p>Now, i tried hard to make everything here as clear as possible, so i’d love to hear from you whether or not:</p>
<ol>
<li>you understood it </li>
<li>you like it </li>
<li>you think it’s way over the top </li>
<li>you want to start doing this too </li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/testing-agathas-caching-functionality-with-quicknet/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>How QuickNet Found 2 Bugs That You And I Didn&#8217;t</title>
		<link>http://davybrion.com/blog/2009/12/how-quicknet-found-2-bugs-that-you-and-i-didnt/</link>
		<comments>http://davybrion.com/blog/2009/12/how-quicknet-found-2-bugs-that-you-and-i-didnt/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 21:56:48 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[agatha]]></category>
		<category><![CDATA[quicknet]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2124</guid>
		<description><![CDATA[I recently posted the first draft of the caching implementation of Agatha.&#160; You might want to take another look at the piece of code that deals with processing requests in the Request Processor.&#160; I thought it was alright.&#160; And if only 10% of my readers actually read that code, then it means that about 200 [...]]]></description>
			<content:encoded><![CDATA[<p>I recently posted the <a href="http://davybrion.com/blog/2009/12/agathas-caching-layer-implementation-first-draft/" target="_blank">first draft</a> of the caching implementation of Agatha.&#160; You might want to take another look at the piece of code that deals with processing requests in the Request Processor.&#160; I thought it was alright.&#160; And if only 10% of my readers actually read that code, then it means that about 200 people thought it was alright too because nobody mentioned a possible problem with that part.&#160; Today i was adding some cached requests/responses to the RequestProcessor’s <a href="http://code.google.com/p/quicknet/" target="_blank">QuickNet</a> exception handling test.&#160; And lo and behold, 2 bugs showed up that i hadn’t anticipated.</p>
<p>Before i discuss the two bugs, i’d like to explain what the exception handling quicknet test does, and how it works.&#160; It basically calls the request processing code a bunch of times, each time with a set of requests where either none, one or more of the request handlers will throw an exception during the handling of the requests.&#160; The order of the exceptions or which handlers will throw an exception is completely randomized (thanks to QuickNet) and thus it varies from test to test.&#160; This particular QuickNet test will perform 50 testruns, where each testrun consists of 50 transitions that will be executed.&#160; Each transition is a piece of functionality that will be executed (in this case, the processing of requests) with randomized input (in this case, there is a fixed set of requests but which ones will fail will differ with each run of the transition) and after a transition is executed, the relevant specs are verified (in this case, a bunch of checks to make sure that the whole error handling functionality always does what it needs to do) to make sure that the piece of code that you’re testing (your transition, in this case the processing of requests) actually works.</p>
<p>Instead of testing with a mocked caching layer, i used the real thing in the tests.&#160; I also added 2 cached request types to the test request types, and gave one of them an expiration of 1 second, the other an expiration of 2 seconds.&#160; This means that during the entire QuickNet testrun, some requests will return cached responses, some won’t, some cached responses will expire and subsequent requests of that particular type need to be executed and have their responses cached again until they expire.&#160;&#160; In the meantime, QuickNet is constantly firing requests at the RequestProcessor, and some of them will randomly fail.&#160; Our specs need to verify that the returned responses always contain the correct exception information for the batch of requests that was processed.</p>
<p>And here’s the beauty of the whole thing.&#160; I hadn’t even thought about how exception handling also influences how you deal with cached responses.&#160; I didn’t take exception handling into account when adding the caching code, and without even adding specific tests to see whether the caching worked correctly, my QuickNet specs that intended to cover something that doesn’t really have anything to do with caching uncovered 2 issues with my caching code.&#160;&#160; I was suddenly getting error messages from QuickNet that in some cases, the exception information wasn’t correct.&#160;&#160; And it frequently took over 30 executions of the transition, or sometimes multiple testruns before some of those specs would fail.&#160; The longer it takes for a bug to show up in a QuickNet test, the better the bug is at hiding and the harder it is to find it manually because it almost certainly is an edge-case that you hadn’t considered at all and that only occurs in certain situations.</p>
<p>And those are the type of bugs that you typically don’t find with traditional unit tests.&#160; When you write traditional tests that are aimed at asserting correct behavior, you typically only write tests for the problems that <em>you thought about</em>.&#160; Sometimes you’ll get lucky and traditional unit tests will inform you of incorrect behavior that you didn’t think of,&#160; but it rarely happens for the edge-cases.&#160; And those are the bugs that can be really hard and painful to find and fix.</p>
<p>By now you’re probably wondering what the two bugs are.&#160; The first one is that if an exception occurs during the handling of a request which is eligible for caching, the response with the exception information would be stored in the cache, and the next time that that particular request would be processed while the cached response hadn’t expired yet, you’d simply get the original response with the exception information again instead of actually handling the request again (which might have been handled successfully this time).&#160; The second bug was that if an earlier request in the batch of requests already failed, it could possibly return a valid response later on in the same batch for one of the cached requests if its response hadn’t expired yet.&#160;&#160; Pretty stupid huh?&#160; But they were there nevertheless, and sooner or later somebody would’ve tripped over it and it would’ve caused frustration for the user who would run into it, and pain for me because i’d have to go looking for it long after i’d written the code.</p>
<p>Again, i seriously doubt that traditional unit tests would’ve prevented the existence of these 2 problems since it never occurred to me while i was writing it.&#160; The QuickNet tests did find it, even before i added specific tests for the caching functionality to them, which i found pretty impressive and a huge benefit to this sort of testing.&#160; Granted, the learning curve for writing valuable QuickNet tests is <em>steep</em> (i’m not even halfway there IMO) and it takes a lot of effort when you’re starting out with it.&#160; But if it helps me prevent the sort of bugs that are otherwise easily missed and could negatively influence the perception of my project, then i definitely consider it worth it.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/how-quicknet-found-2-bugs-that-you-and-i-didnt/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Open Source Licensing, Copyright Notices, Contributions: What To Do?</title>
		<link>http://davybrion.com/blog/2009/12/open-source-licensing-copyright-notices-contributions-what-to-do/</link>
		<comments>http://davybrion.com/blog/2009/12/open-source-licensing-copyright-notices-contributions-what-to-do/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 13:09:30 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2104</guid>
		<description><![CDATA[There are two interesting problems that i’m facing with open-sourcing Agatha, and i’m not quite sure how to deal with it correctly.&#160; The first is how to apply the license of my choice to the actual code.&#160; I’m using Apache License 2.0, and this is their recommendation (emphasis mine): To apply the Apache License to [...]]]></description>
			<content:encoded><![CDATA[<p>There are two interesting problems that i’m facing with open-sourcing <a href="http://code.google.com/p/agatha-rrsl/" target="_blank">Agatha</a>, and i’m not quite sure how to deal with it correctly.&#160; The first is how to apply the license of my choice to the actual code.&#160; I’m using Apache License 2.0, and this is their recommendation (emphasis mine):</p>
<blockquote><p>To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets &quot;[]&quot; replaced with your own identifying information. (Don&#8217;t include the brackets!) <strong><em>The text should be enclosed in the appropriate comment syntax for the file format</em></strong>. We also recommend that a file or class name and description of purpose be included on the same &quot;printed page&quot; as the copyright notice for easier identification within third-party archives. </p>
<p>&#160;</p>
<p><code></code></p>
<p>Copyright [yyyy] [name of copyright owner] </p>
<p>&#160;</p>
<p>Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); </p>
<p><em><strong>you may not use this file except in compliance with the License</strong>.</em> </p>
<p>You may obtain a copy of the License at </p>
<p>&#160;</p>
<p><a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a> </p>
<p>&#160;</p>
<p>Unless required by applicable law or agreed to in writing, software </p>
<p>distributed under the License is distributed on an &quot;AS IS&quot; BASIS, </p>
<p>WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. </p>
<p>See the License for the specific language governing permissions and </p>
<p>limitations under the License. </p>
</blockquote>
<p>This sort of implies that i’m required to put this boilerplate notice <em>in each file</em>, which is just tedious and the odds are pretty high that you’re going to forget to put this in one (or more) of the files that will be added later on.</p>
<p>Instead, i put a LICENSE.txt file in the root folder of the project with the following content:</p>
<blockquote><p>Copyright 2009 Agatha RRSL Project, original authors and contributors </p>
<p>&#160;</p>
<p>Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);      <br /><strong><em>you may not use the files in this directory structure except in compliance with the License.          <br /></em></strong>You may obtain a copy of the License at </p>
<p>&#160;</p>
<p><a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a></p>
<p>&#160;</p>
<p>Unless required by applicable law or agreed to in writing, software      <br />distributed under the License is distributed on an &quot;AS IS&quot; BASIS,       <br />WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.       <br />See the License for the specific language governing permissions and       <br />limitations under the License.</p>
</blockquote>
<p>Honestly, i have no idea if i’m allowed to do that.&#160; I can’t think of a reason why i shouldn’t be able to do this since it certainly sounds reasonable to do so, but then again i’m not a lawyer and i wouldn’t be surprised if there are reasons for <em>not</em> doing so.&#160; If anyone can provide advice on this, it would be much appreciated <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Another thing that left me wondering is the actual copyright notice.&#160; In the 1.0 release, i unfortunately used the following copyright statement for both the LICENSE.txt as well as the copyright information on the assembly level:</p>
<blockquote><p>Copyright 2009 Davy Brion</p>
</blockquote>
<p>Now, i did write most of the code, i did start the project and i do own the project.&#160; But i did not write all of the code as there have been some contributions from other people.&#160; I certainly want to give credit where credit is due, so i’ve changed that copyright notice (both in the LICENSE.txt file and the assembly copyright notices) to:</p>
<blockquote><p>Copyright 2009 Agatha RRSL Project, original authors and contributors </p>
</blockquote>
<p>I then added a CONTRIBUTORS.txt file at the same location of the LICENSE.txt file which contains the names of the contributors and the parts they worked on.&#160; I <em>think</em> that’s all i need to do to to be fair and correct, but again, i’m not entirely sure on this.</p>
<p>If anyone can provide feedback on these issues, please do so <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/open-source-licensing-copyright-notices-contributions-what-to-do/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Agatha 1.0</title>
		<link>http://davybrion.com/blog/2009/12/agatha-1-0/</link>
		<comments>http://davybrion.com/blog/2009/12/agatha-1-0/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 13:40:41 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2009/12/agatha-1-0/</guid>
		<description><![CDATA[I just released Agatha 1.0 which you can download here.&#160; The biggest changes since the 1.0 beta 2 release is the support for the one-way requests and the ability to easily run the service layer within the same process as your client (if you don’t need or want WCF for example). As for the next [...]]]></description>
			<content:encoded><![CDATA[<p>I just released Agatha 1.0 which you can download <a href="http://code.google.com/p/agatha-rrsl/downloads/list" target="_blank">here</a>.&#160; The biggest changes since the 1.0 beta 2 release is the support for the <a href="http://davybrion.com/blog/2009/12/one-way-or-fire-and-forget-requests-with-agatha/" target="_blank">one-way requests</a> and the ability to easily run the service layer <a href="http://davybrion.com/blog/2009/12/running-an-agatha-service-layer-in-process-without-wcf/" target="_blank">within the same process as your client</a> (if you don’t need or want WCF for example).</p>
<p>As for the next version (1.1) the biggest focus will be on the caching layer, and the ability to pick your own error handling strategy.</p>
<p>I hope the few users that we have will enjoy this <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/agatha-1-0/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Enabling Logging In Your Agatha Service Layer</title>
		<link>http://davybrion.com/blog/2009/12/enabling-logging-in-your-agatha-service-layer/</link>
		<comments>http://davybrion.com/blog/2009/12/enabling-logging-in-your-agatha-service-layer/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 13:05:22 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1971</guid>
		<description><![CDATA[As of Agatha 1.0 beta 2, you can now use whatever logging library you prefer.&#160; Agatha now uses the Common.Logging project instead of using a logging library directly.&#160; This means you just need to add a bit of configuration to your service host to enable logging and get that logging information in whatever format or [...]]]></description>
			<content:encoded><![CDATA[<p>As of Agatha 1.0 beta 2, you can now use whatever logging library you prefer.&#160; Agatha now uses the <a href="http://netcommon.sourceforge.net/" target="_blank">Common.Logging</a> project instead of using a logging library directly.&#160; This means you just need to add a bit of configuration to your service host to enable logging and get that logging information in whatever format or manner you want.&#160; Enabling this is pretty easy, since you just need to follow the standard approaches for <a href="http://netcommon.sourceforge.net/docs/2.0.0/reference/html/ch01.html#logging-config" target="_blank">configuring Common.Logging</a>, and obviously also for the logging library that you use.&#160; I’m just going to show a short example of using XML in your web.config (or app.config depending on your service host) to get this working.</p>
<p>First, you need to add the configuration section for Common.Logging to the &lt;configSections&gt; element:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">sectionGroup</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">common</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">section</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">logging</span>&quot;<span style="color: blue"> </span><span style="color: red">type</span><span style="color: blue">=</span>&quot;<span style="color: blue">Common.Logging.ConfigurationSectionHandler, Common.Logging</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">sectionGroup</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">section</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">log4net</span>&quot;<span style="color: blue"> </span><span style="color: red">type</span><span style="color: blue">=</span>&quot;<span style="color: blue">log4net.Config.Log4NetConfigurationSectionHandler,Log4net</span>&quot;<span style="color: blue">/&gt;</span></p>
</p></div>
<p>&#160;</p>
<p>As you can see, i also included the definition of my preferred logging library (log4net) in this as well.&#160; In this particular case, your service host needs to reference the following assemblies: Common.Logging.dll, Common.Logging.Log4Net.dll and log4net.dll.</p>
<p>The configuration of the Common.Logging library looks like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">&#160; &lt;</span><span style="color: #a31515">common</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">logging</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">factoryAdapter</span><span style="color: blue"> </span><span style="color: red">type</span><span style="color: blue">=</span>&quot;<span style="color: blue">Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">arg</span><span style="color: blue"> </span><span style="color: red">key</span><span style="color: blue">=</span>&quot;<span style="color: blue">configType</span>&quot;<span style="color: blue"> </span><span style="color: red">value</span><span style="color: blue">=</span>&quot;<span style="color: blue">INLINE</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">factoryAdapter</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">logging</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160; &lt;/</span><span style="color: #a31515">common</span><span style="color: blue">&gt;</span></p>
</p></div>
<p>&#160;</p>
<p>You need to specify which factoryAdapter that Common.Logging will use, in our case the one for log4net.&#160; If you then configure the INLINE value for the configType key, Common.Logging will just initialize log4net in a way that it will simply use the XML configuration that is also present in your config file.&#160; Again, i’ll point you to the <a href="http://netcommon.sourceforge.net/docs/2.0.0/reference/html/ch01.html#logging-config" target="_blank">Common.Logging documentation</a> for configuring it for your specific library if you’re using something else.&#160; </p>
<p>In my case, my log4net configuration looks like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">&#160; &lt;</span><span style="color: #a31515">log4net</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">logger</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Agatha</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">level</span><span style="color: blue"> </span><span style="color: red">value</span><span style="color: blue">=</span>&quot;<span style="color: blue">ERROR</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">appender-ref</span><span style="color: blue"> </span><span style="color: red">ref</span><span style="color: blue">=</span>&quot;<span style="color: blue">LogFileAppender</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">logger</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">logger</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">AgathaPerformance</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">level</span><span style="color: blue"> </span><span style="color: red">value</span><span style="color: blue">=</span>&quot;<span style="color: blue">WARNING</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">appender-ref</span><span style="color: blue"> </span><span style="color: red">ref</span><span style="color: blue">=</span>&quot;<span style="color: blue">LogFileAppender</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">logger</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;</span><span style="color: #a31515">appender</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">LogFileAppender</span>&quot;<span style="color: blue"> </span><span style="color: red">type</span><span style="color: blue">=</span>&quot;<span style="color: blue">log4net.Appender.RollingFileAppender</span>&quot;<span style="color: blue"> &gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">param</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">File</span>&quot;<span style="color: blue"> </span><span style="color: red">value</span><span style="color: blue">=</span>&quot;<span style="color: blue">log.txt</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">param</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">AppendToFile</span>&quot;<span style="color: blue"> </span><span style="color: red">value</span><span style="color: blue">=</span>&quot;<span style="color: blue">true</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">rollingStyle</span><span style="color: blue"> </span><span style="color: red">value</span><span style="color: blue">=</span>&quot;<span style="color: blue">Size</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">maxSizeRollBackups</span><span style="color: blue"> </span><span style="color: red">value</span><span style="color: blue">=</span>&quot;<span style="color: blue">10</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">maximumFileSize</span><span style="color: blue"> </span><span style="color: red">value</span><span style="color: blue">=</span>&quot;<span style="color: blue">10MB</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">staticLogFileName</span><span style="color: blue"> </span><span style="color: red">value</span><span style="color: blue">=</span>&quot;<span style="color: blue">true</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">layout</span><span style="color: blue"> </span><span style="color: red">type</span><span style="color: blue">=</span>&quot;<span style="color: blue">log4net.Layout.PatternLayout</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">param</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">ConversionPattern</span>&quot;<span style="color: blue"> </span><span style="color: red">value</span><span style="color: blue">=</span>&quot;<span style="color: blue">%-5p%d{yyyy-MM-dd hh:mm:ss} &#8211; %m%n</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">layout</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">appender</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: blue">&#160; &lt;/</span><span style="color: #a31515">log4net</span><span style="color: blue">&gt;</span></p>
</p></div>
<p>&#160;</p>
<p>The ‘Agatha’ logger simply logs everything coming from types whose namespace starts with Agatha and that logs at the error level (basically just exceptions caught by the Request Processor) and sends that the LogFileAppender (which simply logs in a log.txt) file.&#160; The ‘AgathaPerformance’ logger specifically logs performance warnings coming from Agatha’s PerformanceLoggingRequestProcessor and also sends it to the LogFileAppender.</p>
<p>And that’s all there is to it. </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/enabling-logging-in-your-agatha-service-layer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Agatha&#8217;s Caching Layer Implementation: First Draft</title>
		<link>http://davybrion.com/blog/2009/12/agathas-caching-layer-implementation-first-draft/</link>
		<comments>http://davybrion.com/blog/2009/12/agathas-caching-layer-implementation-first-draft/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 19:56:27 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2049</guid>
		<description><![CDATA[Well i got a bit carried away and started implementing the caching layer already… i’d like to go over the details of it in this post, but keep in mind that it’s only a first draft and that this will not be in the upcoming 1.0 release.&#160;&#160; In fact, i haven’t even tested this yet… [...]]]></description>
			<content:encoded><![CDATA[<p>Well i got a bit carried away and started implementing the caching layer already… i’d like to go over the details of it in this post, but keep in mind that it’s only a first draft and that this will <em>not</em> be in the upcoming 1.0 release.&#160;&#160; In fact, i haven’t even tested this yet… i don’t even know if it runs.&#160; This is just to get some feedback on the design.&#160; Once it works, it’ll need some time to mature and to be tested properly so it’ll be in the 1.1 release.&#160; With that out of the way, let’s get started.</p>
<p>First of all, there’s the following attribute that you can put on your Request types:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #2b91af">AttributeUsage</span>(<span style="color: #2b91af">AttributeTargets</span>.Class, Inherited = <span style="color: blue">false</span>, AllowMultiple = <span style="color: blue">false</span>)]</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">EnableResponseCachingAttribute</span> : <span style="color: #2b91af">Attribute</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Region { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">int</span> Hours { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">int</span> Minutes { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">int</span> Seconds { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">TimeSpan</span> Expiration </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; { </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">get</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (Hours == 0 &amp;&amp; Minutes == 0 &amp;&amp; Seconds == 0)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">InvalidOperationException</span>(<span style="color: #a31515">&quot;You need to specific at least an hour value, a minute value or a second value&quot;</span>);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">TimeSpan</span>(0, Hours, Minutes, Seconds);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>I really want to support both programmatic configuration as well as XML configuration (which would always override any existing programmatic configuration for a particular request) but for now, this attribute is the only way.&#160; Also, everything in this caching layer assumes that your requests which enable response caching <em>must</em> override the Equals and GetHashCode() methods correctly (hint: use Resharper for that).</p>
<p>When we configure Agatha, we need to transform the caching metadata into objects of the following class:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">RequestCacheConfiguration</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Type</span> RequestType { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">TimeSpan</span> Expiration { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> Region { <span style="color: blue">get</span>; <span style="color: blue">private</span> <span style="color: blue">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> RequestCacheConfiguration(<span style="color: #2b91af">Type</span> requestType, <span style="color: #2b91af">TimeSpan</span> expiration, <span style="color: blue">string</span> region)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RequestType = requestType;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Expiration = expiration;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Region = region;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>And we also need something that contains all of the caching configuration data:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">CacheConfiguration</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: #2b91af">RequestCacheConfiguration</span>&gt; requestCacheConfigurations;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> CacheConfiguration(<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Request</span>&gt; knownRequests)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; BuildMapOfConfigurationsForRequestsThatEnabledResponseCaching(knownRequests);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">void</span> BuildMapOfConfigurationsForRequestsThatEnabledResponseCaching(<span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Request</span>&gt; knownRequests)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; requestCacheConfigurations = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: #2b91af">RequestCacheConfiguration</span>&gt;();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (<span style="color: blue">var</span> request <span style="color: blue">in</span> knownRequests)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> requestType = request.GetType();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> attribute = <span style="color: #2b91af">Attribute</span>.GetCustomAttribute(requestType, <span style="color: blue">typeof</span>(<span style="color: #2b91af">EnableResponseCachingAttribute</span>)) <span style="color: blue">as</span> <span style="color: #2b91af">EnableResponseCachingAttribute</span>;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (attribute != <span style="color: blue">null</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; requestCacheConfigurations.Add(requestType, <span style="color: blue">new</span> <span style="color: #2b91af">RequestCacheConfiguration</span>(requestType, attribute.Expiration, attribute.Region));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">bool</span> IsCachingEnabledFor(<span style="color: #2b91af">Type</span> requestType)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> requestCacheConfigurations.ContainsKey(requestType);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">RequestCacheConfiguration</span> GetConfigurationFor(<span style="color: #2b91af">Type</span> requestType)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> requestCacheConfigurations[requestType];</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">string</span>&gt; GetRegionNames()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">HashSet</span>&lt;<span style="color: blue">string</span>&gt;(requestCacheConfigurations.Values.Where(v =&gt; !<span style="color: blue">string</span>.IsNullOrEmpty(v.Region)).Select(v =&gt; v.Region));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">string</span> GetRegionNameFor(<span style="color: #2b91af">Type</span> requestType)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> requestCacheConfigurations[requestType].Region;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>These classes already give us all we need to know (for now, at least) to set up our cache.&#160; As mentioned previously, i wanted to use a region-based approach like NHibernate’s 2nd Level Cache.&#160; Each region is basically a logical cache (and depending on the caching provider you’ll use, perhaps even a separate physical one) which is separated from the other ones.&#160; It gives us the ability to specifically clear a logical cache.</p>
<p>So, we need something which can create a cache for a region:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">ICacheProvider</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">ICache</span> BuildCache(<span style="color: blue">string</span> region);</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>And the cache itself would need to expose the following methods:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">ICache</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Response</span> GetCachedResponseFor(<span style="color: #2b91af">Request</span> request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Store(<span style="color: #2b91af">Request</span> request, <span style="color: #2b91af">Response</span> response, <span style="color: #2b91af">TimeSpan</span> expiration);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Clear();</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>So now we can write something that manages the whole caching thing.&#160; I couldn’t come up with a good name, so i just named the class CacheManager (i know it sucks, so suggest a better name if you know one):</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">ICacheManager</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">bool</span> IsCachingEnabledFor(<span style="color: #2b91af">Type</span> requestType);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Response</span> GetCachedResponseFor(<span style="color: #2b91af">Request</span> request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> StoreInCache(<span style="color: #2b91af">Request</span> request, <span style="color: #2b91af">Response</span> response);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Clear(<span style="color: blue">string</span> region);</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">CacheManager</span> : <span style="color: #2b91af">ICacheManager</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">const</span> <span style="color: blue">string</span> defaultRegion = <span style="color: #a31515">&quot;_defaultRegion&quot;</span>;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">CacheConfiguration</span> configuration;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">ICacheProvider</span> cacheProvider;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: #2b91af">ICache</span>&gt; caches;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> CacheManager(<span style="color: #2b91af">CacheConfiguration</span> configuration, <span style="color: #2b91af">ICacheProvider</span> cacheProvider)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.configuration = configuration;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.cacheProvider = cacheProvider;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; BuildCaches();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">void</span> BuildCaches()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> regions = configuration.GetRegionNames();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; caches = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: #2b91af">ICache</span>&gt;(regions.Count() + 1);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; caches.Add(defaultRegion, cacheProvider.BuildCache(defaultRegion));</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (<span style="color: blue">var</span> region <span style="color: blue">in</span> regions)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; caches.Add(region, cacheProvider.BuildCache(region));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">bool</span> IsCachingEnabledFor(<span style="color: #2b91af">Type</span> requestType)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> configuration.IsCachingEnabledFor(requestType);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Response</span> GetCachedResponseFor(<span style="color: #2b91af">Request</span> request)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> caches[configuration.GetRegionNameFor(request.GetType()) ?? defaultRegion].GetCachedResponseFor(request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> StoreInCache(<span style="color: #2b91af">Request</span> request, <span style="color: #2b91af">Response</span> response)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> config = configuration.GetConfigurationFor(request.GetType());</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; caches[config.Region ?? defaultRegion].Store(request, response, config.Expiration);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Clear(<span style="color: blue">string</span> region)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; caches[region].Clear();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>The RequestProcessor’s ugly request-processing-loop (i know i need to clean that up) could then look like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (<span style="color: blue">var</span> request <span style="color: blue">in</span> requests)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">try</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> cachingIsEnabledForThisRequest = cacheManager.IsCachingEnabledFor(request.GetType());</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (cachingIsEnabledForThisRequest)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> cachedResponse = cacheManager.GetCachedResponseFor(request);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (cachedResponse != <span style="color: blue">null</span>)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; responses.Add(cachedResponse);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">continue</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">using</span> (<span style="color: blue">var</span> handler = (<span style="color: #2b91af">IRequestHandler</span>)<span style="color: #2b91af">IoC</span>.Container.Resolve(GetResponseHandlerTypeFor(request)))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">try</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!exceptionsPreviouslyOccurred)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> response = GetResponseFromHandler(request, handler);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; exceptionsPreviouslyOccurred = response.ExceptionType != <span style="color: #2b91af">ExceptionType</span>.None;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; responses.Add(response);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (cachingIsEnabledForThisRequest)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cacheManager.StoreInCache(request, response);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">else</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> response = handler.CreateDefaultResponse();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; response.ExceptionType = <span style="color: #2b91af">ExceptionType</span>.EarlierRequestAlreadyFailed;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; response.Exception = <span style="color: blue">new</span> <span style="color: #2b91af">ExceptionInfo</span>(<span style="color: blue">new</span> <span style="color: #2b91af">Exception</span>(<span style="color: #2b91af">ExceptionType</span>.EarlierRequestAlreadyFailed.ToString()));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; responses.Add(response);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">finally</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IoC</span>.Container.Release(handler);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">catch</span> (<span style="color: #2b91af">Exception</span> e)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; logger.Error(e);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>And that’s it, so far.&#160;&#160; Obviously, we need ICache and ICacheProvider implementations for the various caching libraries that are already available.&#160; I already wrote an InMemoryCache and InMemoryCacheProvider to facilitate early experimentation/testing of the caching layer:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">InMemoryCacheProvider</span> : <span style="color: #2b91af">ICacheProvider</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">ICache</span> BuildCache(<span style="color: blue">string</span> s)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">new</span> <span style="color: #2b91af">InMemoryCache</span>();&#160;&#160;&#160; </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: green">// this class should probably never be used in production, but will be useful for testing <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </span></p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">InMemoryCache</span> : <span style="color: #2b91af">ICache</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: blue">object</span> monitor = <span style="color: blue">new</span> <span style="color: blue">object</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Request</span>, <span style="color: #2b91af">Response</span>&gt; cachedResponses;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Timer</span>, <span style="color: #2b91af">Request</span>&gt; timersToRequests;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> InMemoryCache()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Initialize();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">void</span> Initialize()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cachedResponses = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Request</span>, <span style="color: #2b91af">Response</span>&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timersToRequests = <span style="color: blue">new</span> <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Timer</span>, <span style="color: #2b91af">Request</span>&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: #2b91af">Response</span> GetCachedResponseFor(<span style="color: #2b91af">Request</span> request)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span>(monitor)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (!cachedResponses.ContainsKey(request))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">null</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> cachedResponses[request];</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Store(<span style="color: #2b91af">Request</span> request, <span style="color: #2b91af">Response</span> response, <span style="color: #2b91af">TimeSpan</span> expiration)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> timer = <span style="color: blue">new</span> <span style="color: #2b91af">Timer</span>(expiration.Milliseconds);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span> (monitor)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (cachedResponses.ContainsKey(request))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// another request handler stored a cached response already during the processing of this request&#8230;</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// the last one wins, so get rid of the previous one, including its timer</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cachedResponses.Remove(request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> otherTimer = timersToRequests.First(keyValuePair =&gt; keyValuePair.Value.Equals(request)).Key;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; GetRidOfTimer(otherTimer);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timersToRequests.Remove(otherTimer);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cachedResponses[request] = response;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timersToRequests[timer] = request;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timer.Elapsed += timer_Elapsed;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timer.Start();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> timer_Elapsed(<span style="color: blue">object</span> sender, <span style="color: #2b91af">ElapsedEventArgs</span> e)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> timer = (<span style="color: #2b91af">Timer</span>)sender;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span> (monitor)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// if the Clear method has been called before a timer has elapsed, it won&#8217;t be in the timersToRequest dictionary anymore</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (timersToRequests.ContainsKey(timer))</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> request = timersToRequests[timer];</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cachedResponses.Remove(request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timersToRequests.Remove(timer);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timer.Dispose();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Clear()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">lock</span>(monitor)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">foreach</span> (<span style="color: blue">var</span> timer <span style="color: blue">in</span> timersToRequests.Keys)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; GetRidOfTimer(timer);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Initialize();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">void</span> GetRidOfTimer(<span style="color: #2b91af">Timer</span> timer) </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timer.Elapsed -= timer_Elapsed;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timer.Stop();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timer.Dispose();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>That’s it so far… i probably forgot about a lot of stuff that i really should take into account, but at least it’s something that we can start with.&#160; Or to throw away and start over <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/agathas-caching-layer-implementation-first-draft/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Agatha Feature Suggestion: Caching</title>
		<link>http://davybrion.com/blog/2009/12/agatha-feature-suggestion-caching/</link>
		<comments>http://davybrion.com/blog/2009/12/agatha-feature-suggestion-caching/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 15:16:21 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/2009/12/agatha-feature-suggestion-caching/</guid>
		<description><![CDATA[Someone recently asked me if it would be possible to cache responses with an Agatha service layer and just send back the cached responses when matching requests are sent to the service layer.&#160; I’ve been thinking about this a bit lately, and i think it would be a tremendous feature.&#160; In this post, i just [...]]]></description>
			<content:encoded><![CDATA[<p>Someone recently asked me if it would be possible to cache responses with an Agatha service layer and just send back the cached responses when matching requests are sent to the service layer.&#160; I’ve been thinking about this a bit lately, and i think it would be a tremendous feature.&#160; In this post, i just want to list a couple of ideas on how it could work and what we’d need to take into account to make it work.&#160; Please feel free to leave your suggestions/thoughts on this since i can definitely use all of the feedback i can get to implement a feature like this.</p>
<p>The idea is basically to implement a caching layer which works very similar to NHibernate’s Second Level Cache.&#160; You would need to be able to define which request/response combination is eligible for caching.&#160; When a request/response combination is eligible for caching, Agatha should check to see if a cached response is already available for a particular incoming request.&#160; That would mean that the request type would need to override the Equals method to compare the values.&#160;&#160; If a previous request has already been processed which is equal to the current request, the corresponding cached response could be sent back immediately without having to handle it again.&#160; Obviously, this check would only happen for requests which have been made eligible for caching.</p>
<p>We’d also need to provide a way to invalidate the cache.&#160; Both time-based expiration and explicit removal from the cache should be possible.&#160; For time-based expiration we could just provide some sort of time-out setting in the same place where you enable the caching for the request.&#160; I’d probably do that through a .NET attribute which you can put on your Request type, and the time-out would simply be a property of the attribute.&#160; For explicit removal, i’d probably go with a ‘region’-approach, similar to the regions in NHibernate’s Second Level Cache.&#160; For instance, when declaring a request to be eligible for caching, one of the properties of the caching attribute could be a region name, and then we’d need to give you the ability to clear a certain region of the cache in your request handlers when it’s necessary to do so.</p>
<p>That’s pretty much all i can think of for now.&#160; It’s not much yet, but i do think it could really be a tremendous improvement to Agatha.&#160; The performance benefits for requests that deal with mostly static data, or data that doesn’t change frequently could be huge.&#160; And the required coding effort for developers using Agatha would be minimal… simply put the correct attribute on your request type and explicit clearing of the right cache region when necessary would be all there is to it.</p>
<p>Thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/agatha-feature-suggestion-caching/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Agatha&#8217;s First (Vocal) Happy User</title>
		<link>http://davybrion.com/blog/2009/12/agathas-first-vocal-happy-user/</link>
		<comments>http://davybrion.com/blog/2009/12/agathas-first-vocal-happy-user/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 21:47:17 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=2027</guid>
		<description><![CDATA[This made my day. Favorite quote from the post: When I read about this it looked good. After one day of using it, I’m impressed, … really impressed. It doesn’t use a lot of fancy patterns, just some tried and tested basic OO stuff, and it forces you to abide by these rules.]]></description>
			<content:encoded><![CDATA[<p><a href="http://kilfour.wordpress.com/2009/12/09/a-taste-of-mistery/">This</a> made my day.</p>
<p>Favorite quote from the post:</p>
<blockquote><p>When I read about this it looked good. After one day of using it, I’m impressed, … really impressed.<br />
It doesn’t use a lot of fancy patterns, just some tried and tested basic OO stuff, and it forces you to abide by these rules.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/agathas-first-vocal-happy-user/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Running An Agatha Service Layer In-Process (Without WCF)</title>
		<link>http://davybrion.com/blog/2009/12/running-an-agatha-service-layer-in-process-without-wcf/</link>
		<comments>http://davybrion.com/blog/2009/12/running-an-agatha-service-layer-in-process-without-wcf/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 06:00:05 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1999</guid>
		<description><![CDATA[I’ve mentioned previously that Agatha does not require you to use WCF.&#160; You can build your Request/Response Service Layer and host it through WCF, or you can just as well ‘host’ or run it in-process.&#160; Why would you want to do that?&#160; Well, suppose you’re working on a web application where you don’t really need [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve mentioned previously that Agatha does not require you to use WCF.&#160; You can build your Request/Response Service Layer and host it through WCF, or you can just as well ‘host’ or run it in-process.&#160; Why would you want to do that?&#160; Well, suppose you’re working on a web application where you don’t really need (or want) to have your business logic running behind a WCF service.&#160; Using a Request/Response Service Layer in this scenario still offers you quite a few architectural benefits: your service layer would basically be some kind of dynamic facade to your business layer where you can still centralize the way you deal with transactions, exceptions, logging and any other cross cutting concerns you want to handle in <em>one</em> place.&#160; As an added bonus, you could <em>very easily</em> move your service layer and its business logic to a separate machine/service and host it through WCF without any major modifications to your code.&#160; It would actually be trivially easy as long as your service layer doesn’t share state in any way with your upper layers (presentation layer or application layer if you will).</p>
<p>Let’s see how easily you can use your Request/Response Service Layer in-process with Agatha.&#160; Do you remember the <a href="http://davybrion.com/blog/2009/11/hello-world-with-agatha/" target="_blank">full-blown Hello World, Agatha Style example</a>?&#160; Let’s try to use that service layer and use it in-process without WCF.&#160; First of all, i added a <a href="http://code.google.com/p/agatha-rrsl/source/browse/#svn/trunk/examples/Sample.ServiceAndClientInSameProcess" target="_blank">new project to my sample solution</a>.&#160; It references both the Sample.Common and the Sample.ServiceLayer assemblies.&#160; Sample.Common contains my Request/Response types, and Sample.ServiceLayer contains the Request Handlers that the service layer consists of.&#160; To configure both the client-side components (mainly the request dispatchers) as well as the server-side components (mainly the request processor and your request handlers), we just need to do this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: blue">void</span> InitializeAgatha()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ServiceLayerAndClientConfiguration</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">HelloWorldHandler</span>).Assembly, </p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">typeof</span>(<span style="color: #2b91af">HelloWorldRequest</span>).Assembly, <span style="color: blue">new</span> Agatha.Castle.<span style="color: #2b91af">Container</span>())</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Initialize();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>And that’s it.&#160; No WCF configuration necessary.&#160; No required XML at all.&#160; The ServiceLayerAndClientConfiguration class will take care of everything and you can use your Request/Response Service Layer both synchronously and asynchronously in your code.&#160;&#160; The service layer will behave exactly the same as it would when running on another machine or in a different process through WCF.&#160; And you’ll still get to benefit from all of the advantages that Agatha can offer you.</p>
<p>Note: I made some changes to the codebase after the 1.0 beta 2 release to make this so easy (and also to get the async stuff working properly when running in-process) so you will need to use the current trunk version (as of revision 38) or wait for the next release.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/running-an-agatha-service-layer-in-process-without-wcf/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>One-Way (Or Fire And Forget) Requests With Agatha</title>
		<link>http://davybrion.com/blog/2009/12/one-way-or-fire-and-forget-requests-with-agatha/</link>
		<comments>http://davybrion.com/blog/2009/12/one-way-or-fire-and-forget-requests-with-agatha/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 12:48:08 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1989</guid>
		<description><![CDATA[Arjen Smits recently submitted a patch for Agatha which makes it possible to send One-Way (aka Fire And Forget) requests to an Agatha service layer.&#160; As you know, Agatha is a Request/Response framework where each Request sent to the service layer will receive a corresponding Response.&#160; A One-Way request doesn’t have a response however.&#160; It [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://danthar.tweakblogs.net/" target="_blank">Arjen Smits</a> recently submitted a patch for <a href="http://code.google.com/p/agatha-rrsl/" target="_blank">Agatha</a> which makes it possible to send One-Way (aka Fire And Forget) requests to an Agatha service layer.&#160; As you know, Agatha is a Request/Response framework where each Request sent to the service layer will receive a corresponding Response.&#160; A One-Way request doesn’t have a response however.&#160; It can be very useful when you just need the service layer to do <em>something</em> where the client that sent the request doesn’t need a response, and thus, shouldn’t have to wait for it.</p>
<p>The biggest benefit here is that client can simply send a One-Way request to the service layer, and instead of waiting for the service layer to process it, the call to the service layer will return <em>immediately.&#160; </em>You could achieve similar behavior by using the asynchronous service proxy and supplying an empty delegate to be called after the response was received.&#160; The downside of that is obviously that the communication channel between the client and the server would remain in use throughout the handling of the request, only to return a response later on that will be ignored anyway.&#160; Sending a One-Way request avoids this kind of waste.</p>
<p>Let’s see how we can use One-Way requests with Agatha.</p>
<p>First of all, you will need to create a Request class which inherits from Agatha’s OneWayRequest class:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">abstract</span> <span style="color: blue">class</span> <span style="color: #2b91af">OneWayRequest</span> : <span style="color: #2b91af">Request</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Normally you’d also create a corresponding Response type for your Request type, but for Requests that inherit from OneWayRequest that’s useless.&#160; You’ll also need to create a Request Handler which inherits from Agatha’s OneWayRequestHandler class:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">abstract</span> <span style="color: blue">class</span> <span style="color: #2b91af">OneWayRequestHandler</span>&lt;TRequest&gt; : <span style="color: #2b91af">OneWayRequestHandler</span>, <span style="color: #2b91af">IOneWayRequestHandler</span>&lt;TRequest&gt; <span style="color: blue">where</span> TRequest : <span style="color: #2b91af">OneWayRequest</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">override</span> <span style="color: blue">void</span> Handle(<span style="color: #2b91af">OneWayRequest</span> request)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> typedRequest = (TRequest) request;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; BeforeHandle(typedRequest);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Handle(typedRequest);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AfterHandle(typedRequest);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> BeforeHandle(TRequest request) { }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">void</span> AfterHandle(TRequest request) { }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">abstract</span> <span style="color: blue">void</span> Handle(TRequest request);</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>This is very similar to a regular RequestHandler, except that there’s no way for you to return a response.&#160; Simply inherit from this class, implement the Handle(TRequest) method to perform whatever it is that you need to do and that’s it as far as the service layer is concerned.&#160; If you want to, you can (as always) override the BeforeHandle and AfterHandle methods to add some extra stuff.&#160; If you don’t want to inherit from this OneWayRequestHandler class, you can just as well create a class which implements Agatha’s IOneWayRequestHandler&lt;TRequest&gt; interface.&#160; Your One Way Request Handlers will be picked up and registered by Agatha automatically, just as it does with regular Request Handlers.</p>
<p>As for sending One Way Requests to your service layer, you can do that with both the synchronous IRequestDispatcher and the IAsyncRequestDispatcher.&#160; The IRequestDispatcher interface now looks like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IRequestDispatcher</span> : <span style="color: #2b91af">IDisposable</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: #2b91af">Response</span>&gt; Responses { <span style="color: blue">get</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Add(<span style="color: #2b91af">Request</span> request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Add(<span style="color: blue">params</span> <span style="color: #2b91af">Request</span>[] requestsToAdd);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Add(<span style="color: blue">string</span> key, <span style="color: #2b91af">Request</span> request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Send(<span style="color: blue">params</span> <span style="color: #2b91af">OneWayRequest</span>[] oneWayRequests);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">bool</span> HasResponse&lt;TResponse&gt;() <span style="color: blue">where</span> TResponse : <span style="color: #2b91af">Response</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; TResponse Get&lt;TResponse&gt;() <span style="color: blue">where</span> TResponse : <span style="color: #2b91af">Response</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; TResponse Get&lt;TResponse&gt;(<span style="color: blue">string</span> key) <span style="color: blue">where</span> TResponse : <span style="color: #2b91af">Response</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; TResponse Get&lt;TResponse&gt;(<span style="color: #2b91af">Request</span> request) <span style="color: blue">where</span> TResponse : <span style="color: #2b91af">Response</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Clear();</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>Sending OneWayRequests can be done through the Send method, which will immediately send the given OneWayRequests to the service layer and the call will return as soon as the requests have been transmitted.&#160; This means that this call will block for a short time until the requests have actually been <em>sent</em>.&#160; It will return before the requests are actually processed by the service layer however.</p>
<p>The IAsyncRequestDispatcher now looks like this:</p>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IAsyncRequestDispatcher</span> : <span style="color: #2b91af">IDisposable</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Add(<span style="color: #2b91af">Request</span> request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Add(<span style="color: blue">params</span> <span style="color: #2b91af">Request</span>[] requestsToAdd);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Add(<span style="color: blue">string</span> key, <span style="color: #2b91af">Request</span> request);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Add(<span style="color: blue">params</span> <span style="color: #2b91af">OneWayRequest</span>[] oneWayRequests);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> ProcessOneWayRequests();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> ProcessRequests(<span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">ReceivedResponses</span>&gt; receivedResponsesDelegate, <span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">ExceptionInfo</span>&gt; exceptionOccurredDelegate);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> ProcessRequests(<span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">ReceivedResponses</span>&gt; receivedResponsesDelegate, <span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">ExceptionInfo</span>, <span style="color: #2b91af">ExceptionType</span>&gt; exceptionAndTypeOccurredDelegate);</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p>&#160;</p>
<p>You can add your OneWayRequest through the correct Add method, and then have them processed by the server by calling the ProcessOneWayRequests method.&#160; This call will not block and will return <em>immediately</em>. That means that it won’t wait until the requests have actually been sent to the server like it does with the IRequestDispatcher.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/12/one-way-or-fire-and-forget-requests-with-agatha/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Agatha 1.0 Beta 2 Is Out</title>
		<link>http://davybrion.com/blog/2009/11/agatha-1-0-beta-2-is-out/</link>
		<comments>http://davybrion.com/blog/2009/11/agatha-1-0-beta-2-is-out/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 21:31:21 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1972</guid>
		<description><![CDATA[I just released Agatha 1.0 Beta 2&#8230; while the changes aren&#8217;t spectacular, i considered them important enough to release a new version. Here&#8217;s the changelog: Logging in the servicelayer now happens through the Common.Logging instead of using log4net directly&#8230; which means you can use whatever logging library you prefer (Breaking Change): If you want to [...]]]></description>
			<content:encoded><![CDATA[<p>I just released Agatha 1.0 Beta 2&#8230; while the changes aren&#8217;t spectacular, i considered them important enough to release a new version.</p>
<p>Here&#8217;s the changelog:</p>
<ul>
<li>Logging in the servicelayer now happens through the Common.Logging instead of using log4net directly&#8230; which means you can use whatever logging library you prefer</li>
<li>(Breaking Change): If you want to pass your container instance to an Agatha container wrapper, you have to pass it to the constructor of the wrapper, and then pass that IContainer instance to your ServiceLayerConfiguration or ClientConfiguration instance before initializing</li>
<li>Added Agatha.Unity and Agatha.Unity.Silverlight</li>
<li>Added simple example of Agatha in use, with a small service layer, an IIS host for the service layer, a .NET client which performs a synchronous and an asynchronous client, and a Silverlight client which also performs an asynchronous call</li>
</ul>
<p>The 2 most important changes are obviously that you can now integrate your own IOC container more easily and cleanly, and that you can use whatever logging library that you prefer.  If you download the source of beta 2, you&#8217;ll also get the example solution which shows you how easily you can start using Agatha in your projects.</p>
<p>You can download the bits <a href="http://code.google.com/p/agatha-rrsl/downloads/list">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/11/agatha-1-0-beta-2-is-out/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Integrating Your IOC Container With Agatha</title>
		<link>http://davybrion.com/blog/2009/11/integrating-your-ioc-container-with-agatha/</link>
		<comments>http://davybrion.com/blog/2009/11/integrating-your-ioc-container-with-agatha/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 17:59:03 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1966</guid>
		<description><![CDATA[Agatha relies on the presence of an IOC container, both client-side as well as server-side.&#160; When it was still a closed-source library used by my company, we could get away with using our preferred IOC container (Castle Windsor) directly.&#160; Obviously, when making an open-source library available you want your users to have the ability to [...]]]></description>
			<content:encoded><![CDATA[<p>Agatha relies on the presence of an IOC container, both client-side as well as server-side.&#160; When it was still a closed-source library used by my company, we could get away with using our preferred IOC container (Castle Windsor) directly.&#160; Obviously, when making an open-source library available you want your users to have the ability to not only use their preferred container, but to integrate with their container as well.&#160; With ‘integrate’ i mean that it should be possible to register Agatha’s components in your container, so that you can easily resolve Agatha components (such as the IRequestDispatcher or the IAsyncRequestDispatcher), or to have the ability to have the container automatically inject your dependencies (which are registered in your container) in your Request Handlers (which are resolved and used by Agatha).</p>
<p>I originally planned to use the <a href="http://www.codeplex.com/CommonServiceLocator" target="_blank">Common Service Locator</a> project for this, because the project description certainly seemed to fit my need:</p>
<blockquote><p>The Common Service Locator library contains a shared interface for service location which application and framework developers can reference. The library provides an abstraction over IoC containers and service locators. Using the library allows an application to indirectly access the capabilities without relying on hard references. The hope is that using this library, third-party applications and frameworks can begin to leverage IoC/Service Location without tying themselves down to a specific implementation.</p>
</blockquote>
<p>Unfortunately, the shared interface defined in this project only contains method for <em>resolving</em> components.&#160; I really wanted to avoid having an Agatha-user be responsible for the correct <em>registration</em> of each component in their container, so the Common Service Locator project doesn’t really offer me any benefits.&#160; Instead, i defined my own IContainer interface in Agatha which looks like this:</p>
<p> <code>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">interface</span> <span style="color: #2b91af">IContainer</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Register(<span style="color: #2b91af">Type</span> componentType, <span style="color: #2b91af">Type</span> implementationType, <span style="color: #2b91af">Lifestyle</span> lifeStyle);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Register&lt;TComponent, TImplementation&gt;(<span style="color: #2b91af">Lifestyle</span> lifestyle);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> RegisterInstance(<span style="color: #2b91af">Type</span> componentType, <span style="color: blue">object</span> instance);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> RegisterInstance&lt;TComponent&gt;(TComponent instance);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; TComponent Resolve&lt;TComponent&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">object</span> Resolve(<span style="color: #2b91af">Type</span> componentType);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">void</span> Release(<span style="color: blue">object</span> component);</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
</p></div>
<p> </code>
<p>&#160;</p>
<p>This interface enables me to perform automatic registration of all of the required components, and whenever Agatha needs to resolve components it will also do so through an instance which implements this interface.&#160; We currently have two specific implementations of this interface: one for Castle Windsor, the other for Unity.&#160; I’m going to cover how these implementations work later on in this post (as it will be useful to anyone who wants to use another IOC container together with Agatha) but first i want to show how Agatha will either obtain a reference to your container, or instantiate its own container if you don’t give it a container instance.</p>
<p>Agatha has two configuration classes: ServiceLayerConfiguration and ClientConfiguration.&#160; ServiceLayerConfiguration defines the following constructors:</p>
<p> <code>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> ServiceLayerConfiguration(<span style="color: #2b91af">Assembly</span> requestHandlersAssembly, <span style="color: #2b91af">Assembly</span> requestsAndResponsesAssembly, <span style="color: #2b91af">IContainer</span> container)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// not relevant to this post, so i'm skipping this</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> ServiceLayerConfiguration(<span style="color: #2b91af">Assembly</span> requestHandlersAssembly, <span style="color: #2b91af">Assembly</span> requestsAndResponsesAssembly, <span style="color: #2b91af">Type</span> containerImplementation)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// not relevant to this post, so i'm skipping this</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p> </code>
<p>&#160;</p>
<p>And ClientConfiguration defines the following ones:</p>
<p> <code>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> ClientConfiguration(<span style="color: #2b91af">Assembly</span> requestsAndResponsesAssembly, <span style="color: #2b91af">IContainer</span> container)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// not relevant to this post, so i'm skipping this</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> ClientConfiguration(<span style="color: #2b91af">Assembly</span> requestsAndResponsesAssembly, <span style="color: #2b91af">Type</span> containerImplementation)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: green">// not relevant to this post, so i'm skipping this</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</p></div>
<p> </code>
<p>&#160;</p>
<p>As you can see, you can pass either an instance of an object that implements Agatha’s IContainer interface, or you can just pass in the type of the IContainer implementation.&#160; If you pass in an instance, Agatha will simply reuse that instance for both the registration and the resolving of components.&#160; If you pass a type instead of an instance, Agatha will create its own instance and use that for the registration and resolving of components.</p>
<p>Now, how do you integrate your own container? Quite simple, just create a type which implements the IContainer interface and pass an instance of that type to Agatha’s configuration objects.&#160; As an example, i’ll show how i did it for Castle Windsor.&#160; I created a new assembly called Agatha.Castle which contains the following Container class:</p>
<p> <code>
<div style="font-family: consolas; background: white; color: black; font-size: 10pt">
<p style="margin: 0px"><span style="color: blue">using</span> System;</p>
<p style="margin: 0px"><span style="color: blue">using</span> Agatha.Common.InversionOfControl;</p>
<p style="margin: 0px"><span style="color: blue">using</span> Castle.MicroKernel.Registration;</p>
<p style="margin: 0px"><span style="color: blue">using</span> Castle.Windsor;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: blue">namespace</span> Agatha.Castle</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Container</span> : <span style="color: #2b91af">IContainer</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">readonly</span> <span style="color: #2b91af">IWindsorContainer</span> windsorContainer;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Container() : <span style="color: blue">this</span>(<span style="color: blue">new</span> <span style="color: #2b91af">WindsorContainer</span>()) {}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> Container(<span style="color: #2b91af">IWindsorContainer</span> windsorContainer)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">this</span>.windsorContainer = windsorContainer;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Register(<span style="color: #2b91af">Type</span> componentType, <span style="color: #2b91af">Type</span> implementationType, <span style="color: #2b91af">Lifestyle</span> lifeStyle)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var</span> registration = <span style="color: #2b91af">Component</span>.For(componentType).ImplementedBy(implementationType);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; windsorContainer.Register(AddLifeStyleToRegistration(lifeStyle, registration));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Register&lt;TComponent, TImplementation&gt;(<span style="color: #2b91af">Lifestyle</span> lifestyle)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Register(<span style="color: blue">typeof</span>(TComponent), <span style="color: blue">typeof</span>(TImplementation), lifestyle);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> RegisterInstance(<span style="color: #2b91af">Type</span> componentType, <span style="color: blue">object</span> instance)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; windsorContainer.Register(<span style="color: #2b91af">Component</span>.For(componentType).Instance(instance));</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> RegisterInstance&lt;TComponent&gt;(TComponent instance)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RegisterInstance(<span style="color: blue">typeof</span>(TComponent), instance);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> TComponent Resolve&lt;TComponent&gt;()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> windsorContainer.Resolve&lt;TComponent&gt;();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">object</span> Resolve(<span style="color: #2b91af">Type</span> componentType)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> windsorContainer.Resolve(componentType);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> <span style="color: blue">void</span> Release(<span style="color: blue">object</span> component)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; windsorContainer.Release(component);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">private</span> <span style="color: blue">static</span> <span style="color: #2b91af">ComponentRegistration</span>&lt;TInterface&gt; AddLifeStyleToRegistration&lt;TInterface&gt;(<span style="color: #2b91af">Lifestyle</span> lifestyle, <span style="color: #2b91af">ComponentRegistration</span>&lt;TInterface&gt; registration)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (lifestyle == <span style="color: #2b91af">Lifestyle</span>.Singleton)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; registration = registration.LifeStyle.Singleton;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">else</span> <span style="color: blue">if</span> (lifestyle == <span style="color: #2b91af">Lifestyle</span>.Transient)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; registration = registration.LifeStyle.Transient;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">else</span></p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">throw</span> <span style="color: blue">new</span> <span style="color: #2b91af">ArgumentOutOfRangeException</span>(<span style="color: #a31515">&quot;lifestyle&quot;</span>, <span style="color: #a31515">&quot;Only Transient and Singleton is supported&quot;</span>);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span> registration;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">}</p>
</p></div>
<p> </code>
<p>&#160;</p>
<p>The Agatha.Castle.Container class defines two constructors.&#160; One is the default constructor, which will create a new instance of the Windsor container, and the other requires you to pass an instance to a Windsor container.&#160; The rest of the type simply implements Agatha’s IContainer interface methods and delegates to the real container instance.&#160; If you pass an instance of the Agatha.Castle.Container class to Agatha’s ServiceLayerConfiguration class, Agatha will use this type to register and resolve all components.&#160; Which means it either reuses <em>your</em> container instance, or creates its own if you don’t pass one (which i’d only recommend if you’re not using an IOC container in your code). This makes it pretty easy to integrate whatever IOC container you want to use.&#160; </p>
<p>Agatha currently has ‘out-of-the-box’ support for Castle Windsor and Microsoft’s Unity.&#160; If you want to use your own container, you now know how easily you can integrate it with Agatha.&#160; And i’d be very happy to accept patches which add more Agatha.YourPreferredContainer assemblies <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/11/integrating-your-ioc-container-with-agatha/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Hello World With Agatha</title>
		<link>http://davybrion.com/blog/2009/11/hello-world-with-agatha/</link>
		<comments>http://davybrion.com/blog/2009/11/hello-world-with-agatha/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 20:05:33 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[agatha]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1949</guid>
		<description><![CDATA[I wrote a small Hello World sample with Agatha to demonstrate how easy it is to use in a new project. Well, it&#8217;s not really a small example since it&#8217;s probably the most over-engineered Hello World app ever. Doesn&#8217;t matter though, the objective is to demonstrate how you can get started with Agatha and it [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a small <a href="http://code.google.com/p/agatha-rrsl/source/browse/#svn/trunk/examples">Hello World sample</a> with Agatha to demonstrate how easy it is to use in a new project.  Well, it&#8217;s not really a small example since it&#8217;s probably the most over-engineered Hello World app <em>ever</em>.  Doesn&#8217;t matter though, the objective is to demonstrate how you can get started with Agatha and it shows that pretty well.  Let&#8217;s go over the steps, shall we?</p>
<p>First of all, you&#8217;ll need an assembly that contains the types you want to share between your service layer and your clients.  More specifically, this assembly needs to contain the Request/Response types and any other types that are contained in your Requests and Responses.  This assembly needs to reference Agatha.Common.  In my example, this example only contains one file:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">using</span> Agatha.Common;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;"><span style="color: blue;">namespace</span> Sample.Common.RequestsAndResponses</p>
<p style="margin: 0px;">{</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">HelloWorldRequest</span> : <span style="color: #2b91af;">Request</span> {}</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">HelloWorldResponse</span> : <span style="color: #2b91af;">Response</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Message { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">}</p>
</div>
<p></code></p>
<p>Once we have that, we need to implement our service layer.  You basically need an assembly that references both Agatha.Common and Agatha.ServiceLayer.  This assembly also needs to reference your shared assembly.  Then you can start implementing your request handlers.  Since my service layer only has one &#8216;operation&#8217;, i only have the following Request Handler:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">using</span> Agatha.Common;</p>
<p style="margin: 0px;"><span style="color: blue;">using</span> Agatha.ServiceLayer;</p>
<p style="margin: 0px;"><span style="color: blue;">using</span> Sample.Common.RequestsAndResponses;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;"><span style="color: blue;">namespace</span> Sample.ServiceLayer.Handlers</p>
<p style="margin: 0px;">{</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">HelloWorldHandler</span> : <span style="color: #2b91af;">RequestHandler</span>&lt;<span style="color: #2b91af;">HelloWorldRequest</span>, <span style="color: #2b91af;">HelloWorldResponse</span>&gt;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: #2b91af;">Response</span> Handle(<span style="color: #2b91af;">HelloWorldRequest</span> request)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> response = CreateTypedResponse();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; response.Message = <span style="color: #a31515;">&quot;Hello World!&quot;</span>;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> response;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">}</p>
</div>
<p></code></p>
<p>I also always include a ComponentRegistration class with a static method to perform all of the initialization.  The only thing that needs to be done to initialize Agatha in your service layer is this:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">using</span> System.Reflection;</p>
<p style="margin: 0px;"><span style="color: blue;">using</span> Agatha.ServiceLayer;</p>
<p style="margin: 0px;"><span style="color: blue;">using</span> Sample.Common.RequestsAndResponses;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;"><span style="color: blue;">namespace</span> Sample.ServiceLayer</p>
<p style="margin: 0px;">{</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ComponentRegistration</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Register()</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">ServiceLayerConfiguration</span>(<span style="color: #2b91af;">Assembly</span>.GetExecutingAssembly(), <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">HelloWorldRequest</span>).Assembly, </p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">typeof</span>(Agatha.Castle.<span style="color: #2b91af;">Container</span>)).Initialize();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">}</p>
</div>
<p></code></p>
<p>The ServiceLayerConfiguration&#8217;s constructor requires 3 parameters.  The first is the assembly that contains the Request Handlers, the second the one that contains your Request and Response types.  The third parameter is either a reference to a Type which implement&#8217;s Agatha&#8217;s IContainer interface, or an instance of IContainer if you want Agatha to reuse an existing IOC container instance (like, the one the rest of your application is using).  If you pass in a reference to a type which implements IContainer instead of an actual instance, Agatha will create a new container and use that instead.  All of the Request Handlers and Request and Response types found in the passed in assemblies will all be registered within the container automatically so you don&#8217;t have to worry about that at all.  I&#8217;m going to write another detailed post soon to show how you can integrate your favorite IOC container with Agatha, so i&#8217;m not going to get into the specifics of this right now.  The only thing you need to remember from this part is how little work it takes to initialize everything.  Oh, you&#8217;ll also need to reference the assembly which contains the relevant IOC container wrapper for what you want.  In my case, that would be Agatha.Castle but i also have an Agatha.Unity assembly for those who want to use Unity.  Agatha.StructureMap will be added soon.</p>
<p>Alright, you still need to host this service layer somewhere.  In my example, i chose to host it in an ASP.NET Web Application through IIS.  My Web Application project obviously has a reference to my Sample.ServiceLayer assembly.  In the Global.asax.cs file i have the following code:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">using</span> System;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;"><span style="color: blue;">namespace</span> Sample.ServiceLayer.Host</p>
<p style="margin: 0px;">{</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Global</span> : System.Web.<span style="color: #2b91af;">HttpApplication</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> Application_Start(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">ComponentRegistration</span>.Register();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> Session_Start(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e) {}</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> Application_BeginRequest(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e) {}</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> Application_AuthenticateRequest(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e) {}</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> Application_Error(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e) {}</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> Session_End(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e) {}</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">void</span> Application_End(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e) {}</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">}</p>
</div>
<p></code></p>
<p>I also have a Service.svc file which contains the following:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="background: #ffee62;">&lt;%</span><span style="color: blue;">@</span> <span style="color: #a31515;">ServiceHost</span> <span style="color: red;">Language</span><span style="color: blue;">=&quot;C#&quot;</span> <span style="color: red;">Debug</span><span style="color: blue;">=&quot;true&quot;</span> <span style="color: red;">Service</span><span style="color: blue;">=&quot;Agatha.ServiceLayer.WCF.WcfRequestProcessor&quot;</span> <span style="background: #ffee62;">%&gt;</span></p>
</div>
<p></code></p>
<p>My web.config has the following WCF configuration:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">system.serviceModel</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">services</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">service</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Agatha.ServiceLayer.WCF.WcfRequestProcessor</span>&quot;<span style="color: blue;"> </span><span style="color: red;">behaviorConfiguration</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">RequestProcessorBehavior</span>&quot;<span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">endpoint</span><span style="color: blue;"> </span><span style="color: red;">address</span><span style="color: blue;">=</span>&quot;&quot;<span style="color: blue;"> </span><span style="color: red;">contract</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Agatha.Common.WCF.IWcfRequestProcessor</span>&quot;<span style="color: blue;"> </span><span style="color: red;">binding</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">basicHttpBinding</span>&quot;<span style="color: blue;"> </span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: red;">bindingConfiguration</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">RequestProcessorBinding</span>&quot;<span style="color: blue;">/&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">service</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">services</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">bindings</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">basicHttpBinding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">binding</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">RequestProcessorBinding</span>&quot;<span style="color: blue;"> </span><span style="color: red;">maxReceivedMessageSize</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">2147483647</span>&quot;<span style="color: blue;"> </span><span style="color: red;">receiveTimeout</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">00:30:00</span>&quot;<span style="color: blue;"> </span><span style="color: red;">sendTimeout</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">00:30:00</span>&quot;<span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">readerQuotas</span><span style="color: blue;"> </span><span style="color: red;">maxStringContentLength</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">2147483647</span>&quot;<span style="color: blue;"> </span><span style="color: red;">maxArrayLength</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">2147483647</span>&quot;<span style="color: blue;">/&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">security</span><span style="color: blue;"> </span><span style="color: red;">mode</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">None</span>&quot;<span style="color: blue;"> /&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">binding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">basicHttpBinding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">bindings</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">behaviors</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">serviceBehaviors</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">behavior</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">RequestProcessorBehavior</span>&quot;<span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">serviceMetadata</span><span style="color: blue;"> </span><span style="color: red;">httpGetEnabled</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">true</span>&quot;<span style="color: blue;">/&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">serviceDebug</span><span style="color: blue;"> </span><span style="color: red;">includeExceptionDetailInFaults</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">true</span>&quot;<span style="color: blue;">/&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">dataContractSerializer</span><span style="color: blue;"> </span><span style="color: red;">maxItemsInObjectGraph</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">2147483647</span>&quot;<span style="color: blue;">/&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">serviceThrottling</span><span style="color: blue;"> </span><span style="color: red;">maxConcurrentCalls</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">500</span>&quot;<span style="color: blue;"> </span><span style="color: red;">maxConcurrentInstances</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">500</span>&quot;<span style="color: blue;">/&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">behavior</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">serviceBehaviors</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">behaviors</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">system.serviceModel</span><span style="color: blue;">&gt;</span></p>
</div>
<p></code></p>
<p>And that is it.  The service layer is now functional and being hosted through IIS.  You can obviously use whatever WCF hosting option you prefer, though the example only uses IIS as a host.  Feel free to contribute other hosts <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Then we have a .NET client which is able to call the service layer both synchronously and asynchronously.  In my sample, it&#8217;s just a console app but it could just as well be another ASP.NET Web Application, a Windows Server, a WPF application, a Winforms application (if you&#8217;re down with the whole retro thing, that is), or any other .NET process you can think of.</p>
<p>In the case of my sample console app, i need to reference Agatha.Common and Agatha.Castle.  In my app.config file, i need to add the following WCF configuration block:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">system.serviceModel</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">bindings</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">basicHttpBinding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">binding</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">RequestProcessorBinding</span>&quot;<span style="color: blue;"> </span><span style="color: red;">maxBufferSize</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">2147483647</span>&quot;<span style="color: blue;"> </span><span style="color: red;">maxReceivedMessageSize</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">2147483647</span>&quot;</p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span><span style="color: red;">receiveTimeout</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">00:30:00</span>&quot;<span style="color: blue;"> </span><span style="color: red;">sendTimeout</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">00:30:00</span>&quot;<span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">security</span><span style="color: blue;"> </span><span style="color: red;">mode</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">None</span>&quot;<span style="color: blue;"> /&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">binding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">basicHttpBinding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">bindings</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">client</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">endpoint</span><span style="color: blue;"> </span><span style="color: red;">address</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">http://localhost/Sample.ServiceLayer.Host/Service.svc</span>&quot;</p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: red;">binding</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">basicHttpBinding</span>&quot;<span style="color: blue;"> </span><span style="color: red;">bindingConfiguration</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">RequestProcessorBinding</span>&quot;</p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: red;">contract</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Agatha.Common.WCF.IWcfRequestProcessor</span>&quot;<span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Agatha_IWcfRequestProcessor</span>&quot;<span style="color: blue;"> /&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">client</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">system.serviceModel</span><span style="color: blue;">&gt;</span></p>
</div>
<p></code></p>
<p>Before i can call the service, my console app obviously needs to initialize Agatha:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> InitializeAgatha()</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">ClientConfiguration</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">HelloWorldRequest</span>).Assembly, <span style="color: blue;">typeof</span>(Agatha.Castle.<span style="color: #2b91af;">Container</span>)).Initialize();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>That should be self-explanatory.</p>
<p>Then i can start making service calls:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; requestDispatcher = <span style="color: #2b91af;">IoC</span>.Container.Resolve&lt;<span style="color: #2b91af;">IRequestDispatcher</span>&gt;();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> response = requestDispatcher.Get&lt;<span style="color: #2b91af;">HelloWorldResponse</span>&gt;(<span style="color: blue;">new</span> <span style="color: #2b91af;">HelloWorldRequest</span>());</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(response.Message);</p>
</div>
<p></code></p>
<p>And of course, asynchronous usage is also possible:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> CallTheServiceAsynchronously()</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> requestDispatcher = <span style="color: #2b91af;">IoC</span>.Container.Resolve&lt;<span style="color: #2b91af;">IAsyncRequestDispatcher</span>&gt;();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; requestDispatcher.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">HelloWorldRequest</span>());</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; requestDispatcher.ProcessRequests(ResponsesReceived, e =&gt; <span style="color: #2b91af;">Console</span>.WriteLine(e.ToString()));</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> ResponsesReceived(<span style="color: #2b91af;">ReceivedResponses</span> receivedResponses)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Console</span>.WriteLine(receivedResponses.Get&lt;<span style="color: #2b91af;">HelloWorldResponse</span>&gt;().Message);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>In case you&#8217;re wondering about the usage of IoC.Container in this code: Agatha provides a static IoC class which has an IContainer getter property.  If you configured Agatha to use <em>your</em> container instance, then you can simply resolve the IRequestDispatcher or the IAsyncRequestDispatcher through your usual methods, or preferably, have them injected in your components.  And if you&#8217;re not into the whole IOC thing, you can just use the RequestDispatcherFactory or the AsyncRequestDispatcherFactory directly.</p>
<p>And that is it.  Yeah, probably the most over-engineered Hello World app ever, but still, i think it shows nicely how <em>easy</em> it is to use Agatha in your projects.</p>
<p>Oh wait, i almost forgot about the Silverlight sample&#8230; In your Silverlight application, reference Agatha.Common.Silverlight, and add the following ServiceReferences.ClientConfig file:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">configuration</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;</span><span style="color: #a31515;">system.serviceModel</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">bindings</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">basicHttpBinding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">binding</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">RequestProcessorBinding</span>&quot;<span style="color: blue;"> </span><span style="color: red;">maxBufferSize</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">2147483647</span>&quot;<span style="color: blue;"> </span><span style="color: red;">maxReceivedMessageSize</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">2147483647</span>&quot;</p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; </span><span style="color: red;">receiveTimeout</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">00:30:00</span>&quot;<span style="color: blue;"> </span><span style="color: red;">sendTimeout</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">00:30:00</span>&quot;<span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">security</span><span style="color: blue;"> </span><span style="color: red;">mode</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">None</span>&quot;<span style="color: blue;"> /&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">binding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">basicHttpBinding</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">bindings</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: #a31515;">client</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">endpoint</span><span style="color: blue;"> </span><span style="color: red;">binding</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">basicHttpBinding</span>&quot;<span style="color: blue;"> </span><span style="color: red;">bindingConfiguration</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">RequestProcessorBinding</span>&quot;</p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: red;">contract</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Agatha.Common.WCF.IWcfRequestProcessor</span>&quot;</p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: red;">name</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Agatha_IWcfRequestProcessor</span>&quot;<span style="color: blue;"> </span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: red;">address</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">http://localhost/Sample.ServiceLayer.Host/Service.svc</span>&quot;<span style="color: blue;"> /&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">client</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;/</span><span style="color: #a31515;">system.serviceModel</span><span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&lt;/</span><span style="color: #a31515;">configuration</span><span style="color: blue;">&gt;</span></p>
</div>
<p></code></p>
<p>provide a way to initialize Agatha in your Silverlight app like this:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">using</span> Agatha.Common;</p>
<p style="margin: 0px;"><span style="color: blue;">using</span> Sample.Common.RequestsAndResponses;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;"><span style="color: blue;">namespace</span> Sample.SilverlightClient</p>
<p style="margin: 0px;">{</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ComponentRegistration</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> Register()</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">new</span> <span style="color: #2b91af;">ClientConfiguration</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">HelloWorldRequest</span>).Assembly, <span style="color: blue;">typeof</span>(Agatha.Unity.<span style="color: #2b91af;">Container</span>)).Initialize();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">}</p>
</div>
<p></code></p>
<p>Make sure the ComponentRegistration.Register method is called when the silverlight client starts, and then you can do something like this:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> requestDispatcher = <span style="color: #2b91af;">IoC</span>.Container.Resolve&lt;<span style="color: #2b91af;">IAsyncRequestDispatcher</span>&gt;();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; requestDispatcher.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">HelloWorldRequest</span>());</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; requestDispatcher.ProcessRequests(responses =&gt; MyTextBlock.Text = responses.Get&lt;<span style="color: #2b91af;">HelloWorldResponse</span>&gt;().Message, </p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exception =&gt; { <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">Exception</span>(exception.ToString()); });</p>
</div>
<p></code></p>
<p>Now you tell me, is that easy or what? <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/11/hello-world-with-agatha/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
