<?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>Jonne Kats</title>
	<atom:link href="http://jonnekats.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonnekats.wordpress.com</link>
	<description>Blog about .NET, TDD, Unit Testing, Software Architecture, etc.</description>
	<lastBuildDate>Fri, 13 May 2011 06:41:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jonnekats.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Jonne Kats</title>
		<link>http://jonnekats.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jonnekats.wordpress.com/osd.xml" title="Jonne Kats" />
	<atom:link rel='hub' href='http://jonnekats.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Debugging dotnet production problems</title>
		<link>http://jonnekats.wordpress.com/2010/04/05/debugging-dotnet-production-problems/</link>
		<comments>http://jonnekats.wordpress.com/2010/04/05/debugging-dotnet-production-problems/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 13:12:47 +0000</pubDate>
		<dc:creator>jonnekats</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Commerce Server]]></category>
		<category><![CDATA[Catalog Import]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[Error]]></category>
		<category><![CDATA[Msdbg]]></category>
		<category><![CDATA[Production]]></category>

		<guid isPermaLink="false">http://jonnekats.wordpress.com/2010/04/05/debugging-dotnet-production-problems/</guid>
		<description><![CDATA[This post described how you can debug difficult dotnet problems in production environments. To illustrate this I’ll describe how we solved a problem we had with Microsoft Commerce Server in production. On one of our current projects we are using Commerce Server 2007. The project is currently in its stabilizing phase and we were setting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=59&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This post described how you can debug difficult dotnet problems in production environments. To illustrate this I’ll describe how we solved a problem we had with Microsoft Commerce Server in production. </p>
<p>On one of our current projects we are using Commerce Server 2007. The project is currently in its stabilizing phase and we were setting up the production environment. However, we were getting errors on both the acceptation and production server when importing the final commerce server product catalog. This is standard commerce server code, the only thing we did was import a product xml file, which was exported from another commerce server installation. The following is the error we got from commerce server:</p>
<p>&#160;</p>
<p><em>“Line 2166: The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)”</em></p>
<p>&#160;</p>
<p>There was no additional error information, not in logs, not in the eventlogs, not anywhere. We enabled the exotic commerce server tracing and found nothing. Next to this, we were not able to reproduce this problem on our development machines. On those, the import would just succeed. When reading the error message, you would suspect some path to be invalid. There were no filepaths in the import xml, so this could not be the problem. So I fired up Windows <a href="http://technet.microsoft.com/en-us/sysinternals/bb545046.aspx" target="_blank">Sysinternals Process Monitor</a> to checkout errors in file activity, nothing standing out there. So at this point we had already spend a complete day trying to solve this problem, we were getting hopeless. We knew of windbg.exe, but this requires you to have the debug symbols and we didn’t have those for commerce server. Finally, we stumbled on <a href="http://msdn.microsoft.com/en-us/library/ms229861.aspx" target="_blank">mdbg.exe</a>. This is a simple command line debugger, which makes it possible to debug .net applications from the command line. It doesn’t require symbols, because it can debug using Intermediate Language. </p>
<p>&#160;</p>
<p>So we copied Mdgb.exe, MdbgCore.dll and MdbgDis.dll to the server, which can be found in the Windows SDK, normally under C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin. There is also a x64 folder in there, containing the 64 bits version. When you fire up the exe, you get an interactive command line debugger. To attach to a process, you need&#160; to know its PID, which can be found in the Task manager. You attach by using the following command: <strong>a </strong>&lt;PID&gt;. When you are attached, it is possible to set a breakpoint. However, to do this you need to know the namespace and the name of the method to put the breakpoint at. You will need reflector or ildasm to figure out at which method to put the breakpoint. In our case we figured an exception was being swallowed somewhere, so we only wanted to break at the exception. This is possible with the <strong>ca ex </strong>command, which makes the debugger break on every exception.</p>
<p>&#160;</p>
<p><a href="http://jonnekats.files.wordpress.com/2010/04/image.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://jonnekats.files.wordpress.com/2010/04/image_thumb.png?w=406&#038;h=207" width="406" height="207" /></a> </p>
<p>So at this point, we where attached to the IIS worker process of the commerce server webservices, we had the debugger break on exceptions, we were set to go. However, when we ran the import again, we got the same exception, without the debugger breaking. How could this be possible? We fired up reflector once more and when we looked at the source, we discovered that one of the .net components responsible for the import was a ServicedComponent. Meaning this was running in a separate COM+ application. So this part of the import .NET code was running in a different process. We would need to attach to this process as well. You can find the running COM+ processes and their PIDS in the Component Services tool (Administrative tools). </p>
<p>&#160;</p>
<p><a href="http://jonnekats.files.wordpress.com/2010/04/comservices.png"><img style="display:inline;border-width:0;" title="Comservices" border="0" alt="Comservices" src="http://jonnekats.files.wordpress.com/2010/04/comservices_thumb.png?w=364&#038;h=256" width="364" height="256" /></a> </p>
<p>&#160;</p>
<p>So we attached to this process as well, when we restarted the import the debugger did break on an exception. I don’t have access to a similar commerce server environment at the moment, so I’ll show how it looks when a random exception is thrown:</p>
<p>&#160;</p>
<p><a href="http://jonnekats.files.wordpress.com/2010/04/image1.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://jonnekats.files.wordpress.com/2010/04/image_thumb1.png?w=363&#038;h=185" width="363" height="185" /></a> </p>
<p>&#160;</p>
<p>As you can see the debugger breaks because an AppDomainUnloadedException is thrown. </p>
<p>&#160;</p>
<p>The exception we got in production indicated something went wrong when Commerce Server was creating scopes using Azman, which is a standard windows library for authorization. Using the p command, which prints all current variables to the debugger window, we could clearly see that this was only the case for a couple property definitions in our import xml file. After some investigation, we found out that all the property definitions that where failing had a name which ended with a whitespace. On our development machines we had disabled authorization for the commerce webservices, which is probably the reason we were not able to reproduce this problem on our development machines. After we fixed the property definitions the import succeeded, yeah!</p>
<p>&#160;</p>
<p>So tooling like Mdbg.exe can be a lifesaver when you are diagnosing problems in a production environment. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonnekats.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonnekats.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonnekats.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonnekats.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jonnekats.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jonnekats.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jonnekats.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jonnekats.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonnekats.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonnekats.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonnekats.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonnekats.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonnekats.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonnekats.wordpress.com/59/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=59&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jonnekats.wordpress.com/2010/04/05/debugging-dotnet-production-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e3f1ef2b9717a4f5497b79acda6e424?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jonnekats</media:title>
		</media:content>

		<media:content url="http://jonnekats.files.wordpress.com/2010/04/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://jonnekats.files.wordpress.com/2010/04/comservices_thumb.png" medium="image">
			<media:title type="html">Comservices</media:title>
		</media:content>

		<media:content url="http://jonnekats.files.wordpress.com/2010/04/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Filtering Linq to Nhibernate queries with Rhino Security</title>
		<link>http://jonnekats.wordpress.com/2009/10/05/filtering-linq-to-nhibernate-queries-with-rhino-security/</link>
		<comments>http://jonnekats.wordpress.com/2009/10/05/filtering-linq-to-nhibernate-queries-with-rhino-security/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 20:07:23 +0000</pubDate>
		<dc:creator>jonnekats</dc:creator>
				<category><![CDATA[Design & Architecture]]></category>
		<category><![CDATA[Rhino Security Linq-to-nhibernate]]></category>

		<guid isPermaLink="false">http://jonnekats.wordpress.com/2009/10/05/filtering-linq-to-nhibernate-queries-with-rhino-security/</guid>
		<description><![CDATA[Rhino Security provides the ability to filter NHibernate queries based on permissions. To do this, an IAuthorizationService interface is provided, that can be used in the following way: var criteria = DetachedCriteria.For&#60;Company&#62;(); AuthorizationService.AddPermissionsToQuery(currentUser.Value, "/Company/View", criteria); return criteria.GetExecutableCriteria(Session).List&#60;Company&#62;(); This way, a user only gets to see the companies he has permissions for. The AddPermissionsToQuery method expects [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=51&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://go2.wordpress.com/?id=725X1342&amp;site=jonnekats.wordpress.com&amp;url=http%3A%2F%2Fayende.com%2FBlog%2Farchive%2F2008%2F01%2F22%2FRhino-Security-Overview-Part-I.aspx" target="_blank">Rhino Security</a> provides the ability to filter NHibernate queries based on permissions. To do this, an <strong>IAuthorizationService</strong> interface is provided, that can be used in the following way:</p>
<p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:08911ac0-4fe2-42b8-af19-6c8331da5815" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var criteria </span><span style="color:#000000;">=</span><span style="color:#000000;"> DetachedCriteria.For</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Company</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();
AuthorizationService.AddPermissionsToQuery(currentUser.Value, </span><span style="color:#800000;">"</span><span style="color:#800000;">/Company/View</span><span style="color:#800000;">"</span><span style="color:#000000;">, criteria);
</span><span style="color:#0000FF;">return</span><span style="color:#000000;"> criteria.GetExecutableCriteria(Session).List</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Company</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>This way, a user only gets to see the companies he has permissions for. The <strong>AddPermissionsToQuery</strong> method expects a user, an operation and an NHibernate <strong>ICiteria</strong> object. The <strong>ICriteria</strong> object is the NHibernate Criteria that needs to be extended with permissions. </p>
<p>This works really well, a nice optimized query is generated. However, when working with Linq-to-Nhibernate, you don’t have an <strong>ICriteria</strong> object, so how can we add the permissions to those queries? </p>
<p>To figure this out, I downloaded the latest sources for Linq-to-Nhibernate and discovered a <strong>QueryOptions</strong> property on the <strong>INhibernateQueryable</strong> interface, which returns a <strong>QueryOptions</strong> type. The <strong>QueryOptions</strong> type provides a <strong>RegisterCustomAction</strong> method, which takes a parameter of the the type <strong>Action&lt;ICriteria&gt;</strong>, this looks hopeful.</p>
<p>By doing some tests, I found out that internally Linq-to-Nhibernate translates linq queries into a Criteria object. The <strong>RegisterCustomAction</strong> method allows us to perform custom actions on this criteria object, making it possible to add our own restrictions, excellent! </p>
<p>Integrating Rhino Security with Linq-to-NHibernate then gets us the following code:</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:73e9e6f2-a7f6-4a57-ac75-353a823a5d78" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">INHibernateQueryable</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">User</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> query </span><span style="color:#000000;">=</span><span style="color:#000000;"> Session.Linq</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">User</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();
query.QueryOptions.RegisterCustomAction(x</span><span style="color:#000000;">=&gt;</span><span style="color:#000000;">
          AuthorizationService.AddPermissionsToQuery(user, </span><span style="color:#800000;">"</span><span style="color:#800000;">/User/View</span><span style="color:#800000;">"</span><span style="color:#000000;">, x));
</span><span style="color:#0000FF;">return</span><span style="color:#000000;"> query.ToList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">User</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>This results in the same query as the one resulting from the DetachedCriteria scenario. So this way you can have <a href="http://go2.wordpress.com/?id=725X1342&amp;site=jonnekats.wordpress.com&amp;url=http%3A%2F%2Fayende.com%2FBlog%2Farchive%2F2008%2F01%2F22%2FRhino-Security-Overview-Part-I.aspx" target="_blank">Rhino Security</a> filter your queries based on permissions and still use Linq-to-Nhibernate. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonnekats.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonnekats.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonnekats.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonnekats.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jonnekats.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jonnekats.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jonnekats.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jonnekats.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonnekats.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonnekats.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonnekats.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonnekats.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonnekats.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonnekats.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=51&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jonnekats.wordpress.com/2009/10/05/filtering-linq-to-nhibernate-queries-with-rhino-security/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e3f1ef2b9717a4f5497b79acda6e424?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jonnekats</media:title>
		</media:content>
	</item>
		<item>
		<title>Rhino security with Unity</title>
		<link>http://jonnekats.wordpress.com/2009/09/15/rhino-security-with-unity/</link>
		<comments>http://jonnekats.wordpress.com/2009/09/15/rhino-security-with-unity/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 19:08:18 +0000</pubDate>
		<dc:creator>jonnekats</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Design & Architecture]]></category>
		<category><![CDATA[Domain driven design]]></category>
		<category><![CDATA[Rhino Security]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://jonnekats.wordpress.com/2009/09/15/rhino-security-with-unity/</guid>
		<description><![CDATA[If you are using NHibernate and need an integrated authorization framework, then have a look at Rhino Security. Rhino Security is a security framework developed by Ayende, creator of Rhino Mocks and the Nhibernate Profiler, among other things.&#160; Rhino Security automatically adds entities and services to your application for managing security. It provides the ability [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=48&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you are using NHibernate and need an integrated authorization framework, then have a look at <a href="http://ayende.com/Blog/archive/2008/01/22/Rhino-Security-Overview-Part-I.aspx" target="_blank">Rhino Security</a>. Rhino Security is a security framework developed by <a href="http://ayende.com/" target="_blank">Ayende</a>, creator of <a href="http://ayende.com/projects/rhino-mocks.aspx" target="_blank">Rhino Mocks</a> and the <a href="http://www.nhprof.com/" target="_blank">Nhibernate Profiler</a>, among other things.&#160; </p>
<p>Rhino Security automatically adds entities and services to your application for managing security. It provides the ability to decorate your own User entity with an IUser interface, making it suitable for use in authorization. There is the notion of user groups (Roles), operations, entities, entity groups and permissions. Authorization is done based on both operation and entity level. So you can define permissions for users and usergroups on operations, entities and even groups of entities. </p>
<p>It is amazingly flexible and even adds the ability to filter queries based on permissions. So for example, as a marketing manager, you only get to see the products you have permissions for.</p>
<p>For more information on how to use Rhino Security checkout:</p>
<p>- Ayendes blog: <a title="http://ayende.com/Blog/category/548.aspx" href="http://ayende.com/Blog/category/548.aspx">http://ayende.com/Blog/category/548.aspx</a></p>
<p>- Bart Reyserhove’s blog: <a title="http://bartreyserhove.blogspot.com/search/label/rhino%20security" href="http://bartreyserhove.blogspot.com/search/label/rhino%20security">http://bartreyserhove.blogspot.com/search/label/rhino%20security</a></p>
<p>Rhino security used to have dependencies on Castle Windsor and Rhino Tools, making it quite hard to use if you had already chosen another IOC container, like Unity for example. Luckily, in the latest version, Ayende has eliminated those dependencies and swapped them out for the <a href="http://www.codeplex.com/CommonServiceLocator" target="_blank">Common Service Locator</a>. </p>
<p><strong>Common Service Locator</strong></p>
<p>For those of you who are unfamiliar with the Common Service Locator, the Common Service Locator is a shared interface for IOC containers available on <a href="http://www.codeplex.com/CommonServiceLocator" target="_blank">codeplex</a>, which abstracts the specifics of IOC containers. The goal of this generic interface is to have frameworks only rely on this generic interface, making it possible for the users of the framework to plug in the IOC container they are using. So, because Rhino Security is now making use of this Common Service Locator, it is now possible to have it use our own Unity container, this way having better integration.</p>
<p>So, how do you use Rhino Security with Unity?</p>
<p><strong>Using Rhino Security with Unity</strong></p>
<p>First, get the latest version of Rhino Security from Github:</p>
<p><a title="http://github.com/ayende/rhino-security" href="http://github.com/ayende/rhino-security">http://github.com/ayende/rhino-security</a></p>
<p>Next, get the latest version of the Unity adapter for the Common Service Locator from codeplex:</p>
<p><a title="http://www.codeplex.com/CommonServiceLocator" href="http://www.codeplex.com/CommonServiceLocator">http://www.codeplex.com/CommonServiceLocator</a></p>
<p>Now what I did is write a Unity container extensions to provide the Rhino Security integration:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d02c05f5-428a-4316-8835-56c77a8bde99" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">    </span><span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;summary&gt;</span><span style="color:#008000;">
    </span><span style="color:#808080;">///</span><span style="color:#008000;"> Custom extension that sets up the dependencies needed to use Rhino Security.
    </span><span style="color:#808080;">///</span><span style="color:#008000;"> </span><span style="color:#808080;">&lt;/summary&gt;</span><span style="color:#808080;">
</span><span style="color:#000000;">    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> UnityRhinoSecurityExtension : UnityContainerExtension
    {
        </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Initialize()
        {
            </span><span style="color:#008000;">//</span><span style="color:#008000;"> Setup dependencies needed by Rhino Security.</span><span style="color:#008000;">
</span><span style="color:#000000;">            Container.RegisterType</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">IAuthorizationService, AuthorizationService</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();
            Container.RegisterType</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">IAuthorizationRepository, AuthorizationRepository</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();
            Container.RegisterType</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">IPermissionsBuilderService, PermissionsBuilderService</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();
            Container.RegisterType</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">IPermissionsService, PermissionsService</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();

            </span><span style="color:#008000;">//</span><span style="color:#008000;"> Make our Nhibernate Session available to Rhino Security.</span><span style="color:#008000;">
</span><span style="color:#000000;">            Container.AddNewExtension</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">StaticFactoryExtension</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();

            Container.Configure</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">IStaticFactoryConfiguration</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">()
                .RegisterFactory</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">ISession</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">(GetSession);

            </span><span style="color:#008000;">//</span><span style="color:#008000;"> Indicate that Rhino Security should use our Unity container for looking up dependencies.</span><span style="color:#008000;">
</span><span style="color:#000000;">            ServiceLocator.SetLocatorProvider(() </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> UnityServiceLocator(Container));
        }

        </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">static</span><span style="color:#000000;"> ISession GetSession(IUnityContainer container)
        {
            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> container.Resolve</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">ISessionManager</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">().Session;
        }
    }</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>In the GetSession method you have to return the instance of the Nhibernate Session you are using. After this, add the extension to your container:</p>
<p><div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:59df4b20-d540-45c5-8646-3cc00580930f" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">container.AddNewExtension</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">UnityRhinoSecurityExtension</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The following line in the extension configures the Common Service Locator to use the Unity adapter and passes the instance of the container we are using:</p>
<p><div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e00e9745-1da6-42ce-9079-5007effcc833" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">ServiceLocator.SetLocatorProvider(() </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> UnityServiceLocator(Container));</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Now the only thing left to do, is to give Rhino Security access to our Nhibernate configuration, so It can add the mappings for its own entities. We are using Fluent Nhibernate, which makes our configuration look like the following:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3a477864-5e95-4faa-8767-2a6f76149bbf" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">return</span><span style="color:#000000;"> Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2005.
                  ConnectionString(
                  x </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> x.FromConnectionStringWithKey(ConnectionStringConfigKey))
                  .ProxyFactoryFactory(</span><span style="color:#0000FF;">typeof</span><span style="color:#000000;"> (ProxyFactoryFactory).AssemblyQualifiedName))
    .Mappings(m </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> m.AutoMappings.Add(AutoPersistenceModel.MapEntitiesFromAssemblyOf</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">User</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">()))
    .ExposeConfiguration(cfg </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> Security.Configure</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">User</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">(cfg, SecurityTableStructure.Prefix))
    .BuildSessionFactory();</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The following line exposes our configuration to Rhino Security:</p>
<p><div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c6394255-134a-4b7d-a8f9-24c138c5f2e2" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">.ExposeConfiguration(cfg </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> Security.Configure</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">User</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">(cfg, SecurityTableStructure.Prefix))</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Now when you generate the database schema from you Nhibernate configuration, you are able to use the Rhino Security services to implement authorization in you application.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonnekats.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonnekats.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonnekats.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonnekats.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jonnekats.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jonnekats.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jonnekats.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jonnekats.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonnekats.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonnekats.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonnekats.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonnekats.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonnekats.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonnekats.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=48&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jonnekats.wordpress.com/2009/09/15/rhino-security-with-unity/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e3f1ef2b9717a4f5497b79acda6e424?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jonnekats</media:title>
		</media:content>
	</item>
		<item>
		<title>Convention over configuration</title>
		<link>http://jonnekats.wordpress.com/2009/08/04/convention-over-configuration/</link>
		<comments>http://jonnekats.wordpress.com/2009/08/04/convention-over-configuration/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 20:22:05 +0000</pubDate>
		<dc:creator>jonnekats</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Design & Architecture]]></category>
		<category><![CDATA[Convention over configuration]]></category>

		<guid isPermaLink="false">http://jonnekats.wordpress.com/2009/08/04/convention-over-configuration/</guid>
		<description><![CDATA[Ruby on rails makes heavily use of the Convention over configuration design paradigm. At the moment it is getting a hot topic in the .NET world too. If you are like me and are lazy and hate doing repetitive work, then you should really check it out, if you are not, you should check it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=47&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ruby on rails makes heavily use of the <a href="http://en.wikipedia.org/wiki/Convention_over_configuration" target="_blank">Convention over configuration</a> design paradigm. At the moment it is getting a hot topic in the .NET world too. If you are like me and are lazy and hate doing repetitive work, then you should really check it out, if you are not, you should check it out anyway. In my opinion, when properly applied, convention over configuration can save developers a lot of time and effort. </p>
<p>So what is convention over configuration? One of the best examples can be found in the <a href="http://fluentnhibernate.org/" target="_blank">Fluent Nhibernate project</a>. This is a project that facilitates configuring Nhibernate, without the use of those horrible XML mapping files. One feature of this project is <a href="http://wiki.fluentnhibernate.org/show/AutoMapping" target="_blank">Automapping</a>, which is convention over configuration in its purest form. I will just assume you are familiar with the Nhibernate mapping files, if not, you can read all about in the <a href="http://www.google.nl/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Fnhforge.org%2Fwikis%2Freference2-0en%2Fnhibernate-2-0-reference-documentation.aspx&amp;ei=-5N4SqCWG8HZ-QafjeHSBQ&amp;usg=AFQjCNFj0NVGMdg-YEjx9Ia3Jh2VfW_bVg&amp;sig2=WONilrfqLkOV1Im0c3J9xw" target="_blank">Nhibernate documentation</a>.</p>
<p>Let’s say you have a domain entity Customer:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7b7d9483-fcbb-4e5e-81d2-b2b21c698254" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> Customer
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> Id { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> Name { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>&#160;</p>
<p>And you would have your corresponding Nhibernate mapping file as we know it all too well:</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ef1e47d4-bbf6-44c1-9758-00d80d136833" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">&lt;?</span><span style="color:#000000;">xml version</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">1.0</span><span style="color:#800000;">"</span><span style="color:#000000;"> encoding</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">utf-8</span><span style="color:#800000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">?&gt;</span><span style="color:#000000;">
</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">hibernate</span><span style="color:#000000;">-</span><span style="color:#000000;">mapping xmlns</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">urn:nhibernate-mapping-2.0</span><span style="color:#800000;">"</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
  </span><span style="color:#000000;">&lt;</span><span style="color:#0000FF;">class</span><span style="color:#000000;"> name</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">NHibernate.Examples.Customer, NHibernate.Examples</span><span style="color:#800000;">"</span><span style="color:#000000;"> table</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">Customer</span><span style="color:#800000;">"</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
    </span><span style="color:#000000;">&lt;</span><span style="color:#000000;">id name</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">Id</span><span style="color:#800000;">"</span><span style="color:#000000;"> column</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">Id</span><span style="color:#800000;">"</span><span style="color:#000000;"> type</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">Int</span><span style="color:#800000;">"</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
      </span><span style="color:#000000;">&lt;</span><span style="color:#000000;">generator </span><span style="color:#0000FF;">class</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">identity</span><span style="color:#800000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">/&gt;</span><span style="color:#000000;">
    </span><span style="color:#000000;">&lt;/</span><span style="color:#000000;">id</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
    </span><span style="color:#000000;">&lt;</span><span style="color:#000000;">property name</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">Name</span><span style="color:#800000;">"</span><span style="color:#000000;"> column</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">Name</span><span style="color:#800000;">"</span><span style="color:#000000;"> type</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">String</span><span style="color:#800000;">"</span><span style="color:#000000;"> length</span><span style="color:#000000;">=</span><span style="color:#800000;">"</span><span style="color:#800000;">40</span><span style="color:#800000;">"</span><span style="color:#000000;">/&gt;</span><span style="color:#000000;">
  </span><span style="color:#000000;">&lt;/</span><span style="color:#0000FF;">class</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
</span><span style="color:#000000;">&lt;/</span><span style="color:#000000;">hibernate</span><span style="color:#000000;">-</span><span style="color:#000000;">mapping</span><span style="color:#000000;">&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>&#160;</p>
</p>
<p>I used to have mapping files like these all over the place. Writing these mapping files gets really boring and mistakes are easily made and hard to debug. The Automapping feature makes all of this a thing of the past. With Automapping it is possible to provide, for example a namespace, and automatically map all the entities in that namespace to a database. This mapping takes place based on conventions. One such convention can be that an entity’s property with the name Id always maps on an identity field named Id in the database. Or that a many to one relationship always maps on to a certain column. For example Order –&gt; Customer automatically maps to the column Customer_id in the Order table. With the fluent nhibernate automapping it is also really easy to overwrite the default conventions by providing your own. Off course, this is not very useful when mapping to a legacy database, but in most of the green field Nhibernate projects it can be applied successfully. So in this case, in stead of configuring all of the mappings in XML files, we have certain conventions that allow Fluent nhibernate to assume entities are mapped to the database in the same consistent way.</p>
<p>&#160;</p>
<p>Another good example of convention over configuration is the registration of services with an Inversion of Control container. In a lot of TDD based systems, there are loads of interfaces that only have one implementation. So why have to register all of these services one at the time? Why not just say to the container automap all services that fulfill a certain requirement, like being in a namespace or deriving from a certain base interface. Structuremap has this functionality out of the box. Unity doesn’t have this yet, but <a href="http://www.ctrl-shift-b.com" target="_blank">Derek Greer</a> has created a Unity extensions to provide this, which he describes in this <a href="http://www.ctrl-shift-b.com/2009/06/convention-based-registration-extension.html" target="_blank">blog post</a>.</p>
<p>So instead of doing this: </p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a0af7cbe-6dd7-45fe-975b-3f191ab95c58" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">Container.RegisterType</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">ICategoryRepository, CategoryRepository</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();
Container.RegisterType</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">ICustomerRepository, CustomerRepository</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();
Container.RegisterType</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">IOrderRepository, OrderRepository</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>It allows you to do stuff like this:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8a95f4b0-d65e-4b4a-9d5c-6041c2eef799" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">Container
    .AddNewExtension</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">ConventionExtension</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">()
    .Using</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">IConventionExtension</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">()
    .Configure(x </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;">
                  {
                     x.Conventions.Add</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">ImplementationConvention</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">IRepository</span><span style="color:#000000;">&gt;&gt;</span><span style="color:#000000;">();
                     x.Assemblies.Add(Assembly.GetExecutingAssembly());
                   })
    .Register();</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Which automatically maps all the services deriving from IRepository in the executing assembly. This can save you a lot of work, personally I always forget to add the service I made to the container. However, it also involves a lot of black magic. The wiring gets done under the hood, so how you debug a problem when something goes wrong? Well you will need some diagnostics. For example, it is possible to have Fluent Nhibernate spit out all the mapping xml file that are used under the hood, which makes it easy to debug. So when diagnostics are provided, convention over configuration can be really useful and is a welcome addition to my bag of tricks.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonnekats.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonnekats.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonnekats.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonnekats.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jonnekats.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jonnekats.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jonnekats.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jonnekats.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonnekats.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonnekats.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonnekats.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonnekats.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonnekats.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonnekats.wordpress.com/47/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=47&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jonnekats.wordpress.com/2009/08/04/convention-over-configuration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e3f1ef2b9717a4f5497b79acda6e424?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jonnekats</media:title>
		</media:content>
	</item>
		<item>
		<title>Xpath queries with Fitnesse webtest</title>
		<link>http://jonnekats.wordpress.com/2009/07/27/xpath-queries-with-fitnesse-webtest/</link>
		<comments>http://jonnekats.wordpress.com/2009/07/27/xpath-queries-with-fitnesse-webtest/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 20:30:17 +0000</pubDate>
		<dc:creator>jonnekats</dc:creator>
				<category><![CDATA[Acceptance based testing]]></category>
		<category><![CDATA[Fitnesse]]></category>
		<category><![CDATA[webtest]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://jonnekats.wordpress.com/2009/07/27/xpath-queries-with-fitnesse-webtest/</guid>
		<description><![CDATA[As I wrote earlier, I’m using Fitnesse with the webtest driver to do acceptance based testing. Using webtest, it is really easy to write selenium based acceptance tests in a DSL way. However, the id’s that ASP.NET controls get assigned can get pretty ugly, especially when working with controls like the ASP.NET repeater. When working [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=44&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As I wrote earlier, I’m using Fitnesse with the webtest driver to do acceptance based testing. Using webtest, it is really easy to write selenium based acceptance tests in a DSL way. However, the id’s that ASP.NET controls get assigned can get pretty ugly, especially when working with controls like the ASP.NET repeater. </p>
<p>When working with these controls, xpath can be really useful. To use xpath queries for looking up controls with webtest, you have to enclose the command that needs to use xpath with the following statements:</p>
<p>|Set locator lookup|false|</p>
<p>&#8211; The statements that use xpath, for example:</p>
<p>|click|//input[Contains(@id, ‘ageSelector’)]|</p>
<p>|Set locator lookup|true|</p>
<p>By setting the locator lookup to false, it is possible to use xpath queries in stead of the normal lookup strategy used by Webtest (Ids, names or values).</p>
<p>When you are having trouble coming up with these xpath queries, then firebug really is your friend. It is possible to open up the firebug console and using a special command: $x(‘’) you can&#160; then debug your commands:</p>
<p><a href="http://jonnekats.files.wordpress.com/2009/07/image.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://jonnekats.files.wordpress.com/2009/07/image_thumb.png?w=394&#038;h=149" width="394" height="149" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonnekats.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonnekats.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonnekats.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonnekats.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jonnekats.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jonnekats.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jonnekats.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jonnekats.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonnekats.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonnekats.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonnekats.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonnekats.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonnekats.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonnekats.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=44&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jonnekats.wordpress.com/2009/07/27/xpath-queries-with-fitnesse-webtest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e3f1ef2b9717a4f5497b79acda6e424?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jonnekats</media:title>
		</media:content>

		<media:content url="http://jonnekats.files.wordpress.com/2009/07/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Screencasts Todo</title>
		<link>http://jonnekats.wordpress.com/2009/07/15/screencasts-todo/</link>
		<comments>http://jonnekats.wordpress.com/2009/07/15/screencasts-todo/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 11:34:03 +0000</pubDate>
		<dc:creator>jonnekats</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonnekats.wordpress.com/2009/07/15/screencasts-todo/</guid>
		<description><![CDATA[Lately I’ve seen more and more screencast announcments pop by in my RSS reader, but unfortunately, the moment I see these, is not the moment I have the time to watch a 30 minute screencast. So I decided to make a list of screencasts I want to see when I have some time at hand. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=41&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Lately I’ve seen more and more screencast announcments pop by in my RSS reader, but unfortunately, the moment I see these, is not the moment I have the time to watch a 30 minute screencast. So I decided to make a list of screencasts I want to see when I have some time at hand.</p>
<p><strong>Jeremy Miller</strong> &#8211; The joys and pains of a long lived codebase</p>
<p><a title="http://www.infoq.com/presentations/Lessons-Learned-Jeremy-Miller" href="http://www.infoq.com/presentations/Lessons-Learned-Jeremy-Miller">http://www.infoq.com/presentations/Lessons-Learned-Jeremy-Miller</a></p>
<p><strong>Eric Evans -</strong> on the state of Domain Driven Development</p>
<p><a title="http://www.infoq.com/news/2009/06/eric-evans-interview-ddd" href="http://www.infoq.com/news/2009/06/eric-evans-interview-ddd">http://www.infoq.com/news/2009/06/eric-evans-interview-ddd</a></p>
<p>Udi Dahan &#8211; on SOA</p>
<p><a href="http://www.vimeo.com/5022174">http://www.vimeo.com/5022174</a></p>
<p><strong>Greg Young</strong> – on DDD</p>
<p><a href="http://www.vimeo.com/3171910">http://www.vimeo.com/3171910</a></p>
<p><strong>Eric Evans</strong> – on DDD: Strategic Design</p>
<p><a title="http://www.infoq.com/presentations/strategic-design-evans" href="http://www.infoq.com/presentations/strategic-design-evans">http://www.infoq.com/presentations/strategic-design-evans</a></p>
<p><strong>Eric Evans</strong> – on DDD: putting the model to work</p>
<p><a title="http://www.infoq.com/presentations/model-to-work-evans" href="http://www.infoq.com/presentations/model-to-work-evans">http://www.infoq.com/presentations/model-to-work-evans</a></p>
<p><strong>Tim McCarthy</strong> – on Framework and DDD: Keeping the Model Clean</p>
<p><a title="http://www.infoq.com/presentations/Clean-Model-Tim-McCarthy" href="http://www.infoq.com/presentations/Clean-Model-Tim-McCarthy">http://www.infoq.com/presentations/Clean-Model-Tim-McCarthy</a></p>
<p><strong>Dave Astels</strong> and <strong>Steven Baker</strong> – on RSpec and Behavior Driven Development</p>
<p><a title="http://www.infoq.com/interviews/Dave-Astels-and-Steven-Baker" href="http://www.infoq.com/interviews/Dave-Astels-and-Steven-Baker">http://www.infoq.com/interviews/Dave-Astels-and-Steven-Baker</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonnekats.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonnekats.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonnekats.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonnekats.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jonnekats.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jonnekats.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jonnekats.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jonnekats.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonnekats.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonnekats.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonnekats.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonnekats.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonnekats.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonnekats.wordpress.com/41/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=41&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jonnekats.wordpress.com/2009/07/15/screencasts-todo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e3f1ef2b9717a4f5497b79acda6e424?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jonnekats</media:title>
		</media:content>
	</item>
		<item>
		<title>Acceptance based testing tools</title>
		<link>http://jonnekats.wordpress.com/2009/05/12/acceptance-based-testing-tools/</link>
		<comments>http://jonnekats.wordpress.com/2009/05/12/acceptance-based-testing-tools/#comments</comments>
		<pubDate>Tue, 12 May 2009 09:54:58 +0000</pubDate>
		<dc:creator>jonnekats</dc:creator>
				<category><![CDATA[Design & Architecture]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Acceptance testing]]></category>
		<category><![CDATA[Fitnesse]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[StoryTeller]]></category>
		<category><![CDATA[Twist]]></category>

		<guid isPermaLink="false">http://jonnekats.wordpress.com/?p=34</guid>
		<description><![CDATA[I really like the idea of having executable specifications. They can be used as requirements documentation and describe what your stakeholder expects from the software. Besides that, it is also useful as an indication of the progress being made by the development team. But what tools are there for automating acceptance tests? Fitnesse One of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=34&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I really like the idea of having executable specifications. They can be used as requirements documentation and describe what your stakeholder expects from the software. Besides that, it is also useful as an indication of the progress being made by the development team. But what tools are there for automating acceptance tests?</p>
<p><strong>Fitnesse</strong></p>
<p>One of the most commonly used tools is <a href="http://www.fitnesse.org/" target="_blank">Fitnesse</a>. This is a wiki in which the tests can be described as tables. These tests can be described by the stakeholder and be mapped to test drivers that are created by the developers. A test driver is a mapping of the acceptance test to the code that needs to be tested, thus making the test executable.</p>
<p>There also is an extension to Fitnesse called <a href="http://www.fitnesse.info/webtest" target="_blank">Webtest</a>. This actually is a generic test driver for mapping Fitnesse tests to <a href="http://seleniumhq.org/" target="_blank">Selenium</a> scripts. If you are not familiar with Selenium, Selenium is a tool for testing web applications. For example, in c# a Selenium script looks like the following:</p>
<p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4e567043-72df-4287-9503-8bebc3ca1c40" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">ISelenium sel </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> DefaultSelenium(
</span><span style="color:#800000;">"</span><span style="color:#800000;">localhost</span><span style="color:#800000;">"</span><span style="color:#000000;">, </span><span style="color:#800080;">4444</span><span style="color:#000000;">, </span><span style="color:#800000;">"</span><span style="color:#800000;">*firefox</span><span style="color:#800000;">"</span><span style="color:#000000;">, </span><span style="color:#800000;">"</span><span style="color:#800000;">http://www.google.com</span><span style="color:#800000;">"</span><span style="color:#000000;">);
sel.Start();
sel.Open(</span><span style="color:#800000;">"</span><span style="color:#800000;">http://www.google.com/</span><span style="color:#800000;">"</span><span style="color:#000000;">);
sel.Type(</span><span style="color:#800000;">"</span><span style="color:#800000;">q</span><span style="color:#800000;">"</span><span style="color:#000000;">, </span><span style="color:#800000;">"</span><span style="color:#800000;">FitNesse</span><span style="color:#800000;">"</span><span style="color:#000000;">);
sel.Click(</span><span style="color:#800000;">"</span><span style="color:#800000;">btnG</span><span style="color:#800000;">"</span><span style="color:#000000;">);
sel.WaitForPageToLoad(</span><span style="color:#800000;">"</span><span style="color:#800000;">3000</span><span style="color:#800000;">"</span><span style="color:#000000;">);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The webtest extension for Fitnesse allows you to write acceptance tests like the following:</p>
<h5><a href="http://jonnekats.files.wordpress.com/2009/05/image2.png" target="_blank"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://jonnekats.files.wordpress.com/2009/05/image_thumb2.png?w=455&#038;h=299" width="455" height="299" /></a> </h5>
<p>The test can then be executed from within the browser, which results in:</p>
<p><a href="http://jonnekats.files.wordpress.com/2009/05/image3.png" target="_blank"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://jonnekats.files.wordpress.com/2009/05/image_thumb3.png?w=455&#038;h=297" width="455" height="297" /></a> </p>
<p>So this is really cool, I really like the ability for the customer to open the tests via a browser and run the tests from the browser. But there are some things about Fitnesse that I don’t really like. As far as I know there is no source control integration and there is some friction in writing the test drivers. Writing the web tests is easy, but sometimes you need to test different functionality, which requires writing custom test drivers / fixtures. Some of these fixtures can become really cumbersome and complex. Besides that, I also think the tests still contain to much technical details. The organization of the tests (think test suites) and also the definition of the tests still contain technical details and are pretty fragile. The customer can easily break the tests. So overall, I really like the fitnesse approach, but there are some shortcomings. So are there any alternatives? </p>
<p><strong>StoryTeller</strong></p>
<p><a href="http://storyteller.tigris.org/" target="_blank">StoryTeller</a> is a replacement for Fitnesse. The first version of StoryTeller was based on the Fitnesse test engine, but the creator ran into a wall with the Fitnesse test engine. So he decided to reboot the StoryTeller project from scratch and created his own test engine. The progress looks promising, but the project is still very fresh. I’m eagerly following its progress, but to me it looks to immature to be used in a real live project at the moment. However, its creator <a href="http://codebetter.com/blogs/jeremy.miller/default.aspx" target="_blank">Jeremy Miller</a>, is a respectable member in the .NET community with an impressive track record, so I would definitely keep my eye on this one.</p>
<p><strong>Twist</strong></p>
<p>Thoughtworks offers a commercial product called <a href="http://studios.thoughtworks.com/twist-agile-test-automation" target="_blank">Twist</a>, which really is the way to go in my opinion. The user has it’s own user interface for defining the user stories in its purest form. The developers can then map these stories to executable tests from their own development environment. I really like the way this separates&#160; concerns. However the product uses Eclipse as a host IDE and the test drivers can only be written in java, which is not an option to me. Now what to use?</p>
<p><strong>What to use</strong></p>
<p>In my opinion, Twist provides the best experience to the end user and makes it really easy for developers to write the test drivers. If twist were to be integrated into visual studio and provided the ability to write test drivers in .NET, I would probably go for Twist. Unfortunately, this is not the case. </p>
<p>I’m also really interested in which way StoryTeller is going. Jeremy Miller just released a <a href="http://codebetter.com/blogs/jeremy.miller/archive/2009/05/11/sneek-peek-at-storyteller-i-e-my-fitnesse-killer-for-net.aspx" target="_blank">sneak preview</a>, but it does not provide any information about how the user will be defining the tests. I hope it will be anything like Twist. </p>
<p>For now, despite its shortcomings, I’m going to continue my work with Fitnesse, because it simply is the most widely used and mature tool out there.&#160; However, I’m keeping my eyes open for alternatives.&#160; </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonnekats.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonnekats.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonnekats.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonnekats.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jonnekats.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jonnekats.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jonnekats.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jonnekats.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonnekats.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonnekats.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonnekats.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonnekats.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonnekats.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonnekats.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=34&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jonnekats.wordpress.com/2009/05/12/acceptance-based-testing-tools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e3f1ef2b9717a4f5497b79acda6e424?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jonnekats</media:title>
		</media:content>

		<media:content url="http://jonnekats.files.wordpress.com/2009/05/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://jonnekats.files.wordpress.com/2009/05/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Integrate xUnit tests into your daily Team Build</title>
		<link>http://jonnekats.wordpress.com/2009/05/07/integrate-xunit-tests-into-your-daily-team-build/</link>
		<comments>http://jonnekats.wordpress.com/2009/05/07/integrate-xunit-tests-into-your-daily-team-build/#comments</comments>
		<pubDate>Thu, 07 May 2009 08:28:59 +0000</pubDate>
		<dc:creator>jonnekats</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Test driven development]]></category>
		<category><![CDATA[tfs]]></category>
		<category><![CDATA[xunit]]></category>

		<guid isPermaLink="false">http://jonnekats.wordpress.com/2009/05/07/integrate-xunit-tests-into-your-daily-team-build/</guid>
		<description><![CDATA[Doing proper Test Driven Development with the default visual studio test runner (mstest) is like playing Gears of War 2 on an old fashioned tube television, it’s just wrong .. There are several alternatives out there like mbUnit and nUnit, but my favorite is xUnit. xUnit is heavily extensible and there is a project called [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=27&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Doing proper Test Driven Development with the default visual studio test runner (mstest) is like playing Gears of War 2 on an old fashioned tube television, it’s just wrong <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .. There are several alternatives out there like mbUnit and nUnit, but my favorite is xUnit. xUnit is heavily extensible and there is a project called <a href="http://code.google.com/p/xunitbddextensions/" target="_blank">xunitbddextensions</a>, which extends xunit making it possible to do specication based testing (<a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development" target="_blank">BDD</a>). Using resharper or testdriven.net this makes for an excellent experience when developing in visual studio. xUnit tests can also be integrated into your daily TFS (Microsoft Team Foundation Server) build by using the xUnit build task. However, the details of the testresults are not displayed in the build overview and the results are not used in the reporting functionality of TFS (Like the Quality Indicators report). </p>
<p>On the xUnit site there is some discussion about this functionality, but at the moment there is no implementation. For nUnit there is a project on codeplex called <a href="http://www.codeplex.com/nunit4teambuild" target="_blank">nUnit for Team Build</a>. This consists of a sample build script that uses an XSLT file to translate the xml results of nUnit into a visual studio test result file and publishes this result to TFS. This way integrating the nUnit test results into TFS.</p>
<p>I figured I could modify the XSLT file to translate the xUnit tests results instead. However, having a better look at the xUnit build task I noticed a NunitXml property, which makes it possible to output the xUnit results into a nUnit compatible xml file.&#160; So I could just use the XSLT file and integrate the xUnit results into our TFS build. </p>
<p>To make this work I only had to modify the build script to use the xUnit build task instead of&#160; the nUnit build task. This resulted in the following script (This just needs to be added to the end of your build definition):</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:611e04d8-0ebe-46f2-b3f3-5a8abbeddeee" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">  </span><span style="color:#008000;">&lt;!--</span><span style="color:#008000;"> Xunit Build task</span><span style="color:#008000;">--&gt;</span><span style="color:#000000;">
  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">PropertyGroup</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">XunitBuildTaskPath</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">$(ProgramFiles)\MSBuild\Xunit\</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">XunitBuildTaskPath</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
  </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">PropertyGroup</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">UsingTask </span><span style="color:#FF0000;">AssemblyFile</span><span style="color:#0000FF;">="$(XunitBuildTaskPath)\xunit.runner.msbuild.dll"</span><span style="color:#FF0000;">
    TaskName</span><span style="color:#0000FF;">="Xunit.Runner.MSBuild.xunit"</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">

  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Target </span><span style="color:#FF0000;">Name</span><span style="color:#0000FF;">="AfterCompile"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
    </span><span style="color:#008000;">&lt;!--</span><span style="color:#008000;"> Create a Custom Build Step </span><span style="color:#008000;">--&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">BuildStep </span><span style="color:#FF0000;">TeamFoundationServerUrl</span><span style="color:#0000FF;">="$(TeamFoundationServerUrl)"</span><span style="color:#FF0000;"> BuildUri</span><span style="color:#0000FF;">="$(BuildUri)"</span><span style="color:#FF0000;"> Name</span><span style="color:#0000FF;">="XUnitTestStep"</span><span style="color:#FF0000;"> Message</span><span style="color:#0000FF;">="Running Xunit Tests"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
      </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Output </span><span style="color:#FF0000;">TaskParameter</span><span style="color:#0000FF;">="Id"</span><span style="color:#FF0000;"> PropertyName</span><span style="color:#0000FF;">="XUnitStepId"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">BuildStep</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">CreateItem </span><span style="color:#FF0000;">Include</span><span style="color:#0000FF;">="$(OutDir)\*.Test.dll"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
      </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Output </span><span style="color:#FF0000;">TaskParameter</span><span style="color:#0000FF;">="Include"</span><span style="color:#FF0000;"> ItemName</span><span style="color:#0000FF;">="TestAssemblies"</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">CreateItem</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

    </span><span style="color:#008000;">&lt;!--</span><span style="color:#008000;"> Run the tests </span><span style="color:#008000;">--&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">xunit </span><span style="color:#FF0000;">ContinueOnError</span><span style="color:#0000FF;">="true"</span><span style="color:#FF0000;"> Assembly</span><span style="color:#0000FF;">="@(TestAssemblies)"</span><span style="color:#FF0000;"> NUnitXml</span><span style="color:#0000FF;">="$(OutDir)nunit_results.xml"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
      </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Output </span><span style="color:#FF0000;">TaskParameter</span><span style="color:#0000FF;">="ExitCode"</span><span style="color:#FF0000;"> PropertyName</span><span style="color:#0000FF;">="XUnitResult"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">xunit</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

    </span><span style="color:#008000;">&lt;!--</span><span style="color:#008000;"> Set the status of the buildstep in the build overview </span><span style="color:#008000;">--&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">BuildStep </span><span style="color:#FF0000;">Condition</span><span style="color:#0000FF;">="'$(XUnitResult)'=='0'"</span><span style="color:#FF0000;"> TeamFoundationServerUrl</span><span style="color:#0000FF;">="$(TeamFoundationServerUrl)"</span><span style="color:#FF0000;"> BuildUri</span><span style="color:#0000FF;">="$(BuildUri)"</span><span style="color:#FF0000;"> Id</span><span style="color:#0000FF;">="$(XUnitStepId)"</span><span style="color:#FF0000;"> Status</span><span style="color:#0000FF;">="Succeeded"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">BuildStep </span><span style="color:#FF0000;">Condition</span><span style="color:#0000FF;">="'$(XUnitResult)'!='0'"</span><span style="color:#FF0000;"> TeamFoundationServerUrl</span><span style="color:#0000FF;">="$(TeamFoundationServerUrl)"</span><span style="color:#FF0000;"> BuildUri</span><span style="color:#0000FF;">="$(BuildUri)"</span><span style="color:#FF0000;"> Id</span><span style="color:#0000FF;">="$(XUnitStepId)"</span><span style="color:#FF0000;"> Status</span><span style="color:#0000FF;">="Failed"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">

    </span><span style="color:#008000;">&lt;!--</span><span style="color:#008000;"> Regardless of NUnit success/failure merge results into the build </span><span style="color:#008000;">--&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Exec </span><span style="color:#FF0000;">Command</span><span style="color:#0000FF;">="&amp;quot;$(XunitBuildTaskPath)\nxslt3.exe&amp;quot; &amp;quot;$(OutDir)nunit_results.xml&amp;quot; &amp;quot;$(XunitBuildTaskPath)\nunit transform.xslt&amp;quot; -o &amp;quot;$(OutDir)xunit_results.trx&amp;quot;"</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Exec </span><span style="color:#FF0000;">Command</span><span style="color:#0000FF;">="&amp;quot;$(ProgramFiles)\Microsoft Visual Studio 9.0\Common7\IDE\mstest.exe&amp;quot; /publish:$(TeamFoundationServerUrl) /publishbuild:&amp;quot;$(BuildNumber)&amp;quot; /publishresultsfile:&amp;quot;$(OutDir)xunit_results.trx&amp;quot; /teamproject:&amp;quot;$(TeamProject)&amp;quot; /platform:&amp;quot;%(ConfigurationToBuild.PlatformToBuild)&amp;quot; /flavor:&amp;quot;%(ConfigurationToBuild.FlavorToBuild)&amp;quot;"</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">

    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">Error </span><span style="color:#FF0000;">Condition</span><span style="color:#0000FF;">="'$(XUnitResult)'!='0'"</span><span style="color:#FF0000;"> Text</span><span style="color:#0000FF;">="XUnit Tests Failed"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
  </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">Target</span><span style="color:#0000FF;">&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This script assumes you have copied the xunit&#160; build task, the XSLT file and the nxslt3.exe file to a \MSBuild\Xunit\ folder under the program files folder on your build server. </p>
<p>Whether the tests fail or not, the resuls should always be published. However, the xUnit build task does not have an ExitCode and I could not find another way to get the result from the xUnit task. So I have modified the xUnit task to provide an ExitCode. Open up the xUnit code from codeplex. Locate the xunit.runner.msbuild project and open up the xUnit class. Add the following property and modify the Excute method to modify the value:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e5c91279-12f7-4286-85b0-7e6bffb8566d" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">        [Output]
        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> ExitCode { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }

        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">bool</span><span style="color:#000000;"> Execute()
        {
            </span><span style="color:#0000FF;">try</span><span style="color:#000000;">
            {
                </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> assemblyFilename </span><span style="color:#000000;">=</span><span style="color:#000000;"> Assembly.GetMetadata(</span><span style="color:#800000;">"</span><span style="color:#800000;">FullPath</span><span style="color:#800000;">"</span><span style="color:#000000;">);

                </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (WorkingFolder </span><span style="color:#000000;">!=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">)
                    Directory.SetCurrentDirectory(WorkingFolder);

                </span><span style="color:#0000FF;">using</span><span style="color:#000000;"> (ExecutorWrapper wrapper </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> ExecutorWrapper(assemblyFilename, ConfigFile, ShadowCopy))
                {
                    Log.LogMessage(MessageImportance.High, </span><span style="color:#800000;">"</span><span style="color:#800000;">xUnit.net MSBuild runner (xunit.dll version {0})</span><span style="color:#800000;">"</span><span style="color:#000000;">, wrapper.XunitVersion);
                    Log.LogMessage(MessageImportance.High, </span><span style="color:#800000;">"</span><span style="color:#800000;">Test assembly: {0}</span><span style="color:#800000;">"</span><span style="color:#000000;">, assemblyFilename);

                    IRunnerLogger logger </span><span style="color:#000000;">=</span><span style="color:#000000;">
                        TeamCity </span><span style="color:#000000;">?</span><span style="color:#000000;"> (IRunnerLogger)</span><span style="color:#0000FF;">new</span><span style="color:#000000;"> TeamCityLogger(Log) :
                        Verbose </span><span style="color:#000000;">?</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> VerboseLogger(Log) :
                        </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> StandardLogger(Log);

                    List</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">IResultXmlTransform</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> transforms </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> List</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">IResultXmlTransform</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();

                    </span><span style="color:#0000FF;">using</span><span style="color:#000000;"> (Stream htmlStream </span><span style="color:#000000;">=</span><span style="color:#000000;"> ResourceStream(</span><span style="color:#800000;">"</span><span style="color:#800000;">HTML.xslt</span><span style="color:#800000;">"</span><span style="color:#000000;">))
                    </span><span style="color:#0000FF;">using</span><span style="color:#000000;"> (Stream nunitStream </span><span style="color:#000000;">=</span><span style="color:#000000;"> ResourceStream(</span><span style="color:#800000;">"</span><span style="color:#800000;">NUnitXml.xslt</span><span style="color:#800000;">"</span><span style="color:#000000;">))
                    {
                        </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (Xml </span><span style="color:#000000;">!=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">)
                            transforms.Add(</span><span style="color:#0000FF;">new</span><span style="color:#000000;"> NullTransformer(Xml.GetMetadata(</span><span style="color:#800000;">"</span><span style="color:#800000;">FullPath</span><span style="color:#800000;">"</span><span style="color:#000000;">)));
                        </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (Html </span><span style="color:#000000;">!=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">)
                            transforms.Add(</span><span style="color:#0000FF;">new</span><span style="color:#000000;"> XslStreamTransformer(htmlStream, Html.GetMetadata(</span><span style="color:#800000;">"</span><span style="color:#800000;">FullPath</span><span style="color:#800000;">"</span><span style="color:#000000;">), </span><span style="color:#800000;">"</span><span style="color:#800000;">HTML</span><span style="color:#800000;">"</span><span style="color:#000000;">));
                        </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (NUnitXml </span><span style="color:#000000;">!=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">)
                            transforms.Add(</span><span style="color:#0000FF;">new</span><span style="color:#000000;"> XslStreamTransformer(nunitStream, NUnitXml.GetMetadata(</span><span style="color:#800000;">"</span><span style="color:#800000;">FullPath</span><span style="color:#800000;">"</span><span style="color:#000000;">), </span><span style="color:#800000;">"</span><span style="color:#800000;">NUnit XML</span><span style="color:#800000;">"</span><span style="color:#000000;">));

                        TestRunner runner </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> TestRunner(wrapper, logger);
                        </span><span style="color:#0000FF;">if</span><span style="color:#000000;">(runner.RunAssembly(transforms) </span><span style="color:#000000;">==</span><span style="color:#000000;"> TestRunnerResult.Failed)
                        {
                            ExitCode </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">-</span><span style="color:#800080;">1</span><span style="color:#000000;">;
                            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#0000FF;">false</span><span style="color:#000000;">;
                        }

                        ExitCode </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">0</span><span style="color:#000000;">;
                        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#0000FF;">true</span><span style="color:#000000;">;
                    }
                }
            }
            </span><span style="color:#0000FF;">catch</span><span style="color:#000000;"> (Exception ex)
            {
                Exception e </span><span style="color:#000000;">=</span><span style="color:#000000;"> ex;

                </span><span style="color:#0000FF;">while</span><span style="color:#000000;"> (e </span><span style="color:#000000;">!=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">)
                {
                    Log.LogError(e.GetType().FullName </span><span style="color:#000000;">+</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">: </span><span style="color:#800000;">"</span><span style="color:#000000;"> </span><span style="color:#000000;">+</span><span style="color:#000000;"> e.Message);

                    </span><span style="color:#0000FF;">foreach</span><span style="color:#000000;"> (</span><span style="color:#0000FF;">string</span><span style="color:#000000;"> stackLine </span><span style="color:#0000FF;">in</span><span style="color:#000000;"> e.StackTrace.Split(</span><span style="color:#0000FF;">new</span><span style="color:#000000;">[] { </span><span style="color:#800000;">"</span><span style="color:#800000;">\r\n</span><span style="color:#800000;">"</span><span style="color:#000000;"> }, StringSplitOptions.RemoveEmptyEntries))
                        Log.LogError(stackLine);

                    e </span><span style="color:#000000;">=</span><span style="color:#000000;"> e.InnerException;
                }

                ExitCode </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#000000;">-</span><span style="color:#800080;">1</span><span style="color:#000000;">;

                </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#0000FF;">false</span><span style="color:#000000;">;
            }
        }</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Now build the project and deploy the xUnit build task to the server. To realize the xUnit build integration just follow the steps you find on the nunit4teambuild site and use the previous mentioned modifications. After this, you should be set to go.</p>
<p><strong>BTW</strong>: In the nunit4teambuild they use nxslt2, I used nxslt3. So depending on which one you use, modify the script accordingly.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonnekats.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonnekats.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonnekats.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonnekats.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jonnekats.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jonnekats.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jonnekats.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jonnekats.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonnekats.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonnekats.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonnekats.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonnekats.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonnekats.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonnekats.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=27&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jonnekats.wordpress.com/2009/05/07/integrate-xunit-tests-into-your-daily-team-build/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e3f1ef2b9717a4f5497b79acda6e424?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jonnekats</media:title>
		</media:content>
	</item>
		<item>
		<title>State based Domain Entities (State pattern)</title>
		<link>http://jonnekats.wordpress.com/2008/10/20/state-based-domain-entities-state-pattern/</link>
		<comments>http://jonnekats.wordpress.com/2008/10/20/state-based-domain-entities-state-pattern/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 19:31:22 +0000</pubDate>
		<dc:creator>jonnekats</dc:creator>
				<category><![CDATA[Domain driven design]]></category>

		<guid isPermaLink="false">http://jonnekats.wordpress.com/2008/10/20/state-based-domain-entities-state-pattern/</guid>
		<description><![CDATA[This post is actually just about bringing the state pattern into practice. At my current project, we have some entities that are based on state, which are forcing us to use some not so elegant mechanisms for change their state. I will try to explain this with the following example: So there is a Product [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=19&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This post is actually just about bringing the state pattern into practice. At my current project, we have some entities that are based on state, which are forcing us to use some not so elegant mechanisms for change their state. I will try to explain this with the following example:</p>
<p><img src="http://jonnekats.files.wordpress.com/2008/10/102008-1931-statebasedd11.png?w=480" alt="" /></p>
<p>So there is a <strong>Product</strong> that can either be a <strong>PlannedProduct</strong> or a <strong>ManufactoredProduct</strong>. Now let&#8217;s say our business domain tells us that there is some transition from a <strong>PlannedProduct</strong> to a <strong>ManufactoredProduct</strong>. With the above model you would have to convert the entities, because you cannot just change the inheritance type at runtime. In case you are persisting the domain, you would also have to write code to delete the old entity and save the newly created entity, using NHibernate this code would probably look like the following:</p>
<p>ManufactoredProduct manufactoredProduct = ProductConverter.Convert&lt; ManufactoredProduct&gt;(plannedProduct);</p>
<p>NHibernateSession.Delete(plannedProduct);</p>
<p>NHibernateSession.Flush();</p>
<p>NHibernateSession.SaveOrUpdate(manufactoredProduct);</p>
<p>NHibernateSession.Flush();</p>
<p>The double flush is needed if you have some unique constraints on the product fields. As you would probably agree, not the most elegant solution. Here comes the state pattern. <a href="http://en.wikipedia.org/wiki/State_pattern">Wikipedia</a> mentions that the State pattern provides a clean way for an object to change its type at runtime. Here is the refactored model:</p>
<p><img src="http://jonnekats.files.wordpress.com/2008/10/102008-1931-statebasedd21.png?w=480" alt="" /></p>
<p> </p>
<p>Now if we would want to change the product type we would just have to assign a new <strong>ManufactoredProductState</strong> to its State property, no conversion needed. Though, we would still have to delete the previous <strong>PlannedProductState</strong> entity, but without the strange double flush constructions.</p>
<p>So to conclude, now burning water here. You&#8217;ve probably all been using the state pattern this way for ages. For me it was the first time using the state pattern in this way, before I&#8217;ve used it solely for separating behavior based on state. I thought it was a nice improvement to our code and wanted to write it down as a reference.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonnekats.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonnekats.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonnekats.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonnekats.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jonnekats.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jonnekats.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jonnekats.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jonnekats.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonnekats.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonnekats.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonnekats.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonnekats.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonnekats.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonnekats.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=19&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jonnekats.wordpress.com/2008/10/20/state-based-domain-entities-state-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e3f1ef2b9717a4f5497b79acda6e424?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jonnekats</media:title>
		</media:content>

		<media:content url="http://jonnekats.files.wordpress.com/2008/10/102008-1931-statebasedd11.png" medium="image" />

		<media:content url="http://jonnekats.files.wordpress.com/2008/10/102008-1931-statebasedd21.png" medium="image" />
	</item>
		<item>
		<title>Unit Tests as documentation</title>
		<link>http://jonnekats.wordpress.com/2007/10/27/unit-tests-as-documentation/</link>
		<comments>http://jonnekats.wordpress.com/2007/10/27/unit-tests-as-documentation/#comments</comments>
		<pubDate>Sat, 27 Oct 2007 11:21:35 +0000</pubDate>
		<dc:creator>jonnekats</dc:creator>
				<category><![CDATA[Test driven development]]></category>
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">http://jonnekats.wordpress.com/2007/10/27/unit-tests-as-documentation/</guid>
		<description><![CDATA[One of the advantages of doing test driven development &#38; unit testing is having the unit tests as documentation of the code. When you read this in the unit testing / TDD books, it is hard to imagine how this would be of use in practice. As one of my favorite bloggers, Jeremy Miller, already writes, the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=12&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the advantages of doing test driven development &amp; unit testing is having the unit tests as documentation of the code. When you read this in the unit testing / TDD books, it is hard to imagine how this would be of use in practice.</p>
<p>As one of my favorite bloggers, <a href="http://codebetter.com/blogs/jeremy.miller/">Jeremy Miller</a>, already <a href="http://codebetter.com/blogs/jeremy.miller/archive/2006/02/20/138732.aspx">writes</a>, the <a href="http://www.nhibernate.org/">NHibernate</a> source is a really good example of using the tests as documentation. I think this is especially the case when developing frameworks or libraries. Because the code will always be used by developers, the best way for a developer to see how the code should be used is by looking at the unit tests as an example.</p>
<p>I experienced this myself a couple of days ago with the same code that Jeremy mentions. I also used the NHibernate EnumStringType to use an enum type that is represented in the database as a string column. This works great, but when I wanted to use the enum type in a HQL query, it didn&#8217;t work the way I had expected:</p>
<p> </p>
<div class="wlWriterSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:White;">
<div><span style="color:#000000;">            IQuery q </span><span style="color:#000000;">=</span><span style="color:#000000;"> s.CreateQuery(</span><span style="color:#000000;">"</span><span style="color:#000000;">from Order as order where order.State =:orderState</span><span style="color:#000000;">"</span><span style="color:#000000;">);
            q.SetEnum(</span><span style="color:#000000;">"</span><span style="color:#000000;">orderState</span><span style="color:#000000;">"</span><span style="color:#000000;">, OrderState.Open);
            IList results </span><span style="color:#000000;">=</span><span style="color:#000000;"> q.List();</span></div>
</pre>
</div>
<p>The code above gave me a vague oracle exception. So i needed to figure out how to pass the parameter to the HQL query. I fired up the NHIbernate source to open the unit test for the EnumStringType:</p>
<p><a href="http://jonnekats.files.wordpress.com/2007/10/windowslivewriterunittestsasdocumentation-b7f2image082.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" src="http://jonnekats.files.wordpress.com/2007/10/windowslivewriterunittestsasdocumentation-b7f2image0-thumb42.png?w=545&#038;h=249" border="0" alt="" width="545" height="249" /></a></p>
<p>I then opened the test, which gave me the right code:</p>
<p> </p>
<div class="wlWriterSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:White;">
<div><span style="color:#000000;">        [Test]
        </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ReadFromQuery()
        {
            ISession s </span><span style="color:#000000;">=</span><span style="color:#000000;"> OpenSession();

            IQuery q </span><span style="color:#000000;">=</span><span style="color:#000000;"> s.CreateQuery(</span><span style="color:#000000;">"</span><span style="color:#000000;">from EnumStringClass as esc where esc.EnumValue=:enumValue</span><span style="color:#000000;">"</span><span style="color:#000000;">);
            q.SetParameter(</span><span style="color:#000000;">"</span><span style="color:#000000;">enumValue</span><span style="color:#000000;">"</span><span style="color:#000000;">, SampleEnum.On, </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> SampleEnumType());
            q.SetEnum(</span><span style="color:#000000;">"</span><span style="color:#000000;">enumValue</span><span style="color:#000000;">"</span><span style="color:#000000;">, SampleEnum.On);
            IList results </span><span style="color:#000000;">=</span><span style="color:#000000;"> q.List();

            Assert.AreEqual(</span><span style="color:#000000;">1</span><span style="color:#000000;">, results.Count, </span><span style="color:#000000;">"</span><span style="color:#000000;">only 1 was 'On'</span><span style="color:#000000;">"</span><span style="color:#000000;">);

            q.SetParameter(</span><span style="color:#000000;">"</span><span style="color:#000000;">enumValue</span><span style="color:#000000;">"</span><span style="color:#000000;">, SampleEnum.Off, </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> SampleEnumType());
            results </span><span style="color:#000000;">=</span><span style="color:#000000;"> q.List();

            Assert.AreEqual(</span><span style="color:#000000;">0</span><span style="color:#000000;">, results.Count, </span><span style="color:#000000;">"</span><span style="color:#000000;">should not be any in the 'Off' status</span><span style="color:#000000;">"</span><span style="color:#000000;">);

            s.Close();

            </span><span style="color:#008000;">//</span><span style="color:#008000;"> it will also be possible to query based on a string value
            </span><span style="color:#008000;">//</span><span style="color:#008000;"> since that is what is in the db</span><span style="color:#008000;">
</span><span style="color:#000000;">            s </span><span style="color:#000000;">=</span><span style="color:#000000;"> OpenSession();

            q </span><span style="color:#000000;">=</span><span style="color:#000000;"> s.CreateQuery(</span><span style="color:#000000;">"</span><span style="color:#000000;">from EnumStringClass as esc where esc.EnumValue=:stringValue</span><span style="color:#000000;">"</span><span style="color:#000000;">);
            q.SetString(</span><span style="color:#000000;">"</span><span style="color:#000000;">stringValue</span><span style="color:#000000;">"</span><span style="color:#000000;">, </span><span style="color:#000000;">"</span><span style="color:#000000;">On</span><span style="color:#000000;">"</span><span style="color:#000000;">);
            results </span><span style="color:#000000;">=</span><span style="color:#000000;"> q.List();

            Assert.AreEqual(</span><span style="color:#000000;">1</span><span style="color:#000000;">, results.Count, </span><span style="color:#000000;">"</span><span style="color:#000000;">only 1 was 'On' string</span><span style="color:#000000;">"</span><span style="color:#000000;">);

            q.SetString(</span><span style="color:#000000;">"</span><span style="color:#000000;">stringValue</span><span style="color:#000000;">"</span><span style="color:#000000;">, </span><span style="color:#000000;">"</span><span style="color:#000000;">Off</span><span style="color:#000000;">"</span><span style="color:#000000;">);
            results </span><span style="color:#000000;">=</span><span style="color:#000000;"> q.List();

            Assert.AreEqual(</span><span style="color:#000000;">0</span><span style="color:#000000;">, results.Count, </span><span style="color:#000000;">"</span><span style="color:#000000;">should not be any in the 'Off' string</span><span style="color:#000000;">"</span><span style="color:#000000;">);

            s.Close();
        }</span></div>
</pre>
</div>
<p>As Jeremy also mentions, the NHibernate documentation isn&#8217;t as complete as we would have hoped, the EnumStringType isn&#8217;t documented for example. But as the example above illustrates, these unit tests can be really helpful as technical documentation, especially when it concerns frameworks and libraries.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jonnekats.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jonnekats.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jonnekats.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jonnekats.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jonnekats.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jonnekats.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jonnekats.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jonnekats.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jonnekats.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jonnekats.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jonnekats.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jonnekats.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jonnekats.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jonnekats.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jonnekats.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jonnekats.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jonnekats.wordpress.com&amp;blog=88655&amp;post=12&amp;subd=jonnekats&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jonnekats.wordpress.com/2007/10/27/unit-tests-as-documentation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6e3f1ef2b9717a4f5497b79acda6e424?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jonnekats</media:title>
		</media:content>

		<media:content url="http://jonnekats.files.wordpress.com/2007/10/windowslivewriterunittestsasdocumentation-b7f2image0-thumb42.png" medium="image" />
	</item>
	</channel>
</rss>
