<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Valeriu Caraulean &#187; Castle</title>
	<atom:link href="http://blog.caraulean.com/category/castle/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.caraulean.com</link>
	<description>Keep it simple. It will work...</description>
	<lastBuildDate>Fri, 10 Feb 2012 04:38:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.caraulean.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Valeriu Caraulean &#187; Castle</title>
		<link>http://blog.caraulean.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.caraulean.com/osd.xml" title="Valeriu Caraulean" />
	<atom:link rel='hub' href='http://blog.caraulean.com/?pushpress=hub'/>
		<item>
		<title>Multi-targeting with Project Linker by example: Castle.Core for Silverlight 4</title>
		<link>http://blog.caraulean.com/2010/10/13/multi-targeting-with-project-linker-by-example-castle-core-for-silverlight-4/</link>
		<comments>http://blog.caraulean.com/2010/10/13/multi-targeting-with-project-linker-by-example-castle-core-for-silverlight-4/#comments</comments>
		<pubDate>Wed, 13 Oct 2010 14:37:54 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">https://varely.wordpress.com/2010/10/13/multi-targeting-with-project-linker-by-example-castle-core-for-silverlight-4/</guid>
		<description><![CDATA[In this post I would try to explore multi-targeting of a .NET project/solution using Project Linker tool. My main driver is desire to target one of our internal projects to Silverlight. I’ve picked Castle.Core to serve as an example, since his Silverlight build is now broken and (from what I’ve seen) there is no convenient [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=158&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In this post I would try to explore multi-targeting of a .NET project/solution using <a href="http://msdn.microsoft.com/en-us/library/ff921108(PandP.20).aspx">Project Linker</a> tool. My main driver is desire to target one of our internal projects to Silverlight. I’ve picked <a href="http://github.com/castleproject/Castle.Core">Castle.Core</a> to serve as an example, since his Silverlight build is now broken and (from what I’ve seen) there is no convenient way to work with this project in Visual Studio while targeting Silverlight. </p>
<p>For more details and screenshots of Project Linker, please refer to <a href="http://msdn.microsoft.com/en-us/library/ff921108(PandP.20).aspx">this MSDN article</a>.</p>
<p>Let’s go, step by step instructions:</p>
<ol>
<li>Go to Extension Manager in VS 2010 and search for Project Linker. </li>
<li>Install Project Linker extension </li>
<li>Create new solution, say Castle.Core-SL </li>
<li>Add to this solution the existent Castle.Core project (targeted for NET 4 Client Profile) </li>
<li>Add to solution new Silverlight 4 project, named Castle.Core-SL </li>
<li>Click on Castle.Core-SL project and from context menu choose “Add project link…” </li>
<li>In “Select Source Project” dialog choose the Castle.Core project, click OK. </li>
<li>To verify that projects are linked correctly, in VS Project menu choose “Edit links”: Source Project should be set to “Castle.Core”, and Target Project – “Castle.Core-SL”. </li>
<li>In order to trigger real linking process (Project Linker is observing file changes in source project) I have a trick for you. Go to Castle.Core project, then select all folders containing source files (Components.DictionaryAdapter, Core and DynamicProxy) and click on “Exclude from project” in context menu. Then, after enabling “Show all files” options, add them back. Here Project Linker will kick in and will link all added source files to Target Project. </li>
</ol>
<p>Then, we can do the same for project containing unit tests.</p>
<p>In case of Castle.Core there is another manual intervention required: filter out CastleBuild.snk file from being linked. To do that you have to open project file for your project and find an attribute in it called ProjectLinkerExcludeFilter. Add to the end of that value the string “;\.snk”. After that you should “Ad As Link” the key file manually.</p>
<p>If you want to “re-sync” both projects, you have to repeat only the 9th step.</p>
<p>Now, you’ll have your Castle.Core targeted to Silverlight with all source files from original .NET project. You’re now should be confortable enough exploring the code in Visual Studio and, why not, contribute…</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/varely.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/varely.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/varely.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/varely.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/varely.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/varely.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/varely.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/varely.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/varely.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/varely.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/varely.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/varely.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/varely.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/varely.wordpress.com/158/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=158&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.caraulean.com/2010/10/13/multi-targeting-with-project-linker-by-example-castle-core-for-silverlight-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d96a7814c830eb5ee8e12b677212cae?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">varely</media:title>
		</media:content>
	</item>
		<item>
		<title>Unit tests with Automatic Transaction Management</title>
		<link>http://blog.caraulean.com/2008/10/03/unit-tests-with-automatic-transaction-management/</link>
		<comments>http://blog.caraulean.com/2008/10/03/unit-tests-with-automatic-transaction-management/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 16:18:43 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://varely.wordpress.com/2008/10/03/unit-tests-with-automatic-transaction-management/</guid>
		<description><![CDATA[In our projects we&#8217;re using Castle&#8217;s Automatic Transaction Management facility (ATM). This facility allows to declaratively define transaction boundaries: by marking a method by a specific attribute the call to this method will be wrapped in a transaction, so that a transaction will be opened before calling the method and committed right after finishing method [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=31&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In our projects we&#8217;re using Castle&#8217;s Automatic Transaction Management facility (ATM). This facility allows to declaratively define transaction boundaries: by marking a method by a specific attribute the call to this method will be wrapped in a transaction, so that a transaction will be opened before calling the method and committed right after finishing method execution.</p>
<p>In code, it looks like this:</p>
<pre style="border-right:#cecece 1px solid;border-top:#cecece 1px solid;min-height:40px;overflow:auto;border-left:#cecece 1px solid;width:486px;border-bottom:#cecece 1px solid;height:162px;background-color:#fbfbfb;padding:5px;">
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  1: [Transactional]
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;">  2: <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> Foo
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  3: {
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;">  4:     [Transaction]
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  5:     <span style="color:#0000ff;">public</span> virtual void Bar()
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;">  6:     {
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  7:         // <span style="color:#0000ff;">Do</span> something <span style="color:#0000ff;">in</span> a transaction
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;">  8:     }
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  9: }
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 10: </pre>
</pre>
<p>Let&#8217;s suppose that later, we want to test this code. Say, we want to verify that code in Foo.Bar() method is executed in a transaction. Moreover, we want to assure that the marking attributes will not be deleted accidentally.</p>
<p>We can test that within two aspects. First &#8211; an integration test: a real application part will be spawned and a call will be performed. After, we should verify that changes was committed by transaction. It takes time to run such a test (integration test usually runs slower) and effort to write &amp; verify such a test. Second way to test that &#8211; is to check presence or required attributes. This will be a fast unit test that will just check that attribute is still applied to a method.</p>
<p>Those methods complements each other. Ideally, it should be both written.</p>
<p>Let&#8217;s see how we can verify that attributes required by ATM are applied to required methods.</p>
<p>First, we&#8217;ve defined the API that we want to use to do our check:</p>
<pre style="border-right:#cecece 1px solid;border-top:#cecece 1px solid;min-height:40px;overflow:auto;border-left:#cecece 1px solid;width:468px;border-bottom:#cecece 1px solid;height:64px;background-color:#fbfbfb;padding:5px;">
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  1: CustomAssertions.ShouldUseATM&lt;Foo&gt;(foo =&gt; foo.Bar());</pre>
</pre>
<p>Yes, you see it right, we want to make it type save, refactor-able and nice looking.</p>
<p>This helper class should verify next assumptions, all required by ATM:</p>
<ul>
<li>Tested class should be marked with TransactionalAttribute;
<li>Method should be marked by TransactionAttribute;
<li>Method should be virtual.</li>
</ul>
<p>Solution we came to this problem is the next class:</p>
<pre style="border-right:#cecece 1px solid;border-top:#cecece 1px solid;min-height:40px;overflow:auto;border-left:#cecece 1px solid;width:495px;border-bottom:#cecece 1px solid;height:636px;background-color:#fbfbfb;padding:5px;">
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  1: <span style="color:#0000ff;">public</span> static partial <span style="color:#0000ff;">class</span> CustomAssertions
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;">  2: {
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  3:     <span style="color:#0000ff;">public</span> static void ShouldUseATM&lt;T&gt;(Expression&lt;Action&lt;T&gt;&gt; methodExpression) where T : <span style="color:#0000ff;">class</span>
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;">  4:     {
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  5:         ThrowIfTypeNotMarkedAsTransactional(typeof (T));
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;">  6:
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  7:         var methodInfo = ((MethodCallExpression) methodExpression.Body).Method;
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;">  8:
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;">  9:         ThrowIfMethodIsNotMarkedWithTransactionAttribute(methodInfo);
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 10:         ThrowIfMethodMarkedWithTransactionAttributeIsVirtual(methodInfo);
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 11:     }
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 12:
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 13:     <span style="color:#0000ff;">private</span> static void ThrowIfTypeNotMarkedAsTransactional(Type classType)
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 14:     {
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 15:         object[] foundAttributes = classType.GetCustomAttributes(typeof (TransactionalAttribute), false);
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 16:         <span style="color:#0000ff;">if</span> (foundAttributes.Length == 0)
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 17:             Assert.Fail("<span style="color:#8b0000;">The type '{0}' is not marked with [Transactional] attribute</span>", classType.Name);
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 18:     }
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 19:
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 20:     <span style="color:#0000ff;">private</span> static void ThrowIfMethodIsNotMarkedWithTransactionAttribute(MethodInfo info)
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 21:     {
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 22:         var found = info.GetCustomAttributes(typeof (TransactionAttribute), false);
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 23:         <span style="color:#0000ff;">if</span> (found.Length == 0)
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 24:             Assert.Fail("<span style="color:#8b0000;">Method '{0}' from '{1}' type is not marked with [Transaction] attribute</span>",
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 25:                         info.Name,
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 26:                         info.DeclaringType.Name);
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 27:     }
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 28:
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 29:     <span style="color:#0000ff;">private</span> static void ThrowIfMethodMarkedWithTransactionAttributeIsVirtual(MethodBase methodInfo)
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 30:     {
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 31:         <span style="color:#0000ff;">if</span> (methodInfo.IsVirtual &amp;&amp; !methodInfo.IsFinal)
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 32:             return;
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 33:
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 34:         Assert.Fail("<span style="color:#8b0000;">Method '{0}' from '{1}' type is marked with [Transaction] attribute, but is not declared as virtual.</span>",
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 35:                     methodInfo.Name,
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 36:                     methodInfo.DeclaringType.Name);
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 37:     }
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#ffffff;margin:0;"> 38: }
</pre>
<pre style="font-size:12px;width:100%;font-family:consolas,'Courier New',courier,monospace;background-color:#fbfbfb;margin:0;"> 39: </pre>
</pre>
<p>So far, we&#8217;re pleased with how it works for us. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/varely.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/varely.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/varely.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/varely.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/varely.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/varely.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/varely.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/varely.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/varely.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/varely.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/varely.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/varely.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/varely.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/varely.wordpress.com/31/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=31&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.caraulean.com/2008/10/03/unit-tests-with-automatic-transaction-management/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d96a7814c830eb5ee8e12b677212cae?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">varely</media:title>
		</media:content>
	</item>
		<item>
		<title>Configuring NServiceBus using Binsor</title>
		<link>http://blog.caraulean.com/2008/07/13/configuring-nservicebus-using-binsor/</link>
		<comments>http://blog.caraulean.com/2008/07/13/configuring-nservicebus-using-binsor/#comments</comments>
		<pubDate>Sun, 13 Jul 2008 20:58:29 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[NServiceBus]]></category>

		<guid isPermaLink="false">http://varely.wordpress.com/2008/07/13/configuring-nservicebus-using-binsor/</guid>
		<description><![CDATA[I&#8217;m evaluating the NServiceBus communication framework for using it in some parts of application that we&#8217;re working on. I like the messaging infrastructure that it offers and how it all is implemented &#8211; behind a simple interface is hidden a modular, extensible, reliable and extremely powerful messaging engine. Sure, like any other framework, it has [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=23&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m evaluating the <a href="http://nservicebus.com/">NServiceBus</a> communication framework for using it in some parts of application that we&#8217;re working on. I like the messaging infrastructure that it offers and how it all is implemented &#8211; behind a simple interface is hidden a modular, extensible, reliable and extremely powerful messaging engine. Sure, like any other framework, it has his own pitfalls and places that requires a special knowledge about how things works to use it efficiently. </p>
<p>One of those tricky places is his configuration system. To start use NServiceBus you have to do few steps to properly configure it. You have to use 2-3 &#8220;app.config&#8221; configuration sections with approximately 10-15 various parameters and few configuration classes provided by NServiceBus. </p>
<p>Another place that make me stuck is his integration with IoC (Inversion of Control) container. It has an abstraction over all container stuff, so you can pretty easily create an adapter to use it with container of your choice. I don&#8217;t like how this abstraction is used and implemented, a bit unnatural for my habits on how to put container to work.</p>
<p>Anyway, we have to deal with it, and use the NServiceBus with Castle Windsor container. As I said to do that you have to create an adapter for IBuilder interface. The NServiceBus Contrib project contains a patch that do this, but it is done in a completely wrong way &#8211; by mimicking the adapter for Spring framework. Bad, bad, bad&#8230;</p>
<p>So let&#8217;s take our big gun &#8211; Binsor from Rhino.Tools (I wrote about <a href="http://blog.caraulean.com/2008/05/14/configuring-nhibernateintegration-facility-with-binsor/">here</a>) that should do all dirty work by configuring NServiceBus and integrating it in our existing Windsor container. The link to VS solution with all code shown here is provided at the end of the page.</p>
<p>Here we go &#8211; how to configure a subscriber for Pub/Sub interactions:</p>
<p>1. Binsor configuration (.boo file):</p>
<pre class="code">import System.Reflection
import NServiceBus
import NServiceBus.Unicast
import NServiceBus.Serialization
import NServiceBus.Serializers.Binary
import NServiceBus.Unicast.Transport
import NServiceBus.Unicast.Transport.Msmq
import NServiceBus.Unicast.Subscriptions
import NServiceBus.Unicast.Subscriptions.Msmq

import ObjectBuilder
import nServiceBus.CastleIntegration

component IBus, NServiceBus.Unicast.UnicastBus:
    MessageOwners = {"Messages":"messagebus"}
    MessageHandlerAssemblies = [Assembly.Load("Subscriber1")]

component ITransport, MsmqTransport:
    InputQueue = "worker"
    ErrorQueue = "error"
    NumberOfWorkerThreads = 1
    MaxRetries = 5
    IsTransactional = false
    PurgeOnStartup = false

component IMessageSerializer, MessageSerializer
component IBuilder, Builder
</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><a href="http://11011.net/software/vspaste"></a>&nbsp;</p>
<p>2. Program initialization:</p>
<pre class="code"><span style="color:blue;">var </span>container = <span style="color:blue;">new </span><span style="color:#2b91af;">WindsorContainer</span>();

<span style="color:#2b91af;">BooReader</span>.Read(container, <span style="color:#a31515;">"nServiceBus.boo"</span>);

container.Register(<span style="color:#2b91af;">SagasAndMessageHandlers</span>.From(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">EventMessageHandler</span>).Assembly));

<span style="color:blue;">var </span>bus = container.Resolve&lt;<span style="color:#2b91af;">IBus</span>&gt;();

bus.Start();
</pre>
<p><a href="http://11011.net/software/vspaste"></a>&nbsp;</p>
<p>That&#8217;s all. Now you can start send/receive messages.</p>
<p>For my taste this is much more readable and maintainable than traditional NServiceBus configuration. Less code, no XML, much cleaner, container friendly.</p>
<p>To make this work you have need for adapter class itself. His responsibility is to forward IBuilder.Build&lt;T&gt; calls to Castle&#8217;s Resolve&lt;T&gt; methods. Also, a small helper class that will register Sagas and MessageHandlers in container. Here it is:</p>
<pre class="code"><span style="color:blue;">public static class </span><span style="color:#2b91af;">SagasAndMessageHandlers
</span>{
    <span style="color:blue;">public static </span><span style="color:#2b91af;">IRegistration</span>[] From(<span style="color:blue;">params </span><span style="color:#2b91af;">Assembly</span>[] assemblies)
    {
        <span style="color:blue;">var </span>registrations = <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">ComponentRegistration</span>&gt;();
        <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>assembly <span style="color:blue;">in </span>assemblies)
        {
            <span style="color:blue;">var </span>types = assembly.GetTypes();
            <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>type <span style="color:blue;">in </span>types)
            {
                <span style="color:blue;">var </span>implementedInterfaces = type.GetInterfaces();
                <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>interf <span style="color:blue;">in </span>implementedInterfaces)
                {
                    <span style="color:blue;">if </span>(!interf.IsGenericType)
                        <span style="color:blue;">continue</span>;
                    <span style="color:blue;">var </span>genericArguments = interf.GetGenericArguments();
                    <span style="color:blue;">if </span>(genericArguments.Length != 1)
                        <span style="color:blue;">continue</span>;

                    <span style="color:blue;">if </span>(!<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IMessage</span>).IsAssignableFrom(genericArguments[0]))
                        <span style="color:blue;">continue</span>;

                    <span style="color:blue;">var </span>sagaHandlerType = <span style="color:blue;">typeof</span>(<span style="color:#2b91af;">ISaga</span>&lt;&gt;).MakeGenericType(genericArguments[0]);
                    <span style="color:blue;">if </span>(sagaHandlerType.IsAssignableFrom(interf))
                    {
                        registrations.Add(CreateRegistration(type, sagaHandlerType));
                        registrations.Add(CreateRegistration(type, type));
                    }

                    <span style="color:blue;">var </span>messageHandlerType = <span style="color:blue;">typeof </span>(<span style="color:#2b91af;">IMessageHandler</span>&lt;&gt;).MakeGenericType(genericArguments[0]);
                    <span style="color:blue;">if </span>(messageHandlerType.IsAssignableFrom(interf))
                        registrations.Add(CreateRegistration(type, type));
                }
            }
        }
        <span style="color:blue;">return </span>registrations.ToArray();
    }

    <span style="color:blue;">private static </span><span style="color:#2b91af;">ComponentRegistration </span>CreateRegistration(<span style="color:#2b91af;">Type </span>type, <span style="color:#2b91af;">Type </span>handlerType)
    {
        <span style="color:blue;">string </span>componentName;
        <span style="color:blue;">if </span>(type == handlerType)
            componentName = type.FullName;
        <span style="color:blue;">else
            </span>componentName = type.FullName + handlerType.FullName;

        <span style="color:blue;">return </span>(<span style="color:#2b91af;">ComponentRegistration</span>)<span style="color:#2b91af;">Component
                                          </span>.For(handlerType)
                                          .ImplementedBy(type)
                                          .LifeStyle.Is(<span style="color:#2b91af;">LifestyleType</span>.Transient)
                                          .Named(componentName);
    }
}
</pre>
<p>&nbsp;</p>
<p>One thing to notice here, NServiceBus required that a saga to be registered as a saga handler ( ISaga&lt;Message&gt;) and as a Saga class itself. A better way will be to use Castle&#8217;s newly added forwarding ability, when a component implementation can be accessed through multiple interfaces all forwarded to component&#8217;s instance. Bud I had no time to play with this feature yet, so this duplication will still here some time given the fact that no bugs showed up.</p>
<p>You can download a Visual Studio solution with all code shown here from <a href="http://cid-dcef5f73996bad79.skydrive.live.com/self.aspx/Blog/nServiceBus.CastleIntegration.zip">this place</a>. It contains all infrastructure code and two working sample projects ported from original NServiceBus samples &#8211; Pub/Sub &amp; Saga.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/varely.wordpress.com/23/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/varely.wordpress.com/23/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/varely.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=23&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.caraulean.com/2008/07/13/configuring-nservicebus-using-binsor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d96a7814c830eb5ee8e12b677212cae?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">varely</media:title>
		</media:content>
	</item>
		<item>
		<title>What integration tests should you write first</title>
		<link>http://blog.caraulean.com/2008/06/18/what-integration-tests-should-you-write-first/</link>
		<comments>http://blog.caraulean.com/2008/06/18/what-integration-tests-should-you-write-first/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 10:18:10 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://varely.wordpress.com/2008/06/18/what-integration-tests-should-you-write-first/</guid>
		<description><![CDATA[Generally, the goal of integration tests is to verify that different application parts are glued well together and interacts as expected. Thus, they are a bit different thing from unit tests that tends to test how a small, separate part is acting. Below are two tests that I can&#8217;t classify to be pure integration tests [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=22&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Generally, the goal of integration tests is to verify that different application parts are glued well together and interacts as expected. Thus, they are a bit different thing from unit tests that tends to test how a small, separate part is acting. Below are two tests that I can&#8217;t classify to be pure integration tests because they touch a sensible part of application/infrastructure configuration like Windsor&#8217;s configuration files and NHibernate&#8217;s mappings. These simple tests can save a lot of time at very initial development stage when application bits are changed frequently and later, as a guarantee that you&#8217;ve not broken something in a recent refactoring.</p>
<h4>Can_resolve_all_services_in_container()</h4>
<p><a href="http://weblogs.asp.net/bsimser/default.aspx">Bill Simser</a> wrote some time ago about <a href="http://weblogs.asp.net/bsimser/archive/2008/06/04/the-first-spec-you-should-write-when-using-castle.aspx">the first test to be written when using Castle&#8217;s Windsor Container</a>. It&#8217;s a real gem that let&#8217;s you verify if any of services registered in container can be resolved (have all required dependencies). Here is it, a bit improved for skipping verification for generic types, since, at that moment, we don&#8217;t know the generic argument for type to be constructed:</p>
<pre class="code">[<span style="color:#2b91af;">Test</span>]
<span style="color:blue;">public void </span>Can_resolve_all_services_in_container()
{
    <span style="color:blue;">var </span>container = <span style="color:blue;">new </span><span style="color:#2b91af;">WindsorContainer</span>(<span style="color:#a31515;">"container.xml"</span>);
    <span style="color:blue;">foreach </span>(<span style="color:#2b91af;">IHandler </span>handler <span style="color:blue;">in </span>container.Kernel.GetAssignableHandlers(<span style="color:blue;">typeof</span>(<span style="color:blue;">object</span>)))
    {
        <span style="color:blue;">if </span>(handler.ComponentModel.Service.IsGenericType)
            <span style="color:blue;">continue</span>;

        container.Resolve(handler.ComponentModel.Service);
    }
}
</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h4>Can_compile_NHibernate_mapping_configuration()</h4>
<p>This test was sitting for a long time in our codebase, but I&#8217;ve seen it recently in <a href="http://codebetter.com/blogs/david_laribee/archive/2008/06/17/test-your-nhibernate-mappings.aspx">this post</a> of <a href="http://codebetter.com/blogs/david_laribee/default.aspx">David Larebee</a>. I wrote it in times when writing our first mappings and the only one way to verify if you don&#8217;t have any faults was to write one of CRUD operations and run the test. But that&#8217;s a long process. Shorter response cycle is always better. Here it is:</p>
<pre class="code">[<span style="color:#2b91af;">Test</span>]
<span style="color:blue;">public void </span>Can_compile_NHibernate_mapping_configuration()
{
    <span style="color:#2b91af;">ISessionFactory </span>factory =
        <span style="color:blue;">new </span><span style="color:#2b91af;">Configuration</span>()
            .Configure()
            .AddAssembly(<span style="color:#a31515;">"MyApp.MyDomainAssembly"</span>)
            .BuildSessionFactory();

    <span style="color:#2b91af;">Assert</span>.IsNotNull(factory);
}
</pre>
<p>This test suppose that you have HNibernate configuration in your app.config and tries to register all mappings from given assembly file.</p>
<p>&nbsp;</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/varely.wordpress.com/22/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/varely.wordpress.com/22/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/varely.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/varely.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/varely.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/varely.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/varely.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/varely.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/varely.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/varely.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/varely.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/varely.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/varely.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/varely.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/varely.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/varely.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=22&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.caraulean.com/2008/06/18/what-integration-tests-should-you-write-first/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d96a7814c830eb5ee8e12b677212cae?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">varely</media:title>
		</media:content>
	</item>
	</channel>
</rss>
