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

<channel>
	<title>Elegant Code &#187; NHibernate</title>
	<atom:link href="http://elegantcode.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NHibernate, HttpModules, ASP.NET JSON Web Services and Database Transactions</title>
		<link>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions</link>
		<comments>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:08:03 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/</guid>
		<description><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play: An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor ) NHibernate The use [...]]]></description>
			<content:encoded><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play:
<ol>
	<li>An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor )</li>
	<li>NHibernate</li>
	<li>The use of an HttpModule to implement the <a href="https://www.hibernate.org/43.html">Open Session in View</a> pattern and provide a Unit of Work implementation (one ISession per HTTP request)</li>
	<li>ASP.NET web services returning JSON (might well be a problem if the result was XML as well)</li>
</ol>
In an “old fashioned” Web Forms scenario everything works perfectly. If some unexpected event occurs during a postback then an Exception is thrown and it bubbles up to the HttpModule which rolls back the transaction.

However with a JSON web service (we’re not using WCF so we just have an System.Web.Services.WebService flagged with a ScriptService attribute) when an unexpected event occurs the magic of the framework catches it and the ScriptMethod simply returns the Exception converted to JSON. This was being correctly handled in our UI but because an Exception is not thrown so the HttpModule thinks that everything is OK and incorrectly commits our transaction. And because processing didn’t necessarily finish and our data could therefore get into an inconsistent state (which is even worse than a plain wrong state).

In order to work around this feature I hooked into the EndRequest event in the HttpModule and check the StatusCode of the Repsonse. Here’s the method that I created:
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">bool</span> IsAjaxError()
{
  <span class="kwrd">return</span> HttpContext.Current.Response.StatusCode == 500 &amp;&amp; HttpContext.Current.Request.RawUrl.Contains(<span class="str">".asmx"</span>);
}</pre>
<!--.csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Obviously if an error is detected I can take the appropriate action. So if you’re using the above combination of technologies be careful because what you think is happening and what is happening might be two entirely different things.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database Change Management with Tarantino</title>
		<link>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=database-change-management-with-tarantino</link>
		<comments>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 10:17:12 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/</guid>
		<description><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard. Why [...]]]></description>
			<content:encoded><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard.
<h2>Why do we need DB Change management</h2>
Okay then Mr. Developer (or Mrs.), Why do we need source control? We need database change management for the same reason we need source control. To help manage the code and dependencies our applications rely on.
<h4>Recipes for Disaster:</h4>
<blockquote>Team of developers all working/developing off of a central database</blockquote>
<blockquote>Team of developers working/developing off of local database, with no way to keep changes in sync.</blockquote>
So what’s the problem with these recipes? As new features are developed for an application it is very likely that the database will need schema changes to support these features. These could be as simple as new tables or adding a column. If we have schema changes, we have to make sure that the code base for the app is deployed concurrently with the schema changes.

When deploying software to production, code files and libraries can usually be overwritten with a new version. Databases however must be updated intelligently.
<h2>So we’ve got a problem, do we have a solution?</h2>
Enter Tarantino. I was talking with Jarod F. and he said he had heard some stuff about it, but had never used it so I started digging around and found enough pieces to get it working. So first thing I did was head over to the <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino project</a> site. Starting reading around and then i did a little googling and managed to dig up enough information to really wet my appetite and to get it working. Then Eric Hexter was kind enough to give me some time via IM and cleared up several areas for me.

Before I get into the specifics of how to set all this up, I want to explain how Tarantino finds and keeps changes in sync.
<ol>
	<li>Tarantino is a forward only change management system, you can’t roll back.</li>
	<li>If you use Tarantino, you can’t go make changes by hand manually</li>
	<li>Tarantino uses RedGate SQL Compare if you want, to compare DB Schemas</li>
	<li>Tarantino makes a table in every DB it touches to keep track of what scripts have been executed on that DB</li>
</ol>
The system is based on a set of conventions which allow incremental changes to the database schema. The conventions are to create two subdirectories in your database scripts folder. Create– this is where your inital schema change scripts go. Update – this is where your change scripts should be placed. They should be named with the following convention ####SCRIPTNAME.sql where #### is the script number with leading zeros. This will ensure the first script 0001_first_schema_change.sql would be executed first. There is a third folder, ExistingSchema, that you will need if you are going to start using Tarantino on an app that is already in development or production.

There are two ways to use Tarantino, command line, and as a NAnt task. I will be showing the NAnt task method, as it has the most examples available on the net.
<h4>Project Setup</h4>
So what I have done, is created a very simple project. A class library with a Fluent NHibernate configuration, a test fixture to create the database schema, and two simple entities which I have listed below:
<div class="csharpcode">
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Order</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> OrderNumber { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> PONumber { get; set; }</pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;OrderItem&gt; OrderItems { get; set;}</pre>
<pre></pre>
<pre class="alt">   <span class="kwrd">public</span> Order()</pre>
<pre>   {</pre>
<pre class="alt">       OrderItems = <span class="kwrd">new</span> List&lt;OrderItem&gt;();</pre>
<pre>   }</pre>
<pre class="alt">}</pre>
<pre></pre>
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> OrderItem</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> ItemName { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Description { get; set; }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

and my NHibernate and Test Fixture setup:
<div class="csharpcode">
<pre class="alt">[TestFixture]</pre>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> DB_Setup_Fixture</pre>
<pre class="alt">{</pre>
<pre>    <span class="kwrd">private</span> Configuration _configuration;</pre>
<pre class="alt"></pre>
<pre>    [TestFixtureSetUp]</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> FixtureSetup()</pre>
<pre>    {</pre>
<pre class="alt"></pre>
<pre>        _configuration = Fluently.Configure()</pre>
<pre class="alt">            .Database(MsSqlConfiguration.MsSql2008</pre>
<pre>                          .ConnectionString(c =&gt;</pre>
<pre class="alt">                                            c.FromConnectionStringWithKey(<span class="str">"testData"</span>))</pre>
<pre>                          .UseReflectionOptimizer()</pre>
<pre class="alt">                          .ShowSql())</pre>
<pre>            .Mappings(m =&gt;</pre>
<pre class="alt">                      m.AutoMappings.Add(AutoMap.AssemblyOf&lt;Order&gt;()</pre>
<pre>                                             .Where(x =&gt; x.Namespace.EndsWith(<span class="str">"Domain"</span>))))</pre>
<pre class="alt">            .BuildConfiguration();</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre></pre>
<pre class="alt">    [Test, Explicit, Category(<span class="str">"DBSetup"</span>)]</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">void</span> Create_DB_Schema()</pre>
<pre class="alt">    {</pre>
<pre>        var exporter = <span class="kwrd">new</span> SchemaExport(_configuration);</pre>
<pre class="alt">        exporter.Create(<span class="kwrd">true</span>, <span class="kwrd">true</span>);</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --> <!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<h5>The one thing to make note of here, is that my test is set to explicit, and I gave it a category of “DBSetup”. This will be important once we start configuring NAnt and Tarantino.</h5>
So now we have a very simple application that we can use to play around with Tarantino. To get Tarantino working, I am using the modified NAnt files that CodeCampServer is using. I just copied over their NAnt folder they have checked into source control. There are two NAnt build files that are required to use Tarantino. Disclaimer: I am a total NAnt noob, this is not my usual build utility, but I figured out enough to make it work.
<h4>Common.build</h4>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database change management --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="rebuildDatabase"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="updateDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Update"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Create"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="dropDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Drop"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="attr">failonerror</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="manageSqlDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="${action}"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="${database.integrated}"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}"</span></pre>
<pre class="alt">      <span class="attr">username</span><span class="kwrd">="${database.username}"</span></pre>
<pre>      <span class="attr">password</span><span class="kwrd">="${database.password}"</span></pre>
<pre class="alt">        <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${action != 'Drop'}"</span><span class="kwrd">&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Current Database Version: ${usdDatabaseVersion}"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt"></pre>
<pre>  <span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">language</span><span class="kwrd">="C#"</span> <span class="attr">prefix</span><span class="kwrd">="migration"</span> <span class="kwrd">&gt;</span></pre>
<pre class="alt">    &lt;references&gt;</pre>
<pre>      &lt;include name=<span class="str">"System.IO.dll"</span> /&gt;</pre>
<pre class="alt">    &lt;/references&gt;</pre>
<pre>    &lt;code&gt;</pre>
<pre class="alt">      &lt;![CDATA[</pre>
<pre>              [Function(<span class="str">"next-migration-number"</span>)]</pre>
<pre class="alt">              <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> NextMigration(<span class="kwrd">string</span> path  ) {</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">string</span>[] files = System.IO.Directory.GetFiles(path);</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">int</span> count=1;</pre>
<pre></pre>
<pre class="alt">                    <span class="kwrd">if</span>(files.Length &gt; 0)</pre>
<pre>                    {</pre>
<pre class="alt">                        <span class="kwrd">string</span> filename = System.IO.Path.GetFileName(files[files.Length-1]);</pre>
<pre></pre>
<pre class="alt">                        <span class="kwrd">try</span></pre>
<pre>                        {</pre>
<pre class="alt">                            count = Convert.ToInt32(filename.Substring(0, 4));</pre>
<pre>                            count++;</pre>
<pre class="alt"></pre>
<pre>                            <span class="kwrd">if</span>(count%2 == 0)</pre>
<pre class="alt">                                count++;</pre>
<pre>                        }</pre>
<pre class="alt">                        <span class="kwrd">catch</span></pre>
<pre>                        {</pre>
<pre class="alt"></pre>
<pre>                        }</pre>
<pre class="alt">                    }</pre>
<pre>                  <span class="kwrd">return</span> <span class="kwrd">string</span>.Format(<span class="str">"{0:0000}"</span>, count);</pre>
<pre class="alt">              }</pre>
<pre>            ]]&gt;</pre>
<pre class="alt">    &lt;/code&gt;</pre>
<pre>  <span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

This file sets up most of the plumbing that hooks up the Tarantino NAnt targets for use in the nant.build file
<h4>NAnt.build</h4>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="rem">&lt;!--EXTERNAL_PROPERTIES: usdDatabaseVersion, CCNetLabel--&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">name</span><span class="kwrd">="TarantinoSample"</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre>      <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Initialization --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">buildfile</span><span class="kwrd">="common.build"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">loadtasks</span> <span class="attr">assembly</span><span class="kwrd">="lib\nant\Tarantino.Nant.Tasks.dll"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- Master Properties --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="company.name"</span> <span class="attr">value</span><span class="kwrd">="Tarantino Test"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="assembly.unittests"</span> <span class="attr">value</span><span class="kwrd">="TarantinoSample.dll"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- Version settings --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="project.config"</span> <span class="attr">value</span><span class="kwrd">="debug"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Folder references --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.solution"</span> <span class="attr">value</span><span class="kwrd">="src"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.build"</span> <span class="attr">value</span><span class="kwrd">="build"</span> <span class="attr">dynamic</span><span class="kwrd">="true"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Compilation settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="nant.settings.currentframework"</span> <span class="attr">value</span><span class="kwrd">="net-3.5"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="file.solution"</span> <span class="attr">value</span><span class="kwrd">="${dir.solution}/${project::get-name()}.sln"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database migration settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.script.directory"</span> <span class="attr">value</span><span class="kwrd">="dbChangeScripts"</span> <span class="kwrd">/&gt;</span></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.server"</span> <span class="attr">value</span><span class="kwrd">="localhost"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.name"</span> <span class="attr">value</span><span class="kwrd">="OrderData"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.integrated"</span> <span class="attr">value</span><span class="kwrd">="true"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.username"</span> <span class="attr">value</span><span class="kwrd">="dbuser"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.password"</span> <span class="attr">value</span><span class="kwrd">="P@ssword1"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Database Change Management Targets --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="reset-database"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">nunit2</span> <span class="attr">failonerror</span><span class="kwrd">="true"</span> <span class="attr">verbose</span><span class="kwrd">="true"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">formatter</span> <span class="attr">type</span><span class="kwrd">="Plain"</span> <span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">test</span> <span class="attr">assemblyname</span><span class="kwrd">="${dir.solution}/bin/${project.config}/${assembly.unittests}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">                <span class="kwrd">&lt;</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre>                    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">name</span><span class="kwrd">="DBSetup"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">                    <span class="kwrd">&lt;</span><span class="html">exclude</span> <span class="attr">name</span><span class="kwrd">="*"</span><span class="kwrd">/&gt;</span></pre>
<pre>                <span class="kwrd">&lt;/</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;/</span><span class="html">test</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">nunit2</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="db-migration"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">delete</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span>  <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="reset-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="create-versioned-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="migrationScriptName"</span> <span class="attr">value</span><span class="kwrd">="${migration::next-migration-number(database.script.directory+'/Update')}_AutoGeneratedMigration.sql"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">exec</span></pre>
<pre>              <span class="attr">program</span><span class="kwrd">="${redgate.exe}"</span></pre>
<pre class="alt">              <span class="attr">commandline</span><span class="kwrd">="/f /v /server1:${database.server} /server2:${database.server} /database1:${database.name} /database2:${database.name}Versioned /scriptfile:${database.script.directory}/Update/_New_Script.sql /exclude:Table:usd_AppliedDatabaseScript"</span></pre>
<pre>              <span class="attr">resultproperty</span><span class="kwrd">="execReturnCode"</span></pre>
<pre class="alt">              <span class="attr">failonerror</span> <span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>          <span class="kwrd">&lt;</span><span class="html">fail</span> <span class="attr">if</span><span class="kwrd">="${execReturnCode != '0' and execReturnCode != '63'}"</span> <span class="attr">message</span><span class="kwrd">="Redgate Compare Failed!"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql') == false}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">=""</span><span class="kwrd">/&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="---------------------"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="No Migration Required"</span><span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql')}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">move</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span> <span class="attr">tofile</span><span class="kwrd">="${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Created Migration File ${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">          <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="create-versioned-database"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">tstamp</span><span class="kwrd">&gt;&lt;/</span><span class="html">tstamp</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">action</span><span class="kwrd">="Drop"</span></pre>
<pre class="alt">            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre>            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre class="alt">            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre>            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre class="alt">            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="Create"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre class="alt">            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre>            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Inside this file there are a few pieces that need to be configured for use in your own app, these are all listed under “Master Properties”
<h4>Change script creation</h4>
So without going into too many details, what I have done is created my initial schema using my Unit Test. From here I could script this out to a text file, but what I did was use SQL Compare, comparing my initial schema to an empty database. This gave me my initial SQL script that creates my database.

Just to verify that my initial schema creation script is good, I will have Tarantino create the Versioned database for me using this command:
<blockquote>lib\nant\nant.exe –buildfile:nant.build create-versioned-database</blockquote>
Tarantino then looks in the ExistingSchema folder, runs any scripts found there, then does the same thing for the Updates folder.

In order to test out the change management features, I made a small change to my domain, adding a decimal of cost to the OrderItem class should do the trick. Rebuild my solution, then back to the command line to run my “Create Database Migration Script.bat” file. This file is just running the NAnt task.

What happens then, is NAnt goes out and runs my DBSetup test that is in my project. Which recreates the “OrderData” database. Tarantino then creates the “Versioned” database, then uses SQL Compare to find the changes between the two, and creates a file in the update folder for me named, <strong>0001_AutoGeneratedMigration.sql</strong> <span style="color: #ff0000;">NOTE: It is important to rename this file before it is committed to source control, or before you apply these changes to a database. Tarantino tracks the scripts that have been run by filename.</span>
<h4>Push to Production or Dev</h4>
So I have mentioned two databases so far in this post, OrderData and OrderDataVersioned. These two databases contain no data, that’s not to say that they couldn’t, if you had scripts that put it in. But that is not what they were designed to do. Our app.config is pointing to OrderData, so when we ask NHibernate to build the schema, it drops all the tables and recreates them, losing all data. OrderDataVersioned, is Tarantinos database that is used to keep changes in sync. In our development environment, every dev has a third database, we would call OrderDataProd for instance. This database could have sample data and other information in it. Let’s say, we don’t want to lose our data in this DB so what we do is let Tarantino handle the first two databases, then we deploy our schema changes to our development database. We use this same technique to push changes to production, after backups of course.

We can do this easily with the following command:
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build updateDatabase –D:database.name=OrderDataProd</span></blockquote>
Now when I tried to run this, my database OrderDataProd did not exist, so I went to SQL manager, created it, and then ran the command again, only to be slammed with another error. Since the database existed, Tarantino did not run the create schema script, only the update, which created another error. The next command will create the database I want, by executing the schema creation script, then applying any updates to it.
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build createDatabase –D:database.name=OrderDataProd</span></blockquote>
Bingo! I now have my 3rd database that I can dump data into and start developing on top of. Just to be sure that we can create another change script, and deploy it to this database, I will modify the domain again, by adding a quantity to the OrderItem object. I will leave this step up to you to try out.
<h4>Source Code</h4>
I know the blog formatting sometimes jacks up code samples a lot so I am making all the source for this post available two ways:
<ul>
	<li><a href="http://elegantcode.googlecode.com/files/TarantinoTest.zip" target="_blank">Zip File Download</a></li>
	<li><a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/RyanKelley/TarantinoTest" target="_blank">Elegant Code SVN Repository</a></li>
</ul>
<h3>Information Sources</h3>
<a href="http://www.lostechies.com/blogs/hex/default.aspx" target="_blank">Eric Hexter</a> – Huge Help

<a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a>

<a href="http://jeffreypalermo.com/blog/tarantino-project-rsquo-s-database-migrations-screencast/" target="_blank">Palermo and Hexter Screencast on Tarantino</a>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why Future&lt;T&gt; should be in your future</title>
		<link>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-futuret-should-be-in-your-future</link>
		<comments>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:50:24 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/</guid>
		<description><![CDATA[Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature [...]]]></description>
			<content:encoded><![CDATA[<p>Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature <a href="http://davybrion.com/blog/2009/01/nhibernate-and-future-queries/">here</a> so I am not going to rehash that. What I am going to focus on here if the why.</p>  <p>Building a UI can be a very database intensive operation in terms of the number of separate calls required. And for each one of those calls the time taken to actually execute the query can be a small compared to the duration of the roundtrip to request and fetch the data. The obvious solution is some type of batching and with Future&lt;T&gt; this happens automagically without any change to the consuming logic (for databases that support it). Let me restate that, you get a huge performance benefit with only a simple change to your query methods in the Repository. So why aren’t you using it?</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate 3.0 QueryOver</title>
		<link>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-3-0-queryover</link>
		<comments>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 19:23:53 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/</guid>
		<description><![CDATA[One of the personal reasons that I had for co-founding Guild 3 was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in Avoiding (Or Recovering From) Burnout. For me the age old adage of “a change is as good as a rest” [...]]]></description>
			<content:encoded><![CDATA[One of the personal reasons that I had for co-founding <a href="http://guild3.com">Guild 3</a> was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in <a href="http://davybrion.com/blog/2009/09/avoiding-or-recovering-from-burnout/">Avoiding (Or Recovering From) Burnout</a>. For me the age old adage of “a change is as good as a rest” has proven to be an extremely successful strategy.

One of the things that I had stopped doing was keeping an eye on various OSS projects to see what was on the horizon. Yesterday I (finally!) started to experiment with NHibernate 3.0. The first thing that caught my eye was <a href="http://fabiomaulo.blogspot.com/2009/06/criteria-on-nh300.html">QueryOver</a>. As Fabio explained there are a <a href="http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html">lot of different ways of executing queries in NH</a>. Certainly in the pre-LINQ days ICriteria was the predominantly recommended option because it has elements of type safety to it and its fluent-ish API broke everything down into small pieces and avoided string concatenation hell.

QueryOver is fluent a layer on top of ICriteria. It looks very LINQesque but it’s a very different animal, not least of all because there are some concepts in ICriteria that do not have a LINQ equivalent (caching etc.). In the short to medium term I suspect that it will become my de facto approach for NHibernate queries (I’ve used NHibernate LINQ and it’s great for simple queries but I’ve experienced significant issues with more complicated ones).

What I haven’t figured out yet is how, if at all, this will affect my data access testing strategy. Historically I’ve favored smoke tests that have really been doing little more than verifying that a given ICriteria query was semantically valid. Typically I only resorted to actually worrying about the results in specific cases (mainly due to the burden of maintaining test data for each possible scenario). Then again this sounds like a topic for another post doesn’t it…]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate 2.1 and Collection Event Listeners</title>
		<link>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-2-1-and-collection-event-listeners</link>
		<comments>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 00:21:37 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/</guid>
		<description><![CDATA[In a previous post, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to read about it first. Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">previous post</a>, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">read about it</a> first.</p> <p>Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't provide this feature or, as in my case, the database in question does support this feature but the DBA's don't want anything to do with it? In case of a parent domain object having a collection of many child objects, you still might want to have a one-shot delete capability instead of having separate DELETE statements for each child record. </p> <p>The <a href="http://nhforge.org/blogs/nhibernate/archive/2009/07/19/welcome-nh2-1-0.aspx">newly released NHibernate 2.1</a> (congratulations to the entire team for their efforts and hard work) comes to the rescue, which introduces a couple of new event listeners that deal with collections.</p> <p>First we need an example. Suppose we are building an auction web site and the domain has a class called <em>Item</em> which in turn has a collection of <em>Bid</em>s. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Item
{
    <span class="kwrd">private</span> ISet&lt;Bid&gt; _bids;    
    <span class="kwrd">public</span> Int64 Id { get; <span class="kwrd">private</span> set; }
    
    ...
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Bid
{
    <span class="kwrd">public</span> Double Amount { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> Sting Code { get; <span class="kwrd">private</span> set; }

    ...
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>The mapping for these classes looks something like this:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Item, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Item"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Int64"</span> <span class="attr">unsaved-value</span><span class="kwrd">="-1"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">sql-type</span><span class="kwrd">="integer"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="native"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    
    ...
    
    <span class="kwrd">&lt;</span><span class="html">set</span> <span class="attr">name</span><span class="kwrd">="Bids"</span> 
         <span class="attr">access</span><span class="kwrd">="field.camelcase-underscore"</span> 
         <span class="attr">lazy</span><span class="kwrd">="false"</span> 
         <span class="attr">cascade</span><span class="kwrd">="all-delete-orphan"</span> 
         <span class="attr">inverse</span><span class="kwrd">="true"</span> 
         <span class="attr">optimistic-lock</span><span class="kwrd">="false"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">key</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">one-to-many</span> <span class="attr">class</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">set</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Bid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">column</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">type</span><span class="kwrd">="Int64"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="Code"</span> 
                      <span class="attr">column</span><span class="kwrd">="Code"</span> 
                      <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
    
    ...
    
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Just to give you a general idea of the situation here. Now suppose we want to delete a quite popular <em>Item</em> object&nbsp; which has a numerous amount of <em>Bid</em>s. Because the collection of <em>Bids</em> is mapped as <em>inverse</em>, NHibernate will remove every record for a <em>Bid</em> with a separate DELETE statement for each row. </p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1001'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1003'</span>
...
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We could solve this by creating a collection event listener. The first thing we have to do is figure out how to issue a one-shot delete instead of those separate DELETE statements. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IOneShotDeleteHandler
{   
    Type ForEntity();
    Type[] ForChildEntities();
    <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> OneShotDeleteHandlerForItem : IOneShotDeleteHandler
{
    <span class="kwrd">public</span> Type ForEntity()
    {
        <span class="kwrd">return</span> <span class="kwrd">typeof</span>(Item);
    }

    <span class="kwrd">public</span> Type[] ForChildEntities()
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span>[] { <span class="kwrd">typeof</span>(Bid) };
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity)
    {
        var item = (Item)entity;

        session.CreateQuery(<span class="str">"delete Bid where ItemId = :itemId"</span>)
            .SetInt64(<span class="str">"itemId"</span>, item.Id)
            .ExecuteUpdate();
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We created an <em>IOneShotDeleteHandler</em> interface with one implementation for the <em>Item</em> class.&nbsp; The most notable aspect of this implementation is the use of the HQL delete statement that removes all <em>Bid</em>s for a particular <em>Item</em>.</p>
<p>Next step is to create a collection event listener that implements the <em>IPreCollectionRemoveEventListener</em> interface.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEventListener
{
    <span class="kwrd">void</span> ConfigureFor(Configuration configuration);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> CollectionRemoveEventListener 
    : IPreCollectionRemoveEventListener,
      IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;
    
    <span class="kwrd">public</span> CollectionRemoveEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> OnPreRemoveCollection(
        PreCollectionRemoveEvent @<span class="kwrd">event</span>)
    {
        var affectedOwner = @<span class="kwrd">event</span>.AffectedOwnerOrNull;
        <span class="kwrd">if</span>(<span class="kwrd">null</span> == affectedOwner)
            <span class="kwrd">return</span>;

        var oneShotDeleteHandler = 
            _oneShotDeleteHandlers.SingleOrDefault(handler =&gt;
            handler.ForEntity() == affectedOwner.GetType());

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler)
            <span class="kwrd">return</span>;

        oneShotDeleteHandler
            .GiveItAShot(@<span class="kwrd">event</span>.Session, affectedOwner);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration
            .SetListener(ListenerType.PreCollectionRemove, <span class="kwrd">this</span>);
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Don't worry about the IEventListener interface. Its just there for registering all NHibernate event listeners in an IoC container.By doing so, it enables us to inject a collection of <em>IOneShotDeleteHandler</em> objects into the constructor of our event listener. When the <em>OnPreRemoveCollection</em> method is called, we simply lookup whether there's a handler available for the type of entity that's going to be deleted and give it a shot at removing its child collection in one sweep. </p>
<p>Now we only have to register this event listener:</p><pre class="csharpcode">var eventListeners = _dependencyContainer
    .ResolveAll&lt;IEventListener&gt;();
eventListeners.ForEach(eventListener =&gt; eventListener
    .ConfigureFor(configuration));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now, if we would use this 'as is', NHibernate will give us the following error:</p>
<blockquote>
<p><em>Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)</em></p></blockquote>
<p>This is due to the fact that the collection event listener does its work and deletes all <em>Bid</em>s by executing the HQL statement, but NHibernate still tries to issue a DELETE statement for each <em>Bid</em>. This is the result of the <em>all-delete-orphan</em> cascading rule we imposed in the mapping. We could reduce it to <em>save-update</em>, but then no individual DELETE statements are executed when a singe <em>Bid</em> is removed from the collection. Now what?</p>
<p>Well, we could provide a regular delete event listener that allows individual DELETE statements for <em>Bid</em> entities as long as their parent <em>Item</em> is not removed.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> DeleteEventListener : 
    DefaultDeleteEventListener,
    IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;

    <span class="kwrd">public</span> DeleteEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> DeleteEntity(
        IEventSource session, <span class="kwrd">object</span> entity, 
        EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, 
        IEntityPersister persister, ISet transientEntities)
    {
        var oneShotDeleteHandler = _oneShotDeleteHandlers
            .SingleOrDefault(handler =&gt;
                handler.ForChildEntities()
                    .Contains(entity.GetType()));

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler ||
           !IsParentAlsoDeletedIn(
                session.PersistenceContext, 
                oneShotDeleteHandler.ForEntity())
            )
        {
            <span class="kwrd">base</span>.DeleteEntity(session, entity, entityEntry, 
                              isCascadeDeleteEnabled, persister, 
                              transientEntities);
            <span class="kwrd">return</span>;
        }

        CascadeBeforeDelete(session, persister, entity, 
                            entityEntry, transientEntities);
        CascadeAfterDelete(session, persister, entity, 
                           transientEntities);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration.SetListener(ListenerType.Delete, <span class="kwrd">this</span>);
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> Boolean IsParentAlsoDeletedIn(
        IPersistenceContext persistenceContext, Type typeOfParent)
    {
        <span class="kwrd">foreach</span>(DictionaryEntry entry <span class="kwrd">in</span> 
                                    persistenceContext.EntityEntries)
        {
            <span class="kwrd">if</span>(typeOfParent != entry.Key.GetType())
                <span class="kwrd">continue</span>;
            
            var entityEntry = (EntityEntry)entry.Value;
            <span class="kwrd">if</span>(Status.Deleted == entityEntry.Status)
                <span class="kwrd">return</span> <span class="kwrd">true</span>;
        }

        <span class="kwrd">return</span> <span class="kwrd">false</span>;
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>With both event listeners registered, deleting a single Bid results in a single DELETE statement as one would expect:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <span class="kwrd">AND</span> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>and&nbsp; removing an entire <em>Item</em> now results in a one-shot delete for all <em>Bid</em>s:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0; @p0 = 2
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Make sure you use this solution for one-shot deletes wisely and only if you have to. If you can use the CASCADE DELETE foreign key constraints, then by all means, this is the preferred option. If not, only resort to this kind of solution only if you must and that you can prove that its going to give you a tremendous performance benefit. Also take a look at the <a href="http://ayende.com/Blog/2006/09/16/BatchingSupportInNHibernate.aspx">batching support</a> that NHibernate provides (at the moment only SQL Server and Oracle are supported). </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using NHibernate for Legacy Databases</title>
		<link>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-nhibernate-for-legacy-databases</link>
		<comments>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:32:11 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/</guid>
		<description><![CDATA[One of the downsides of being confronted with a shared legacy database day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides of being confronted with a <a href="http://elegantcode.com/2009/03/28/about-a-shared-database/">shared legacy database</a> day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required for your application. These extra columns are specifically there to serve those other legacy applications. Heck, to make matters even worse, there are probably some new columns added specifically for your application as well. This is the fairy tale of shared legacy databases. </p> <p>Using NHibernate in these scenarios can be challenging sometimes but its built in flexibility and extensibility really helps you to deal with those cases. The problem I ran into last week was that we needed to store a domain object into a table that had a lot more columns than were actually required for our application. If it would be possible to store null values in these columns or if they had default values configured for them in the schema, then this would not be a problem. Instead, these unnecessary columns could not store null values and had no default values associated with them. </p> <p>First option would be to make some changes to the schema of the table. Alas, no luck there because the other legacy applications that are using the same table would break. Now what?</p> <p>We needed to insert the default values ourselves, but those columns are not known by NHibernate because they are not mapped to any members of the domain object. One way to solve this, is to pollute the domain object by adding private fields that are initialized to the required default values.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainEntity
{
    <span class="rem">// Legacy fields with no purpose for the domain but required </span>
    <span class="rem">// by the database.</span>
    <span class="kwrd">private</span> Int32 _legacyField1 = 2;
    <span class="kwrd">private</span> Boolean _legacyField2 = <span class="kwrd">false</span>;
    <span class="kwrd">private</span> String _legacyField3 = <span class="str">""</span>;
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably the simplest option, but imposes a <a href="http://en.wikipedia.org/wiki/Fixing_Broken_Windows">broken window</a> as infrastructure concerns are bleeding into the domain this way. In other words, this is not a viable solution. Keeping the legacy stuff isolated as much as possible, NHibernate provides some ways to deal with this by providing an extensive extensibility model.</p>
<p>After some snooping around in the source code of NHibernate, the solution we chose for dealing with this issue is by creating a custom access strategy. The built in property access strategies are probably already well known, but its also possible to write your own access strategy by implementing the <em>IPropertyAccessor</em> interface.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainObjectAccessor : IPropertyAccessor
{
    <span class="kwrd">private</span> IEnumerable&lt;IGetter&gt; _defaultValueGetters;
    
    <span class="kwrd">public</span> SomeDomainObjectAccessor()
    {
        _defaultValueGetters = <span class="kwrd">new</span> List&lt;IGetter&gt;()
        {
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Int32&gt;(<span class="str">"LegacyColumn1"</span>, 2) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Boolean&gt;(<span class="str">"LegacyColumn2"</span>, <span class="kwrd">false</span>) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;String&gt;(<span class="str">"LegacyColumn3"</span>, 2) }
        }
    }

    <span class="kwrd">public</span> IGetter GetGetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> _defaultValueGetters        
            .Where(getter =&gt; getter.PropertyName == propertyName)        
            .SingleOrDefault();
    }
    
    <span class="kwrd">public </span>ISetter GetSetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> NoopSetter();
    }

    <span class="kwrd">public</span> Boolean CanAccessTroughReflectionOptimizer
    {
        get { <span class="kwrd">return</span> <span class="kwrd">true</span>; }
    }
}

<span class="kwrd">private</span> <span class="kwrd">class</span> DefaultValueGetter&lt;T&gt; : IGetter
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> String _propertyName;
    <span class="kwrd">private</span> T Value { get; set; }

    <span class="kwrd">public</span> DefaultValueGetter(String propertyName, T <span class="kwrd">value</span>)
    {
        _propertyName = propertyName;
        Value = <span class="kwrd">value</span>;
    }

    <span class="kwrd">public</span> Object Get(Object target)
    {
        <span class="kwrd">return</span> Value;
    }

    <span class="kwrd">public</span> Type ReturnType
    {
        get { <span class="kwrd">return</span> <span class="kwrd">typeof</span>(T); }
    }

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> _propertyName; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get
        {
            var method = <span class="kwrd">typeof</span>(BasicPropertyAccessor)              
                .GetMethod(<span class="str">"GetGetterOrNull"</span>,
                           BindingFlags.Static | BindingFlags.NonPublic);

            var result = (BasicPropertyAccessor.BasicGetter)method              
                .Invoke(<span class="kwrd">null</span>, <span class="kwrd">new</span> Object[] { GetType(), <span class="str">"Value"</span> });

            <span class="kwrd">return</span> result.Method;
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> GetForInsert(Object owner, IDictionary mergeMap,
                               ISessionImplementor session)
    {
        <span class="kwrd">return</span> Get(owner);
    }
}

<span class="kwrd">private</span> <span class="kwrd">sealed</span> <span class="kwrd">class</span> NoopSetter : ISetter
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Set(Object target, Object <span class="kwrd">value</span>)
    {}

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This simply involves a <em>getter</em> for providing default values and a dummy <em>setter</em> as we're not interested in setting any values on the domain objects. The <em>DefaultValueGetter</em> class uses a trick so that we can keep using the reflection optimizer of NHibernate. This also seems to be necessary when using NHibernate Profiler. </p>
<p>Now we only have to provide some properties in the mapping of the domain object using our custom access strategy:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Int32"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Boolean"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="String"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably not the best solution, but it does the job and prevents polluting the domain objects as a result of database quirks like these. I'm interested in hearing feedback or any better approaches. </p>
<p>Anyway, the easy extensibility of NHibernate makes it the best data access solution around. This way, one can deal with all edge case scenarios that weren't anticipated by the framework builders. </p>
<p>Till next time&nbsp;&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Sybase working to improve NHibernate support</title>
		<link>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sybase-working-to-improve-nhibernate-support</link>
		<comments>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/#comments</comments>
		<pubDate>Fri, 15 May 2009 15:19:16 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Sybase]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/</guid>
		<description><![CDATA[If you also subscribe to Chris’ feed here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 [...]]]></description>
			<content:encoded><![CDATA[<p>If you also subscribe to <a href="http://elegantcode.com/author/brandsma/feed/">Chris’ feed</a> here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 – 10 production applications using this combination of technologies.</p>  <p>But support in NHibernate for Sybase is not great. The basics work but some of the more advanced features are not implemented at all or are done poorly. Fortunately we have an excellent relationship with our vendor and have had for many years. Earlier this week they contacted me and mentioned that they will be undertaking an effort to improve their support for NHibernate and were looking for some beta testers.</p>  <p>For this effort they can be commended. It’s also telling (and significant) that OR/M usage is becoming mainstream enough for them to consider this effort worthwhile.</p>  <p><a href="http://iablog.sybase.com/paulley">Glenn</a> is going to be leading the effort (as he did for the Java version last year). If you want to influence the direction that we go then please tell us what needs to be fixed.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FubuMVC From Scratch &#8211; Part 4 Persistence</title>
		<link>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fubumvc-from-scratch-part-4-persistence</link>
		<comments>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 21:22:00 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[FubuMVC]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/</guid>
		<description><![CDATA[Previous posts in series: FubuMVC From Scratch Part 1 – Basic project structure setup FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup FubuMVC From Scratch – Part 3 (Adding View to project) Persistence is a requirement in virtually every application that we write today, and now the time has come for us [...]]]></description>
			<content:encoded><![CDATA[<h3>Previous posts in series:</h3>  <ul>   <li><a href="http://elegantcode.com/2009/01/30/setting-up-a-fubumvc-project-from-scratch/" target="_blank">FubuMVC From Scratch Part 1 – Basic project structure setup</a> </li>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-2/" target="_blank">FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup</a></li>    <ul></ul>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-3-adding-view-to-project/" target="_blank">FubuMVC From Scratch – Part 3 (Adding View to project)</a></li> </ul>  <p>Persistence is a requirement in virtually every application that we write today, and now the time has come for us to add a persistence model to our <a href="http://fubumvc.googlecode.com/" target="_blank">FubuMVC</a> app. I know I mentioned moving my samples to the FubuCart source, but I am going to go ahead and implement the persistence piece in the same project the previous articles using, FubuSample. I have also added all the code for this series to the <a href="http://code.google.com/p/fubumvc-contrib/" target="_blank">FubuMVC-Contrib</a> project</p>  <p>We will be implementing our persistence model to use NHibernate, Fluent NHibernate, Repository Pattern and UnitOfWork pattern all on top of SQL Express or standard. This will also work for sqllite and other database servers.</p>  <p>Class and Interfaces we have to implement: </p>  <ul>   <li>ISessionSourceConfiguration </li>    <li>SQLServerSessionSourceConfiguration </li>    <li>IUnitOfWork </li>    <li>INHibernateUnitOfWork </li>    <li>NHibernateUnitOfWork </li>    <li>IRepository </li>    <li>NHibernateRepository </li>    <li>DomainEntity </li>    <li>IDomainQuery </li>    <li>FubuSamplePersistenceModel </li> </ul>  <p>Note: I put the first two in the list in my Core.Config namespace, and the rest in Core.Persistence</p>  <p>Before we start you probably want to go ahead and add the following references to your core project and Web project</p>  <p><a href="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref.jpg"><img title="FubuMVC-Persist-Ref" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="409" alt="FubuMVC-Persist-Ref" src="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref-thumb.jpg" width="274" border="0" /></a> </p>  <p>Let’s just work our way down the list and get these files implemented. </p>  <h3>ISessionSourceConfiguration</h3>  <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">   <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">     <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">bool</span> IsNewDatabase { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ISessionSource CreateSessionSource(PersistenceModel model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>SQLServerSessionSourceConfiguration</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 131.67%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 263px; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SQLServerSessionSourceConfiguration : ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#region</span> Implementation of ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsNewDatabase</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        get { <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISessionSource CreateSessionSource(PersistenceModel model)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        var properties = GetProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var source = <span style="color: #0000ff">new</span> SessionSource(properties, model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        create_schema_if_it_does_not_already_exist(source);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> create_schema_if_it_does_not_already_exist(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (IsNewDatabase) source.BuildSchema();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">protected</span> IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">string</span>&gt; GetProperties()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        MsSqlConfiguration config = MsSqlConfiguration.MsSql2005;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.ConnectionString.FromConnectionStringWithKey(“MYDBKEY”);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        config.ShowSql();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.UseOuterJoin();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> config.ToProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #cc6633">#endregion</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>A quick note here: We told SQLServerSessionSourceConfiguration to use a connection string from the app settings file (Web.Config) with a key of “MYDBKEY” This will be important later.</p>

<h3>IUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IUnitOfWork : IDisposable</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">void</span> Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>INHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> INHibernateUnitOfWork : IUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ISession CurrentSession { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateUnitOfWork : INHibernateUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> ITransaction _transaction;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isDisposed;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ISessionSource _source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isInitialized;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateUnitOfWork(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _source = source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Initialize()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        CurrentSession = _source.CreateSession();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isInitialized = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISession CurrentSession { get; <span style="color: #0000ff">private</span> set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Commit()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> begin_new_transaction()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_transaction != <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction = CurrentSession.BeginTransaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Rollback()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_not_currently_be_disposed()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ObjectDisposedException(GetType().Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_be_initialized_first()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (!_isInitialized) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> InvalidOperationException(<span style="color: #006080">&quot;Must initialize (call Initialize()) on NHibernateUnitOfWork before commiting or rolling back&quot;</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Dispose()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed || !_isInitialized) <span style="color: #0000ff">return</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        CurrentSession.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isDisposed = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ENTITY Load&lt;ENTITY&gt;(Guid id)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateRepository : IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> INHibernateUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateRepository(INHibernateUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.SaveOrUpdate(entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ENTITY Load&lt;ENTITY&gt;(Guid id) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Load&lt;ENTITY&gt;(id);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;() <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;().Where(whereQuery.Expression);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(entity);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var query = String.Format(<span style="color: #006080">&quot;from {0}&quot;</span>, <span style="color: #0000ff">typeof</span>(ENTITY).Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(query);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>DomainEntity</h3>

<p>Note: I put both of these classes in my Core.Domain namespace, also XML comments are in Source Repository</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DomainEntity : IEquatable&lt;DomainEntity&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> Guid ID { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">bool</span> Equals(DomainEntity other)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> other.ID.Equals(ID);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> Equals(<span style="color: #0000ff">object</span> obj)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (obj.GetType() != GetType()) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals((DomainEntity)obj);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> GetHashCode()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> ID.GetHashCode();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> ==(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> !=(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> !Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IDomainQuery</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IDomainQuery&lt;ENTITY&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    Expression&lt;Func&lt;ENTITY, <span style="color: #0000ff">bool</span>&gt;&gt; Expression { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>FubuSamplePersistenceModel</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Persistence</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FubuSamplePersistenceModel : PersistenceModel</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> FubuSamplePersistenceModel()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            addMappingsFromThisAssembly();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now that we have all the puzzle pieces in place we need to hook them up and make them do some work. Open up FubuSampleWebRegistry </p>

<p>And add the following code:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> configure()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSourceConfiguration&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;SQLServerSessionSourceConfiguration&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSource&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.ConstructedBy(ctx =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                                     ctx.GetInstance&lt;ISessionSourceConfiguration&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                                         .CreateSessionSource(<span style="color: #0000ff">new</span> FubuSamplePersistenceModel()));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;IUnitOfWork&gt;().TheDefault.Is.ConstructedBy(ctx =&gt; ctx.GetInstance&lt;INHibernateUnitOfWork&gt;());</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;INHibernateUnitOfWork&gt;().CacheBy(InstanceScope.Hybrid)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;NHibernateUnitOfWork&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;IRepository&gt;().TheDefault.Is.OfConcreteType&lt;NHibernateRepository&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>With a little luck and all of our mad skills, we should be able to talk to the database now. So we need to plumb our simple product object up to talk to NHibernate. We will do this with a mapping file, and since we are using FluentNHibernate we can do this strongly typed.</p>

<p>Make the following modifications to Product: Extend DomainEntity and get rid of Id Property, it is inherited</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Product : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> virtual <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual string</span> Description { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Add mapping file for Product:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Mapping</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ProductMap : ClassMap&lt;Product&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> ProductMap()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Id(e =&gt; e.ID).GeneratedBy.GuidComb();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Map(p =&gt; p.Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Map(p =&gt; p.Description);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>You may also want to setup your connection string at this point in the Web.Config file, mine looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&lt;add name=<span style="color: #006080">&quot;MYDBKEY&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=LOCALHOST;initial catalog=FubuSample;Integrated Security=SSPI;&quot;</span> providerName=<span style="color: #006080">&quot;System.Data.SqlClient&quot;</span> /&gt;</pre>
  </div>
</div>

<p>I also created an empty database in my local SQL Server instance called “FubuSample” And then I went back to SQLServerSessionSourceConfiguration and changed the value of IsNewDatabase to return true. This will create the schema for the database when the application first runs. There is a multitude of different ways that we can handle this, but this was easy for the tutorial purposes.</p>

<p>Next we will add a behavior to our core project and tell FubuMVC to apply that behavior to all or our ControllerActions.</p>

<p>Create a class named “access_the_database_through_a_unit_of_work” in your Core.Web.Behaviors namespace. The contents will look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> access_the_database_through_a_unit_of_work : IControllerActionBehavior</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> IUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> access_the_database_through_a_unit_of_work(IUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IControllerActionBehavior InsideBehavior { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IInvocationResult Result { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> OUTPUT Invoke&lt;INPUT, OUTPUT&gt;(INPUT input, Func&lt;INPUT, OUTPUT&gt; func)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> INPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> OUTPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork.Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">try</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            var output = InsideBehavior.Invoke(input, func);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Result = InsideBehavior.Result;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">return</span> output;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">catch</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">throw</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">finally</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _unitOfWork.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now to tell FubuMVC what to do, open up Global.asax.cs or your Bootstrapper, wherever you are doing the configuration of FubuMVC. In this project we are doing it in Global.asax.cs Add a new behavior to the section that starts with “x.ByDefault.EveryControllerAction” so it looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">x.ByDefault.EveryControllerAction(d =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">       d.Will&lt;execute_the_result&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">       d.Will&lt;access_the_database_through_a_unit_of_work&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">   });</pre>
  </div>
</div>

<p>The last thing we will do is open up HomeController and add this code above the Index action:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">private</span> IRepository _repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> HomeController(IRepository repository)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository = repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>I also modified the Index action to go ahead and create some database values before we query for them just so we can test this thing. (I only did this for the tutorial, I swear) So modify the Index action to look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> IndexViewModel Index(IndexSetupViewModel inModel)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod1 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct1&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod1);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod2 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct2&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod2);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var outModel = <span style="color: #0000ff">new</span> IndexViewModel();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var productList = _repository.Query&lt;Product&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    outModel.Products = productList.ToList().Select(x =&gt; <span style="color: #0000ff">new</span> ProductDisplay(x));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">return</span> outModel;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now with you should be able to run your web app and browse to /home and all this will be executed and you will be on your way to creating a whole bunch of persist able objects for use with your FubuMVC app.</p>

<p>As always, feedback is welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>What I Really, Really, Really Like About Using Fluent NHibernate</title>
		<link>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-i-really-really-really-like-about-using-fluent-nhibernate</link>
		<comments>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 00:32:10 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/</guid>
		<description><![CDATA[Reusable Mappings Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files: Catalog: &#60;class name="Catalog" table="`Catalog`" optimistic-lock="version"&#62; &#60;id name="Id" column="Id" type="Guid"&#62; &#60;generator class="guid.comb" /&#62; &#60;/id&#62; &#60;version column="Version" name="Version" /&#62; &#60;property name="Name" length="100" type="String"&#62; &#60;column [...]]]></description>
			<content:encoded><![CDATA[<h3>Reusable Mappings</h3>
Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files:
<h4>Catalog:</h4>
<pre class="csharpcode">  <span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="Catalog"</span> <span class="attr">table</span><span class="kwrd">="`Catalog`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>
    ...
  <span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategory:</h4>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="CatalogCategory"</span> <span class="attr">table</span><span class="kwrd">="`CatalogCategory`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>

   ...

<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Suppose we have the luxury to choose how we deal with the database in our project. I do realize that's not always feasible because at some point in our careers, we've all seen what database masochism can do. Anyway, if would be making the shots, I would like to use a surrogate key for every table and I would recommend sequential GUIDs (called <a href="http://www.informit.com/articles/article.aspx?p=25862" target="_blank">COMBs</a>) for that.

The mapping of the classes shown above both have the same configuration for the <em>Id</em> and <em>Version</em> properties. These properties typically live in a <em>DomainEntity</em> base class of some sort because we don't want to repeat that tedious code of putting those in every entity of our domain over and over again. It would be nice if we could somehow do the same for the NHibernate mapping files, which we can't (or at least, not that I know of).

Using <a href="http://fluentnhibernate.org/" target="_blank">Fluent NHibernate</a> we can create an abstract mapping class for our <em>DomainEntity</em> from which we derive all entity mapping classes. The following code would give us the same result as the XML mapping files shown above:
<h4>DomainEntityMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> DomainEntityMapping&lt;TDomainEntity&gt; :
ClassMap&lt;TDomainEntity&gt; <span class="kwrd">where</span> TDomainEntity : DomainEntity
{
    <span class="kwrd">protected</span> DomainEntityMapping()
    {
        Id(entity =&gt; entity.Id, <span class="str">"Id"</span>)
            .GeneratedBy.GuidComb();
        OptimisticLock.Version();
        Version(entity =&gt; entity.Version)
            .TheColumnNameIs(<span class="str">"Version"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogMapping : DomainEntityMapping&lt;Catalog&gt;
{
  <span class="kwrd">public</span> CatalogMapping()
  {
      Map(catalog=&gt; catalog.Name, <span class="str">"Name"</span>);
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategoryMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogCategoryMapping : DomainEntityMapping&lt;CatalogCategory&gt;
{
    <span class="kwrd">public</span> CatalogCategoryMapping()
    {
        Map(category=&gt; category.Description, <span class="str">"Name"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

This way,  I can specify the mapping configuration for <em>Id</em> and <em>Version</em> in a single place which is really nice.
<h3>Mapping Tests</h3>
This is probably my most favorite feature of Fluent NHibernate. Given the fluent mapping configuration for the Category class shown earlier, the following test checks whether this mapping is valid or not:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> When_verifying_the_class_mapping_of_a_catalog
    : NHibernateSpecification
{
  [Test]
  <span class="kwrd">public</span> <span class="kwrd">void</span> Then_a_catalog_object_should_be_persistable()
  {
      <span class="kwrd">new</span> PersistenceSpecification&lt;Catalog&gt;(Session)
          .VerifyTheMappings();
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Running this test results in the following SQL statements being executed to an in-memory SQLite database:

NHibernate: INSERT INTO "Catalog" (Version, Name, Id) VALUES (@p0, @p1, @p2); @p0 = '1', @p1 = '', @p2 = 'c52126cb-f11e-47e4-a481-9bc600134d39'
NHibernate: SELECT catalog0_.Id as Id1_0_, catalog0_.Version as Version1_0_, catalog0_.Name as Name1_0_ FROM "Catalog" catalog0_ WHERE catalog0_.Id=@p0; @p0 = 'c52126cb-f11e-47e4-a481-9bc600134d39'

The following code shows the NHibernateSpecification base class for all my database tests. The code in this class deals with setting up the in-memory SQLite database and building the required schema based on the mappings. The Fluent NHibernate framework gives some really nice support for this as well:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> NHibernateSpecification
    : Specification
{
    <span class="kwrd">protected</span> ISession Session { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Establish_context()
    {
        var config = <span class="kwrd">new</span> SQLiteConfiguration()
            .InMemory()
            .ShowSql()
            .ToProperties();

        var sessionSource = <span class="kwrd">new</span> SessionSource(config,
            <span class="kwrd">new</span> RetailerPersistenceModel());

        Session = sessionSource.CreateSession();
        sessionSource.BuildSchema(Session);

        ProvideInitialData(Session);

        Session.Flush();
        Session.Clear();
   }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Dispose_context()
    {
        <span class="kwrd">if</span>(<span class="kwrd">null</span> != Session)
        {
            Session.Dispose();
            Session = <span class="kwrd">null</span>;
        }
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Fluent NHibernate really lowers the barrier for configuring NHibernate, which is a really big thing in my book. I must admit that I was a bit sceptical at first, but now I noticed that I'm having a hard time going back to the standard XML mapping configuration of NHibernate itself. As Fluent NHibernate is still in an early development stage, some issues can come up. But my personal experience so far is that the <a href="http://groups.google.com/group/fluent-nhibernate?hl=en" target="_blank">user group</a> is very responsive.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Stored Procedure Patterns</title>
	<atom:link href="http://elegantcode.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Elegant Code &#187; NHibernate</title>
	<atom:link href="http://elegantcode.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NHibernate, HttpModules, ASP.NET JSON Web Services and Database Transactions</title>
		<link>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions</link>
		<comments>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:08:03 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/</guid>
		<description><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play: An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor ) NHibernate The use [...]]]></description>
			<content:encoded><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play:
<ol>
	<li>An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor )</li>
	<li>NHibernate</li>
	<li>The use of an HttpModule to implement the <a href="https://www.hibernate.org/43.html">Open Session in View</a> pattern and provide a Unit of Work implementation (one ISession per HTTP request)</li>
	<li>ASP.NET web services returning JSON (might well be a problem if the result was XML as well)</li>
</ol>
In an “old fashioned” Web Forms scenario everything works perfectly. If some unexpected event occurs during a postback then an Exception is thrown and it bubbles up to the HttpModule which rolls back the transaction.

However with a JSON web service (we’re not using WCF so we just have an System.Web.Services.WebService flagged with a ScriptService attribute) when an unexpected event occurs the magic of the framework catches it and the ScriptMethod simply returns the Exception converted to JSON. This was being correctly handled in our UI but because an Exception is not thrown so the HttpModule thinks that everything is OK and incorrectly commits our transaction. And because processing didn’t necessarily finish and our data could therefore get into an inconsistent state (which is even worse than a plain wrong state).

In order to work around this feature I hooked into the EndRequest event in the HttpModule and check the StatusCode of the Repsonse. Here’s the method that I created:
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">bool</span> IsAjaxError()
{
  <span class="kwrd">return</span> HttpContext.Current.Response.StatusCode == 500 &amp;&amp; HttpContext.Current.Request.RawUrl.Contains(<span class="str">".asmx"</span>);
}</pre>
<!--.csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Obviously if an error is detected I can take the appropriate action. So if you’re using the above combination of technologies be careful because what you think is happening and what is happening might be two entirely different things.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database Change Management with Tarantino</title>
		<link>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=database-change-management-with-tarantino</link>
		<comments>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 10:17:12 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/</guid>
		<description><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard. Why [...]]]></description>
			<content:encoded><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard.
<h2>Why do we need DB Change management</h2>
Okay then Mr. Developer (or Mrs.), Why do we need source control? We need database change management for the same reason we need source control. To help manage the code and dependencies our applications rely on.
<h4>Recipes for Disaster:</h4>
<blockquote>Team of developers all working/developing off of a central database</blockquote>
<blockquote>Team of developers working/developing off of local database, with no way to keep changes in sync.</blockquote>
So what’s the problem with these recipes? As new features are developed for an application it is very likely that the database will need schema changes to support these features. These could be as simple as new tables or adding a column. If we have schema changes, we have to make sure that the code base for the app is deployed concurrently with the schema changes.

When deploying software to production, code files and libraries can usually be overwritten with a new version. Databases however must be updated intelligently.
<h2>So we’ve got a problem, do we have a solution?</h2>
Enter Tarantino. I was talking with Jarod F. and he said he had heard some stuff about it, but had never used it so I started digging around and found enough pieces to get it working. So first thing I did was head over to the <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino project</a> site. Starting reading around and then i did a little googling and managed to dig up enough information to really wet my appetite and to get it working. Then Eric Hexter was kind enough to give me some time via IM and cleared up several areas for me.

Before I get into the specifics of how to set all this up, I want to explain how Tarantino finds and keeps changes in sync.
<ol>
	<li>Tarantino is a forward only change management system, you can’t roll back.</li>
	<li>If you use Tarantino, you can’t go make changes by hand manually</li>
	<li>Tarantino uses RedGate SQL Compare if you want, to compare DB Schemas</li>
	<li>Tarantino makes a table in every DB it touches to keep track of what scripts have been executed on that DB</li>
</ol>
The system is based on a set of conventions which allow incremental changes to the database schema. The conventions are to create two subdirectories in your database scripts folder. Create– this is where your inital schema change scripts go. Update – this is where your change scripts should be placed. They should be named with the following convention ####SCRIPTNAME.sql where #### is the script number with leading zeros. This will ensure the first script 0001_first_schema_change.sql would be executed first. There is a third folder, ExistingSchema, that you will need if you are going to start using Tarantino on an app that is already in development or production.

There are two ways to use Tarantino, command line, and as a NAnt task. I will be showing the NAnt task method, as it has the most examples available on the net.
<h4>Project Setup</h4>
So what I have done, is created a very simple project. A class library with a Fluent NHibernate configuration, a test fixture to create the database schema, and two simple entities which I have listed below:
<div class="csharpcode">
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Order</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> OrderNumber { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> PONumber { get; set; }</pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;OrderItem&gt; OrderItems { get; set;}</pre>
<pre></pre>
<pre class="alt">   <span class="kwrd">public</span> Order()</pre>
<pre>   {</pre>
<pre class="alt">       OrderItems = <span class="kwrd">new</span> List&lt;OrderItem&gt;();</pre>
<pre>   }</pre>
<pre class="alt">}</pre>
<pre></pre>
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> OrderItem</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> ItemName { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Description { get; set; }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

and my NHibernate and Test Fixture setup:
<div class="csharpcode">
<pre class="alt">[TestFixture]</pre>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> DB_Setup_Fixture</pre>
<pre class="alt">{</pre>
<pre>    <span class="kwrd">private</span> Configuration _configuration;</pre>
<pre class="alt"></pre>
<pre>    [TestFixtureSetUp]</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> FixtureSetup()</pre>
<pre>    {</pre>
<pre class="alt"></pre>
<pre>        _configuration = Fluently.Configure()</pre>
<pre class="alt">            .Database(MsSqlConfiguration.MsSql2008</pre>
<pre>                          .ConnectionString(c =&gt;</pre>
<pre class="alt">                                            c.FromConnectionStringWithKey(<span class="str">"testData"</span>))</pre>
<pre>                          .UseReflectionOptimizer()</pre>
<pre class="alt">                          .ShowSql())</pre>
<pre>            .Mappings(m =&gt;</pre>
<pre class="alt">                      m.AutoMappings.Add(AutoMap.AssemblyOf&lt;Order&gt;()</pre>
<pre>                                             .Where(x =&gt; x.Namespace.EndsWith(<span class="str">"Domain"</span>))))</pre>
<pre class="alt">            .BuildConfiguration();</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre></pre>
<pre class="alt">    [Test, Explicit, Category(<span class="str">"DBSetup"</span>)]</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">void</span> Create_DB_Schema()</pre>
<pre class="alt">    {</pre>
<pre>        var exporter = <span class="kwrd">new</span> SchemaExport(_configuration);</pre>
<pre class="alt">        exporter.Create(<span class="kwrd">true</span>, <span class="kwrd">true</span>);</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --> <!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<h5>The one thing to make note of here, is that my test is set to explicit, and I gave it a category of “DBSetup”. This will be important once we start configuring NAnt and Tarantino.</h5>
So now we have a very simple application that we can use to play around with Tarantino. To get Tarantino working, I am using the modified NAnt files that CodeCampServer is using. I just copied over their NAnt folder they have checked into source control. There are two NAnt build files that are required to use Tarantino. Disclaimer: I am a total NAnt noob, this is not my usual build utility, but I figured out enough to make it work.
<h4>Common.build</h4>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database change management --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="rebuildDatabase"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="updateDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Update"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Create"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="dropDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Drop"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="attr">failonerror</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="manageSqlDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="${action}"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="${database.integrated}"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}"</span></pre>
<pre class="alt">      <span class="attr">username</span><span class="kwrd">="${database.username}"</span></pre>
<pre>      <span class="attr">password</span><span class="kwrd">="${database.password}"</span></pre>
<pre class="alt">        <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${action != 'Drop'}"</span><span class="kwrd">&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Current Database Version: ${usdDatabaseVersion}"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt"></pre>
<pre>  <span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">language</span><span class="kwrd">="C#"</span> <span class="attr">prefix</span><span class="kwrd">="migration"</span> <span class="kwrd">&gt;</span></pre>
<pre class="alt">    &lt;references&gt;</pre>
<pre>      &lt;include name=<span class="str">"System.IO.dll"</span> /&gt;</pre>
<pre class="alt">    &lt;/references&gt;</pre>
<pre>    &lt;code&gt;</pre>
<pre class="alt">      &lt;![CDATA[</pre>
<pre>              [Function(<span class="str">"next-migration-number"</span>)]</pre>
<pre class="alt">              <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> NextMigration(<span class="kwrd">string</span> path  ) {</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">string</span>[] files = System.IO.Directory.GetFiles(path);</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">int</span> count=1;</pre>
<pre></pre>
<pre class="alt">                    <span class="kwrd">if</span>(files.Length &gt; 0)</pre>
<pre>                    {</pre>
<pre class="alt">                        <span class="kwrd">string</span> filename = System.IO.Path.GetFileName(files[files.Length-1]);</pre>
<pre></pre>
<pre class="alt">                        <span class="kwrd">try</span></pre>
<pre>                        {</pre>
<pre class="alt">                            count = Convert.ToInt32(filename.Substring(0, 4));</pre>
<pre>                            count++;</pre>
<pre class="alt"></pre>
<pre>                            <span class="kwrd">if</span>(count%2 == 0)</pre>
<pre class="alt">                                count++;</pre>
<pre>                        }</pre>
<pre class="alt">                        <span class="kwrd">catch</span></pre>
<pre>                        {</pre>
<pre class="alt"></pre>
<pre>                        }</pre>
<pre class="alt">                    }</pre>
<pre>                  <span class="kwrd">return</span> <span class="kwrd">string</span>.Format(<span class="str">"{0:0000}"</span>, count);</pre>
<pre class="alt">              }</pre>
<pre>            ]]&gt;</pre>
<pre class="alt">    &lt;/code&gt;</pre>
<pre>  <span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

This file sets up most of the plumbing that hooks up the Tarantino NAnt targets for use in the nant.build file
<h4>NAnt.build</h4>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="rem">&lt;!--EXTERNAL_PROPERTIES: usdDatabaseVersion, CCNetLabel--&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">name</span><span class="kwrd">="TarantinoSample"</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre>      <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Initialization --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">buildfile</span><span class="kwrd">="common.build"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">loadtasks</span> <span class="attr">assembly</span><span class="kwrd">="lib\nant\Tarantino.Nant.Tasks.dll"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- Master Properties --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="company.name"</span> <span class="attr">value</span><span class="kwrd">="Tarantino Test"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="assembly.unittests"</span> <span class="attr">value</span><span class="kwrd">="TarantinoSample.dll"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- Version settings --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="project.config"</span> <span class="attr">value</span><span class="kwrd">="debug"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Folder references --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.solution"</span> <span class="attr">value</span><span class="kwrd">="src"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.build"</span> <span class="attr">value</span><span class="kwrd">="build"</span> <span class="attr">dynamic</span><span class="kwrd">="true"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Compilation settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="nant.settings.currentframework"</span> <span class="attr">value</span><span class="kwrd">="net-3.5"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="file.solution"</span> <span class="attr">value</span><span class="kwrd">="${dir.solution}/${project::get-name()}.sln"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database migration settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.script.directory"</span> <span class="attr">value</span><span class="kwrd">="dbChangeScripts"</span> <span class="kwrd">/&gt;</span></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.server"</span> <span class="attr">value</span><span class="kwrd">="localhost"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.name"</span> <span class="attr">value</span><span class="kwrd">="OrderData"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.integrated"</span> <span class="attr">value</span><span class="kwrd">="true"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.username"</span> <span class="attr">value</span><span class="kwrd">="dbuser"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.password"</span> <span class="attr">value</span><span class="kwrd">="P@ssword1"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Database Change Management Targets --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="reset-database"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">nunit2</span> <span class="attr">failonerror</span><span class="kwrd">="true"</span> <span class="attr">verbose</span><span class="kwrd">="true"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">formatter</span> <span class="attr">type</span><span class="kwrd">="Plain"</span> <span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">test</span> <span class="attr">assemblyname</span><span class="kwrd">="${dir.solution}/bin/${project.config}/${assembly.unittests}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">                <span class="kwrd">&lt;</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre>                    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">name</span><span class="kwrd">="DBSetup"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">                    <span class="kwrd">&lt;</span><span class="html">exclude</span> <span class="attr">name</span><span class="kwrd">="*"</span><span class="kwrd">/&gt;</span></pre>
<pre>                <span class="kwrd">&lt;/</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;/</span><span class="html">test</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">nunit2</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="db-migration"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">delete</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span>  <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="reset-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="create-versioned-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="migrationScriptName"</span> <span class="attr">value</span><span class="kwrd">="${migration::next-migration-number(database.script.directory+'/Update')}_AutoGeneratedMigration.sql"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">exec</span></pre>
<pre>              <span class="attr">program</span><span class="kwrd">="${redgate.exe}"</span></pre>
<pre class="alt">              <span class="attr">commandline</span><span class="kwrd">="/f /v /server1:${database.server} /server2:${database.server} /database1:${database.name} /database2:${database.name}Versioned /scriptfile:${database.script.directory}/Update/_New_Script.sql /exclude:Table:usd_AppliedDatabaseScript"</span></pre>
<pre>              <span class="attr">resultproperty</span><span class="kwrd">="execReturnCode"</span></pre>
<pre class="alt">              <span class="attr">failonerror</span> <span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>          <span class="kwrd">&lt;</span><span class="html">fail</span> <span class="attr">if</span><span class="kwrd">="${execReturnCode != '0' and execReturnCode != '63'}"</span> <span class="attr">message</span><span class="kwrd">="Redgate Compare Failed!"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql') == false}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">=""</span><span class="kwrd">/&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="---------------------"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="No Migration Required"</span><span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql')}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">move</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span> <span class="attr">tofile</span><span class="kwrd">="${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Created Migration File ${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">          <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="create-versioned-database"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">tstamp</span><span class="kwrd">&gt;&lt;/</span><span class="html">tstamp</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">action</span><span class="kwrd">="Drop"</span></pre>
<pre class="alt">            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre>            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre class="alt">            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre>            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre class="alt">            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="Create"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre class="alt">            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre>            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Inside this file there are a few pieces that need to be configured for use in your own app, these are all listed under “Master Properties”
<h4>Change script creation</h4>
So without going into too many details, what I have done is created my initial schema using my Unit Test. From here I could script this out to a text file, but what I did was use SQL Compare, comparing my initial schema to an empty database. This gave me my initial SQL script that creates my database.

Just to verify that my initial schema creation script is good, I will have Tarantino create the Versioned database for me using this command:
<blockquote>lib\nant\nant.exe –buildfile:nant.build create-versioned-database</blockquote>
Tarantino then looks in the ExistingSchema folder, runs any scripts found there, then does the same thing for the Updates folder.

In order to test out the change management features, I made a small change to my domain, adding a decimal of cost to the OrderItem class should do the trick. Rebuild my solution, then back to the command line to run my “Create Database Migration Script.bat” file. This file is just running the NAnt task.

What happens then, is NAnt goes out and runs my DBSetup test that is in my project. Which recreates the “OrderData” database. Tarantino then creates the “Versioned” database, then uses SQL Compare to find the changes between the two, and creates a file in the update folder for me named, <strong>0001_AutoGeneratedMigration.sql</strong> <span style="color: #ff0000;">NOTE: It is important to rename this file before it is committed to source control, or before you apply these changes to a database. Tarantino tracks the scripts that have been run by filename.</span>
<h4>Push to Production or Dev</h4>
So I have mentioned two databases so far in this post, OrderData and OrderDataVersioned. These two databases contain no data, that’s not to say that they couldn’t, if you had scripts that put it in. But that is not what they were designed to do. Our app.config is pointing to OrderData, so when we ask NHibernate to build the schema, it drops all the tables and recreates them, losing all data. OrderDataVersioned, is Tarantinos database that is used to keep changes in sync. In our development environment, every dev has a third database, we would call OrderDataProd for instance. This database could have sample data and other information in it. Let’s say, we don’t want to lose our data in this DB so what we do is let Tarantino handle the first two databases, then we deploy our schema changes to our development database. We use this same technique to push changes to production, after backups of course.

We can do this easily with the following command:
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build updateDatabase –D:database.name=OrderDataProd</span></blockquote>
Now when I tried to run this, my database OrderDataProd did not exist, so I went to SQL manager, created it, and then ran the command again, only to be slammed with another error. Since the database existed, Tarantino did not run the create schema script, only the update, which created another error. The next command will create the database I want, by executing the schema creation script, then applying any updates to it.
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build createDatabase –D:database.name=OrderDataProd</span></blockquote>
Bingo! I now have my 3rd database that I can dump data into and start developing on top of. Just to be sure that we can create another change script, and deploy it to this database, I will modify the domain again, by adding a quantity to the OrderItem object. I will leave this step up to you to try out.
<h4>Source Code</h4>
I know the blog formatting sometimes jacks up code samples a lot so I am making all the source for this post available two ways:
<ul>
	<li><a href="http://elegantcode.googlecode.com/files/TarantinoTest.zip" target="_blank">Zip File Download</a></li>
	<li><a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/RyanKelley/TarantinoTest" target="_blank">Elegant Code SVN Repository</a></li>
</ul>
<h3>Information Sources</h3>
<a href="http://www.lostechies.com/blogs/hex/default.aspx" target="_blank">Eric Hexter</a> – Huge Help

<a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a>

<a href="http://jeffreypalermo.com/blog/tarantino-project-rsquo-s-database-migrations-screencast/" target="_blank">Palermo and Hexter Screencast on Tarantino</a>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why Future&lt;T&gt; should be in your future</title>
		<link>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-futuret-should-be-in-your-future</link>
		<comments>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:50:24 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/</guid>
		<description><![CDATA[Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature [...]]]></description>
			<content:encoded><![CDATA[<p>Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature <a href="http://davybrion.com/blog/2009/01/nhibernate-and-future-queries/">here</a> so I am not going to rehash that. What I am going to focus on here if the why.</p>  <p>Building a UI can be a very database intensive operation in terms of the number of separate calls required. And for each one of those calls the time taken to actually execute the query can be a small compared to the duration of the roundtrip to request and fetch the data. The obvious solution is some type of batching and with Future&lt;T&gt; this happens automagically without any change to the consuming logic (for databases that support it). Let me restate that, you get a huge performance benefit with only a simple change to your query methods in the Repository. So why aren’t you using it?</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate 3.0 QueryOver</title>
		<link>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-3-0-queryover</link>
		<comments>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 19:23:53 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/</guid>
		<description><![CDATA[One of the personal reasons that I had for co-founding Guild 3 was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in Avoiding (Or Recovering From) Burnout. For me the age old adage of “a change is as good as a rest” [...]]]></description>
			<content:encoded><![CDATA[One of the personal reasons that I had for co-founding <a href="http://guild3.com">Guild 3</a> was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in <a href="http://davybrion.com/blog/2009/09/avoiding-or-recovering-from-burnout/">Avoiding (Or Recovering From) Burnout</a>. For me the age old adage of “a change is as good as a rest” has proven to be an extremely successful strategy.

One of the things that I had stopped doing was keeping an eye on various OSS projects to see what was on the horizon. Yesterday I (finally!) started to experiment with NHibernate 3.0. The first thing that caught my eye was <a href="http://fabiomaulo.blogspot.com/2009/06/criteria-on-nh300.html">QueryOver</a>. As Fabio explained there are a <a href="http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html">lot of different ways of executing queries in NH</a>. Certainly in the pre-LINQ days ICriteria was the predominantly recommended option because it has elements of type safety to it and its fluent-ish API broke everything down into small pieces and avoided string concatenation hell.

QueryOver is fluent a layer on top of ICriteria. It looks very LINQesque but it’s a very different animal, not least of all because there are some concepts in ICriteria that do not have a LINQ equivalent (caching etc.). In the short to medium term I suspect that it will become my de facto approach for NHibernate queries (I’ve used NHibernate LINQ and it’s great for simple queries but I’ve experienced significant issues with more complicated ones).

What I haven’t figured out yet is how, if at all, this will affect my data access testing strategy. Historically I’ve favored smoke tests that have really been doing little more than verifying that a given ICriteria query was semantically valid. Typically I only resorted to actually worrying about the results in specific cases (mainly due to the burden of maintaining test data for each possible scenario). Then again this sounds like a topic for another post doesn’t it…]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate 2.1 and Collection Event Listeners</title>
		<link>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-2-1-and-collection-event-listeners</link>
		<comments>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 00:21:37 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/</guid>
		<description><![CDATA[In a previous post, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to read about it first. Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">previous post</a>, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">read about it</a> first.</p> <p>Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't provide this feature or, as in my case, the database in question does support this feature but the DBA's don't want anything to do with it? In case of a parent domain object having a collection of many child objects, you still might want to have a one-shot delete capability instead of having separate DELETE statements for each child record. </p> <p>The <a href="http://nhforge.org/blogs/nhibernate/archive/2009/07/19/welcome-nh2-1-0.aspx">newly released NHibernate 2.1</a> (congratulations to the entire team for their efforts and hard work) comes to the rescue, which introduces a couple of new event listeners that deal with collections.</p> <p>First we need an example. Suppose we are building an auction web site and the domain has a class called <em>Item</em> which in turn has a collection of <em>Bid</em>s. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Item
{
    <span class="kwrd">private</span> ISet&lt;Bid&gt; _bids;    
    <span class="kwrd">public</span> Int64 Id { get; <span class="kwrd">private</span> set; }
    
    ...
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Bid
{
    <span class="kwrd">public</span> Double Amount { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> Sting Code { get; <span class="kwrd">private</span> set; }

    ...
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>The mapping for these classes looks something like this:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Item, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Item"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Int64"</span> <span class="attr">unsaved-value</span><span class="kwrd">="-1"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">sql-type</span><span class="kwrd">="integer"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="native"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    
    ...
    
    <span class="kwrd">&lt;</span><span class="html">set</span> <span class="attr">name</span><span class="kwrd">="Bids"</span> 
         <span class="attr">access</span><span class="kwrd">="field.camelcase-underscore"</span> 
         <span class="attr">lazy</span><span class="kwrd">="false"</span> 
         <span class="attr">cascade</span><span class="kwrd">="all-delete-orphan"</span> 
         <span class="attr">inverse</span><span class="kwrd">="true"</span> 
         <span class="attr">optimistic-lock</span><span class="kwrd">="false"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">key</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">one-to-many</span> <span class="attr">class</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">set</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Bid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">column</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">type</span><span class="kwrd">="Int64"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="Code"</span> 
                      <span class="attr">column</span><span class="kwrd">="Code"</span> 
                      <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
    
    ...
    
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Just to give you a general idea of the situation here. Now suppose we want to delete a quite popular <em>Item</em> object&nbsp; which has a numerous amount of <em>Bid</em>s. Because the collection of <em>Bids</em> is mapped as <em>inverse</em>, NHibernate will remove every record for a <em>Bid</em> with a separate DELETE statement for each row. </p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1001'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1003'</span>
...
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We could solve this by creating a collection event listener. The first thing we have to do is figure out how to issue a one-shot delete instead of those separate DELETE statements. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IOneShotDeleteHandler
{   
    Type ForEntity();
    Type[] ForChildEntities();
    <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> OneShotDeleteHandlerForItem : IOneShotDeleteHandler
{
    <span class="kwrd">public</span> Type ForEntity()
    {
        <span class="kwrd">return</span> <span class="kwrd">typeof</span>(Item);
    }

    <span class="kwrd">public</span> Type[] ForChildEntities()
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span>[] { <span class="kwrd">typeof</span>(Bid) };
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity)
    {
        var item = (Item)entity;

        session.CreateQuery(<span class="str">"delete Bid where ItemId = :itemId"</span>)
            .SetInt64(<span class="str">"itemId"</span>, item.Id)
            .ExecuteUpdate();
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We created an <em>IOneShotDeleteHandler</em> interface with one implementation for the <em>Item</em> class.&nbsp; The most notable aspect of this implementation is the use of the HQL delete statement that removes all <em>Bid</em>s for a particular <em>Item</em>.</p>
<p>Next step is to create a collection event listener that implements the <em>IPreCollectionRemoveEventListener</em> interface.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEventListener
{
    <span class="kwrd">void</span> ConfigureFor(Configuration configuration);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> CollectionRemoveEventListener 
    : IPreCollectionRemoveEventListener,
      IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;
    
    <span class="kwrd">public</span> CollectionRemoveEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> OnPreRemoveCollection(
        PreCollectionRemoveEvent @<span class="kwrd">event</span>)
    {
        var affectedOwner = @<span class="kwrd">event</span>.AffectedOwnerOrNull;
        <span class="kwrd">if</span>(<span class="kwrd">null</span> == affectedOwner)
            <span class="kwrd">return</span>;

        var oneShotDeleteHandler = 
            _oneShotDeleteHandlers.SingleOrDefault(handler =&gt;
            handler.ForEntity() == affectedOwner.GetType());

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler)
            <span class="kwrd">return</span>;

        oneShotDeleteHandler
            .GiveItAShot(@<span class="kwrd">event</span>.Session, affectedOwner);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration
            .SetListener(ListenerType.PreCollectionRemove, <span class="kwrd">this</span>);
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Don't worry about the IEventListener interface. Its just there for registering all NHibernate event listeners in an IoC container.By doing so, it enables us to inject a collection of <em>IOneShotDeleteHandler</em> objects into the constructor of our event listener. When the <em>OnPreRemoveCollection</em> method is called, we simply lookup whether there's a handler available for the type of entity that's going to be deleted and give it a shot at removing its child collection in one sweep. </p>
<p>Now we only have to register this event listener:</p><pre class="csharpcode">var eventListeners = _dependencyContainer
    .ResolveAll&lt;IEventListener&gt;();
eventListeners.ForEach(eventListener =&gt; eventListener
    .ConfigureFor(configuration));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now, if we would use this 'as is', NHibernate will give us the following error:</p>
<blockquote>
<p><em>Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)</em></p></blockquote>
<p>This is due to the fact that the collection event listener does its work and deletes all <em>Bid</em>s by executing the HQL statement, but NHibernate still tries to issue a DELETE statement for each <em>Bid</em>. This is the result of the <em>all-delete-orphan</em> cascading rule we imposed in the mapping. We could reduce it to <em>save-update</em>, but then no individual DELETE statements are executed when a singe <em>Bid</em> is removed from the collection. Now what?</p>
<p>Well, we could provide a regular delete event listener that allows individual DELETE statements for <em>Bid</em> entities as long as their parent <em>Item</em> is not removed.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> DeleteEventListener : 
    DefaultDeleteEventListener,
    IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;

    <span class="kwrd">public</span> DeleteEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> DeleteEntity(
        IEventSource session, <span class="kwrd">object</span> entity, 
        EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, 
        IEntityPersister persister, ISet transientEntities)
    {
        var oneShotDeleteHandler = _oneShotDeleteHandlers
            .SingleOrDefault(handler =&gt;
                handler.ForChildEntities()
                    .Contains(entity.GetType()));

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler ||
           !IsParentAlsoDeletedIn(
                session.PersistenceContext, 
                oneShotDeleteHandler.ForEntity())
            )
        {
            <span class="kwrd">base</span>.DeleteEntity(session, entity, entityEntry, 
                              isCascadeDeleteEnabled, persister, 
                              transientEntities);
            <span class="kwrd">return</span>;
        }

        CascadeBeforeDelete(session, persister, entity, 
                            entityEntry, transientEntities);
        CascadeAfterDelete(session, persister, entity, 
                           transientEntities);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration.SetListener(ListenerType.Delete, <span class="kwrd">this</span>);
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> Boolean IsParentAlsoDeletedIn(
        IPersistenceContext persistenceContext, Type typeOfParent)
    {
        <span class="kwrd">foreach</span>(DictionaryEntry entry <span class="kwrd">in</span> 
                                    persistenceContext.EntityEntries)
        {
            <span class="kwrd">if</span>(typeOfParent != entry.Key.GetType())
                <span class="kwrd">continue</span>;
            
            var entityEntry = (EntityEntry)entry.Value;
            <span class="kwrd">if</span>(Status.Deleted == entityEntry.Status)
                <span class="kwrd">return</span> <span class="kwrd">true</span>;
        }

        <span class="kwrd">return</span> <span class="kwrd">false</span>;
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>With both event listeners registered, deleting a single Bid results in a single DELETE statement as one would expect:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <span class="kwrd">AND</span> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>and&nbsp; removing an entire <em>Item</em> now results in a one-shot delete for all <em>Bid</em>s:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0; @p0 = 2
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Make sure you use this solution for one-shot deletes wisely and only if you have to. If you can use the CASCADE DELETE foreign key constraints, then by all means, this is the preferred option. If not, only resort to this kind of solution only if you must and that you can prove that its going to give you a tremendous performance benefit. Also take a look at the <a href="http://ayende.com/Blog/2006/09/16/BatchingSupportInNHibernate.aspx">batching support</a> that NHibernate provides (at the moment only SQL Server and Oracle are supported). </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using NHibernate for Legacy Databases</title>
		<link>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-nhibernate-for-legacy-databases</link>
		<comments>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:32:11 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/</guid>
		<description><![CDATA[One of the downsides of being confronted with a shared legacy database day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides of being confronted with a <a href="http://elegantcode.com/2009/03/28/about-a-shared-database/">shared legacy database</a> day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required for your application. These extra columns are specifically there to serve those other legacy applications. Heck, to make matters even worse, there are probably some new columns added specifically for your application as well. This is the fairy tale of shared legacy databases. </p> <p>Using NHibernate in these scenarios can be challenging sometimes but its built in flexibility and extensibility really helps you to deal with those cases. The problem I ran into last week was that we needed to store a domain object into a table that had a lot more columns than were actually required for our application. If it would be possible to store null values in these columns or if they had default values configured for them in the schema, then this would not be a problem. Instead, these unnecessary columns could not store null values and had no default values associated with them. </p> <p>First option would be to make some changes to the schema of the table. Alas, no luck there because the other legacy applications that are using the same table would break. Now what?</p> <p>We needed to insert the default values ourselves, but those columns are not known by NHibernate because they are not mapped to any members of the domain object. One way to solve this, is to pollute the domain object by adding private fields that are initialized to the required default values.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainEntity
{
    <span class="rem">// Legacy fields with no purpose for the domain but required </span>
    <span class="rem">// by the database.</span>
    <span class="kwrd">private</span> Int32 _legacyField1 = 2;
    <span class="kwrd">private</span> Boolean _legacyField2 = <span class="kwrd">false</span>;
    <span class="kwrd">private</span> String _legacyField3 = <span class="str">""</span>;
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably the simplest option, but imposes a <a href="http://en.wikipedia.org/wiki/Fixing_Broken_Windows">broken window</a> as infrastructure concerns are bleeding into the domain this way. In other words, this is not a viable solution. Keeping the legacy stuff isolated as much as possible, NHibernate provides some ways to deal with this by providing an extensive extensibility model.</p>
<p>After some snooping around in the source code of NHibernate, the solution we chose for dealing with this issue is by creating a custom access strategy. The built in property access strategies are probably already well known, but its also possible to write your own access strategy by implementing the <em>IPropertyAccessor</em> interface.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainObjectAccessor : IPropertyAccessor
{
    <span class="kwrd">private</span> IEnumerable&lt;IGetter&gt; _defaultValueGetters;
    
    <span class="kwrd">public</span> SomeDomainObjectAccessor()
    {
        _defaultValueGetters = <span class="kwrd">new</span> List&lt;IGetter&gt;()
        {
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Int32&gt;(<span class="str">"LegacyColumn1"</span>, 2) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Boolean&gt;(<span class="str">"LegacyColumn2"</span>, <span class="kwrd">false</span>) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;String&gt;(<span class="str">"LegacyColumn3"</span>, 2) }
        }
    }

    <span class="kwrd">public</span> IGetter GetGetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> _defaultValueGetters        
            .Where(getter =&gt; getter.PropertyName == propertyName)        
            .SingleOrDefault();
    }
    
    <span class="kwrd">public </span>ISetter GetSetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> NoopSetter();
    }

    <span class="kwrd">public</span> Boolean CanAccessTroughReflectionOptimizer
    {
        get { <span class="kwrd">return</span> <span class="kwrd">true</span>; }
    }
}

<span class="kwrd">private</span> <span class="kwrd">class</span> DefaultValueGetter&lt;T&gt; : IGetter
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> String _propertyName;
    <span class="kwrd">private</span> T Value { get; set; }

    <span class="kwrd">public</span> DefaultValueGetter(String propertyName, T <span class="kwrd">value</span>)
    {
        _propertyName = propertyName;
        Value = <span class="kwrd">value</span>;
    }

    <span class="kwrd">public</span> Object Get(Object target)
    {
        <span class="kwrd">return</span> Value;
    }

    <span class="kwrd">public</span> Type ReturnType
    {
        get { <span class="kwrd">return</span> <span class="kwrd">typeof</span>(T); }
    }

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> _propertyName; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get
        {
            var method = <span class="kwrd">typeof</span>(BasicPropertyAccessor)              
                .GetMethod(<span class="str">"GetGetterOrNull"</span>,
                           BindingFlags.Static | BindingFlags.NonPublic);

            var result = (BasicPropertyAccessor.BasicGetter)method              
                .Invoke(<span class="kwrd">null</span>, <span class="kwrd">new</span> Object[] { GetType(), <span class="str">"Value"</span> });

            <span class="kwrd">return</span> result.Method;
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> GetForInsert(Object owner, IDictionary mergeMap,
                               ISessionImplementor session)
    {
        <span class="kwrd">return</span> Get(owner);
    }
}

<span class="kwrd">private</span> <span class="kwrd">sealed</span> <span class="kwrd">class</span> NoopSetter : ISetter
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Set(Object target, Object <span class="kwrd">value</span>)
    {}

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This simply involves a <em>getter</em> for providing default values and a dummy <em>setter</em> as we're not interested in setting any values on the domain objects. The <em>DefaultValueGetter</em> class uses a trick so that we can keep using the reflection optimizer of NHibernate. This also seems to be necessary when using NHibernate Profiler. </p>
<p>Now we only have to provide some properties in the mapping of the domain object using our custom access strategy:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Int32"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Boolean"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="String"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably not the best solution, but it does the job and prevents polluting the domain objects as a result of database quirks like these. I'm interested in hearing feedback or any better approaches. </p>
<p>Anyway, the easy extensibility of NHibernate makes it the best data access solution around. This way, one can deal with all edge case scenarios that weren't anticipated by the framework builders. </p>
<p>Till next time&nbsp;&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Sybase working to improve NHibernate support</title>
		<link>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sybase-working-to-improve-nhibernate-support</link>
		<comments>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/#comments</comments>
		<pubDate>Fri, 15 May 2009 15:19:16 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Sybase]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/</guid>
		<description><![CDATA[If you also subscribe to Chris’ feed here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 [...]]]></description>
			<content:encoded><![CDATA[<p>If you also subscribe to <a href="http://elegantcode.com/author/brandsma/feed/">Chris’ feed</a> here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 – 10 production applications using this combination of technologies.</p>  <p>But support in NHibernate for Sybase is not great. The basics work but some of the more advanced features are not implemented at all or are done poorly. Fortunately we have an excellent relationship with our vendor and have had for many years. Earlier this week they contacted me and mentioned that they will be undertaking an effort to improve their support for NHibernate and were looking for some beta testers.</p>  <p>For this effort they can be commended. It’s also telling (and significant) that OR/M usage is becoming mainstream enough for them to consider this effort worthwhile.</p>  <p><a href="http://iablog.sybase.com/paulley">Glenn</a> is going to be leading the effort (as he did for the Java version last year). If you want to influence the direction that we go then please tell us what needs to be fixed.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FubuMVC From Scratch &#8211; Part 4 Persistence</title>
		<link>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fubumvc-from-scratch-part-4-persistence</link>
		<comments>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 21:22:00 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[FubuMVC]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/</guid>
		<description><![CDATA[Previous posts in series: FubuMVC From Scratch Part 1 – Basic project structure setup FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup FubuMVC From Scratch – Part 3 (Adding View to project) Persistence is a requirement in virtually every application that we write today, and now the time has come for us [...]]]></description>
			<content:encoded><![CDATA[<h3>Previous posts in series:</h3>  <ul>   <li><a href="http://elegantcode.com/2009/01/30/setting-up-a-fubumvc-project-from-scratch/" target="_blank">FubuMVC From Scratch Part 1 – Basic project structure setup</a> </li>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-2/" target="_blank">FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup</a></li>    <ul></ul>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-3-adding-view-to-project/" target="_blank">FubuMVC From Scratch – Part 3 (Adding View to project)</a></li> </ul>  <p>Persistence is a requirement in virtually every application that we write today, and now the time has come for us to add a persistence model to our <a href="http://fubumvc.googlecode.com/" target="_blank">FubuMVC</a> app. I know I mentioned moving my samples to the FubuCart source, but I am going to go ahead and implement the persistence piece in the same project the previous articles using, FubuSample. I have also added all the code for this series to the <a href="http://code.google.com/p/fubumvc-contrib/" target="_blank">FubuMVC-Contrib</a> project</p>  <p>We will be implementing our persistence model to use NHibernate, Fluent NHibernate, Repository Pattern and UnitOfWork pattern all on top of SQL Express or standard. This will also work for sqllite and other database servers.</p>  <p>Class and Interfaces we have to implement: </p>  <ul>   <li>ISessionSourceConfiguration </li>    <li>SQLServerSessionSourceConfiguration </li>    <li>IUnitOfWork </li>    <li>INHibernateUnitOfWork </li>    <li>NHibernateUnitOfWork </li>    <li>IRepository </li>    <li>NHibernateRepository </li>    <li>DomainEntity </li>    <li>IDomainQuery </li>    <li>FubuSamplePersistenceModel </li> </ul>  <p>Note: I put the first two in the list in my Core.Config namespace, and the rest in Core.Persistence</p>  <p>Before we start you probably want to go ahead and add the following references to your core project and Web project</p>  <p><a href="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref.jpg"><img title="FubuMVC-Persist-Ref" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="409" alt="FubuMVC-Persist-Ref" src="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref-thumb.jpg" width="274" border="0" /></a> </p>  <p>Let’s just work our way down the list and get these files implemented. </p>  <h3>ISessionSourceConfiguration</h3>  <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">   <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">     <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">bool</span> IsNewDatabase { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ISessionSource CreateSessionSource(PersistenceModel model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>SQLServerSessionSourceConfiguration</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 131.67%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 263px; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SQLServerSessionSourceConfiguration : ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#region</span> Implementation of ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsNewDatabase</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        get { <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISessionSource CreateSessionSource(PersistenceModel model)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        var properties = GetProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var source = <span style="color: #0000ff">new</span> SessionSource(properties, model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        create_schema_if_it_does_not_already_exist(source);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> create_schema_if_it_does_not_already_exist(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (IsNewDatabase) source.BuildSchema();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">protected</span> IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">string</span>&gt; GetProperties()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        MsSqlConfiguration config = MsSqlConfiguration.MsSql2005;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.ConnectionString.FromConnectionStringWithKey(“MYDBKEY”);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        config.ShowSql();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.UseOuterJoin();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> config.ToProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #cc6633">#endregion</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>A quick note here: We told SQLServerSessionSourceConfiguration to use a connection string from the app settings file (Web.Config) with a key of “MYDBKEY” This will be important later.</p>

<h3>IUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IUnitOfWork : IDisposable</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">void</span> Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>INHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> INHibernateUnitOfWork : IUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ISession CurrentSession { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateUnitOfWork : INHibernateUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> ITransaction _transaction;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isDisposed;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ISessionSource _source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isInitialized;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateUnitOfWork(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _source = source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Initialize()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        CurrentSession = _source.CreateSession();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isInitialized = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISession CurrentSession { get; <span style="color: #0000ff">private</span> set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Commit()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> begin_new_transaction()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_transaction != <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction = CurrentSession.BeginTransaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Rollback()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_not_currently_be_disposed()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ObjectDisposedException(GetType().Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_be_initialized_first()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (!_isInitialized) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> InvalidOperationException(<span style="color: #006080">&quot;Must initialize (call Initialize()) on NHibernateUnitOfWork before commiting or rolling back&quot;</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Dispose()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed || !_isInitialized) <span style="color: #0000ff">return</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        CurrentSession.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isDisposed = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ENTITY Load&lt;ENTITY&gt;(Guid id)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateRepository : IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> INHibernateUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateRepository(INHibernateUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.SaveOrUpdate(entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ENTITY Load&lt;ENTITY&gt;(Guid id) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Load&lt;ENTITY&gt;(id);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;() <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;().Where(whereQuery.Expression);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(entity);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var query = String.Format(<span style="color: #006080">&quot;from {0}&quot;</span>, <span style="color: #0000ff">typeof</span>(ENTITY).Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(query);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>DomainEntity</h3>

<p>Note: I put both of these classes in my Core.Domain namespace, also XML comments are in Source Repository</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DomainEntity : IEquatable&lt;DomainEntity&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> Guid ID { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">bool</span> Equals(DomainEntity other)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> other.ID.Equals(ID);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> Equals(<span style="color: #0000ff">object</span> obj)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (obj.GetType() != GetType()) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals((DomainEntity)obj);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> GetHashCode()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> ID.GetHashCode();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> ==(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> !=(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> !Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IDomainQuery</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IDomainQuery&lt;ENTITY&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    Expression&lt;Func&lt;ENTITY, <span style="color: #0000ff">bool</span>&gt;&gt; Expression { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>FubuSamplePersistenceModel</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Persistence</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FubuSamplePersistenceModel : PersistenceModel</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> FubuSamplePersistenceModel()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            addMappingsFromThisAssembly();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now that we have all the puzzle pieces in place we need to hook them up and make them do some work. Open up FubuSampleWebRegistry </p>

<p>And add the following code:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> configure()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSourceConfiguration&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;SQLServerSessionSourceConfiguration&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSource&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.ConstructedBy(ctx =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                                     ctx.GetInstance&lt;ISessionSourceConfiguration&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                                         .CreateSessionSource(<span style="color: #0000ff">new</span> FubuSamplePersistenceModel()));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;IUnitOfWork&gt;().TheDefault.Is.ConstructedBy(ctx =&gt; ctx.GetInstance&lt;INHibernateUnitOfWork&gt;());</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;INHibernateUnitOfWork&gt;().CacheBy(InstanceScope.Hybrid)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;NHibernateUnitOfWork&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;IRepository&gt;().TheDefault.Is.OfConcreteType&lt;NHibernateRepository&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>With a little luck and all of our mad skills, we should be able to talk to the database now. So we need to plumb our simple product object up to talk to NHibernate. We will do this with a mapping file, and since we are using FluentNHibernate we can do this strongly typed.</p>

<p>Make the following modifications to Product: Extend DomainEntity and get rid of Id Property, it is inherited</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Product : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> virtual <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual string</span> Description { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Add mapping file for Product:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Mapping</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ProductMap : ClassMap&lt;Product&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> ProductMap()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Id(e =&gt; e.ID).GeneratedBy.GuidComb();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Map(p =&gt; p.Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Map(p =&gt; p.Description);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>You may also want to setup your connection string at this point in the Web.Config file, mine looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&lt;add name=<span style="color: #006080">&quot;MYDBKEY&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=LOCALHOST;initial catalog=FubuSample;Integrated Security=SSPI;&quot;</span> providerName=<span style="color: #006080">&quot;System.Data.SqlClient&quot;</span> /&gt;</pre>
  </div>
</div>

<p>I also created an empty database in my local SQL Server instance called “FubuSample” And then I went back to SQLServerSessionSourceConfiguration and changed the value of IsNewDatabase to return true. This will create the schema for the database when the application first runs. There is a multitude of different ways that we can handle this, but this was easy for the tutorial purposes.</p>

<p>Next we will add a behavior to our core project and tell FubuMVC to apply that behavior to all or our ControllerActions.</p>

<p>Create a class named “access_the_database_through_a_unit_of_work” in your Core.Web.Behaviors namespace. The contents will look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> access_the_database_through_a_unit_of_work : IControllerActionBehavior</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> IUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> access_the_database_through_a_unit_of_work(IUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IControllerActionBehavior InsideBehavior { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IInvocationResult Result { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> OUTPUT Invoke&lt;INPUT, OUTPUT&gt;(INPUT input, Func&lt;INPUT, OUTPUT&gt; func)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> INPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> OUTPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork.Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">try</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            var output = InsideBehavior.Invoke(input, func);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Result = InsideBehavior.Result;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">return</span> output;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">catch</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">throw</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">finally</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _unitOfWork.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now to tell FubuMVC what to do, open up Global.asax.cs or your Bootstrapper, wherever you are doing the configuration of FubuMVC. In this project we are doing it in Global.asax.cs Add a new behavior to the section that starts with “x.ByDefault.EveryControllerAction” so it looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">x.ByDefault.EveryControllerAction(d =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">       d.Will&lt;execute_the_result&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">       d.Will&lt;access_the_database_through_a_unit_of_work&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">   });</pre>
  </div>
</div>

<p>The last thing we will do is open up HomeController and add this code above the Index action:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">private</span> IRepository _repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> HomeController(IRepository repository)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository = repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>I also modified the Index action to go ahead and create some database values before we query for them just so we can test this thing. (I only did this for the tutorial, I swear) So modify the Index action to look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> IndexViewModel Index(IndexSetupViewModel inModel)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod1 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct1&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod1);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod2 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct2&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod2);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var outModel = <span style="color: #0000ff">new</span> IndexViewModel();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var productList = _repository.Query&lt;Product&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    outModel.Products = productList.ToList().Select(x =&gt; <span style="color: #0000ff">new</span> ProductDisplay(x));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">return</span> outModel;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now with you should be able to run your web app and browse to /home and all this will be executed and you will be on your way to creating a whole bunch of persist able objects for use with your FubuMVC app.</p>

<p>As always, feedback is welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>What I Really, Really, Really Like About Using Fluent NHibernate</title>
		<link>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-i-really-really-really-like-about-using-fluent-nhibernate</link>
		<comments>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 00:32:10 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/</guid>
		<description><![CDATA[Reusable Mappings Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files: Catalog: &#60;class name="Catalog" table="`Catalog`" optimistic-lock="version"&#62; &#60;id name="Id" column="Id" type="Guid"&#62; &#60;generator class="guid.comb" /&#62; &#60;/id&#62; &#60;version column="Version" name="Version" /&#62; &#60;property name="Name" length="100" type="String"&#62; &#60;column [...]]]></description>
			<content:encoded><![CDATA[<h3>Reusable Mappings</h3>
Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files:
<h4>Catalog:</h4>
<pre class="csharpcode">  <span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="Catalog"</span> <span class="attr">table</span><span class="kwrd">="`Catalog`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>
    ...
  <span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategory:</h4>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="CatalogCategory"</span> <span class="attr">table</span><span class="kwrd">="`CatalogCategory`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>

   ...

<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Suppose we have the luxury to choose how we deal with the database in our project. I do realize that's not always feasible because at some point in our careers, we've all seen what database masochism can do. Anyway, if would be making the shots, I would like to use a surrogate key for every table and I would recommend sequential GUIDs (called <a href="http://www.informit.com/articles/article.aspx?p=25862" target="_blank">COMBs</a>) for that.

The mapping of the classes shown above both have the same configuration for the <em>Id</em> and <em>Version</em> properties. These properties typically live in a <em>DomainEntity</em> base class of some sort because we don't want to repeat that tedious code of putting those in every entity of our domain over and over again. It would be nice if we could somehow do the same for the NHibernate mapping files, which we can't (or at least, not that I know of).

Using <a href="http://fluentnhibernate.org/" target="_blank">Fluent NHibernate</a> we can create an abstract mapping class for our <em>DomainEntity</em> from which we derive all entity mapping classes. The following code would give us the same result as the XML mapping files shown above:
<h4>DomainEntityMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> DomainEntityMapping&lt;TDomainEntity&gt; :
ClassMap&lt;TDomainEntity&gt; <span class="kwrd">where</span> TDomainEntity : DomainEntity
{
    <span class="kwrd">protected</span> DomainEntityMapping()
    {
        Id(entity =&gt; entity.Id, <span class="str">"Id"</span>)
            .GeneratedBy.GuidComb();
        OptimisticLock.Version();
        Version(entity =&gt; entity.Version)
            .TheColumnNameIs(<span class="str">"Version"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogMapping : DomainEntityMapping&lt;Catalog&gt;
{
  <span class="kwrd">public</span> CatalogMapping()
  {
      Map(catalog=&gt; catalog.Name, <span class="str">"Name"</span>);
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategoryMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogCategoryMapping : DomainEntityMapping&lt;CatalogCategory&gt;
{
    <span class="kwrd">public</span> CatalogCategoryMapping()
    {
        Map(category=&gt; category.Description, <span class="str">"Name"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

This way,  I can specify the mapping configuration for <em>Id</em> and <em>Version</em> in a single place which is really nice.
<h3>Mapping Tests</h3>
This is probably my most favorite feature of Fluent NHibernate. Given the fluent mapping configuration for the Category class shown earlier, the following test checks whether this mapping is valid or not:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> When_verifying_the_class_mapping_of_a_catalog
    : NHibernateSpecification
{
  [Test]
  <span class="kwrd">public</span> <span class="kwrd">void</span> Then_a_catalog_object_should_be_persistable()
  {
      <span class="kwrd">new</span> PersistenceSpecification&lt;Catalog&gt;(Session)
          .VerifyTheMappings();
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Running this test results in the following SQL statements being executed to an in-memory SQLite database:

NHibernate: INSERT INTO "Catalog" (Version, Name, Id) VALUES (@p0, @p1, @p2); @p0 = '1', @p1 = '', @p2 = 'c52126cb-f11e-47e4-a481-9bc600134d39'
NHibernate: SELECT catalog0_.Id as Id1_0_, catalog0_.Version as Version1_0_, catalog0_.Name as Name1_0_ FROM "Catalog" catalog0_ WHERE catalog0_.Id=@p0; @p0 = 'c52126cb-f11e-47e4-a481-9bc600134d39'

The following code shows the NHibernateSpecification base class for all my database tests. The code in this class deals with setting up the in-memory SQLite database and building the required schema based on the mappings. The Fluent NHibernate framework gives some really nice support for this as well:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> NHibernateSpecification
    : Specification
{
    <span class="kwrd">protected</span> ISession Session { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Establish_context()
    {
        var config = <span class="kwrd">new</span> SQLiteConfiguration()
            .InMemory()
            .ShowSql()
            .ToProperties();

        var sessionSource = <span class="kwrd">new</span> SessionSource(config,
            <span class="kwrd">new</span> RetailerPersistenceModel());

        Session = sessionSource.CreateSession();
        sessionSource.BuildSchema(Session);

        ProvideInitialData(Session);

        Session.Flush();
        Session.Clear();
   }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Dispose_context()
    {
        <span class="kwrd">if</span>(<span class="kwrd">null</span> != Session)
        {
            Session.Dispose();
            Session = <span class="kwrd">null</span>;
        }
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Fluent NHibernate really lowers the barrier for configuring NHibernate, which is a really big thing in my book. I must admit that I was a bit sceptical at first, but now I noticed that I'm having a hard time going back to the standard XML mapping configuration of NHibernate itself. As Fluent NHibernate is still in an early development stage, some issues can come up. But my personal experience so far is that the <a href="http://groups.google.com/group/fluent-nhibernate?hl=en" target="_blank">user group</a> is very responsive.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Stored Procedure Patterns</title>
		<link>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions</link>
		<comments>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:08:03 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/</guid>
		<description><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play: An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor ) NHibernate The use [...]]]></description>
			<content:encoded><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play:
<ol>
	<li>An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor )</li>
	<li>NHibernate</li>
	<li>The use of an HttpModule to implement the <a href="https://www.hibernate.org/43.html">Open Session in View</a> pattern and provide a Unit of Work implementation (one ISession per HTTP request)</li>
	<li>ASP.NET web services returning JSON (might well be a problem if the result was XML as well)</li>
</ol>
In an “old fashioned” Web Forms scenario everything works perfectly. If some unexpected event occurs during a postback then an Exception is thrown and it bubbles up to the HttpModule which rolls back the transaction.

However with a JSON web service (we’re not using WCF so we just have an System.Web.Services.WebService flagged with a ScriptService attribute) when an unexpected event occurs the magic of the framework catches it and the ScriptMethod simply returns the Exception converted to JSON. This was being correctly handled in our UI but because an Exception is not thrown so the HttpModule thinks that everything is OK and incorrectly commits our transaction. And because processing didn’t necessarily finish and our data could therefore get into an inconsistent state (which is even worse than a plain wrong state).

In order to work around this feature I hooked into the EndRequest event in the HttpModule and check the StatusCode of the Repsonse. Here’s the method that I created:
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">bool</span> IsAjaxError()
{
  <span class="kwrd">return</span> HttpContext.Current.Response.StatusCode == 500 &amp;&amp; HttpContext.Current.Request.RawUrl.Contains(<span class="str">".asmx"</span>);
}</pre>
<!--.csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Obviously if an error is detected I can take the appropriate action. So if you’re using the above combination of technologies be careful because what you think is happening and what is happening might be two entirely different things.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; NHibernate</title>
	<atom:link href="http://elegantcode.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NHibernate, HttpModules, ASP.NET JSON Web Services and Database Transactions</title>
		<link>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions</link>
		<comments>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:08:03 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/</guid>
		<description><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play: An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor ) NHibernate The use [...]]]></description>
			<content:encoded><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play:
<ol>
	<li>An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor )</li>
	<li>NHibernate</li>
	<li>The use of an HttpModule to implement the <a href="https://www.hibernate.org/43.html">Open Session in View</a> pattern and provide a Unit of Work implementation (one ISession per HTTP request)</li>
	<li>ASP.NET web services returning JSON (might well be a problem if the result was XML as well)</li>
</ol>
In an “old fashioned” Web Forms scenario everything works perfectly. If some unexpected event occurs during a postback then an Exception is thrown and it bubbles up to the HttpModule which rolls back the transaction.

However with a JSON web service (we’re not using WCF so we just have an System.Web.Services.WebService flagged with a ScriptService attribute) when an unexpected event occurs the magic of the framework catches it and the ScriptMethod simply returns the Exception converted to JSON. This was being correctly handled in our UI but because an Exception is not thrown so the HttpModule thinks that everything is OK and incorrectly commits our transaction. And because processing didn’t necessarily finish and our data could therefore get into an inconsistent state (which is even worse than a plain wrong state).

In order to work around this feature I hooked into the EndRequest event in the HttpModule and check the StatusCode of the Repsonse. Here’s the method that I created:
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">bool</span> IsAjaxError()
{
  <span class="kwrd">return</span> HttpContext.Current.Response.StatusCode == 500 &amp;&amp; HttpContext.Current.Request.RawUrl.Contains(<span class="str">".asmx"</span>);
}</pre>
<!--.csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Obviously if an error is detected I can take the appropriate action. So if you’re using the above combination of technologies be careful because what you think is happening and what is happening might be two entirely different things.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database Change Management with Tarantino</title>
		<link>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=database-change-management-with-tarantino</link>
		<comments>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 10:17:12 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/</guid>
		<description><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard. Why [...]]]></description>
			<content:encoded><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard.
<h2>Why do we need DB Change management</h2>
Okay then Mr. Developer (or Mrs.), Why do we need source control? We need database change management for the same reason we need source control. To help manage the code and dependencies our applications rely on.
<h4>Recipes for Disaster:</h4>
<blockquote>Team of developers all working/developing off of a central database</blockquote>
<blockquote>Team of developers working/developing off of local database, with no way to keep changes in sync.</blockquote>
So what’s the problem with these recipes? As new features are developed for an application it is very likely that the database will need schema changes to support these features. These could be as simple as new tables or adding a column. If we have schema changes, we have to make sure that the code base for the app is deployed concurrently with the schema changes.

When deploying software to production, code files and libraries can usually be overwritten with a new version. Databases however must be updated intelligently.
<h2>So we’ve got a problem, do we have a solution?</h2>
Enter Tarantino. I was talking with Jarod F. and he said he had heard some stuff about it, but had never used it so I started digging around and found enough pieces to get it working. So first thing I did was head over to the <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino project</a> site. Starting reading around and then i did a little googling and managed to dig up enough information to really wet my appetite and to get it working. Then Eric Hexter was kind enough to give me some time via IM and cleared up several areas for me.

Before I get into the specifics of how to set all this up, I want to explain how Tarantino finds and keeps changes in sync.
<ol>
	<li>Tarantino is a forward only change management system, you can’t roll back.</li>
	<li>If you use Tarantino, you can’t go make changes by hand manually</li>
	<li>Tarantino uses RedGate SQL Compare if you want, to compare DB Schemas</li>
	<li>Tarantino makes a table in every DB it touches to keep track of what scripts have been executed on that DB</li>
</ol>
The system is based on a set of conventions which allow incremental changes to the database schema. The conventions are to create two subdirectories in your database scripts folder. Create– this is where your inital schema change scripts go. Update – this is where your change scripts should be placed. They should be named with the following convention ####SCRIPTNAME.sql where #### is the script number with leading zeros. This will ensure the first script 0001_first_schema_change.sql would be executed first. There is a third folder, ExistingSchema, that you will need if you are going to start using Tarantino on an app that is already in development or production.

There are two ways to use Tarantino, command line, and as a NAnt task. I will be showing the NAnt task method, as it has the most examples available on the net.
<h4>Project Setup</h4>
So what I have done, is created a very simple project. A class library with a Fluent NHibernate configuration, a test fixture to create the database schema, and two simple entities which I have listed below:
<div class="csharpcode">
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Order</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> OrderNumber { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> PONumber { get; set; }</pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;OrderItem&gt; OrderItems { get; set;}</pre>
<pre></pre>
<pre class="alt">   <span class="kwrd">public</span> Order()</pre>
<pre>   {</pre>
<pre class="alt">       OrderItems = <span class="kwrd">new</span> List&lt;OrderItem&gt;();</pre>
<pre>   }</pre>
<pre class="alt">}</pre>
<pre></pre>
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> OrderItem</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> ItemName { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Description { get; set; }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

and my NHibernate and Test Fixture setup:
<div class="csharpcode">
<pre class="alt">[TestFixture]</pre>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> DB_Setup_Fixture</pre>
<pre class="alt">{</pre>
<pre>    <span class="kwrd">private</span> Configuration _configuration;</pre>
<pre class="alt"></pre>
<pre>    [TestFixtureSetUp]</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> FixtureSetup()</pre>
<pre>    {</pre>
<pre class="alt"></pre>
<pre>        _configuration = Fluently.Configure()</pre>
<pre class="alt">            .Database(MsSqlConfiguration.MsSql2008</pre>
<pre>                          .ConnectionString(c =&gt;</pre>
<pre class="alt">                                            c.FromConnectionStringWithKey(<span class="str">"testData"</span>))</pre>
<pre>                          .UseReflectionOptimizer()</pre>
<pre class="alt">                          .ShowSql())</pre>
<pre>            .Mappings(m =&gt;</pre>
<pre class="alt">                      m.AutoMappings.Add(AutoMap.AssemblyOf&lt;Order&gt;()</pre>
<pre>                                             .Where(x =&gt; x.Namespace.EndsWith(<span class="str">"Domain"</span>))))</pre>
<pre class="alt">            .BuildConfiguration();</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre></pre>
<pre class="alt">    [Test, Explicit, Category(<span class="str">"DBSetup"</span>)]</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">void</span> Create_DB_Schema()</pre>
<pre class="alt">    {</pre>
<pre>        var exporter = <span class="kwrd">new</span> SchemaExport(_configuration);</pre>
<pre class="alt">        exporter.Create(<span class="kwrd">true</span>, <span class="kwrd">true</span>);</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --> <!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<h5>The one thing to make note of here, is that my test is set to explicit, and I gave it a category of “DBSetup”. This will be important once we start configuring NAnt and Tarantino.</h5>
So now we have a very simple application that we can use to play around with Tarantino. To get Tarantino working, I am using the modified NAnt files that CodeCampServer is using. I just copied over their NAnt folder they have checked into source control. There are two NAnt build files that are required to use Tarantino. Disclaimer: I am a total NAnt noob, this is not my usual build utility, but I figured out enough to make it work.
<h4>Common.build</h4>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database change management --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="rebuildDatabase"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="updateDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Update"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Create"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="dropDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Drop"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="attr">failonerror</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="manageSqlDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="${action}"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="${database.integrated}"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}"</span></pre>
<pre class="alt">      <span class="attr">username</span><span class="kwrd">="${database.username}"</span></pre>
<pre>      <span class="attr">password</span><span class="kwrd">="${database.password}"</span></pre>
<pre class="alt">        <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${action != 'Drop'}"</span><span class="kwrd">&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Current Database Version: ${usdDatabaseVersion}"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt"></pre>
<pre>  <span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">language</span><span class="kwrd">="C#"</span> <span class="attr">prefix</span><span class="kwrd">="migration"</span> <span class="kwrd">&gt;</span></pre>
<pre class="alt">    &lt;references&gt;</pre>
<pre>      &lt;include name=<span class="str">"System.IO.dll"</span> /&gt;</pre>
<pre class="alt">    &lt;/references&gt;</pre>
<pre>    &lt;code&gt;</pre>
<pre class="alt">      &lt;![CDATA[</pre>
<pre>              [Function(<span class="str">"next-migration-number"</span>)]</pre>
<pre class="alt">              <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> NextMigration(<span class="kwrd">string</span> path  ) {</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">string</span>[] files = System.IO.Directory.GetFiles(path);</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">int</span> count=1;</pre>
<pre></pre>
<pre class="alt">                    <span class="kwrd">if</span>(files.Length &gt; 0)</pre>
<pre>                    {</pre>
<pre class="alt">                        <span class="kwrd">string</span> filename = System.IO.Path.GetFileName(files[files.Length-1]);</pre>
<pre></pre>
<pre class="alt">                        <span class="kwrd">try</span></pre>
<pre>                        {</pre>
<pre class="alt">                            count = Convert.ToInt32(filename.Substring(0, 4));</pre>
<pre>                            count++;</pre>
<pre class="alt"></pre>
<pre>                            <span class="kwrd">if</span>(count%2 == 0)</pre>
<pre class="alt">                                count++;</pre>
<pre>                        }</pre>
<pre class="alt">                        <span class="kwrd">catch</span></pre>
<pre>                        {</pre>
<pre class="alt"></pre>
<pre>                        }</pre>
<pre class="alt">                    }</pre>
<pre>                  <span class="kwrd">return</span> <span class="kwrd">string</span>.Format(<span class="str">"{0:0000}"</span>, count);</pre>
<pre class="alt">              }</pre>
<pre>            ]]&gt;</pre>
<pre class="alt">    &lt;/code&gt;</pre>
<pre>  <span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

This file sets up most of the plumbing that hooks up the Tarantino NAnt targets for use in the nant.build file
<h4>NAnt.build</h4>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="rem">&lt;!--EXTERNAL_PROPERTIES: usdDatabaseVersion, CCNetLabel--&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">name</span><span class="kwrd">="TarantinoSample"</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre>      <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Initialization --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">buildfile</span><span class="kwrd">="common.build"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">loadtasks</span> <span class="attr">assembly</span><span class="kwrd">="lib\nant\Tarantino.Nant.Tasks.dll"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- Master Properties --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="company.name"</span> <span class="attr">value</span><span class="kwrd">="Tarantino Test"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="assembly.unittests"</span> <span class="attr">value</span><span class="kwrd">="TarantinoSample.dll"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- Version settings --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="project.config"</span> <span class="attr">value</span><span class="kwrd">="debug"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Folder references --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.solution"</span> <span class="attr">value</span><span class="kwrd">="src"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.build"</span> <span class="attr">value</span><span class="kwrd">="build"</span> <span class="attr">dynamic</span><span class="kwrd">="true"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Compilation settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="nant.settings.currentframework"</span> <span class="attr">value</span><span class="kwrd">="net-3.5"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="file.solution"</span> <span class="attr">value</span><span class="kwrd">="${dir.solution}/${project::get-name()}.sln"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database migration settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.script.directory"</span> <span class="attr">value</span><span class="kwrd">="dbChangeScripts"</span> <span class="kwrd">/&gt;</span></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.server"</span> <span class="attr">value</span><span class="kwrd">="localhost"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.name"</span> <span class="attr">value</span><span class="kwrd">="OrderData"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.integrated"</span> <span class="attr">value</span><span class="kwrd">="true"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.username"</span> <span class="attr">value</span><span class="kwrd">="dbuser"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.password"</span> <span class="attr">value</span><span class="kwrd">="P@ssword1"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Database Change Management Targets --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="reset-database"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">nunit2</span> <span class="attr">failonerror</span><span class="kwrd">="true"</span> <span class="attr">verbose</span><span class="kwrd">="true"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">formatter</span> <span class="attr">type</span><span class="kwrd">="Plain"</span> <span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">test</span> <span class="attr">assemblyname</span><span class="kwrd">="${dir.solution}/bin/${project.config}/${assembly.unittests}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">                <span class="kwrd">&lt;</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre>                    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">name</span><span class="kwrd">="DBSetup"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">                    <span class="kwrd">&lt;</span><span class="html">exclude</span> <span class="attr">name</span><span class="kwrd">="*"</span><span class="kwrd">/&gt;</span></pre>
<pre>                <span class="kwrd">&lt;/</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;/</span><span class="html">test</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">nunit2</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="db-migration"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">delete</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span>  <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="reset-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="create-versioned-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="migrationScriptName"</span> <span class="attr">value</span><span class="kwrd">="${migration::next-migration-number(database.script.directory+'/Update')}_AutoGeneratedMigration.sql"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">exec</span></pre>
<pre>              <span class="attr">program</span><span class="kwrd">="${redgate.exe}"</span></pre>
<pre class="alt">              <span class="attr">commandline</span><span class="kwrd">="/f /v /server1:${database.server} /server2:${database.server} /database1:${database.name} /database2:${database.name}Versioned /scriptfile:${database.script.directory}/Update/_New_Script.sql /exclude:Table:usd_AppliedDatabaseScript"</span></pre>
<pre>              <span class="attr">resultproperty</span><span class="kwrd">="execReturnCode"</span></pre>
<pre class="alt">              <span class="attr">failonerror</span> <span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>          <span class="kwrd">&lt;</span><span class="html">fail</span> <span class="attr">if</span><span class="kwrd">="${execReturnCode != '0' and execReturnCode != '63'}"</span> <span class="attr">message</span><span class="kwrd">="Redgate Compare Failed!"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql') == false}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">=""</span><span class="kwrd">/&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="---------------------"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="No Migration Required"</span><span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql')}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">move</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span> <span class="attr">tofile</span><span class="kwrd">="${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Created Migration File ${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">          <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="create-versioned-database"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">tstamp</span><span class="kwrd">&gt;&lt;/</span><span class="html">tstamp</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">action</span><span class="kwrd">="Drop"</span></pre>
<pre class="alt">            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre>            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre class="alt">            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre>            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre class="alt">            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="Create"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre class="alt">            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre>            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Inside this file there are a few pieces that need to be configured for use in your own app, these are all listed under “Master Properties”
<h4>Change script creation</h4>
So without going into too many details, what I have done is created my initial schema using my Unit Test. From here I could script this out to a text file, but what I did was use SQL Compare, comparing my initial schema to an empty database. This gave me my initial SQL script that creates my database.

Just to verify that my initial schema creation script is good, I will have Tarantino create the Versioned database for me using this command:
<blockquote>lib\nant\nant.exe –buildfile:nant.build create-versioned-database</blockquote>
Tarantino then looks in the ExistingSchema folder, runs any scripts found there, then does the same thing for the Updates folder.

In order to test out the change management features, I made a small change to my domain, adding a decimal of cost to the OrderItem class should do the trick. Rebuild my solution, then back to the command line to run my “Create Database Migration Script.bat” file. This file is just running the NAnt task.

What happens then, is NAnt goes out and runs my DBSetup test that is in my project. Which recreates the “OrderData” database. Tarantino then creates the “Versioned” database, then uses SQL Compare to find the changes between the two, and creates a file in the update folder for me named, <strong>0001_AutoGeneratedMigration.sql</strong> <span style="color: #ff0000;">NOTE: It is important to rename this file before it is committed to source control, or before you apply these changes to a database. Tarantino tracks the scripts that have been run by filename.</span>
<h4>Push to Production or Dev</h4>
So I have mentioned two databases so far in this post, OrderData and OrderDataVersioned. These two databases contain no data, that’s not to say that they couldn’t, if you had scripts that put it in. But that is not what they were designed to do. Our app.config is pointing to OrderData, so when we ask NHibernate to build the schema, it drops all the tables and recreates them, losing all data. OrderDataVersioned, is Tarantinos database that is used to keep changes in sync. In our development environment, every dev has a third database, we would call OrderDataProd for instance. This database could have sample data and other information in it. Let’s say, we don’t want to lose our data in this DB so what we do is let Tarantino handle the first two databases, then we deploy our schema changes to our development database. We use this same technique to push changes to production, after backups of course.

We can do this easily with the following command:
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build updateDatabase –D:database.name=OrderDataProd</span></blockquote>
Now when I tried to run this, my database OrderDataProd did not exist, so I went to SQL manager, created it, and then ran the command again, only to be slammed with another error. Since the database existed, Tarantino did not run the create schema script, only the update, which created another error. The next command will create the database I want, by executing the schema creation script, then applying any updates to it.
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build createDatabase –D:database.name=OrderDataProd</span></blockquote>
Bingo! I now have my 3rd database that I can dump data into and start developing on top of. Just to be sure that we can create another change script, and deploy it to this database, I will modify the domain again, by adding a quantity to the OrderItem object. I will leave this step up to you to try out.
<h4>Source Code</h4>
I know the blog formatting sometimes jacks up code samples a lot so I am making all the source for this post available two ways:
<ul>
	<li><a href="http://elegantcode.googlecode.com/files/TarantinoTest.zip" target="_blank">Zip File Download</a></li>
	<li><a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/RyanKelley/TarantinoTest" target="_blank">Elegant Code SVN Repository</a></li>
</ul>
<h3>Information Sources</h3>
<a href="http://www.lostechies.com/blogs/hex/default.aspx" target="_blank">Eric Hexter</a> – Huge Help

<a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a>

<a href="http://jeffreypalermo.com/blog/tarantino-project-rsquo-s-database-migrations-screencast/" target="_blank">Palermo and Hexter Screencast on Tarantino</a>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why Future&lt;T&gt; should be in your future</title>
		<link>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-futuret-should-be-in-your-future</link>
		<comments>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:50:24 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/</guid>
		<description><![CDATA[Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature [...]]]></description>
			<content:encoded><![CDATA[<p>Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature <a href="http://davybrion.com/blog/2009/01/nhibernate-and-future-queries/">here</a> so I am not going to rehash that. What I am going to focus on here if the why.</p>  <p>Building a UI can be a very database intensive operation in terms of the number of separate calls required. And for each one of those calls the time taken to actually execute the query can be a small compared to the duration of the roundtrip to request and fetch the data. The obvious solution is some type of batching and with Future&lt;T&gt; this happens automagically without any change to the consuming logic (for databases that support it). Let me restate that, you get a huge performance benefit with only a simple change to your query methods in the Repository. So why aren’t you using it?</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate 3.0 QueryOver</title>
		<link>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-3-0-queryover</link>
		<comments>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 19:23:53 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/</guid>
		<description><![CDATA[One of the personal reasons that I had for co-founding Guild 3 was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in Avoiding (Or Recovering From) Burnout. For me the age old adage of “a change is as good as a rest” [...]]]></description>
			<content:encoded><![CDATA[One of the personal reasons that I had for co-founding <a href="http://guild3.com">Guild 3</a> was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in <a href="http://davybrion.com/blog/2009/09/avoiding-or-recovering-from-burnout/">Avoiding (Or Recovering From) Burnout</a>. For me the age old adage of “a change is as good as a rest” has proven to be an extremely successful strategy.

One of the things that I had stopped doing was keeping an eye on various OSS projects to see what was on the horizon. Yesterday I (finally!) started to experiment with NHibernate 3.0. The first thing that caught my eye was <a href="http://fabiomaulo.blogspot.com/2009/06/criteria-on-nh300.html">QueryOver</a>. As Fabio explained there are a <a href="http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html">lot of different ways of executing queries in NH</a>. Certainly in the pre-LINQ days ICriteria was the predominantly recommended option because it has elements of type safety to it and its fluent-ish API broke everything down into small pieces and avoided string concatenation hell.

QueryOver is fluent a layer on top of ICriteria. It looks very LINQesque but it’s a very different animal, not least of all because there are some concepts in ICriteria that do not have a LINQ equivalent (caching etc.). In the short to medium term I suspect that it will become my de facto approach for NHibernate queries (I’ve used NHibernate LINQ and it’s great for simple queries but I’ve experienced significant issues with more complicated ones).

What I haven’t figured out yet is how, if at all, this will affect my data access testing strategy. Historically I’ve favored smoke tests that have really been doing little more than verifying that a given ICriteria query was semantically valid. Typically I only resorted to actually worrying about the results in specific cases (mainly due to the burden of maintaining test data for each possible scenario). Then again this sounds like a topic for another post doesn’t it…]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate 2.1 and Collection Event Listeners</title>
		<link>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-2-1-and-collection-event-listeners</link>
		<comments>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 00:21:37 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/</guid>
		<description><![CDATA[In a previous post, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to read about it first. Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">previous post</a>, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">read about it</a> first.</p> <p>Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't provide this feature or, as in my case, the database in question does support this feature but the DBA's don't want anything to do with it? In case of a parent domain object having a collection of many child objects, you still might want to have a one-shot delete capability instead of having separate DELETE statements for each child record. </p> <p>The <a href="http://nhforge.org/blogs/nhibernate/archive/2009/07/19/welcome-nh2-1-0.aspx">newly released NHibernate 2.1</a> (congratulations to the entire team for their efforts and hard work) comes to the rescue, which introduces a couple of new event listeners that deal with collections.</p> <p>First we need an example. Suppose we are building an auction web site and the domain has a class called <em>Item</em> which in turn has a collection of <em>Bid</em>s. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Item
{
    <span class="kwrd">private</span> ISet&lt;Bid&gt; _bids;    
    <span class="kwrd">public</span> Int64 Id { get; <span class="kwrd">private</span> set; }
    
    ...
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Bid
{
    <span class="kwrd">public</span> Double Amount { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> Sting Code { get; <span class="kwrd">private</span> set; }

    ...
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>The mapping for these classes looks something like this:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Item, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Item"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Int64"</span> <span class="attr">unsaved-value</span><span class="kwrd">="-1"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">sql-type</span><span class="kwrd">="integer"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="native"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    
    ...
    
    <span class="kwrd">&lt;</span><span class="html">set</span> <span class="attr">name</span><span class="kwrd">="Bids"</span> 
         <span class="attr">access</span><span class="kwrd">="field.camelcase-underscore"</span> 
         <span class="attr">lazy</span><span class="kwrd">="false"</span> 
         <span class="attr">cascade</span><span class="kwrd">="all-delete-orphan"</span> 
         <span class="attr">inverse</span><span class="kwrd">="true"</span> 
         <span class="attr">optimistic-lock</span><span class="kwrd">="false"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">key</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">one-to-many</span> <span class="attr">class</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">set</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Bid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">column</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">type</span><span class="kwrd">="Int64"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="Code"</span> 
                      <span class="attr">column</span><span class="kwrd">="Code"</span> 
                      <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
    
    ...
    
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Just to give you a general idea of the situation here. Now suppose we want to delete a quite popular <em>Item</em> object&nbsp; which has a numerous amount of <em>Bid</em>s. Because the collection of <em>Bids</em> is mapped as <em>inverse</em>, NHibernate will remove every record for a <em>Bid</em> with a separate DELETE statement for each row. </p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1001'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1003'</span>
...
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We could solve this by creating a collection event listener. The first thing we have to do is figure out how to issue a one-shot delete instead of those separate DELETE statements. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IOneShotDeleteHandler
{   
    Type ForEntity();
    Type[] ForChildEntities();
    <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> OneShotDeleteHandlerForItem : IOneShotDeleteHandler
{
    <span class="kwrd">public</span> Type ForEntity()
    {
        <span class="kwrd">return</span> <span class="kwrd">typeof</span>(Item);
    }

    <span class="kwrd">public</span> Type[] ForChildEntities()
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span>[] { <span class="kwrd">typeof</span>(Bid) };
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity)
    {
        var item = (Item)entity;

        session.CreateQuery(<span class="str">"delete Bid where ItemId = :itemId"</span>)
            .SetInt64(<span class="str">"itemId"</span>, item.Id)
            .ExecuteUpdate();
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We created an <em>IOneShotDeleteHandler</em> interface with one implementation for the <em>Item</em> class.&nbsp; The most notable aspect of this implementation is the use of the HQL delete statement that removes all <em>Bid</em>s for a particular <em>Item</em>.</p>
<p>Next step is to create a collection event listener that implements the <em>IPreCollectionRemoveEventListener</em> interface.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEventListener
{
    <span class="kwrd">void</span> ConfigureFor(Configuration configuration);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> CollectionRemoveEventListener 
    : IPreCollectionRemoveEventListener,
      IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;
    
    <span class="kwrd">public</span> CollectionRemoveEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> OnPreRemoveCollection(
        PreCollectionRemoveEvent @<span class="kwrd">event</span>)
    {
        var affectedOwner = @<span class="kwrd">event</span>.AffectedOwnerOrNull;
        <span class="kwrd">if</span>(<span class="kwrd">null</span> == affectedOwner)
            <span class="kwrd">return</span>;

        var oneShotDeleteHandler = 
            _oneShotDeleteHandlers.SingleOrDefault(handler =&gt;
            handler.ForEntity() == affectedOwner.GetType());

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler)
            <span class="kwrd">return</span>;

        oneShotDeleteHandler
            .GiveItAShot(@<span class="kwrd">event</span>.Session, affectedOwner);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration
            .SetListener(ListenerType.PreCollectionRemove, <span class="kwrd">this</span>);
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Don't worry about the IEventListener interface. Its just there for registering all NHibernate event listeners in an IoC container.By doing so, it enables us to inject a collection of <em>IOneShotDeleteHandler</em> objects into the constructor of our event listener. When the <em>OnPreRemoveCollection</em> method is called, we simply lookup whether there's a handler available for the type of entity that's going to be deleted and give it a shot at removing its child collection in one sweep. </p>
<p>Now we only have to register this event listener:</p><pre class="csharpcode">var eventListeners = _dependencyContainer
    .ResolveAll&lt;IEventListener&gt;();
eventListeners.ForEach(eventListener =&gt; eventListener
    .ConfigureFor(configuration));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now, if we would use this 'as is', NHibernate will give us the following error:</p>
<blockquote>
<p><em>Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)</em></p></blockquote>
<p>This is due to the fact that the collection event listener does its work and deletes all <em>Bid</em>s by executing the HQL statement, but NHibernate still tries to issue a DELETE statement for each <em>Bid</em>. This is the result of the <em>all-delete-orphan</em> cascading rule we imposed in the mapping. We could reduce it to <em>save-update</em>, but then no individual DELETE statements are executed when a singe <em>Bid</em> is removed from the collection. Now what?</p>
<p>Well, we could provide a regular delete event listener that allows individual DELETE statements for <em>Bid</em> entities as long as their parent <em>Item</em> is not removed.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> DeleteEventListener : 
    DefaultDeleteEventListener,
    IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;

    <span class="kwrd">public</span> DeleteEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> DeleteEntity(
        IEventSource session, <span class="kwrd">object</span> entity, 
        EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, 
        IEntityPersister persister, ISet transientEntities)
    {
        var oneShotDeleteHandler = _oneShotDeleteHandlers
            .SingleOrDefault(handler =&gt;
                handler.ForChildEntities()
                    .Contains(entity.GetType()));

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler ||
           !IsParentAlsoDeletedIn(
                session.PersistenceContext, 
                oneShotDeleteHandler.ForEntity())
            )
        {
            <span class="kwrd">base</span>.DeleteEntity(session, entity, entityEntry, 
                              isCascadeDeleteEnabled, persister, 
                              transientEntities);
            <span class="kwrd">return</span>;
        }

        CascadeBeforeDelete(session, persister, entity, 
                            entityEntry, transientEntities);
        CascadeAfterDelete(session, persister, entity, 
                           transientEntities);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration.SetListener(ListenerType.Delete, <span class="kwrd">this</span>);
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> Boolean IsParentAlsoDeletedIn(
        IPersistenceContext persistenceContext, Type typeOfParent)
    {
        <span class="kwrd">foreach</span>(DictionaryEntry entry <span class="kwrd">in</span> 
                                    persistenceContext.EntityEntries)
        {
            <span class="kwrd">if</span>(typeOfParent != entry.Key.GetType())
                <span class="kwrd">continue</span>;
            
            var entityEntry = (EntityEntry)entry.Value;
            <span class="kwrd">if</span>(Status.Deleted == entityEntry.Status)
                <span class="kwrd">return</span> <span class="kwrd">true</span>;
        }

        <span class="kwrd">return</span> <span class="kwrd">false</span>;
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>With both event listeners registered, deleting a single Bid results in a single DELETE statement as one would expect:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <span class="kwrd">AND</span> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>and&nbsp; removing an entire <em>Item</em> now results in a one-shot delete for all <em>Bid</em>s:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0; @p0 = 2
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Make sure you use this solution for one-shot deletes wisely and only if you have to. If you can use the CASCADE DELETE foreign key constraints, then by all means, this is the preferred option. If not, only resort to this kind of solution only if you must and that you can prove that its going to give you a tremendous performance benefit. Also take a look at the <a href="http://ayende.com/Blog/2006/09/16/BatchingSupportInNHibernate.aspx">batching support</a> that NHibernate provides (at the moment only SQL Server and Oracle are supported). </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using NHibernate for Legacy Databases</title>
		<link>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-nhibernate-for-legacy-databases</link>
		<comments>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:32:11 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/</guid>
		<description><![CDATA[One of the downsides of being confronted with a shared legacy database day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides of being confronted with a <a href="http://elegantcode.com/2009/03/28/about-a-shared-database/">shared legacy database</a> day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required for your application. These extra columns are specifically there to serve those other legacy applications. Heck, to make matters even worse, there are probably some new columns added specifically for your application as well. This is the fairy tale of shared legacy databases. </p> <p>Using NHibernate in these scenarios can be challenging sometimes but its built in flexibility and extensibility really helps you to deal with those cases. The problem I ran into last week was that we needed to store a domain object into a table that had a lot more columns than were actually required for our application. If it would be possible to store null values in these columns or if they had default values configured for them in the schema, then this would not be a problem. Instead, these unnecessary columns could not store null values and had no default values associated with them. </p> <p>First option would be to make some changes to the schema of the table. Alas, no luck there because the other legacy applications that are using the same table would break. Now what?</p> <p>We needed to insert the default values ourselves, but those columns are not known by NHibernate because they are not mapped to any members of the domain object. One way to solve this, is to pollute the domain object by adding private fields that are initialized to the required default values.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainEntity
{
    <span class="rem">// Legacy fields with no purpose for the domain but required </span>
    <span class="rem">// by the database.</span>
    <span class="kwrd">private</span> Int32 _legacyField1 = 2;
    <span class="kwrd">private</span> Boolean _legacyField2 = <span class="kwrd">false</span>;
    <span class="kwrd">private</span> String _legacyField3 = <span class="str">""</span>;
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably the simplest option, but imposes a <a href="http://en.wikipedia.org/wiki/Fixing_Broken_Windows">broken window</a> as infrastructure concerns are bleeding into the domain this way. In other words, this is not a viable solution. Keeping the legacy stuff isolated as much as possible, NHibernate provides some ways to deal with this by providing an extensive extensibility model.</p>
<p>After some snooping around in the source code of NHibernate, the solution we chose for dealing with this issue is by creating a custom access strategy. The built in property access strategies are probably already well known, but its also possible to write your own access strategy by implementing the <em>IPropertyAccessor</em> interface.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainObjectAccessor : IPropertyAccessor
{
    <span class="kwrd">private</span> IEnumerable&lt;IGetter&gt; _defaultValueGetters;
    
    <span class="kwrd">public</span> SomeDomainObjectAccessor()
    {
        _defaultValueGetters = <span class="kwrd">new</span> List&lt;IGetter&gt;()
        {
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Int32&gt;(<span class="str">"LegacyColumn1"</span>, 2) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Boolean&gt;(<span class="str">"LegacyColumn2"</span>, <span class="kwrd">false</span>) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;String&gt;(<span class="str">"LegacyColumn3"</span>, 2) }
        }
    }

    <span class="kwrd">public</span> IGetter GetGetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> _defaultValueGetters        
            .Where(getter =&gt; getter.PropertyName == propertyName)        
            .SingleOrDefault();
    }
    
    <span class="kwrd">public </span>ISetter GetSetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> NoopSetter();
    }

    <span class="kwrd">public</span> Boolean CanAccessTroughReflectionOptimizer
    {
        get { <span class="kwrd">return</span> <span class="kwrd">true</span>; }
    }
}

<span class="kwrd">private</span> <span class="kwrd">class</span> DefaultValueGetter&lt;T&gt; : IGetter
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> String _propertyName;
    <span class="kwrd">private</span> T Value { get; set; }

    <span class="kwrd">public</span> DefaultValueGetter(String propertyName, T <span class="kwrd">value</span>)
    {
        _propertyName = propertyName;
        Value = <span class="kwrd">value</span>;
    }

    <span class="kwrd">public</span> Object Get(Object target)
    {
        <span class="kwrd">return</span> Value;
    }

    <span class="kwrd">public</span> Type ReturnType
    {
        get { <span class="kwrd">return</span> <span class="kwrd">typeof</span>(T); }
    }

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> _propertyName; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get
        {
            var method = <span class="kwrd">typeof</span>(BasicPropertyAccessor)              
                .GetMethod(<span class="str">"GetGetterOrNull"</span>,
                           BindingFlags.Static | BindingFlags.NonPublic);

            var result = (BasicPropertyAccessor.BasicGetter)method              
                .Invoke(<span class="kwrd">null</span>, <span class="kwrd">new</span> Object[] { GetType(), <span class="str">"Value"</span> });

            <span class="kwrd">return</span> result.Method;
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> GetForInsert(Object owner, IDictionary mergeMap,
                               ISessionImplementor session)
    {
        <span class="kwrd">return</span> Get(owner);
    }
}

<span class="kwrd">private</span> <span class="kwrd">sealed</span> <span class="kwrd">class</span> NoopSetter : ISetter
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Set(Object target, Object <span class="kwrd">value</span>)
    {}

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This simply involves a <em>getter</em> for providing default values and a dummy <em>setter</em> as we're not interested in setting any values on the domain objects. The <em>DefaultValueGetter</em> class uses a trick so that we can keep using the reflection optimizer of NHibernate. This also seems to be necessary when using NHibernate Profiler. </p>
<p>Now we only have to provide some properties in the mapping of the domain object using our custom access strategy:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Int32"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Boolean"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="String"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably not the best solution, but it does the job and prevents polluting the domain objects as a result of database quirks like these. I'm interested in hearing feedback or any better approaches. </p>
<p>Anyway, the easy extensibility of NHibernate makes it the best data access solution around. This way, one can deal with all edge case scenarios that weren't anticipated by the framework builders. </p>
<p>Till next time&nbsp;&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Sybase working to improve NHibernate support</title>
		<link>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sybase-working-to-improve-nhibernate-support</link>
		<comments>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/#comments</comments>
		<pubDate>Fri, 15 May 2009 15:19:16 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Sybase]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/</guid>
		<description><![CDATA[If you also subscribe to Chris’ feed here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 [...]]]></description>
			<content:encoded><![CDATA[<p>If you also subscribe to <a href="http://elegantcode.com/author/brandsma/feed/">Chris’ feed</a> here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 – 10 production applications using this combination of technologies.</p>  <p>But support in NHibernate for Sybase is not great. The basics work but some of the more advanced features are not implemented at all or are done poorly. Fortunately we have an excellent relationship with our vendor and have had for many years. Earlier this week they contacted me and mentioned that they will be undertaking an effort to improve their support for NHibernate and were looking for some beta testers.</p>  <p>For this effort they can be commended. It’s also telling (and significant) that OR/M usage is becoming mainstream enough for them to consider this effort worthwhile.</p>  <p><a href="http://iablog.sybase.com/paulley">Glenn</a> is going to be leading the effort (as he did for the Java version last year). If you want to influence the direction that we go then please tell us what needs to be fixed.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FubuMVC From Scratch &#8211; Part 4 Persistence</title>
		<link>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fubumvc-from-scratch-part-4-persistence</link>
		<comments>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 21:22:00 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[FubuMVC]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/</guid>
		<description><![CDATA[Previous posts in series: FubuMVC From Scratch Part 1 – Basic project structure setup FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup FubuMVC From Scratch – Part 3 (Adding View to project) Persistence is a requirement in virtually every application that we write today, and now the time has come for us [...]]]></description>
			<content:encoded><![CDATA[<h3>Previous posts in series:</h3>  <ul>   <li><a href="http://elegantcode.com/2009/01/30/setting-up-a-fubumvc-project-from-scratch/" target="_blank">FubuMVC From Scratch Part 1 – Basic project structure setup</a> </li>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-2/" target="_blank">FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup</a></li>    <ul></ul>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-3-adding-view-to-project/" target="_blank">FubuMVC From Scratch – Part 3 (Adding View to project)</a></li> </ul>  <p>Persistence is a requirement in virtually every application that we write today, and now the time has come for us to add a persistence model to our <a href="http://fubumvc.googlecode.com/" target="_blank">FubuMVC</a> app. I know I mentioned moving my samples to the FubuCart source, but I am going to go ahead and implement the persistence piece in the same project the previous articles using, FubuSample. I have also added all the code for this series to the <a href="http://code.google.com/p/fubumvc-contrib/" target="_blank">FubuMVC-Contrib</a> project</p>  <p>We will be implementing our persistence model to use NHibernate, Fluent NHibernate, Repository Pattern and UnitOfWork pattern all on top of SQL Express or standard. This will also work for sqllite and other database servers.</p>  <p>Class and Interfaces we have to implement: </p>  <ul>   <li>ISessionSourceConfiguration </li>    <li>SQLServerSessionSourceConfiguration </li>    <li>IUnitOfWork </li>    <li>INHibernateUnitOfWork </li>    <li>NHibernateUnitOfWork </li>    <li>IRepository </li>    <li>NHibernateRepository </li>    <li>DomainEntity </li>    <li>IDomainQuery </li>    <li>FubuSamplePersistenceModel </li> </ul>  <p>Note: I put the first two in the list in my Core.Config namespace, and the rest in Core.Persistence</p>  <p>Before we start you probably want to go ahead and add the following references to your core project and Web project</p>  <p><a href="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref.jpg"><img title="FubuMVC-Persist-Ref" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="409" alt="FubuMVC-Persist-Ref" src="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref-thumb.jpg" width="274" border="0" /></a> </p>  <p>Let’s just work our way down the list and get these files implemented. </p>  <h3>ISessionSourceConfiguration</h3>  <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">   <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">     <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">bool</span> IsNewDatabase { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ISessionSource CreateSessionSource(PersistenceModel model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>SQLServerSessionSourceConfiguration</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 131.67%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 263px; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SQLServerSessionSourceConfiguration : ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#region</span> Implementation of ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsNewDatabase</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        get { <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISessionSource CreateSessionSource(PersistenceModel model)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        var properties = GetProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var source = <span style="color: #0000ff">new</span> SessionSource(properties, model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        create_schema_if_it_does_not_already_exist(source);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> create_schema_if_it_does_not_already_exist(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (IsNewDatabase) source.BuildSchema();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">protected</span> IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">string</span>&gt; GetProperties()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        MsSqlConfiguration config = MsSqlConfiguration.MsSql2005;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.ConnectionString.FromConnectionStringWithKey(“MYDBKEY”);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        config.ShowSql();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.UseOuterJoin();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> config.ToProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #cc6633">#endregion</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>A quick note here: We told SQLServerSessionSourceConfiguration to use a connection string from the app settings file (Web.Config) with a key of “MYDBKEY” This will be important later.</p>

<h3>IUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IUnitOfWork : IDisposable</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">void</span> Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>INHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> INHibernateUnitOfWork : IUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ISession CurrentSession { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateUnitOfWork : INHibernateUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> ITransaction _transaction;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isDisposed;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ISessionSource _source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isInitialized;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateUnitOfWork(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _source = source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Initialize()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        CurrentSession = _source.CreateSession();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isInitialized = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISession CurrentSession { get; <span style="color: #0000ff">private</span> set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Commit()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> begin_new_transaction()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_transaction != <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction = CurrentSession.BeginTransaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Rollback()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_not_currently_be_disposed()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ObjectDisposedException(GetType().Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_be_initialized_first()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (!_isInitialized) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> InvalidOperationException(<span style="color: #006080">&quot;Must initialize (call Initialize()) on NHibernateUnitOfWork before commiting or rolling back&quot;</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Dispose()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed || !_isInitialized) <span style="color: #0000ff">return</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        CurrentSession.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isDisposed = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ENTITY Load&lt;ENTITY&gt;(Guid id)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateRepository : IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> INHibernateUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateRepository(INHibernateUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.SaveOrUpdate(entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ENTITY Load&lt;ENTITY&gt;(Guid id) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Load&lt;ENTITY&gt;(id);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;() <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;().Where(whereQuery.Expression);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(entity);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var query = String.Format(<span style="color: #006080">&quot;from {0}&quot;</span>, <span style="color: #0000ff">typeof</span>(ENTITY).Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(query);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>DomainEntity</h3>

<p>Note: I put both of these classes in my Core.Domain namespace, also XML comments are in Source Repository</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DomainEntity : IEquatable&lt;DomainEntity&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> Guid ID { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">bool</span> Equals(DomainEntity other)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> other.ID.Equals(ID);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> Equals(<span style="color: #0000ff">object</span> obj)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (obj.GetType() != GetType()) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals((DomainEntity)obj);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> GetHashCode()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> ID.GetHashCode();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> ==(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> !=(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> !Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IDomainQuery</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IDomainQuery&lt;ENTITY&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    Expression&lt;Func&lt;ENTITY, <span style="color: #0000ff">bool</span>&gt;&gt; Expression { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>FubuSamplePersistenceModel</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Persistence</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FubuSamplePersistenceModel : PersistenceModel</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> FubuSamplePersistenceModel()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            addMappingsFromThisAssembly();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now that we have all the puzzle pieces in place we need to hook them up and make them do some work. Open up FubuSampleWebRegistry </p>

<p>And add the following code:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> configure()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSourceConfiguration&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;SQLServerSessionSourceConfiguration&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSource&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.ConstructedBy(ctx =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                                     ctx.GetInstance&lt;ISessionSourceConfiguration&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                                         .CreateSessionSource(<span style="color: #0000ff">new</span> FubuSamplePersistenceModel()));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;IUnitOfWork&gt;().TheDefault.Is.ConstructedBy(ctx =&gt; ctx.GetInstance&lt;INHibernateUnitOfWork&gt;());</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;INHibernateUnitOfWork&gt;().CacheBy(InstanceScope.Hybrid)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;NHibernateUnitOfWork&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;IRepository&gt;().TheDefault.Is.OfConcreteType&lt;NHibernateRepository&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>With a little luck and all of our mad skills, we should be able to talk to the database now. So we need to plumb our simple product object up to talk to NHibernate. We will do this with a mapping file, and since we are using FluentNHibernate we can do this strongly typed.</p>

<p>Make the following modifications to Product: Extend DomainEntity and get rid of Id Property, it is inherited</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Product : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> virtual <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual string</span> Description { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Add mapping file for Product:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Mapping</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ProductMap : ClassMap&lt;Product&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> ProductMap()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Id(e =&gt; e.ID).GeneratedBy.GuidComb();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Map(p =&gt; p.Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Map(p =&gt; p.Description);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>You may also want to setup your connection string at this point in the Web.Config file, mine looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&lt;add name=<span style="color: #006080">&quot;MYDBKEY&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=LOCALHOST;initial catalog=FubuSample;Integrated Security=SSPI;&quot;</span> providerName=<span style="color: #006080">&quot;System.Data.SqlClient&quot;</span> /&gt;</pre>
  </div>
</div>

<p>I also created an empty database in my local SQL Server instance called “FubuSample” And then I went back to SQLServerSessionSourceConfiguration and changed the value of IsNewDatabase to return true. This will create the schema for the database when the application first runs. There is a multitude of different ways that we can handle this, but this was easy for the tutorial purposes.</p>

<p>Next we will add a behavior to our core project and tell FubuMVC to apply that behavior to all or our ControllerActions.</p>

<p>Create a class named “access_the_database_through_a_unit_of_work” in your Core.Web.Behaviors namespace. The contents will look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> access_the_database_through_a_unit_of_work : IControllerActionBehavior</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> IUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> access_the_database_through_a_unit_of_work(IUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IControllerActionBehavior InsideBehavior { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IInvocationResult Result { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> OUTPUT Invoke&lt;INPUT, OUTPUT&gt;(INPUT input, Func&lt;INPUT, OUTPUT&gt; func)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> INPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> OUTPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork.Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">try</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            var output = InsideBehavior.Invoke(input, func);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Result = InsideBehavior.Result;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">return</span> output;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">catch</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">throw</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">finally</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _unitOfWork.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now to tell FubuMVC what to do, open up Global.asax.cs or your Bootstrapper, wherever you are doing the configuration of FubuMVC. In this project we are doing it in Global.asax.cs Add a new behavior to the section that starts with “x.ByDefault.EveryControllerAction” so it looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">x.ByDefault.EveryControllerAction(d =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">       d.Will&lt;execute_the_result&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">       d.Will&lt;access_the_database_through_a_unit_of_work&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">   });</pre>
  </div>
</div>

<p>The last thing we will do is open up HomeController and add this code above the Index action:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">private</span> IRepository _repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> HomeController(IRepository repository)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository = repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>I also modified the Index action to go ahead and create some database values before we query for them just so we can test this thing. (I only did this for the tutorial, I swear) So modify the Index action to look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> IndexViewModel Index(IndexSetupViewModel inModel)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod1 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct1&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod1);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod2 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct2&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod2);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var outModel = <span style="color: #0000ff">new</span> IndexViewModel();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var productList = _repository.Query&lt;Product&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    outModel.Products = productList.ToList().Select(x =&gt; <span style="color: #0000ff">new</span> ProductDisplay(x));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">return</span> outModel;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now with you should be able to run your web app and browse to /home and all this will be executed and you will be on your way to creating a whole bunch of persist able objects for use with your FubuMVC app.</p>

<p>As always, feedback is welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>What I Really, Really, Really Like About Using Fluent NHibernate</title>
		<link>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-i-really-really-really-like-about-using-fluent-nhibernate</link>
		<comments>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 00:32:10 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/</guid>
		<description><![CDATA[Reusable Mappings Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files: Catalog: &#60;class name="Catalog" table="`Catalog`" optimistic-lock="version"&#62; &#60;id name="Id" column="Id" type="Guid"&#62; &#60;generator class="guid.comb" /&#62; &#60;/id&#62; &#60;version column="Version" name="Version" /&#62; &#60;property name="Name" length="100" type="String"&#62; &#60;column [...]]]></description>
			<content:encoded><![CDATA[<h3>Reusable Mappings</h3>
Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files:
<h4>Catalog:</h4>
<pre class="csharpcode">  <span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="Catalog"</span> <span class="attr">table</span><span class="kwrd">="`Catalog`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>
    ...
  <span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategory:</h4>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="CatalogCategory"</span> <span class="attr">table</span><span class="kwrd">="`CatalogCategory`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>

   ...

<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Suppose we have the luxury to choose how we deal with the database in our project. I do realize that's not always feasible because at some point in our careers, we've all seen what database masochism can do. Anyway, if would be making the shots, I would like to use a surrogate key for every table and I would recommend sequential GUIDs (called <a href="http://www.informit.com/articles/article.aspx?p=25862" target="_blank">COMBs</a>) for that.

The mapping of the classes shown above both have the same configuration for the <em>Id</em> and <em>Version</em> properties. These properties typically live in a <em>DomainEntity</em> base class of some sort because we don't want to repeat that tedious code of putting those in every entity of our domain over and over again. It would be nice if we could somehow do the same for the NHibernate mapping files, which we can't (or at least, not that I know of).

Using <a href="http://fluentnhibernate.org/" target="_blank">Fluent NHibernate</a> we can create an abstract mapping class for our <em>DomainEntity</em> from which we derive all entity mapping classes. The following code would give us the same result as the XML mapping files shown above:
<h4>DomainEntityMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> DomainEntityMapping&lt;TDomainEntity&gt; :
ClassMap&lt;TDomainEntity&gt; <span class="kwrd">where</span> TDomainEntity : DomainEntity
{
    <span class="kwrd">protected</span> DomainEntityMapping()
    {
        Id(entity =&gt; entity.Id, <span class="str">"Id"</span>)
            .GeneratedBy.GuidComb();
        OptimisticLock.Version();
        Version(entity =&gt; entity.Version)
            .TheColumnNameIs(<span class="str">"Version"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogMapping : DomainEntityMapping&lt;Catalog&gt;
{
  <span class="kwrd">public</span> CatalogMapping()
  {
      Map(catalog=&gt; catalog.Name, <span class="str">"Name"</span>);
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategoryMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogCategoryMapping : DomainEntityMapping&lt;CatalogCategory&gt;
{
    <span class="kwrd">public</span> CatalogCategoryMapping()
    {
        Map(category=&gt; category.Description, <span class="str">"Name"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

This way,  I can specify the mapping configuration for <em>Id</em> and <em>Version</em> in a single place which is really nice.
<h3>Mapping Tests</h3>
This is probably my most favorite feature of Fluent NHibernate. Given the fluent mapping configuration for the Category class shown earlier, the following test checks whether this mapping is valid or not:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> When_verifying_the_class_mapping_of_a_catalog
    : NHibernateSpecification
{
  [Test]
  <span class="kwrd">public</span> <span class="kwrd">void</span> Then_a_catalog_object_should_be_persistable()
  {
      <span class="kwrd">new</span> PersistenceSpecification&lt;Catalog&gt;(Session)
          .VerifyTheMappings();
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Running this test results in the following SQL statements being executed to an in-memory SQLite database:

NHibernate: INSERT INTO "Catalog" (Version, Name, Id) VALUES (@p0, @p1, @p2); @p0 = '1', @p1 = '', @p2 = 'c52126cb-f11e-47e4-a481-9bc600134d39'
NHibernate: SELECT catalog0_.Id as Id1_0_, catalog0_.Version as Version1_0_, catalog0_.Name as Name1_0_ FROM "Catalog" catalog0_ WHERE catalog0_.Id=@p0; @p0 = 'c52126cb-f11e-47e4-a481-9bc600134d39'

The following code shows the NHibernateSpecification base class for all my database tests. The code in this class deals with setting up the in-memory SQLite database and building the required schema based on the mappings. The Fluent NHibernate framework gives some really nice support for this as well:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> NHibernateSpecification
    : Specification
{
    <span class="kwrd">protected</span> ISession Session { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Establish_context()
    {
        var config = <span class="kwrd">new</span> SQLiteConfiguration()
            .InMemory()
            .ShowSql()
            .ToProperties();

        var sessionSource = <span class="kwrd">new</span> SessionSource(config,
            <span class="kwrd">new</span> RetailerPersistenceModel());

        Session = sessionSource.CreateSession();
        sessionSource.BuildSchema(Session);

        ProvideInitialData(Session);

        Session.Flush();
        Session.Clear();
   }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Dispose_context()
    {
        <span class="kwrd">if</span>(<span class="kwrd">null</span> != Session)
        {
            Session.Dispose();
            Session = <span class="kwrd">null</span>;
        }
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Fluent NHibernate really lowers the barrier for configuring NHibernate, which is a really big thing in my book. I must admit that I was a bit sceptical at first, but now I noticed that I'm having a hard time going back to the standard XML mapping configuration of NHibernate itself. As Fluent NHibernate is still in an early development stage, some issues can come up. But my personal experience so far is that the <a href="http://groups.google.com/group/fluent-nhibernate?hl=en" target="_blank">user group</a> is very responsive.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Stored Procedure Patterns</title>
		<link>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=database-change-management-with-tarantino</link>
		<comments>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 10:17:12 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/</guid>
		<description><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard. Why [...]]]></description>
			<content:encoded><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard.
<h2>Why do we need DB Change management</h2>
Okay then Mr. Developer (or Mrs.), Why do we need source control? We need database change management for the same reason we need source control. To help manage the code and dependencies our applications rely on.
<h4>Recipes for Disaster:</h4>
<blockquote>Team of developers all working/developing off of a central database</blockquote>
<blockquote>Team of developers working/developing off of local database, with no way to keep changes in sync.</blockquote>
So what’s the problem with these recipes? As new features are developed for an application it is very likely that the database will need schema changes to support these features. These could be as simple as new tables or adding a column. If we have schema changes, we have to make sure that the code base for the app is deployed concurrently with the schema changes.

When deploying software to production, code files and libraries can usually be overwritten with a new version. Databases however must be updated intelligently.
<h2>So we’ve got a problem, do we have a solution?</h2>
Enter Tarantino. I was talking with Jarod F. and he said he had heard some stuff about it, but had never used it so I started digging around and found enough pieces to get it working. So first thing I did was head over to the <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino project</a> site. Starting reading around and then i did a little googling and managed to dig up enough information to really wet my appetite and to get it working. Then Eric Hexter was kind enough to give me some time via IM and cleared up several areas for me.

Before I get into the specifics of how to set all this up, I want to explain how Tarantino finds and keeps changes in sync.
<ol>
	<li>Tarantino is a forward only change management system, you can’t roll back.</li>
	<li>If you use Tarantino, you can’t go make changes by hand manually</li>
	<li>Tarantino uses RedGate SQL Compare if you want, to compare DB Schemas</li>
	<li>Tarantino makes a table in every DB it touches to keep track of what scripts have been executed on that DB</li>
</ol>
The system is based on a set of conventions which allow incremental changes to the database schema. The conventions are to create two subdirectories in your database scripts folder. Create– this is where your inital schema change scripts go. Update – this is where your change scripts should be placed. They should be named with the following convention ####SCRIPTNAME.sql where #### is the script number with leading zeros. This will ensure the first script 0001_first_schema_change.sql would be executed first. There is a third folder, ExistingSchema, that you will need if you are going to start using Tarantino on an app that is already in development or production.

There are two ways to use Tarantino, command line, and as a NAnt task. I will be showing the NAnt task method, as it has the most examples available on the net.
<h4>Project Setup</h4>
So what I have done, is created a very simple project. A class library with a Fluent NHibernate configuration, a test fixture to create the database schema, and two simple entities which I have listed below:
<div class="csharpcode">
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Order</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> OrderNumber { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> PONumber { get; set; }</pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;OrderItem&gt; OrderItems { get; set;}</pre>
<pre></pre>
<pre class="alt">   <span class="kwrd">public</span> Order()</pre>
<pre>   {</pre>
<pre class="alt">       OrderItems = <span class="kwrd">new</span> List&lt;OrderItem&gt;();</pre>
<pre>   }</pre>
<pre class="alt">}</pre>
<pre></pre>
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> OrderItem</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> ItemName { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Description { get; set; }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

and my NHibernate and Test Fixture setup:
<div class="csharpcode">
<pre class="alt">[TestFixture]</pre>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> DB_Setup_Fixture</pre>
<pre class="alt">{</pre>
<pre>    <span class="kwrd">private</span> Configuration _configuration;</pre>
<pre class="alt"></pre>
<pre>    [TestFixtureSetUp]</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> FixtureSetup()</pre>
<pre>    {</pre>
<pre class="alt"></pre>
<pre>        _configuration = Fluently.Configure()</pre>
<pre class="alt">            .Database(MsSqlConfiguration.MsSql2008</pre>
<pre>                          .ConnectionString(c =&gt;</pre>
<pre class="alt">                                            c.FromConnectionStringWithKey(<span class="str">"testData"</span>))</pre>
<pre>                          .UseReflectionOptimizer()</pre>
<pre class="alt">                          .ShowSql())</pre>
<pre>            .Mappings(m =&gt;</pre>
<pre class="alt">                      m.AutoMappings.Add(AutoMap.AssemblyOf&lt;Order&gt;()</pre>
<pre>                                             .Where(x =&gt; x.Namespace.EndsWith(<span class="str">"Domain"</span>))))</pre>
<pre class="alt">            .BuildConfiguration();</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre></pre>
<pre class="alt">    [Test, Explicit, Category(<span class="str">"DBSetup"</span>)]</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">void</span> Create_DB_Schema()</pre>
<pre class="alt">    {</pre>
<pre>        var exporter = <span class="kwrd">new</span> SchemaExport(_configuration);</pre>
<pre class="alt">        exporter.Create(<span class="kwrd">true</span>, <span class="kwrd">true</span>);</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --> <!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<h5>The one thing to make note of here, is that my test is set to explicit, and I gave it a category of “DBSetup”. This will be important once we start configuring NAnt and Tarantino.</h5>
So now we have a very simple application that we can use to play around with Tarantino. To get Tarantino working, I am using the modified NAnt files that CodeCampServer is using. I just copied over their NAnt folder they have checked into source control. There are two NAnt build files that are required to use Tarantino. Disclaimer: I am a total NAnt noob, this is not my usual build utility, but I figured out enough to make it work.
<h4>Common.build</h4>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database change management --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="rebuildDatabase"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="updateDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Update"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Create"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="dropDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Drop"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="attr">failonerror</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="manageSqlDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="${action}"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="${database.integrated}"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}"</span></pre>
<pre class="alt">      <span class="attr">username</span><span class="kwrd">="${database.username}"</span></pre>
<pre>      <span class="attr">password</span><span class="kwrd">="${database.password}"</span></pre>
<pre class="alt">        <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${action != 'Drop'}"</span><span class="kwrd">&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Current Database Version: ${usdDatabaseVersion}"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt"></pre>
<pre>  <span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">language</span><span class="kwrd">="C#"</span> <span class="attr">prefix</span><span class="kwrd">="migration"</span> <span class="kwrd">&gt;</span></pre>
<pre class="alt">    &lt;references&gt;</pre>
<pre>      &lt;include name=<span class="str">"System.IO.dll"</span> /&gt;</pre>
<pre class="alt">    &lt;/references&gt;</pre>
<pre>    &lt;code&gt;</pre>
<pre class="alt">      &lt;![CDATA[</pre>
<pre>              [Function(<span class="str">"next-migration-number"</span>)]</pre>
<pre class="alt">              <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> NextMigration(<span class="kwrd">string</span> path  ) {</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">string</span>[] files = System.IO.Directory.GetFiles(path);</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">int</span> count=1;</pre>
<pre></pre>
<pre class="alt">                    <span class="kwrd">if</span>(files.Length &gt; 0)</pre>
<pre>                    {</pre>
<pre class="alt">                        <span class="kwrd">string</span> filename = System.IO.Path.GetFileName(files[files.Length-1]);</pre>
<pre></pre>
<pre class="alt">                        <span class="kwrd">try</span></pre>
<pre>                        {</pre>
<pre class="alt">                            count = Convert.ToInt32(filename.Substring(0, 4));</pre>
<pre>                            count++;</pre>
<pre class="alt"></pre>
<pre>                            <span class="kwrd">if</span>(count%2 == 0)</pre>
<pre class="alt">                                count++;</pre>
<pre>                        }</pre>
<pre class="alt">                        <span class="kwrd">catch</span></pre>
<pre>                        {</pre>
<pre class="alt"></pre>
<pre>                        }</pre>
<pre class="alt">                    }</pre>
<pre>                  <span class="kwrd">return</span> <span class="kwrd">string</span>.Format(<span class="str">"{0:0000}"</span>, count);</pre>
<pre class="alt">              }</pre>
<pre>            ]]&gt;</pre>
<pre class="alt">    &lt;/code&gt;</pre>
<pre>  <span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

This file sets up most of the plumbing that hooks up the Tarantino NAnt targets for use in the nant.build file
<h4>NAnt.build</h4>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="rem">&lt;!--EXTERNAL_PROPERTIES: usdDatabaseVersion, CCNetLabel--&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">name</span><span class="kwrd">="TarantinoSample"</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre>      <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Initialization --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">buildfile</span><span class="kwrd">="common.build"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">loadtasks</span> <span class="attr">assembly</span><span class="kwrd">="lib\nant\Tarantino.Nant.Tasks.dll"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- Master Properties --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="company.name"</span> <span class="attr">value</span><span class="kwrd">="Tarantino Test"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="assembly.unittests"</span> <span class="attr">value</span><span class="kwrd">="TarantinoSample.dll"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- Version settings --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="project.config"</span> <span class="attr">value</span><span class="kwrd">="debug"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Folder references --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.solution"</span> <span class="attr">value</span><span class="kwrd">="src"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.build"</span> <span class="attr">value</span><span class="kwrd">="build"</span> <span class="attr">dynamic</span><span class="kwrd">="true"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Compilation settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="nant.settings.currentframework"</span> <span class="attr">value</span><span class="kwrd">="net-3.5"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="file.solution"</span> <span class="attr">value</span><span class="kwrd">="${dir.solution}/${project::get-name()}.sln"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database migration settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.script.directory"</span> <span class="attr">value</span><span class="kwrd">="dbChangeScripts"</span> <span class="kwrd">/&gt;</span></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.server"</span> <span class="attr">value</span><span class="kwrd">="localhost"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.name"</span> <span class="attr">value</span><span class="kwrd">="OrderData"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.integrated"</span> <span class="attr">value</span><span class="kwrd">="true"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.username"</span> <span class="attr">value</span><span class="kwrd">="dbuser"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.password"</span> <span class="attr">value</span><span class="kwrd">="P@ssword1"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Database Change Management Targets --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="reset-database"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">nunit2</span> <span class="attr">failonerror</span><span class="kwrd">="true"</span> <span class="attr">verbose</span><span class="kwrd">="true"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">formatter</span> <span class="attr">type</span><span class="kwrd">="Plain"</span> <span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">test</span> <span class="attr">assemblyname</span><span class="kwrd">="${dir.solution}/bin/${project.config}/${assembly.unittests}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">                <span class="kwrd">&lt;</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre>                    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">name</span><span class="kwrd">="DBSetup"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">                    <span class="kwrd">&lt;</span><span class="html">exclude</span> <span class="attr">name</span><span class="kwrd">="*"</span><span class="kwrd">/&gt;</span></pre>
<pre>                <span class="kwrd">&lt;/</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;/</span><span class="html">test</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">nunit2</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="db-migration"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">delete</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span>  <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="reset-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="create-versioned-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="migrationScriptName"</span> <span class="attr">value</span><span class="kwrd">="${migration::next-migration-number(database.script.directory+'/Update')}_AutoGeneratedMigration.sql"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">exec</span></pre>
<pre>              <span class="attr">program</span><span class="kwrd">="${redgate.exe}"</span></pre>
<pre class="alt">              <span class="attr">commandline</span><span class="kwrd">="/f /v /server1:${database.server} /server2:${database.server} /database1:${database.name} /database2:${database.name}Versioned /scriptfile:${database.script.directory}/Update/_New_Script.sql /exclude:Table:usd_AppliedDatabaseScript"</span></pre>
<pre>              <span class="attr">resultproperty</span><span class="kwrd">="execReturnCode"</span></pre>
<pre class="alt">              <span class="attr">failonerror</span> <span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>          <span class="kwrd">&lt;</span><span class="html">fail</span> <span class="attr">if</span><span class="kwrd">="${execReturnCode != '0' and execReturnCode != '63'}"</span> <span class="attr">message</span><span class="kwrd">="Redgate Compare Failed!"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql') == false}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">=""</span><span class="kwrd">/&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="---------------------"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="No Migration Required"</span><span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql')}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">move</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span> <span class="attr">tofile</span><span class="kwrd">="${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Created Migration File ${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">          <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="create-versioned-database"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">tstamp</span><span class="kwrd">&gt;&lt;/</span><span class="html">tstamp</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">action</span><span class="kwrd">="Drop"</span></pre>
<pre class="alt">            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre>            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre class="alt">            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre>            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre class="alt">            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="Create"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre class="alt">            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre>            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Inside this file there are a few pieces that need to be configured for use in your own app, these are all listed under “Master Properties”
<h4>Change script creation</h4>
So without going into too many details, what I have done is created my initial schema using my Unit Test. From here I could script this out to a text file, but what I did was use SQL Compare, comparing my initial schema to an empty database. This gave me my initial SQL script that creates my database.

Just to verify that my initial schema creation script is good, I will have Tarantino create the Versioned database for me using this command:
<blockquote>lib\nant\nant.exe –buildfile:nant.build create-versioned-database</blockquote>
Tarantino then looks in the ExistingSchema folder, runs any scripts found there, then does the same thing for the Updates folder.

In order to test out the change management features, I made a small change to my domain, adding a decimal of cost to the OrderItem class should do the trick. Rebuild my solution, then back to the command line to run my “Create Database Migration Script.bat” file. This file is just running the NAnt task.

What happens then, is NAnt goes out and runs my DBSetup test that is in my project. Which recreates the “OrderData” database. Tarantino then creates the “Versioned” database, then uses SQL Compare to find the changes between the two, and creates a file in the update folder for me named, <strong>0001_AutoGeneratedMigration.sql</strong> <span style="color: #ff0000;">NOTE: It is important to rename this file before it is committed to source control, or before you apply these changes to a database. Tarantino tracks the scripts that have been run by filename.</span>
<h4>Push to Production or Dev</h4>
So I have mentioned two databases so far in this post, OrderData and OrderDataVersioned. These two databases contain no data, that’s not to say that they couldn’t, if you had scripts that put it in. But that is not what they were designed to do. Our app.config is pointing to OrderData, so when we ask NHibernate to build the schema, it drops all the tables and recreates them, losing all data. OrderDataVersioned, is Tarantinos database that is used to keep changes in sync. In our development environment, every dev has a third database, we would call OrderDataProd for instance. This database could have sample data and other information in it. Let’s say, we don’t want to lose our data in this DB so what we do is let Tarantino handle the first two databases, then we deploy our schema changes to our development database. We use this same technique to push changes to production, after backups of course.

We can do this easily with the following command:
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build updateDatabase –D:database.name=OrderDataProd</span></blockquote>
Now when I tried to run this, my database OrderDataProd did not exist, so I went to SQL manager, created it, and then ran the command again, only to be slammed with another error. Since the database existed, Tarantino did not run the create schema script, only the update, which created another error. The next command will create the database I want, by executing the schema creation script, then applying any updates to it.
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build createDatabase –D:database.name=OrderDataProd</span></blockquote>
Bingo! I now have my 3rd database that I can dump data into and start developing on top of. Just to be sure that we can create another change script, and deploy it to this database, I will modify the domain again, by adding a quantity to the OrderItem object. I will leave this step up to you to try out.
<h4>Source Code</h4>
I know the blog formatting sometimes jacks up code samples a lot so I am making all the source for this post available two ways:
<ul>
	<li><a href="http://elegantcode.googlecode.com/files/TarantinoTest.zip" target="_blank">Zip File Download</a></li>
	<li><a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/RyanKelley/TarantinoTest" target="_blank">Elegant Code SVN Repository</a></li>
</ul>
<h3>Information Sources</h3>
<a href="http://www.lostechies.com/blogs/hex/default.aspx" target="_blank">Eric Hexter</a> – Huge Help

<a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a>

<a href="http://jeffreypalermo.com/blog/tarantino-project-rsquo-s-database-migrations-screencast/" target="_blank">Palermo and Hexter Screencast on Tarantino</a>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; NHibernate</title>
	<atom:link href="http://elegantcode.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NHibernate, HttpModules, ASP.NET JSON Web Services and Database Transactions</title>
		<link>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions</link>
		<comments>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:08:03 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/</guid>
		<description><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play: An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor ) NHibernate The use [...]]]></description>
			<content:encoded><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play:
<ol>
	<li>An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor )</li>
	<li>NHibernate</li>
	<li>The use of an HttpModule to implement the <a href="https://www.hibernate.org/43.html">Open Session in View</a> pattern and provide a Unit of Work implementation (one ISession per HTTP request)</li>
	<li>ASP.NET web services returning JSON (might well be a problem if the result was XML as well)</li>
</ol>
In an “old fashioned” Web Forms scenario everything works perfectly. If some unexpected event occurs during a postback then an Exception is thrown and it bubbles up to the HttpModule which rolls back the transaction.

However with a JSON web service (we’re not using WCF so we just have an System.Web.Services.WebService flagged with a ScriptService attribute) when an unexpected event occurs the magic of the framework catches it and the ScriptMethod simply returns the Exception converted to JSON. This was being correctly handled in our UI but because an Exception is not thrown so the HttpModule thinks that everything is OK and incorrectly commits our transaction. And because processing didn’t necessarily finish and our data could therefore get into an inconsistent state (which is even worse than a plain wrong state).

In order to work around this feature I hooked into the EndRequest event in the HttpModule and check the StatusCode of the Repsonse. Here’s the method that I created:
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">bool</span> IsAjaxError()
{
  <span class="kwrd">return</span> HttpContext.Current.Response.StatusCode == 500 &amp;&amp; HttpContext.Current.Request.RawUrl.Contains(<span class="str">".asmx"</span>);
}</pre>
<!--.csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Obviously if an error is detected I can take the appropriate action. So if you’re using the above combination of technologies be careful because what you think is happening and what is happening might be two entirely different things.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database Change Management with Tarantino</title>
		<link>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=database-change-management-with-tarantino</link>
		<comments>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 10:17:12 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/</guid>
		<description><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard. Why [...]]]></description>
			<content:encoded><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard.
<h2>Why do we need DB Change management</h2>
Okay then Mr. Developer (or Mrs.), Why do we need source control? We need database change management for the same reason we need source control. To help manage the code and dependencies our applications rely on.
<h4>Recipes for Disaster:</h4>
<blockquote>Team of developers all working/developing off of a central database</blockquote>
<blockquote>Team of developers working/developing off of local database, with no way to keep changes in sync.</blockquote>
So what’s the problem with these recipes? As new features are developed for an application it is very likely that the database will need schema changes to support these features. These could be as simple as new tables or adding a column. If we have schema changes, we have to make sure that the code base for the app is deployed concurrently with the schema changes.

When deploying software to production, code files and libraries can usually be overwritten with a new version. Databases however must be updated intelligently.
<h2>So we’ve got a problem, do we have a solution?</h2>
Enter Tarantino. I was talking with Jarod F. and he said he had heard some stuff about it, but had never used it so I started digging around and found enough pieces to get it working. So first thing I did was head over to the <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino project</a> site. Starting reading around and then i did a little googling and managed to dig up enough information to really wet my appetite and to get it working. Then Eric Hexter was kind enough to give me some time via IM and cleared up several areas for me.

Before I get into the specifics of how to set all this up, I want to explain how Tarantino finds and keeps changes in sync.
<ol>
	<li>Tarantino is a forward only change management system, you can’t roll back.</li>
	<li>If you use Tarantino, you can’t go make changes by hand manually</li>
	<li>Tarantino uses RedGate SQL Compare if you want, to compare DB Schemas</li>
	<li>Tarantino makes a table in every DB it touches to keep track of what scripts have been executed on that DB</li>
</ol>
The system is based on a set of conventions which allow incremental changes to the database schema. The conventions are to create two subdirectories in your database scripts folder. Create– this is where your inital schema change scripts go. Update – this is where your change scripts should be placed. They should be named with the following convention ####SCRIPTNAME.sql where #### is the script number with leading zeros. This will ensure the first script 0001_first_schema_change.sql would be executed first. There is a third folder, ExistingSchema, that you will need if you are going to start using Tarantino on an app that is already in development or production.

There are two ways to use Tarantino, command line, and as a NAnt task. I will be showing the NAnt task method, as it has the most examples available on the net.
<h4>Project Setup</h4>
So what I have done, is created a very simple project. A class library with a Fluent NHibernate configuration, a test fixture to create the database schema, and two simple entities which I have listed below:
<div class="csharpcode">
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Order</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> OrderNumber { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> PONumber { get; set; }</pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;OrderItem&gt; OrderItems { get; set;}</pre>
<pre></pre>
<pre class="alt">   <span class="kwrd">public</span> Order()</pre>
<pre>   {</pre>
<pre class="alt">       OrderItems = <span class="kwrd">new</span> List&lt;OrderItem&gt;();</pre>
<pre>   }</pre>
<pre class="alt">}</pre>
<pre></pre>
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> OrderItem</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> ItemName { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Description { get; set; }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

and my NHibernate and Test Fixture setup:
<div class="csharpcode">
<pre class="alt">[TestFixture]</pre>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> DB_Setup_Fixture</pre>
<pre class="alt">{</pre>
<pre>    <span class="kwrd">private</span> Configuration _configuration;</pre>
<pre class="alt"></pre>
<pre>    [TestFixtureSetUp]</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> FixtureSetup()</pre>
<pre>    {</pre>
<pre class="alt"></pre>
<pre>        _configuration = Fluently.Configure()</pre>
<pre class="alt">            .Database(MsSqlConfiguration.MsSql2008</pre>
<pre>                          .ConnectionString(c =&gt;</pre>
<pre class="alt">                                            c.FromConnectionStringWithKey(<span class="str">"testData"</span>))</pre>
<pre>                          .UseReflectionOptimizer()</pre>
<pre class="alt">                          .ShowSql())</pre>
<pre>            .Mappings(m =&gt;</pre>
<pre class="alt">                      m.AutoMappings.Add(AutoMap.AssemblyOf&lt;Order&gt;()</pre>
<pre>                                             .Where(x =&gt; x.Namespace.EndsWith(<span class="str">"Domain"</span>))))</pre>
<pre class="alt">            .BuildConfiguration();</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre></pre>
<pre class="alt">    [Test, Explicit, Category(<span class="str">"DBSetup"</span>)]</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">void</span> Create_DB_Schema()</pre>
<pre class="alt">    {</pre>
<pre>        var exporter = <span class="kwrd">new</span> SchemaExport(_configuration);</pre>
<pre class="alt">        exporter.Create(<span class="kwrd">true</span>, <span class="kwrd">true</span>);</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --> <!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<h5>The one thing to make note of here, is that my test is set to explicit, and I gave it a category of “DBSetup”. This will be important once we start configuring NAnt and Tarantino.</h5>
So now we have a very simple application that we can use to play around with Tarantino. To get Tarantino working, I am using the modified NAnt files that CodeCampServer is using. I just copied over their NAnt folder they have checked into source control. There are two NAnt build files that are required to use Tarantino. Disclaimer: I am a total NAnt noob, this is not my usual build utility, but I figured out enough to make it work.
<h4>Common.build</h4>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database change management --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="rebuildDatabase"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="updateDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Update"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Create"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="dropDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Drop"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="attr">failonerror</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="manageSqlDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="${action}"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="${database.integrated}"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}"</span></pre>
<pre class="alt">      <span class="attr">username</span><span class="kwrd">="${database.username}"</span></pre>
<pre>      <span class="attr">password</span><span class="kwrd">="${database.password}"</span></pre>
<pre class="alt">        <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${action != 'Drop'}"</span><span class="kwrd">&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Current Database Version: ${usdDatabaseVersion}"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt"></pre>
<pre>  <span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">language</span><span class="kwrd">="C#"</span> <span class="attr">prefix</span><span class="kwrd">="migration"</span> <span class="kwrd">&gt;</span></pre>
<pre class="alt">    &lt;references&gt;</pre>
<pre>      &lt;include name=<span class="str">"System.IO.dll"</span> /&gt;</pre>
<pre class="alt">    &lt;/references&gt;</pre>
<pre>    &lt;code&gt;</pre>
<pre class="alt">      &lt;![CDATA[</pre>
<pre>              [Function(<span class="str">"next-migration-number"</span>)]</pre>
<pre class="alt">              <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> NextMigration(<span class="kwrd">string</span> path  ) {</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">string</span>[] files = System.IO.Directory.GetFiles(path);</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">int</span> count=1;</pre>
<pre></pre>
<pre class="alt">                    <span class="kwrd">if</span>(files.Length &gt; 0)</pre>
<pre>                    {</pre>
<pre class="alt">                        <span class="kwrd">string</span> filename = System.IO.Path.GetFileName(files[files.Length-1]);</pre>
<pre></pre>
<pre class="alt">                        <span class="kwrd">try</span></pre>
<pre>                        {</pre>
<pre class="alt">                            count = Convert.ToInt32(filename.Substring(0, 4));</pre>
<pre>                            count++;</pre>
<pre class="alt"></pre>
<pre>                            <span class="kwrd">if</span>(count%2 == 0)</pre>
<pre class="alt">                                count++;</pre>
<pre>                        }</pre>
<pre class="alt">                        <span class="kwrd">catch</span></pre>
<pre>                        {</pre>
<pre class="alt"></pre>
<pre>                        }</pre>
<pre class="alt">                    }</pre>
<pre>                  <span class="kwrd">return</span> <span class="kwrd">string</span>.Format(<span class="str">"{0:0000}"</span>, count);</pre>
<pre class="alt">              }</pre>
<pre>            ]]&gt;</pre>
<pre class="alt">    &lt;/code&gt;</pre>
<pre>  <span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

This file sets up most of the plumbing that hooks up the Tarantino NAnt targets for use in the nant.build file
<h4>NAnt.build</h4>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="rem">&lt;!--EXTERNAL_PROPERTIES: usdDatabaseVersion, CCNetLabel--&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">name</span><span class="kwrd">="TarantinoSample"</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre>      <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Initialization --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">buildfile</span><span class="kwrd">="common.build"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">loadtasks</span> <span class="attr">assembly</span><span class="kwrd">="lib\nant\Tarantino.Nant.Tasks.dll"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- Master Properties --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="company.name"</span> <span class="attr">value</span><span class="kwrd">="Tarantino Test"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="assembly.unittests"</span> <span class="attr">value</span><span class="kwrd">="TarantinoSample.dll"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- Version settings --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="project.config"</span> <span class="attr">value</span><span class="kwrd">="debug"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Folder references --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.solution"</span> <span class="attr">value</span><span class="kwrd">="src"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.build"</span> <span class="attr">value</span><span class="kwrd">="build"</span> <span class="attr">dynamic</span><span class="kwrd">="true"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Compilation settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="nant.settings.currentframework"</span> <span class="attr">value</span><span class="kwrd">="net-3.5"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="file.solution"</span> <span class="attr">value</span><span class="kwrd">="${dir.solution}/${project::get-name()}.sln"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database migration settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.script.directory"</span> <span class="attr">value</span><span class="kwrd">="dbChangeScripts"</span> <span class="kwrd">/&gt;</span></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.server"</span> <span class="attr">value</span><span class="kwrd">="localhost"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.name"</span> <span class="attr">value</span><span class="kwrd">="OrderData"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.integrated"</span> <span class="attr">value</span><span class="kwrd">="true"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.username"</span> <span class="attr">value</span><span class="kwrd">="dbuser"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.password"</span> <span class="attr">value</span><span class="kwrd">="P@ssword1"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Database Change Management Targets --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="reset-database"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">nunit2</span> <span class="attr">failonerror</span><span class="kwrd">="true"</span> <span class="attr">verbose</span><span class="kwrd">="true"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">formatter</span> <span class="attr">type</span><span class="kwrd">="Plain"</span> <span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">test</span> <span class="attr">assemblyname</span><span class="kwrd">="${dir.solution}/bin/${project.config}/${assembly.unittests}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">                <span class="kwrd">&lt;</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre>                    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">name</span><span class="kwrd">="DBSetup"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">                    <span class="kwrd">&lt;</span><span class="html">exclude</span> <span class="attr">name</span><span class="kwrd">="*"</span><span class="kwrd">/&gt;</span></pre>
<pre>                <span class="kwrd">&lt;/</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;/</span><span class="html">test</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">nunit2</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="db-migration"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">delete</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span>  <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="reset-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="create-versioned-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="migrationScriptName"</span> <span class="attr">value</span><span class="kwrd">="${migration::next-migration-number(database.script.directory+'/Update')}_AutoGeneratedMigration.sql"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">exec</span></pre>
<pre>              <span class="attr">program</span><span class="kwrd">="${redgate.exe}"</span></pre>
<pre class="alt">              <span class="attr">commandline</span><span class="kwrd">="/f /v /server1:${database.server} /server2:${database.server} /database1:${database.name} /database2:${database.name}Versioned /scriptfile:${database.script.directory}/Update/_New_Script.sql /exclude:Table:usd_AppliedDatabaseScript"</span></pre>
<pre>              <span class="attr">resultproperty</span><span class="kwrd">="execReturnCode"</span></pre>
<pre class="alt">              <span class="attr">failonerror</span> <span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>          <span class="kwrd">&lt;</span><span class="html">fail</span> <span class="attr">if</span><span class="kwrd">="${execReturnCode != '0' and execReturnCode != '63'}"</span> <span class="attr">message</span><span class="kwrd">="Redgate Compare Failed!"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql') == false}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">=""</span><span class="kwrd">/&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="---------------------"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="No Migration Required"</span><span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql')}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">move</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span> <span class="attr">tofile</span><span class="kwrd">="${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Created Migration File ${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">          <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="create-versioned-database"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">tstamp</span><span class="kwrd">&gt;&lt;/</span><span class="html">tstamp</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">action</span><span class="kwrd">="Drop"</span></pre>
<pre class="alt">            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre>            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre class="alt">            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre>            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre class="alt">            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="Create"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre class="alt">            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre>            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Inside this file there are a few pieces that need to be configured for use in your own app, these are all listed under “Master Properties”
<h4>Change script creation</h4>
So without going into too many details, what I have done is created my initial schema using my Unit Test. From here I could script this out to a text file, but what I did was use SQL Compare, comparing my initial schema to an empty database. This gave me my initial SQL script that creates my database.

Just to verify that my initial schema creation script is good, I will have Tarantino create the Versioned database for me using this command:
<blockquote>lib\nant\nant.exe –buildfile:nant.build create-versioned-database</blockquote>
Tarantino then looks in the ExistingSchema folder, runs any scripts found there, then does the same thing for the Updates folder.

In order to test out the change management features, I made a small change to my domain, adding a decimal of cost to the OrderItem class should do the trick. Rebuild my solution, then back to the command line to run my “Create Database Migration Script.bat” file. This file is just running the NAnt task.

What happens then, is NAnt goes out and runs my DBSetup test that is in my project. Which recreates the “OrderData” database. Tarantino then creates the “Versioned” database, then uses SQL Compare to find the changes between the two, and creates a file in the update folder for me named, <strong>0001_AutoGeneratedMigration.sql</strong> <span style="color: #ff0000;">NOTE: It is important to rename this file before it is committed to source control, or before you apply these changes to a database. Tarantino tracks the scripts that have been run by filename.</span>
<h4>Push to Production or Dev</h4>
So I have mentioned two databases so far in this post, OrderData and OrderDataVersioned. These two databases contain no data, that’s not to say that they couldn’t, if you had scripts that put it in. But that is not what they were designed to do. Our app.config is pointing to OrderData, so when we ask NHibernate to build the schema, it drops all the tables and recreates them, losing all data. OrderDataVersioned, is Tarantinos database that is used to keep changes in sync. In our development environment, every dev has a third database, we would call OrderDataProd for instance. This database could have sample data and other information in it. Let’s say, we don’t want to lose our data in this DB so what we do is let Tarantino handle the first two databases, then we deploy our schema changes to our development database. We use this same technique to push changes to production, after backups of course.

We can do this easily with the following command:
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build updateDatabase –D:database.name=OrderDataProd</span></blockquote>
Now when I tried to run this, my database OrderDataProd did not exist, so I went to SQL manager, created it, and then ran the command again, only to be slammed with another error. Since the database existed, Tarantino did not run the create schema script, only the update, which created another error. The next command will create the database I want, by executing the schema creation script, then applying any updates to it.
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build createDatabase –D:database.name=OrderDataProd</span></blockquote>
Bingo! I now have my 3rd database that I can dump data into and start developing on top of. Just to be sure that we can create another change script, and deploy it to this database, I will modify the domain again, by adding a quantity to the OrderItem object. I will leave this step up to you to try out.
<h4>Source Code</h4>
I know the blog formatting sometimes jacks up code samples a lot so I am making all the source for this post available two ways:
<ul>
	<li><a href="http://elegantcode.googlecode.com/files/TarantinoTest.zip" target="_blank">Zip File Download</a></li>
	<li><a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/RyanKelley/TarantinoTest" target="_blank">Elegant Code SVN Repository</a></li>
</ul>
<h3>Information Sources</h3>
<a href="http://www.lostechies.com/blogs/hex/default.aspx" target="_blank">Eric Hexter</a> – Huge Help

<a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a>

<a href="http://jeffreypalermo.com/blog/tarantino-project-rsquo-s-database-migrations-screencast/" target="_blank">Palermo and Hexter Screencast on Tarantino</a>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why Future&lt;T&gt; should be in your future</title>
		<link>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-futuret-should-be-in-your-future</link>
		<comments>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:50:24 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/</guid>
		<description><![CDATA[Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature [...]]]></description>
			<content:encoded><![CDATA[<p>Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature <a href="http://davybrion.com/blog/2009/01/nhibernate-and-future-queries/">here</a> so I am not going to rehash that. What I am going to focus on here if the why.</p>  <p>Building a UI can be a very database intensive operation in terms of the number of separate calls required. And for each one of those calls the time taken to actually execute the query can be a small compared to the duration of the roundtrip to request and fetch the data. The obvious solution is some type of batching and with Future&lt;T&gt; this happens automagically without any change to the consuming logic (for databases that support it). Let me restate that, you get a huge performance benefit with only a simple change to your query methods in the Repository. So why aren’t you using it?</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate 3.0 QueryOver</title>
		<link>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-3-0-queryover</link>
		<comments>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 19:23:53 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/</guid>
		<description><![CDATA[One of the personal reasons that I had for co-founding Guild 3 was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in Avoiding (Or Recovering From) Burnout. For me the age old adage of “a change is as good as a rest” [...]]]></description>
			<content:encoded><![CDATA[One of the personal reasons that I had for co-founding <a href="http://guild3.com">Guild 3</a> was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in <a href="http://davybrion.com/blog/2009/09/avoiding-or-recovering-from-burnout/">Avoiding (Or Recovering From) Burnout</a>. For me the age old adage of “a change is as good as a rest” has proven to be an extremely successful strategy.

One of the things that I had stopped doing was keeping an eye on various OSS projects to see what was on the horizon. Yesterday I (finally!) started to experiment with NHibernate 3.0. The first thing that caught my eye was <a href="http://fabiomaulo.blogspot.com/2009/06/criteria-on-nh300.html">QueryOver</a>. As Fabio explained there are a <a href="http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html">lot of different ways of executing queries in NH</a>. Certainly in the pre-LINQ days ICriteria was the predominantly recommended option because it has elements of type safety to it and its fluent-ish API broke everything down into small pieces and avoided string concatenation hell.

QueryOver is fluent a layer on top of ICriteria. It looks very LINQesque but it’s a very different animal, not least of all because there are some concepts in ICriteria that do not have a LINQ equivalent (caching etc.). In the short to medium term I suspect that it will become my de facto approach for NHibernate queries (I’ve used NHibernate LINQ and it’s great for simple queries but I’ve experienced significant issues with more complicated ones).

What I haven’t figured out yet is how, if at all, this will affect my data access testing strategy. Historically I’ve favored smoke tests that have really been doing little more than verifying that a given ICriteria query was semantically valid. Typically I only resorted to actually worrying about the results in specific cases (mainly due to the burden of maintaining test data for each possible scenario). Then again this sounds like a topic for another post doesn’t it…]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate 2.1 and Collection Event Listeners</title>
		<link>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-2-1-and-collection-event-listeners</link>
		<comments>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 00:21:37 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/</guid>
		<description><![CDATA[In a previous post, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to read about it first. Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">previous post</a>, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">read about it</a> first.</p> <p>Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't provide this feature or, as in my case, the database in question does support this feature but the DBA's don't want anything to do with it? In case of a parent domain object having a collection of many child objects, you still might want to have a one-shot delete capability instead of having separate DELETE statements for each child record. </p> <p>The <a href="http://nhforge.org/blogs/nhibernate/archive/2009/07/19/welcome-nh2-1-0.aspx">newly released NHibernate 2.1</a> (congratulations to the entire team for their efforts and hard work) comes to the rescue, which introduces a couple of new event listeners that deal with collections.</p> <p>First we need an example. Suppose we are building an auction web site and the domain has a class called <em>Item</em> which in turn has a collection of <em>Bid</em>s. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Item
{
    <span class="kwrd">private</span> ISet&lt;Bid&gt; _bids;    
    <span class="kwrd">public</span> Int64 Id { get; <span class="kwrd">private</span> set; }
    
    ...
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Bid
{
    <span class="kwrd">public</span> Double Amount { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> Sting Code { get; <span class="kwrd">private</span> set; }

    ...
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>The mapping for these classes looks something like this:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Item, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Item"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Int64"</span> <span class="attr">unsaved-value</span><span class="kwrd">="-1"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">sql-type</span><span class="kwrd">="integer"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="native"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    
    ...
    
    <span class="kwrd">&lt;</span><span class="html">set</span> <span class="attr">name</span><span class="kwrd">="Bids"</span> 
         <span class="attr">access</span><span class="kwrd">="field.camelcase-underscore"</span> 
         <span class="attr">lazy</span><span class="kwrd">="false"</span> 
         <span class="attr">cascade</span><span class="kwrd">="all-delete-orphan"</span> 
         <span class="attr">inverse</span><span class="kwrd">="true"</span> 
         <span class="attr">optimistic-lock</span><span class="kwrd">="false"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">key</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">one-to-many</span> <span class="attr">class</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">set</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Bid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">column</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">type</span><span class="kwrd">="Int64"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="Code"</span> 
                      <span class="attr">column</span><span class="kwrd">="Code"</span> 
                      <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
    
    ...
    
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Just to give you a general idea of the situation here. Now suppose we want to delete a quite popular <em>Item</em> object&nbsp; which has a numerous amount of <em>Bid</em>s. Because the collection of <em>Bids</em> is mapped as <em>inverse</em>, NHibernate will remove every record for a <em>Bid</em> with a separate DELETE statement for each row. </p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1001'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1003'</span>
...
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We could solve this by creating a collection event listener. The first thing we have to do is figure out how to issue a one-shot delete instead of those separate DELETE statements. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IOneShotDeleteHandler
{   
    Type ForEntity();
    Type[] ForChildEntities();
    <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> OneShotDeleteHandlerForItem : IOneShotDeleteHandler
{
    <span class="kwrd">public</span> Type ForEntity()
    {
        <span class="kwrd">return</span> <span class="kwrd">typeof</span>(Item);
    }

    <span class="kwrd">public</span> Type[] ForChildEntities()
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span>[] { <span class="kwrd">typeof</span>(Bid) };
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity)
    {
        var item = (Item)entity;

        session.CreateQuery(<span class="str">"delete Bid where ItemId = :itemId"</span>)
            .SetInt64(<span class="str">"itemId"</span>, item.Id)
            .ExecuteUpdate();
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We created an <em>IOneShotDeleteHandler</em> interface with one implementation for the <em>Item</em> class.&nbsp; The most notable aspect of this implementation is the use of the HQL delete statement that removes all <em>Bid</em>s for a particular <em>Item</em>.</p>
<p>Next step is to create a collection event listener that implements the <em>IPreCollectionRemoveEventListener</em> interface.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEventListener
{
    <span class="kwrd">void</span> ConfigureFor(Configuration configuration);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> CollectionRemoveEventListener 
    : IPreCollectionRemoveEventListener,
      IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;
    
    <span class="kwrd">public</span> CollectionRemoveEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> OnPreRemoveCollection(
        PreCollectionRemoveEvent @<span class="kwrd">event</span>)
    {
        var affectedOwner = @<span class="kwrd">event</span>.AffectedOwnerOrNull;
        <span class="kwrd">if</span>(<span class="kwrd">null</span> == affectedOwner)
            <span class="kwrd">return</span>;

        var oneShotDeleteHandler = 
            _oneShotDeleteHandlers.SingleOrDefault(handler =&gt;
            handler.ForEntity() == affectedOwner.GetType());

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler)
            <span class="kwrd">return</span>;

        oneShotDeleteHandler
            .GiveItAShot(@<span class="kwrd">event</span>.Session, affectedOwner);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration
            .SetListener(ListenerType.PreCollectionRemove, <span class="kwrd">this</span>);
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Don't worry about the IEventListener interface. Its just there for registering all NHibernate event listeners in an IoC container.By doing so, it enables us to inject a collection of <em>IOneShotDeleteHandler</em> objects into the constructor of our event listener. When the <em>OnPreRemoveCollection</em> method is called, we simply lookup whether there's a handler available for the type of entity that's going to be deleted and give it a shot at removing its child collection in one sweep. </p>
<p>Now we only have to register this event listener:</p><pre class="csharpcode">var eventListeners = _dependencyContainer
    .ResolveAll&lt;IEventListener&gt;();
eventListeners.ForEach(eventListener =&gt; eventListener
    .ConfigureFor(configuration));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now, if we would use this 'as is', NHibernate will give us the following error:</p>
<blockquote>
<p><em>Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)</em></p></blockquote>
<p>This is due to the fact that the collection event listener does its work and deletes all <em>Bid</em>s by executing the HQL statement, but NHibernate still tries to issue a DELETE statement for each <em>Bid</em>. This is the result of the <em>all-delete-orphan</em> cascading rule we imposed in the mapping. We could reduce it to <em>save-update</em>, but then no individual DELETE statements are executed when a singe <em>Bid</em> is removed from the collection. Now what?</p>
<p>Well, we could provide a regular delete event listener that allows individual DELETE statements for <em>Bid</em> entities as long as their parent <em>Item</em> is not removed.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> DeleteEventListener : 
    DefaultDeleteEventListener,
    IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;

    <span class="kwrd">public</span> DeleteEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> DeleteEntity(
        IEventSource session, <span class="kwrd">object</span> entity, 
        EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, 
        IEntityPersister persister, ISet transientEntities)
    {
        var oneShotDeleteHandler = _oneShotDeleteHandlers
            .SingleOrDefault(handler =&gt;
                handler.ForChildEntities()
                    .Contains(entity.GetType()));

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler ||
           !IsParentAlsoDeletedIn(
                session.PersistenceContext, 
                oneShotDeleteHandler.ForEntity())
            )
        {
            <span class="kwrd">base</span>.DeleteEntity(session, entity, entityEntry, 
                              isCascadeDeleteEnabled, persister, 
                              transientEntities);
            <span class="kwrd">return</span>;
        }

        CascadeBeforeDelete(session, persister, entity, 
                            entityEntry, transientEntities);
        CascadeAfterDelete(session, persister, entity, 
                           transientEntities);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration.SetListener(ListenerType.Delete, <span class="kwrd">this</span>);
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> Boolean IsParentAlsoDeletedIn(
        IPersistenceContext persistenceContext, Type typeOfParent)
    {
        <span class="kwrd">foreach</span>(DictionaryEntry entry <span class="kwrd">in</span> 
                                    persistenceContext.EntityEntries)
        {
            <span class="kwrd">if</span>(typeOfParent != entry.Key.GetType())
                <span class="kwrd">continue</span>;
            
            var entityEntry = (EntityEntry)entry.Value;
            <span class="kwrd">if</span>(Status.Deleted == entityEntry.Status)
                <span class="kwrd">return</span> <span class="kwrd">true</span>;
        }

        <span class="kwrd">return</span> <span class="kwrd">false</span>;
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>With both event listeners registered, deleting a single Bid results in a single DELETE statement as one would expect:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <span class="kwrd">AND</span> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>and&nbsp; removing an entire <em>Item</em> now results in a one-shot delete for all <em>Bid</em>s:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0; @p0 = 2
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Make sure you use this solution for one-shot deletes wisely and only if you have to. If you can use the CASCADE DELETE foreign key constraints, then by all means, this is the preferred option. If not, only resort to this kind of solution only if you must and that you can prove that its going to give you a tremendous performance benefit. Also take a look at the <a href="http://ayende.com/Blog/2006/09/16/BatchingSupportInNHibernate.aspx">batching support</a> that NHibernate provides (at the moment only SQL Server and Oracle are supported). </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using NHibernate for Legacy Databases</title>
		<link>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-nhibernate-for-legacy-databases</link>
		<comments>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:32:11 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/</guid>
		<description><![CDATA[One of the downsides of being confronted with a shared legacy database day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides of being confronted with a <a href="http://elegantcode.com/2009/03/28/about-a-shared-database/">shared legacy database</a> day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required for your application. These extra columns are specifically there to serve those other legacy applications. Heck, to make matters even worse, there are probably some new columns added specifically for your application as well. This is the fairy tale of shared legacy databases. </p> <p>Using NHibernate in these scenarios can be challenging sometimes but its built in flexibility and extensibility really helps you to deal with those cases. The problem I ran into last week was that we needed to store a domain object into a table that had a lot more columns than were actually required for our application. If it would be possible to store null values in these columns or if they had default values configured for them in the schema, then this would not be a problem. Instead, these unnecessary columns could not store null values and had no default values associated with them. </p> <p>First option would be to make some changes to the schema of the table. Alas, no luck there because the other legacy applications that are using the same table would break. Now what?</p> <p>We needed to insert the default values ourselves, but those columns are not known by NHibernate because they are not mapped to any members of the domain object. One way to solve this, is to pollute the domain object by adding private fields that are initialized to the required default values.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainEntity
{
    <span class="rem">// Legacy fields with no purpose for the domain but required </span>
    <span class="rem">// by the database.</span>
    <span class="kwrd">private</span> Int32 _legacyField1 = 2;
    <span class="kwrd">private</span> Boolean _legacyField2 = <span class="kwrd">false</span>;
    <span class="kwrd">private</span> String _legacyField3 = <span class="str">""</span>;
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably the simplest option, but imposes a <a href="http://en.wikipedia.org/wiki/Fixing_Broken_Windows">broken window</a> as infrastructure concerns are bleeding into the domain this way. In other words, this is not a viable solution. Keeping the legacy stuff isolated as much as possible, NHibernate provides some ways to deal with this by providing an extensive extensibility model.</p>
<p>After some snooping around in the source code of NHibernate, the solution we chose for dealing with this issue is by creating a custom access strategy. The built in property access strategies are probably already well known, but its also possible to write your own access strategy by implementing the <em>IPropertyAccessor</em> interface.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainObjectAccessor : IPropertyAccessor
{
    <span class="kwrd">private</span> IEnumerable&lt;IGetter&gt; _defaultValueGetters;
    
    <span class="kwrd">public</span> SomeDomainObjectAccessor()
    {
        _defaultValueGetters = <span class="kwrd">new</span> List&lt;IGetter&gt;()
        {
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Int32&gt;(<span class="str">"LegacyColumn1"</span>, 2) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Boolean&gt;(<span class="str">"LegacyColumn2"</span>, <span class="kwrd">false</span>) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;String&gt;(<span class="str">"LegacyColumn3"</span>, 2) }
        }
    }

    <span class="kwrd">public</span> IGetter GetGetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> _defaultValueGetters        
            .Where(getter =&gt; getter.PropertyName == propertyName)        
            .SingleOrDefault();
    }
    
    <span class="kwrd">public </span>ISetter GetSetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> NoopSetter();
    }

    <span class="kwrd">public</span> Boolean CanAccessTroughReflectionOptimizer
    {
        get { <span class="kwrd">return</span> <span class="kwrd">true</span>; }
    }
}

<span class="kwrd">private</span> <span class="kwrd">class</span> DefaultValueGetter&lt;T&gt; : IGetter
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> String _propertyName;
    <span class="kwrd">private</span> T Value { get; set; }

    <span class="kwrd">public</span> DefaultValueGetter(String propertyName, T <span class="kwrd">value</span>)
    {
        _propertyName = propertyName;
        Value = <span class="kwrd">value</span>;
    }

    <span class="kwrd">public</span> Object Get(Object target)
    {
        <span class="kwrd">return</span> Value;
    }

    <span class="kwrd">public</span> Type ReturnType
    {
        get { <span class="kwrd">return</span> <span class="kwrd">typeof</span>(T); }
    }

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> _propertyName; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get
        {
            var method = <span class="kwrd">typeof</span>(BasicPropertyAccessor)              
                .GetMethod(<span class="str">"GetGetterOrNull"</span>,
                           BindingFlags.Static | BindingFlags.NonPublic);

            var result = (BasicPropertyAccessor.BasicGetter)method              
                .Invoke(<span class="kwrd">null</span>, <span class="kwrd">new</span> Object[] { GetType(), <span class="str">"Value"</span> });

            <span class="kwrd">return</span> result.Method;
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> GetForInsert(Object owner, IDictionary mergeMap,
                               ISessionImplementor session)
    {
        <span class="kwrd">return</span> Get(owner);
    }
}

<span class="kwrd">private</span> <span class="kwrd">sealed</span> <span class="kwrd">class</span> NoopSetter : ISetter
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Set(Object target, Object <span class="kwrd">value</span>)
    {}

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This simply involves a <em>getter</em> for providing default values and a dummy <em>setter</em> as we're not interested in setting any values on the domain objects. The <em>DefaultValueGetter</em> class uses a trick so that we can keep using the reflection optimizer of NHibernate. This also seems to be necessary when using NHibernate Profiler. </p>
<p>Now we only have to provide some properties in the mapping of the domain object using our custom access strategy:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Int32"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Boolean"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="String"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably not the best solution, but it does the job and prevents polluting the domain objects as a result of database quirks like these. I'm interested in hearing feedback or any better approaches. </p>
<p>Anyway, the easy extensibility of NHibernate makes it the best data access solution around. This way, one can deal with all edge case scenarios that weren't anticipated by the framework builders. </p>
<p>Till next time&nbsp;&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Sybase working to improve NHibernate support</title>
		<link>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sybase-working-to-improve-nhibernate-support</link>
		<comments>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/#comments</comments>
		<pubDate>Fri, 15 May 2009 15:19:16 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Sybase]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/</guid>
		<description><![CDATA[If you also subscribe to Chris’ feed here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 [...]]]></description>
			<content:encoded><![CDATA[<p>If you also subscribe to <a href="http://elegantcode.com/author/brandsma/feed/">Chris’ feed</a> here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 – 10 production applications using this combination of technologies.</p>  <p>But support in NHibernate for Sybase is not great. The basics work but some of the more advanced features are not implemented at all or are done poorly. Fortunately we have an excellent relationship with our vendor and have had for many years. Earlier this week they contacted me and mentioned that they will be undertaking an effort to improve their support for NHibernate and were looking for some beta testers.</p>  <p>For this effort they can be commended. It’s also telling (and significant) that OR/M usage is becoming mainstream enough for them to consider this effort worthwhile.</p>  <p><a href="http://iablog.sybase.com/paulley">Glenn</a> is going to be leading the effort (as he did for the Java version last year). If you want to influence the direction that we go then please tell us what needs to be fixed.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FubuMVC From Scratch &#8211; Part 4 Persistence</title>
		<link>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fubumvc-from-scratch-part-4-persistence</link>
		<comments>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 21:22:00 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[FubuMVC]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/</guid>
		<description><![CDATA[Previous posts in series: FubuMVC From Scratch Part 1 – Basic project structure setup FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup FubuMVC From Scratch – Part 3 (Adding View to project) Persistence is a requirement in virtually every application that we write today, and now the time has come for us [...]]]></description>
			<content:encoded><![CDATA[<h3>Previous posts in series:</h3>  <ul>   <li><a href="http://elegantcode.com/2009/01/30/setting-up-a-fubumvc-project-from-scratch/" target="_blank">FubuMVC From Scratch Part 1 – Basic project structure setup</a> </li>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-2/" target="_blank">FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup</a></li>    <ul></ul>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-3-adding-view-to-project/" target="_blank">FubuMVC From Scratch – Part 3 (Adding View to project)</a></li> </ul>  <p>Persistence is a requirement in virtually every application that we write today, and now the time has come for us to add a persistence model to our <a href="http://fubumvc.googlecode.com/" target="_blank">FubuMVC</a> app. I know I mentioned moving my samples to the FubuCart source, but I am going to go ahead and implement the persistence piece in the same project the previous articles using, FubuSample. I have also added all the code for this series to the <a href="http://code.google.com/p/fubumvc-contrib/" target="_blank">FubuMVC-Contrib</a> project</p>  <p>We will be implementing our persistence model to use NHibernate, Fluent NHibernate, Repository Pattern and UnitOfWork pattern all on top of SQL Express or standard. This will also work for sqllite and other database servers.</p>  <p>Class and Interfaces we have to implement: </p>  <ul>   <li>ISessionSourceConfiguration </li>    <li>SQLServerSessionSourceConfiguration </li>    <li>IUnitOfWork </li>    <li>INHibernateUnitOfWork </li>    <li>NHibernateUnitOfWork </li>    <li>IRepository </li>    <li>NHibernateRepository </li>    <li>DomainEntity </li>    <li>IDomainQuery </li>    <li>FubuSamplePersistenceModel </li> </ul>  <p>Note: I put the first two in the list in my Core.Config namespace, and the rest in Core.Persistence</p>  <p>Before we start you probably want to go ahead and add the following references to your core project and Web project</p>  <p><a href="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref.jpg"><img title="FubuMVC-Persist-Ref" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="409" alt="FubuMVC-Persist-Ref" src="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref-thumb.jpg" width="274" border="0" /></a> </p>  <p>Let’s just work our way down the list and get these files implemented. </p>  <h3>ISessionSourceConfiguration</h3>  <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">   <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">     <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">bool</span> IsNewDatabase { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ISessionSource CreateSessionSource(PersistenceModel model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>SQLServerSessionSourceConfiguration</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 131.67%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 263px; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SQLServerSessionSourceConfiguration : ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#region</span> Implementation of ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsNewDatabase</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        get { <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISessionSource CreateSessionSource(PersistenceModel model)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        var properties = GetProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var source = <span style="color: #0000ff">new</span> SessionSource(properties, model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        create_schema_if_it_does_not_already_exist(source);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> create_schema_if_it_does_not_already_exist(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (IsNewDatabase) source.BuildSchema();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">protected</span> IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">string</span>&gt; GetProperties()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        MsSqlConfiguration config = MsSqlConfiguration.MsSql2005;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.ConnectionString.FromConnectionStringWithKey(“MYDBKEY”);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        config.ShowSql();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.UseOuterJoin();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> config.ToProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #cc6633">#endregion</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>A quick note here: We told SQLServerSessionSourceConfiguration to use a connection string from the app settings file (Web.Config) with a key of “MYDBKEY” This will be important later.</p>

<h3>IUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IUnitOfWork : IDisposable</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">void</span> Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>INHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> INHibernateUnitOfWork : IUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ISession CurrentSession { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateUnitOfWork : INHibernateUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> ITransaction _transaction;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isDisposed;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ISessionSource _source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isInitialized;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateUnitOfWork(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _source = source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Initialize()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        CurrentSession = _source.CreateSession();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isInitialized = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISession CurrentSession { get; <span style="color: #0000ff">private</span> set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Commit()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> begin_new_transaction()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_transaction != <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction = CurrentSession.BeginTransaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Rollback()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_not_currently_be_disposed()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ObjectDisposedException(GetType().Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_be_initialized_first()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (!_isInitialized) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> InvalidOperationException(<span style="color: #006080">&quot;Must initialize (call Initialize()) on NHibernateUnitOfWork before commiting or rolling back&quot;</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Dispose()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed || !_isInitialized) <span style="color: #0000ff">return</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        CurrentSession.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isDisposed = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ENTITY Load&lt;ENTITY&gt;(Guid id)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateRepository : IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> INHibernateUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateRepository(INHibernateUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.SaveOrUpdate(entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ENTITY Load&lt;ENTITY&gt;(Guid id) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Load&lt;ENTITY&gt;(id);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;() <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;().Where(whereQuery.Expression);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(entity);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var query = String.Format(<span style="color: #006080">&quot;from {0}&quot;</span>, <span style="color: #0000ff">typeof</span>(ENTITY).Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(query);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>DomainEntity</h3>

<p>Note: I put both of these classes in my Core.Domain namespace, also XML comments are in Source Repository</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DomainEntity : IEquatable&lt;DomainEntity&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> Guid ID { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">bool</span> Equals(DomainEntity other)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> other.ID.Equals(ID);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> Equals(<span style="color: #0000ff">object</span> obj)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (obj.GetType() != GetType()) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals((DomainEntity)obj);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> GetHashCode()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> ID.GetHashCode();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> ==(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> !=(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> !Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IDomainQuery</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IDomainQuery&lt;ENTITY&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    Expression&lt;Func&lt;ENTITY, <span style="color: #0000ff">bool</span>&gt;&gt; Expression { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>FubuSamplePersistenceModel</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Persistence</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FubuSamplePersistenceModel : PersistenceModel</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> FubuSamplePersistenceModel()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            addMappingsFromThisAssembly();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now that we have all the puzzle pieces in place we need to hook them up and make them do some work. Open up FubuSampleWebRegistry </p>

<p>And add the following code:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> configure()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSourceConfiguration&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;SQLServerSessionSourceConfiguration&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSource&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.ConstructedBy(ctx =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                                     ctx.GetInstance&lt;ISessionSourceConfiguration&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                                         .CreateSessionSource(<span style="color: #0000ff">new</span> FubuSamplePersistenceModel()));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;IUnitOfWork&gt;().TheDefault.Is.ConstructedBy(ctx =&gt; ctx.GetInstance&lt;INHibernateUnitOfWork&gt;());</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;INHibernateUnitOfWork&gt;().CacheBy(InstanceScope.Hybrid)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;NHibernateUnitOfWork&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;IRepository&gt;().TheDefault.Is.OfConcreteType&lt;NHibernateRepository&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>With a little luck and all of our mad skills, we should be able to talk to the database now. So we need to plumb our simple product object up to talk to NHibernate. We will do this with a mapping file, and since we are using FluentNHibernate we can do this strongly typed.</p>

<p>Make the following modifications to Product: Extend DomainEntity and get rid of Id Property, it is inherited</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Product : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> virtual <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual string</span> Description { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Add mapping file for Product:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Mapping</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ProductMap : ClassMap&lt;Product&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> ProductMap()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Id(e =&gt; e.ID).GeneratedBy.GuidComb();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Map(p =&gt; p.Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Map(p =&gt; p.Description);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>You may also want to setup your connection string at this point in the Web.Config file, mine looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&lt;add name=<span style="color: #006080">&quot;MYDBKEY&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=LOCALHOST;initial catalog=FubuSample;Integrated Security=SSPI;&quot;</span> providerName=<span style="color: #006080">&quot;System.Data.SqlClient&quot;</span> /&gt;</pre>
  </div>
</div>

<p>I also created an empty database in my local SQL Server instance called “FubuSample” And then I went back to SQLServerSessionSourceConfiguration and changed the value of IsNewDatabase to return true. This will create the schema for the database when the application first runs. There is a multitude of different ways that we can handle this, but this was easy for the tutorial purposes.</p>

<p>Next we will add a behavior to our core project and tell FubuMVC to apply that behavior to all or our ControllerActions.</p>

<p>Create a class named “access_the_database_through_a_unit_of_work” in your Core.Web.Behaviors namespace. The contents will look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> access_the_database_through_a_unit_of_work : IControllerActionBehavior</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> IUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> access_the_database_through_a_unit_of_work(IUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IControllerActionBehavior InsideBehavior { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IInvocationResult Result { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> OUTPUT Invoke&lt;INPUT, OUTPUT&gt;(INPUT input, Func&lt;INPUT, OUTPUT&gt; func)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> INPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> OUTPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork.Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">try</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            var output = InsideBehavior.Invoke(input, func);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Result = InsideBehavior.Result;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">return</span> output;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">catch</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">throw</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">finally</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _unitOfWork.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now to tell FubuMVC what to do, open up Global.asax.cs or your Bootstrapper, wherever you are doing the configuration of FubuMVC. In this project we are doing it in Global.asax.cs Add a new behavior to the section that starts with “x.ByDefault.EveryControllerAction” so it looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">x.ByDefault.EveryControllerAction(d =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">       d.Will&lt;execute_the_result&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">       d.Will&lt;access_the_database_through_a_unit_of_work&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">   });</pre>
  </div>
</div>

<p>The last thing we will do is open up HomeController and add this code above the Index action:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">private</span> IRepository _repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> HomeController(IRepository repository)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository = repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>I also modified the Index action to go ahead and create some database values before we query for them just so we can test this thing. (I only did this for the tutorial, I swear) So modify the Index action to look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> IndexViewModel Index(IndexSetupViewModel inModel)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod1 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct1&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod1);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod2 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct2&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod2);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var outModel = <span style="color: #0000ff">new</span> IndexViewModel();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var productList = _repository.Query&lt;Product&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    outModel.Products = productList.ToList().Select(x =&gt; <span style="color: #0000ff">new</span> ProductDisplay(x));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">return</span> outModel;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now with you should be able to run your web app and browse to /home and all this will be executed and you will be on your way to creating a whole bunch of persist able objects for use with your FubuMVC app.</p>

<p>As always, feedback is welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>What I Really, Really, Really Like About Using Fluent NHibernate</title>
		<link>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-i-really-really-really-like-about-using-fluent-nhibernate</link>
		<comments>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 00:32:10 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/</guid>
		<description><![CDATA[Reusable Mappings Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files: Catalog: &#60;class name="Catalog" table="`Catalog`" optimistic-lock="version"&#62; &#60;id name="Id" column="Id" type="Guid"&#62; &#60;generator class="guid.comb" /&#62; &#60;/id&#62; &#60;version column="Version" name="Version" /&#62; &#60;property name="Name" length="100" type="String"&#62; &#60;column [...]]]></description>
			<content:encoded><![CDATA[<h3>Reusable Mappings</h3>
Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files:
<h4>Catalog:</h4>
<pre class="csharpcode">  <span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="Catalog"</span> <span class="attr">table</span><span class="kwrd">="`Catalog`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>
    ...
  <span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategory:</h4>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="CatalogCategory"</span> <span class="attr">table</span><span class="kwrd">="`CatalogCategory`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>

   ...

<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Suppose we have the luxury to choose how we deal with the database in our project. I do realize that's not always feasible because at some point in our careers, we've all seen what database masochism can do. Anyway, if would be making the shots, I would like to use a surrogate key for every table and I would recommend sequential GUIDs (called <a href="http://www.informit.com/articles/article.aspx?p=25862" target="_blank">COMBs</a>) for that.

The mapping of the classes shown above both have the same configuration for the <em>Id</em> and <em>Version</em> properties. These properties typically live in a <em>DomainEntity</em> base class of some sort because we don't want to repeat that tedious code of putting those in every entity of our domain over and over again. It would be nice if we could somehow do the same for the NHibernate mapping files, which we can't (or at least, not that I know of).

Using <a href="http://fluentnhibernate.org/" target="_blank">Fluent NHibernate</a> we can create an abstract mapping class for our <em>DomainEntity</em> from which we derive all entity mapping classes. The following code would give us the same result as the XML mapping files shown above:
<h4>DomainEntityMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> DomainEntityMapping&lt;TDomainEntity&gt; :
ClassMap&lt;TDomainEntity&gt; <span class="kwrd">where</span> TDomainEntity : DomainEntity
{
    <span class="kwrd">protected</span> DomainEntityMapping()
    {
        Id(entity =&gt; entity.Id, <span class="str">"Id"</span>)
            .GeneratedBy.GuidComb();
        OptimisticLock.Version();
        Version(entity =&gt; entity.Version)
            .TheColumnNameIs(<span class="str">"Version"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogMapping : DomainEntityMapping&lt;Catalog&gt;
{
  <span class="kwrd">public</span> CatalogMapping()
  {
      Map(catalog=&gt; catalog.Name, <span class="str">"Name"</span>);
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategoryMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogCategoryMapping : DomainEntityMapping&lt;CatalogCategory&gt;
{
    <span class="kwrd">public</span> CatalogCategoryMapping()
    {
        Map(category=&gt; category.Description, <span class="str">"Name"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

This way,  I can specify the mapping configuration for <em>Id</em> and <em>Version</em> in a single place which is really nice.
<h3>Mapping Tests</h3>
This is probably my most favorite feature of Fluent NHibernate. Given the fluent mapping configuration for the Category class shown earlier, the following test checks whether this mapping is valid or not:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> When_verifying_the_class_mapping_of_a_catalog
    : NHibernateSpecification
{
  [Test]
  <span class="kwrd">public</span> <span class="kwrd">void</span> Then_a_catalog_object_should_be_persistable()
  {
      <span class="kwrd">new</span> PersistenceSpecification&lt;Catalog&gt;(Session)
          .VerifyTheMappings();
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Running this test results in the following SQL statements being executed to an in-memory SQLite database:

NHibernate: INSERT INTO "Catalog" (Version, Name, Id) VALUES (@p0, @p1, @p2); @p0 = '1', @p1 = '', @p2 = 'c52126cb-f11e-47e4-a481-9bc600134d39'
NHibernate: SELECT catalog0_.Id as Id1_0_, catalog0_.Version as Version1_0_, catalog0_.Name as Name1_0_ FROM "Catalog" catalog0_ WHERE catalog0_.Id=@p0; @p0 = 'c52126cb-f11e-47e4-a481-9bc600134d39'

The following code shows the NHibernateSpecification base class for all my database tests. The code in this class deals with setting up the in-memory SQLite database and building the required schema based on the mappings. The Fluent NHibernate framework gives some really nice support for this as well:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> NHibernateSpecification
    : Specification
{
    <span class="kwrd">protected</span> ISession Session { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Establish_context()
    {
        var config = <span class="kwrd">new</span> SQLiteConfiguration()
            .InMemory()
            .ShowSql()
            .ToProperties();

        var sessionSource = <span class="kwrd">new</span> SessionSource(config,
            <span class="kwrd">new</span> RetailerPersistenceModel());

        Session = sessionSource.CreateSession();
        sessionSource.BuildSchema(Session);

        ProvideInitialData(Session);

        Session.Flush();
        Session.Clear();
   }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Dispose_context()
    {
        <span class="kwrd">if</span>(<span class="kwrd">null</span> != Session)
        {
            Session.Dispose();
            Session = <span class="kwrd">null</span>;
        }
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Fluent NHibernate really lowers the barrier for configuring NHibernate, which is a really big thing in my book. I must admit that I was a bit sceptical at first, but now I noticed that I'm having a hard time going back to the standard XML mapping configuration of NHibernate itself. As Fluent NHibernate is still in an early development stage, some issues can come up. But my personal experience so far is that the <a href="http://groups.google.com/group/fluent-nhibernate?hl=en" target="_blank">user group</a> is very responsive.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Stored Procedure Patterns</title>
		<link>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-futuret-should-be-in-your-future</link>
		<comments>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:50:24 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/</guid>
		<description><![CDATA[Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature [...]]]></description>
			<content:encoded><![CDATA[<p>Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature <a href="http://davybrion.com/blog/2009/01/nhibernate-and-future-queries/">here</a> so I am not going to rehash that. What I am going to focus on here if the why.</p>  <p>Building a UI can be a very database intensive operation in terms of the number of separate calls required. And for each one of those calls the time taken to actually execute the query can be a small compared to the duration of the roundtrip to request and fetch the data. The obvious solution is some type of batching and with Future&lt;T&gt; this happens automagically without any change to the consuming logic (for databases that support it). Let me restate that, you get a huge performance benefit with only a simple change to your query methods in the Repository. So why aren’t you using it?</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; NHibernate</title>
	<atom:link href="http://elegantcode.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NHibernate, HttpModules, ASP.NET JSON Web Services and Database Transactions</title>
		<link>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions</link>
		<comments>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:08:03 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/</guid>
		<description><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play: An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor ) NHibernate The use [...]]]></description>
			<content:encoded><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play:
<ol>
	<li>An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor )</li>
	<li>NHibernate</li>
	<li>The use of an HttpModule to implement the <a href="https://www.hibernate.org/43.html">Open Session in View</a> pattern and provide a Unit of Work implementation (one ISession per HTTP request)</li>
	<li>ASP.NET web services returning JSON (might well be a problem if the result was XML as well)</li>
</ol>
In an “old fashioned” Web Forms scenario everything works perfectly. If some unexpected event occurs during a postback then an Exception is thrown and it bubbles up to the HttpModule which rolls back the transaction.

However with a JSON web service (we’re not using WCF so we just have an System.Web.Services.WebService flagged with a ScriptService attribute) when an unexpected event occurs the magic of the framework catches it and the ScriptMethod simply returns the Exception converted to JSON. This was being correctly handled in our UI but because an Exception is not thrown so the HttpModule thinks that everything is OK and incorrectly commits our transaction. And because processing didn’t necessarily finish and our data could therefore get into an inconsistent state (which is even worse than a plain wrong state).

In order to work around this feature I hooked into the EndRequest event in the HttpModule and check the StatusCode of the Repsonse. Here’s the method that I created:
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">bool</span> IsAjaxError()
{
  <span class="kwrd">return</span> HttpContext.Current.Response.StatusCode == 500 &amp;&amp; HttpContext.Current.Request.RawUrl.Contains(<span class="str">".asmx"</span>);
}</pre>
<!--.csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Obviously if an error is detected I can take the appropriate action. So if you’re using the above combination of technologies be careful because what you think is happening and what is happening might be two entirely different things.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database Change Management with Tarantino</title>
		<link>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=database-change-management-with-tarantino</link>
		<comments>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 10:17:12 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/</guid>
		<description><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard. Why [...]]]></description>
			<content:encoded><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard.
<h2>Why do we need DB Change management</h2>
Okay then Mr. Developer (or Mrs.), Why do we need source control? We need database change management for the same reason we need source control. To help manage the code and dependencies our applications rely on.
<h4>Recipes for Disaster:</h4>
<blockquote>Team of developers all working/developing off of a central database</blockquote>
<blockquote>Team of developers working/developing off of local database, with no way to keep changes in sync.</blockquote>
So what’s the problem with these recipes? As new features are developed for an application it is very likely that the database will need schema changes to support these features. These could be as simple as new tables or adding a column. If we have schema changes, we have to make sure that the code base for the app is deployed concurrently with the schema changes.

When deploying software to production, code files and libraries can usually be overwritten with a new version. Databases however must be updated intelligently.
<h2>So we’ve got a problem, do we have a solution?</h2>
Enter Tarantino. I was talking with Jarod F. and he said he had heard some stuff about it, but had never used it so I started digging around and found enough pieces to get it working. So first thing I did was head over to the <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino project</a> site. Starting reading around and then i did a little googling and managed to dig up enough information to really wet my appetite and to get it working. Then Eric Hexter was kind enough to give me some time via IM and cleared up several areas for me.

Before I get into the specifics of how to set all this up, I want to explain how Tarantino finds and keeps changes in sync.
<ol>
	<li>Tarantino is a forward only change management system, you can’t roll back.</li>
	<li>If you use Tarantino, you can’t go make changes by hand manually</li>
	<li>Tarantino uses RedGate SQL Compare if you want, to compare DB Schemas</li>
	<li>Tarantino makes a table in every DB it touches to keep track of what scripts have been executed on that DB</li>
</ol>
The system is based on a set of conventions which allow incremental changes to the database schema. The conventions are to create two subdirectories in your database scripts folder. Create– this is where your inital schema change scripts go. Update – this is where your change scripts should be placed. They should be named with the following convention ####SCRIPTNAME.sql where #### is the script number with leading zeros. This will ensure the first script 0001_first_schema_change.sql would be executed first. There is a third folder, ExistingSchema, that you will need if you are going to start using Tarantino on an app that is already in development or production.

There are two ways to use Tarantino, command line, and as a NAnt task. I will be showing the NAnt task method, as it has the most examples available on the net.
<h4>Project Setup</h4>
So what I have done, is created a very simple project. A class library with a Fluent NHibernate configuration, a test fixture to create the database schema, and two simple entities which I have listed below:
<div class="csharpcode">
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Order</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> OrderNumber { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> PONumber { get; set; }</pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;OrderItem&gt; OrderItems { get; set;}</pre>
<pre></pre>
<pre class="alt">   <span class="kwrd">public</span> Order()</pre>
<pre>   {</pre>
<pre class="alt">       OrderItems = <span class="kwrd">new</span> List&lt;OrderItem&gt;();</pre>
<pre>   }</pre>
<pre class="alt">}</pre>
<pre></pre>
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> OrderItem</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> ItemName { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Description { get; set; }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

and my NHibernate and Test Fixture setup:
<div class="csharpcode">
<pre class="alt">[TestFixture]</pre>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> DB_Setup_Fixture</pre>
<pre class="alt">{</pre>
<pre>    <span class="kwrd">private</span> Configuration _configuration;</pre>
<pre class="alt"></pre>
<pre>    [TestFixtureSetUp]</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> FixtureSetup()</pre>
<pre>    {</pre>
<pre class="alt"></pre>
<pre>        _configuration = Fluently.Configure()</pre>
<pre class="alt">            .Database(MsSqlConfiguration.MsSql2008</pre>
<pre>                          .ConnectionString(c =&gt;</pre>
<pre class="alt">                                            c.FromConnectionStringWithKey(<span class="str">"testData"</span>))</pre>
<pre>                          .UseReflectionOptimizer()</pre>
<pre class="alt">                          .ShowSql())</pre>
<pre>            .Mappings(m =&gt;</pre>
<pre class="alt">                      m.AutoMappings.Add(AutoMap.AssemblyOf&lt;Order&gt;()</pre>
<pre>                                             .Where(x =&gt; x.Namespace.EndsWith(<span class="str">"Domain"</span>))))</pre>
<pre class="alt">            .BuildConfiguration();</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre></pre>
<pre class="alt">    [Test, Explicit, Category(<span class="str">"DBSetup"</span>)]</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">void</span> Create_DB_Schema()</pre>
<pre class="alt">    {</pre>
<pre>        var exporter = <span class="kwrd">new</span> SchemaExport(_configuration);</pre>
<pre class="alt">        exporter.Create(<span class="kwrd">true</span>, <span class="kwrd">true</span>);</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --> <!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<h5>The one thing to make note of here, is that my test is set to explicit, and I gave it a category of “DBSetup”. This will be important once we start configuring NAnt and Tarantino.</h5>
So now we have a very simple application that we can use to play around with Tarantino. To get Tarantino working, I am using the modified NAnt files that CodeCampServer is using. I just copied over their NAnt folder they have checked into source control. There are two NAnt build files that are required to use Tarantino. Disclaimer: I am a total NAnt noob, this is not my usual build utility, but I figured out enough to make it work.
<h4>Common.build</h4>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database change management --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="rebuildDatabase"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="updateDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Update"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Create"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="dropDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Drop"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="attr">failonerror</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="manageSqlDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="${action}"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="${database.integrated}"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}"</span></pre>
<pre class="alt">      <span class="attr">username</span><span class="kwrd">="${database.username}"</span></pre>
<pre>      <span class="attr">password</span><span class="kwrd">="${database.password}"</span></pre>
<pre class="alt">        <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${action != 'Drop'}"</span><span class="kwrd">&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Current Database Version: ${usdDatabaseVersion}"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt"></pre>
<pre>  <span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">language</span><span class="kwrd">="C#"</span> <span class="attr">prefix</span><span class="kwrd">="migration"</span> <span class="kwrd">&gt;</span></pre>
<pre class="alt">    &lt;references&gt;</pre>
<pre>      &lt;include name=<span class="str">"System.IO.dll"</span> /&gt;</pre>
<pre class="alt">    &lt;/references&gt;</pre>
<pre>    &lt;code&gt;</pre>
<pre class="alt">      &lt;![CDATA[</pre>
<pre>              [Function(<span class="str">"next-migration-number"</span>)]</pre>
<pre class="alt">              <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> NextMigration(<span class="kwrd">string</span> path  ) {</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">string</span>[] files = System.IO.Directory.GetFiles(path);</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">int</span> count=1;</pre>
<pre></pre>
<pre class="alt">                    <span class="kwrd">if</span>(files.Length &gt; 0)</pre>
<pre>                    {</pre>
<pre class="alt">                        <span class="kwrd">string</span> filename = System.IO.Path.GetFileName(files[files.Length-1]);</pre>
<pre></pre>
<pre class="alt">                        <span class="kwrd">try</span></pre>
<pre>                        {</pre>
<pre class="alt">                            count = Convert.ToInt32(filename.Substring(0, 4));</pre>
<pre>                            count++;</pre>
<pre class="alt"></pre>
<pre>                            <span class="kwrd">if</span>(count%2 == 0)</pre>
<pre class="alt">                                count++;</pre>
<pre>                        }</pre>
<pre class="alt">                        <span class="kwrd">catch</span></pre>
<pre>                        {</pre>
<pre class="alt"></pre>
<pre>                        }</pre>
<pre class="alt">                    }</pre>
<pre>                  <span class="kwrd">return</span> <span class="kwrd">string</span>.Format(<span class="str">"{0:0000}"</span>, count);</pre>
<pre class="alt">              }</pre>
<pre>            ]]&gt;</pre>
<pre class="alt">    &lt;/code&gt;</pre>
<pre>  <span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

This file sets up most of the plumbing that hooks up the Tarantino NAnt targets for use in the nant.build file
<h4>NAnt.build</h4>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="rem">&lt;!--EXTERNAL_PROPERTIES: usdDatabaseVersion, CCNetLabel--&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">name</span><span class="kwrd">="TarantinoSample"</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre>      <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Initialization --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">buildfile</span><span class="kwrd">="common.build"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">loadtasks</span> <span class="attr">assembly</span><span class="kwrd">="lib\nant\Tarantino.Nant.Tasks.dll"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- Master Properties --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="company.name"</span> <span class="attr">value</span><span class="kwrd">="Tarantino Test"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="assembly.unittests"</span> <span class="attr">value</span><span class="kwrd">="TarantinoSample.dll"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- Version settings --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="project.config"</span> <span class="attr">value</span><span class="kwrd">="debug"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Folder references --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.solution"</span> <span class="attr">value</span><span class="kwrd">="src"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.build"</span> <span class="attr">value</span><span class="kwrd">="build"</span> <span class="attr">dynamic</span><span class="kwrd">="true"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Compilation settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="nant.settings.currentframework"</span> <span class="attr">value</span><span class="kwrd">="net-3.5"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="file.solution"</span> <span class="attr">value</span><span class="kwrd">="${dir.solution}/${project::get-name()}.sln"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database migration settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.script.directory"</span> <span class="attr">value</span><span class="kwrd">="dbChangeScripts"</span> <span class="kwrd">/&gt;</span></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.server"</span> <span class="attr">value</span><span class="kwrd">="localhost"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.name"</span> <span class="attr">value</span><span class="kwrd">="OrderData"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.integrated"</span> <span class="attr">value</span><span class="kwrd">="true"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.username"</span> <span class="attr">value</span><span class="kwrd">="dbuser"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.password"</span> <span class="attr">value</span><span class="kwrd">="P@ssword1"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Database Change Management Targets --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="reset-database"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">nunit2</span> <span class="attr">failonerror</span><span class="kwrd">="true"</span> <span class="attr">verbose</span><span class="kwrd">="true"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">formatter</span> <span class="attr">type</span><span class="kwrd">="Plain"</span> <span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">test</span> <span class="attr">assemblyname</span><span class="kwrd">="${dir.solution}/bin/${project.config}/${assembly.unittests}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">                <span class="kwrd">&lt;</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre>                    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">name</span><span class="kwrd">="DBSetup"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">                    <span class="kwrd">&lt;</span><span class="html">exclude</span> <span class="attr">name</span><span class="kwrd">="*"</span><span class="kwrd">/&gt;</span></pre>
<pre>                <span class="kwrd">&lt;/</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;/</span><span class="html">test</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">nunit2</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="db-migration"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">delete</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span>  <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="reset-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="create-versioned-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="migrationScriptName"</span> <span class="attr">value</span><span class="kwrd">="${migration::next-migration-number(database.script.directory+'/Update')}_AutoGeneratedMigration.sql"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">exec</span></pre>
<pre>              <span class="attr">program</span><span class="kwrd">="${redgate.exe}"</span></pre>
<pre class="alt">              <span class="attr">commandline</span><span class="kwrd">="/f /v /server1:${database.server} /server2:${database.server} /database1:${database.name} /database2:${database.name}Versioned /scriptfile:${database.script.directory}/Update/_New_Script.sql /exclude:Table:usd_AppliedDatabaseScript"</span></pre>
<pre>              <span class="attr">resultproperty</span><span class="kwrd">="execReturnCode"</span></pre>
<pre class="alt">              <span class="attr">failonerror</span> <span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>          <span class="kwrd">&lt;</span><span class="html">fail</span> <span class="attr">if</span><span class="kwrd">="${execReturnCode != '0' and execReturnCode != '63'}"</span> <span class="attr">message</span><span class="kwrd">="Redgate Compare Failed!"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql') == false}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">=""</span><span class="kwrd">/&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="---------------------"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="No Migration Required"</span><span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql')}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">move</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span> <span class="attr">tofile</span><span class="kwrd">="${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Created Migration File ${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">          <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="create-versioned-database"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">tstamp</span><span class="kwrd">&gt;&lt;/</span><span class="html">tstamp</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">action</span><span class="kwrd">="Drop"</span></pre>
<pre class="alt">            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre>            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre class="alt">            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre>            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre class="alt">            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="Create"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre class="alt">            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre>            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Inside this file there are a few pieces that need to be configured for use in your own app, these are all listed under “Master Properties”
<h4>Change script creation</h4>
So without going into too many details, what I have done is created my initial schema using my Unit Test. From here I could script this out to a text file, but what I did was use SQL Compare, comparing my initial schema to an empty database. This gave me my initial SQL script that creates my database.

Just to verify that my initial schema creation script is good, I will have Tarantino create the Versioned database for me using this command:
<blockquote>lib\nant\nant.exe –buildfile:nant.build create-versioned-database</blockquote>
Tarantino then looks in the ExistingSchema folder, runs any scripts found there, then does the same thing for the Updates folder.

In order to test out the change management features, I made a small change to my domain, adding a decimal of cost to the OrderItem class should do the trick. Rebuild my solution, then back to the command line to run my “Create Database Migration Script.bat” file. This file is just running the NAnt task.

What happens then, is NAnt goes out and runs my DBSetup test that is in my project. Which recreates the “OrderData” database. Tarantino then creates the “Versioned” database, then uses SQL Compare to find the changes between the two, and creates a file in the update folder for me named, <strong>0001_AutoGeneratedMigration.sql</strong> <span style="color: #ff0000;">NOTE: It is important to rename this file before it is committed to source control, or before you apply these changes to a database. Tarantino tracks the scripts that have been run by filename.</span>
<h4>Push to Production or Dev</h4>
So I have mentioned two databases so far in this post, OrderData and OrderDataVersioned. These two databases contain no data, that’s not to say that they couldn’t, if you had scripts that put it in. But that is not what they were designed to do. Our app.config is pointing to OrderData, so when we ask NHibernate to build the schema, it drops all the tables and recreates them, losing all data. OrderDataVersioned, is Tarantinos database that is used to keep changes in sync. In our development environment, every dev has a third database, we would call OrderDataProd for instance. This database could have sample data and other information in it. Let’s say, we don’t want to lose our data in this DB so what we do is let Tarantino handle the first two databases, then we deploy our schema changes to our development database. We use this same technique to push changes to production, after backups of course.

We can do this easily with the following command:
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build updateDatabase –D:database.name=OrderDataProd</span></blockquote>
Now when I tried to run this, my database OrderDataProd did not exist, so I went to SQL manager, created it, and then ran the command again, only to be slammed with another error. Since the database existed, Tarantino did not run the create schema script, only the update, which created another error. The next command will create the database I want, by executing the schema creation script, then applying any updates to it.
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build createDatabase –D:database.name=OrderDataProd</span></blockquote>
Bingo! I now have my 3rd database that I can dump data into and start developing on top of. Just to be sure that we can create another change script, and deploy it to this database, I will modify the domain again, by adding a quantity to the OrderItem object. I will leave this step up to you to try out.
<h4>Source Code</h4>
I know the blog formatting sometimes jacks up code samples a lot so I am making all the source for this post available two ways:
<ul>
	<li><a href="http://elegantcode.googlecode.com/files/TarantinoTest.zip" target="_blank">Zip File Download</a></li>
	<li><a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/RyanKelley/TarantinoTest" target="_blank">Elegant Code SVN Repository</a></li>
</ul>
<h3>Information Sources</h3>
<a href="http://www.lostechies.com/blogs/hex/default.aspx" target="_blank">Eric Hexter</a> – Huge Help

<a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a>

<a href="http://jeffreypalermo.com/blog/tarantino-project-rsquo-s-database-migrations-screencast/" target="_blank">Palermo and Hexter Screencast on Tarantino</a>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why Future&lt;T&gt; should be in your future</title>
		<link>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-futuret-should-be-in-your-future</link>
		<comments>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:50:24 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/</guid>
		<description><![CDATA[Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature [...]]]></description>
			<content:encoded><![CDATA[<p>Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature <a href="http://davybrion.com/blog/2009/01/nhibernate-and-future-queries/">here</a> so I am not going to rehash that. What I am going to focus on here if the why.</p>  <p>Building a UI can be a very database intensive operation in terms of the number of separate calls required. And for each one of those calls the time taken to actually execute the query can be a small compared to the duration of the roundtrip to request and fetch the data. The obvious solution is some type of batching and with Future&lt;T&gt; this happens automagically without any change to the consuming logic (for databases that support it). Let me restate that, you get a huge performance benefit with only a simple change to your query methods in the Repository. So why aren’t you using it?</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate 3.0 QueryOver</title>
		<link>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-3-0-queryover</link>
		<comments>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 19:23:53 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/</guid>
		<description><![CDATA[One of the personal reasons that I had for co-founding Guild 3 was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in Avoiding (Or Recovering From) Burnout. For me the age old adage of “a change is as good as a rest” [...]]]></description>
			<content:encoded><![CDATA[One of the personal reasons that I had for co-founding <a href="http://guild3.com">Guild 3</a> was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in <a href="http://davybrion.com/blog/2009/09/avoiding-or-recovering-from-burnout/">Avoiding (Or Recovering From) Burnout</a>. For me the age old adage of “a change is as good as a rest” has proven to be an extremely successful strategy.

One of the things that I had stopped doing was keeping an eye on various OSS projects to see what was on the horizon. Yesterday I (finally!) started to experiment with NHibernate 3.0. The first thing that caught my eye was <a href="http://fabiomaulo.blogspot.com/2009/06/criteria-on-nh300.html">QueryOver</a>. As Fabio explained there are a <a href="http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html">lot of different ways of executing queries in NH</a>. Certainly in the pre-LINQ days ICriteria was the predominantly recommended option because it has elements of type safety to it and its fluent-ish API broke everything down into small pieces and avoided string concatenation hell.

QueryOver is fluent a layer on top of ICriteria. It looks very LINQesque but it’s a very different animal, not least of all because there are some concepts in ICriteria that do not have a LINQ equivalent (caching etc.). In the short to medium term I suspect that it will become my de facto approach for NHibernate queries (I’ve used NHibernate LINQ and it’s great for simple queries but I’ve experienced significant issues with more complicated ones).

What I haven’t figured out yet is how, if at all, this will affect my data access testing strategy. Historically I’ve favored smoke tests that have really been doing little more than verifying that a given ICriteria query was semantically valid. Typically I only resorted to actually worrying about the results in specific cases (mainly due to the burden of maintaining test data for each possible scenario). Then again this sounds like a topic for another post doesn’t it…]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate 2.1 and Collection Event Listeners</title>
		<link>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-2-1-and-collection-event-listeners</link>
		<comments>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 00:21:37 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/</guid>
		<description><![CDATA[In a previous post, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to read about it first. Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">previous post</a>, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">read about it</a> first.</p> <p>Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't provide this feature or, as in my case, the database in question does support this feature but the DBA's don't want anything to do with it? In case of a parent domain object having a collection of many child objects, you still might want to have a one-shot delete capability instead of having separate DELETE statements for each child record. </p> <p>The <a href="http://nhforge.org/blogs/nhibernate/archive/2009/07/19/welcome-nh2-1-0.aspx">newly released NHibernate 2.1</a> (congratulations to the entire team for their efforts and hard work) comes to the rescue, which introduces a couple of new event listeners that deal with collections.</p> <p>First we need an example. Suppose we are building an auction web site and the domain has a class called <em>Item</em> which in turn has a collection of <em>Bid</em>s. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Item
{
    <span class="kwrd">private</span> ISet&lt;Bid&gt; _bids;    
    <span class="kwrd">public</span> Int64 Id { get; <span class="kwrd">private</span> set; }
    
    ...
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Bid
{
    <span class="kwrd">public</span> Double Amount { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> Sting Code { get; <span class="kwrd">private</span> set; }

    ...
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>The mapping for these classes looks something like this:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Item, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Item"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Int64"</span> <span class="attr">unsaved-value</span><span class="kwrd">="-1"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">sql-type</span><span class="kwrd">="integer"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="native"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    
    ...
    
    <span class="kwrd">&lt;</span><span class="html">set</span> <span class="attr">name</span><span class="kwrd">="Bids"</span> 
         <span class="attr">access</span><span class="kwrd">="field.camelcase-underscore"</span> 
         <span class="attr">lazy</span><span class="kwrd">="false"</span> 
         <span class="attr">cascade</span><span class="kwrd">="all-delete-orphan"</span> 
         <span class="attr">inverse</span><span class="kwrd">="true"</span> 
         <span class="attr">optimistic-lock</span><span class="kwrd">="false"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">key</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">one-to-many</span> <span class="attr">class</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">set</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Bid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">column</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">type</span><span class="kwrd">="Int64"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="Code"</span> 
                      <span class="attr">column</span><span class="kwrd">="Code"</span> 
                      <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
    
    ...
    
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Just to give you a general idea of the situation here. Now suppose we want to delete a quite popular <em>Item</em> object&nbsp; which has a numerous amount of <em>Bid</em>s. Because the collection of <em>Bids</em> is mapped as <em>inverse</em>, NHibernate will remove every record for a <em>Bid</em> with a separate DELETE statement for each row. </p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1001'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1003'</span>
...
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We could solve this by creating a collection event listener. The first thing we have to do is figure out how to issue a one-shot delete instead of those separate DELETE statements. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IOneShotDeleteHandler
{   
    Type ForEntity();
    Type[] ForChildEntities();
    <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> OneShotDeleteHandlerForItem : IOneShotDeleteHandler
{
    <span class="kwrd">public</span> Type ForEntity()
    {
        <span class="kwrd">return</span> <span class="kwrd">typeof</span>(Item);
    }

    <span class="kwrd">public</span> Type[] ForChildEntities()
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span>[] { <span class="kwrd">typeof</span>(Bid) };
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity)
    {
        var item = (Item)entity;

        session.CreateQuery(<span class="str">"delete Bid where ItemId = :itemId"</span>)
            .SetInt64(<span class="str">"itemId"</span>, item.Id)
            .ExecuteUpdate();
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We created an <em>IOneShotDeleteHandler</em> interface with one implementation for the <em>Item</em> class.&nbsp; The most notable aspect of this implementation is the use of the HQL delete statement that removes all <em>Bid</em>s for a particular <em>Item</em>.</p>
<p>Next step is to create a collection event listener that implements the <em>IPreCollectionRemoveEventListener</em> interface.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEventListener
{
    <span class="kwrd">void</span> ConfigureFor(Configuration configuration);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> CollectionRemoveEventListener 
    : IPreCollectionRemoveEventListener,
      IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;
    
    <span class="kwrd">public</span> CollectionRemoveEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> OnPreRemoveCollection(
        PreCollectionRemoveEvent @<span class="kwrd">event</span>)
    {
        var affectedOwner = @<span class="kwrd">event</span>.AffectedOwnerOrNull;
        <span class="kwrd">if</span>(<span class="kwrd">null</span> == affectedOwner)
            <span class="kwrd">return</span>;

        var oneShotDeleteHandler = 
            _oneShotDeleteHandlers.SingleOrDefault(handler =&gt;
            handler.ForEntity() == affectedOwner.GetType());

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler)
            <span class="kwrd">return</span>;

        oneShotDeleteHandler
            .GiveItAShot(@<span class="kwrd">event</span>.Session, affectedOwner);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration
            .SetListener(ListenerType.PreCollectionRemove, <span class="kwrd">this</span>);
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Don't worry about the IEventListener interface. Its just there for registering all NHibernate event listeners in an IoC container.By doing so, it enables us to inject a collection of <em>IOneShotDeleteHandler</em> objects into the constructor of our event listener. When the <em>OnPreRemoveCollection</em> method is called, we simply lookup whether there's a handler available for the type of entity that's going to be deleted and give it a shot at removing its child collection in one sweep. </p>
<p>Now we only have to register this event listener:</p><pre class="csharpcode">var eventListeners = _dependencyContainer
    .ResolveAll&lt;IEventListener&gt;();
eventListeners.ForEach(eventListener =&gt; eventListener
    .ConfigureFor(configuration));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now, if we would use this 'as is', NHibernate will give us the following error:</p>
<blockquote>
<p><em>Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)</em></p></blockquote>
<p>This is due to the fact that the collection event listener does its work and deletes all <em>Bid</em>s by executing the HQL statement, but NHibernate still tries to issue a DELETE statement for each <em>Bid</em>. This is the result of the <em>all-delete-orphan</em> cascading rule we imposed in the mapping. We could reduce it to <em>save-update</em>, but then no individual DELETE statements are executed when a singe <em>Bid</em> is removed from the collection. Now what?</p>
<p>Well, we could provide a regular delete event listener that allows individual DELETE statements for <em>Bid</em> entities as long as their parent <em>Item</em> is not removed.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> DeleteEventListener : 
    DefaultDeleteEventListener,
    IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;

    <span class="kwrd">public</span> DeleteEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> DeleteEntity(
        IEventSource session, <span class="kwrd">object</span> entity, 
        EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, 
        IEntityPersister persister, ISet transientEntities)
    {
        var oneShotDeleteHandler = _oneShotDeleteHandlers
            .SingleOrDefault(handler =&gt;
                handler.ForChildEntities()
                    .Contains(entity.GetType()));

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler ||
           !IsParentAlsoDeletedIn(
                session.PersistenceContext, 
                oneShotDeleteHandler.ForEntity())
            )
        {
            <span class="kwrd">base</span>.DeleteEntity(session, entity, entityEntry, 
                              isCascadeDeleteEnabled, persister, 
                              transientEntities);
            <span class="kwrd">return</span>;
        }

        CascadeBeforeDelete(session, persister, entity, 
                            entityEntry, transientEntities);
        CascadeAfterDelete(session, persister, entity, 
                           transientEntities);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration.SetListener(ListenerType.Delete, <span class="kwrd">this</span>);
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> Boolean IsParentAlsoDeletedIn(
        IPersistenceContext persistenceContext, Type typeOfParent)
    {
        <span class="kwrd">foreach</span>(DictionaryEntry entry <span class="kwrd">in</span> 
                                    persistenceContext.EntityEntries)
        {
            <span class="kwrd">if</span>(typeOfParent != entry.Key.GetType())
                <span class="kwrd">continue</span>;
            
            var entityEntry = (EntityEntry)entry.Value;
            <span class="kwrd">if</span>(Status.Deleted == entityEntry.Status)
                <span class="kwrd">return</span> <span class="kwrd">true</span>;
        }

        <span class="kwrd">return</span> <span class="kwrd">false</span>;
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>With both event listeners registered, deleting a single Bid results in a single DELETE statement as one would expect:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <span class="kwrd">AND</span> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>and&nbsp; removing an entire <em>Item</em> now results in a one-shot delete for all <em>Bid</em>s:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0; @p0 = 2
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Make sure you use this solution for one-shot deletes wisely and only if you have to. If you can use the CASCADE DELETE foreign key constraints, then by all means, this is the preferred option. If not, only resort to this kind of solution only if you must and that you can prove that its going to give you a tremendous performance benefit. Also take a look at the <a href="http://ayende.com/Blog/2006/09/16/BatchingSupportInNHibernate.aspx">batching support</a> that NHibernate provides (at the moment only SQL Server and Oracle are supported). </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using NHibernate for Legacy Databases</title>
		<link>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-nhibernate-for-legacy-databases</link>
		<comments>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:32:11 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/</guid>
		<description><![CDATA[One of the downsides of being confronted with a shared legacy database day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides of being confronted with a <a href="http://elegantcode.com/2009/03/28/about-a-shared-database/">shared legacy database</a> day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required for your application. These extra columns are specifically there to serve those other legacy applications. Heck, to make matters even worse, there are probably some new columns added specifically for your application as well. This is the fairy tale of shared legacy databases. </p> <p>Using NHibernate in these scenarios can be challenging sometimes but its built in flexibility and extensibility really helps you to deal with those cases. The problem I ran into last week was that we needed to store a domain object into a table that had a lot more columns than were actually required for our application. If it would be possible to store null values in these columns or if they had default values configured for them in the schema, then this would not be a problem. Instead, these unnecessary columns could not store null values and had no default values associated with them. </p> <p>First option would be to make some changes to the schema of the table. Alas, no luck there because the other legacy applications that are using the same table would break. Now what?</p> <p>We needed to insert the default values ourselves, but those columns are not known by NHibernate because they are not mapped to any members of the domain object. One way to solve this, is to pollute the domain object by adding private fields that are initialized to the required default values.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainEntity
{
    <span class="rem">// Legacy fields with no purpose for the domain but required </span>
    <span class="rem">// by the database.</span>
    <span class="kwrd">private</span> Int32 _legacyField1 = 2;
    <span class="kwrd">private</span> Boolean _legacyField2 = <span class="kwrd">false</span>;
    <span class="kwrd">private</span> String _legacyField3 = <span class="str">""</span>;
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably the simplest option, but imposes a <a href="http://en.wikipedia.org/wiki/Fixing_Broken_Windows">broken window</a> as infrastructure concerns are bleeding into the domain this way. In other words, this is not a viable solution. Keeping the legacy stuff isolated as much as possible, NHibernate provides some ways to deal with this by providing an extensive extensibility model.</p>
<p>After some snooping around in the source code of NHibernate, the solution we chose for dealing with this issue is by creating a custom access strategy. The built in property access strategies are probably already well known, but its also possible to write your own access strategy by implementing the <em>IPropertyAccessor</em> interface.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainObjectAccessor : IPropertyAccessor
{
    <span class="kwrd">private</span> IEnumerable&lt;IGetter&gt; _defaultValueGetters;
    
    <span class="kwrd">public</span> SomeDomainObjectAccessor()
    {
        _defaultValueGetters = <span class="kwrd">new</span> List&lt;IGetter&gt;()
        {
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Int32&gt;(<span class="str">"LegacyColumn1"</span>, 2) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Boolean&gt;(<span class="str">"LegacyColumn2"</span>, <span class="kwrd">false</span>) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;String&gt;(<span class="str">"LegacyColumn3"</span>, 2) }
        }
    }

    <span class="kwrd">public</span> IGetter GetGetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> _defaultValueGetters        
            .Where(getter =&gt; getter.PropertyName == propertyName)        
            .SingleOrDefault();
    }
    
    <span class="kwrd">public </span>ISetter GetSetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> NoopSetter();
    }

    <span class="kwrd">public</span> Boolean CanAccessTroughReflectionOptimizer
    {
        get { <span class="kwrd">return</span> <span class="kwrd">true</span>; }
    }
}

<span class="kwrd">private</span> <span class="kwrd">class</span> DefaultValueGetter&lt;T&gt; : IGetter
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> String _propertyName;
    <span class="kwrd">private</span> T Value { get; set; }

    <span class="kwrd">public</span> DefaultValueGetter(String propertyName, T <span class="kwrd">value</span>)
    {
        _propertyName = propertyName;
        Value = <span class="kwrd">value</span>;
    }

    <span class="kwrd">public</span> Object Get(Object target)
    {
        <span class="kwrd">return</span> Value;
    }

    <span class="kwrd">public</span> Type ReturnType
    {
        get { <span class="kwrd">return</span> <span class="kwrd">typeof</span>(T); }
    }

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> _propertyName; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get
        {
            var method = <span class="kwrd">typeof</span>(BasicPropertyAccessor)              
                .GetMethod(<span class="str">"GetGetterOrNull"</span>,
                           BindingFlags.Static | BindingFlags.NonPublic);

            var result = (BasicPropertyAccessor.BasicGetter)method              
                .Invoke(<span class="kwrd">null</span>, <span class="kwrd">new</span> Object[] { GetType(), <span class="str">"Value"</span> });

            <span class="kwrd">return</span> result.Method;
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> GetForInsert(Object owner, IDictionary mergeMap,
                               ISessionImplementor session)
    {
        <span class="kwrd">return</span> Get(owner);
    }
}

<span class="kwrd">private</span> <span class="kwrd">sealed</span> <span class="kwrd">class</span> NoopSetter : ISetter
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Set(Object target, Object <span class="kwrd">value</span>)
    {}

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This simply involves a <em>getter</em> for providing default values and a dummy <em>setter</em> as we're not interested in setting any values on the domain objects. The <em>DefaultValueGetter</em> class uses a trick so that we can keep using the reflection optimizer of NHibernate. This also seems to be necessary when using NHibernate Profiler. </p>
<p>Now we only have to provide some properties in the mapping of the domain object using our custom access strategy:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Int32"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Boolean"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="String"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably not the best solution, but it does the job and prevents polluting the domain objects as a result of database quirks like these. I'm interested in hearing feedback or any better approaches. </p>
<p>Anyway, the easy extensibility of NHibernate makes it the best data access solution around. This way, one can deal with all edge case scenarios that weren't anticipated by the framework builders. </p>
<p>Till next time&nbsp;&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Sybase working to improve NHibernate support</title>
		<link>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sybase-working-to-improve-nhibernate-support</link>
		<comments>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/#comments</comments>
		<pubDate>Fri, 15 May 2009 15:19:16 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Sybase]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/</guid>
		<description><![CDATA[If you also subscribe to Chris’ feed here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 [...]]]></description>
			<content:encoded><![CDATA[<p>If you also subscribe to <a href="http://elegantcode.com/author/brandsma/feed/">Chris’ feed</a> here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 – 10 production applications using this combination of technologies.</p>  <p>But support in NHibernate for Sybase is not great. The basics work but some of the more advanced features are not implemented at all or are done poorly. Fortunately we have an excellent relationship with our vendor and have had for many years. Earlier this week they contacted me and mentioned that they will be undertaking an effort to improve their support for NHibernate and were looking for some beta testers.</p>  <p>For this effort they can be commended. It’s also telling (and significant) that OR/M usage is becoming mainstream enough for them to consider this effort worthwhile.</p>  <p><a href="http://iablog.sybase.com/paulley">Glenn</a> is going to be leading the effort (as he did for the Java version last year). If you want to influence the direction that we go then please tell us what needs to be fixed.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FubuMVC From Scratch &#8211; Part 4 Persistence</title>
		<link>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fubumvc-from-scratch-part-4-persistence</link>
		<comments>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 21:22:00 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[FubuMVC]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/</guid>
		<description><![CDATA[Previous posts in series: FubuMVC From Scratch Part 1 – Basic project structure setup FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup FubuMVC From Scratch – Part 3 (Adding View to project) Persistence is a requirement in virtually every application that we write today, and now the time has come for us [...]]]></description>
			<content:encoded><![CDATA[<h3>Previous posts in series:</h3>  <ul>   <li><a href="http://elegantcode.com/2009/01/30/setting-up-a-fubumvc-project-from-scratch/" target="_blank">FubuMVC From Scratch Part 1 – Basic project structure setup</a> </li>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-2/" target="_blank">FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup</a></li>    <ul></ul>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-3-adding-view-to-project/" target="_blank">FubuMVC From Scratch – Part 3 (Adding View to project)</a></li> </ul>  <p>Persistence is a requirement in virtually every application that we write today, and now the time has come for us to add a persistence model to our <a href="http://fubumvc.googlecode.com/" target="_blank">FubuMVC</a> app. I know I mentioned moving my samples to the FubuCart source, but I am going to go ahead and implement the persistence piece in the same project the previous articles using, FubuSample. I have also added all the code for this series to the <a href="http://code.google.com/p/fubumvc-contrib/" target="_blank">FubuMVC-Contrib</a> project</p>  <p>We will be implementing our persistence model to use NHibernate, Fluent NHibernate, Repository Pattern and UnitOfWork pattern all on top of SQL Express or standard. This will also work for sqllite and other database servers.</p>  <p>Class and Interfaces we have to implement: </p>  <ul>   <li>ISessionSourceConfiguration </li>    <li>SQLServerSessionSourceConfiguration </li>    <li>IUnitOfWork </li>    <li>INHibernateUnitOfWork </li>    <li>NHibernateUnitOfWork </li>    <li>IRepository </li>    <li>NHibernateRepository </li>    <li>DomainEntity </li>    <li>IDomainQuery </li>    <li>FubuSamplePersistenceModel </li> </ul>  <p>Note: I put the first two in the list in my Core.Config namespace, and the rest in Core.Persistence</p>  <p>Before we start you probably want to go ahead and add the following references to your core project and Web project</p>  <p><a href="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref.jpg"><img title="FubuMVC-Persist-Ref" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="409" alt="FubuMVC-Persist-Ref" src="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref-thumb.jpg" width="274" border="0" /></a> </p>  <p>Let’s just work our way down the list and get these files implemented. </p>  <h3>ISessionSourceConfiguration</h3>  <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">   <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">     <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">bool</span> IsNewDatabase { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ISessionSource CreateSessionSource(PersistenceModel model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>SQLServerSessionSourceConfiguration</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 131.67%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 263px; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SQLServerSessionSourceConfiguration : ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#region</span> Implementation of ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsNewDatabase</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        get { <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISessionSource CreateSessionSource(PersistenceModel model)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        var properties = GetProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var source = <span style="color: #0000ff">new</span> SessionSource(properties, model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        create_schema_if_it_does_not_already_exist(source);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> create_schema_if_it_does_not_already_exist(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (IsNewDatabase) source.BuildSchema();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">protected</span> IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">string</span>&gt; GetProperties()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        MsSqlConfiguration config = MsSqlConfiguration.MsSql2005;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.ConnectionString.FromConnectionStringWithKey(“MYDBKEY”);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        config.ShowSql();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.UseOuterJoin();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> config.ToProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #cc6633">#endregion</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>A quick note here: We told SQLServerSessionSourceConfiguration to use a connection string from the app settings file (Web.Config) with a key of “MYDBKEY” This will be important later.</p>

<h3>IUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IUnitOfWork : IDisposable</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">void</span> Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>INHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> INHibernateUnitOfWork : IUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ISession CurrentSession { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateUnitOfWork : INHibernateUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> ITransaction _transaction;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isDisposed;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ISessionSource _source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isInitialized;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateUnitOfWork(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _source = source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Initialize()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        CurrentSession = _source.CreateSession();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isInitialized = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISession CurrentSession { get; <span style="color: #0000ff">private</span> set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Commit()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> begin_new_transaction()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_transaction != <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction = CurrentSession.BeginTransaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Rollback()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_not_currently_be_disposed()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ObjectDisposedException(GetType().Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_be_initialized_first()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (!_isInitialized) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> InvalidOperationException(<span style="color: #006080">&quot;Must initialize (call Initialize()) on NHibernateUnitOfWork before commiting or rolling back&quot;</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Dispose()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed || !_isInitialized) <span style="color: #0000ff">return</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        CurrentSession.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isDisposed = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ENTITY Load&lt;ENTITY&gt;(Guid id)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateRepository : IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> INHibernateUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateRepository(INHibernateUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.SaveOrUpdate(entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ENTITY Load&lt;ENTITY&gt;(Guid id) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Load&lt;ENTITY&gt;(id);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;() <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;().Where(whereQuery.Expression);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(entity);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var query = String.Format(<span style="color: #006080">&quot;from {0}&quot;</span>, <span style="color: #0000ff">typeof</span>(ENTITY).Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(query);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>DomainEntity</h3>

<p>Note: I put both of these classes in my Core.Domain namespace, also XML comments are in Source Repository</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DomainEntity : IEquatable&lt;DomainEntity&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> Guid ID { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">bool</span> Equals(DomainEntity other)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> other.ID.Equals(ID);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> Equals(<span style="color: #0000ff">object</span> obj)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (obj.GetType() != GetType()) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals((DomainEntity)obj);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> GetHashCode()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> ID.GetHashCode();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> ==(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> !=(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> !Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IDomainQuery</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IDomainQuery&lt;ENTITY&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    Expression&lt;Func&lt;ENTITY, <span style="color: #0000ff">bool</span>&gt;&gt; Expression { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>FubuSamplePersistenceModel</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Persistence</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FubuSamplePersistenceModel : PersistenceModel</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> FubuSamplePersistenceModel()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            addMappingsFromThisAssembly();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now that we have all the puzzle pieces in place we need to hook them up and make them do some work. Open up FubuSampleWebRegistry </p>

<p>And add the following code:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> configure()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSourceConfiguration&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;SQLServerSessionSourceConfiguration&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSource&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.ConstructedBy(ctx =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                                     ctx.GetInstance&lt;ISessionSourceConfiguration&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                                         .CreateSessionSource(<span style="color: #0000ff">new</span> FubuSamplePersistenceModel()));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;IUnitOfWork&gt;().TheDefault.Is.ConstructedBy(ctx =&gt; ctx.GetInstance&lt;INHibernateUnitOfWork&gt;());</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;INHibernateUnitOfWork&gt;().CacheBy(InstanceScope.Hybrid)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;NHibernateUnitOfWork&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;IRepository&gt;().TheDefault.Is.OfConcreteType&lt;NHibernateRepository&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>With a little luck and all of our mad skills, we should be able to talk to the database now. So we need to plumb our simple product object up to talk to NHibernate. We will do this with a mapping file, and since we are using FluentNHibernate we can do this strongly typed.</p>

<p>Make the following modifications to Product: Extend DomainEntity and get rid of Id Property, it is inherited</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Product : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> virtual <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual string</span> Description { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Add mapping file for Product:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Mapping</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ProductMap : ClassMap&lt;Product&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> ProductMap()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Id(e =&gt; e.ID).GeneratedBy.GuidComb();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Map(p =&gt; p.Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Map(p =&gt; p.Description);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>You may also want to setup your connection string at this point in the Web.Config file, mine looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&lt;add name=<span style="color: #006080">&quot;MYDBKEY&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=LOCALHOST;initial catalog=FubuSample;Integrated Security=SSPI;&quot;</span> providerName=<span style="color: #006080">&quot;System.Data.SqlClient&quot;</span> /&gt;</pre>
  </div>
</div>

<p>I also created an empty database in my local SQL Server instance called “FubuSample” And then I went back to SQLServerSessionSourceConfiguration and changed the value of IsNewDatabase to return true. This will create the schema for the database when the application first runs. There is a multitude of different ways that we can handle this, but this was easy for the tutorial purposes.</p>

<p>Next we will add a behavior to our core project and tell FubuMVC to apply that behavior to all or our ControllerActions.</p>

<p>Create a class named “access_the_database_through_a_unit_of_work” in your Core.Web.Behaviors namespace. The contents will look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> access_the_database_through_a_unit_of_work : IControllerActionBehavior</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> IUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> access_the_database_through_a_unit_of_work(IUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IControllerActionBehavior InsideBehavior { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IInvocationResult Result { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> OUTPUT Invoke&lt;INPUT, OUTPUT&gt;(INPUT input, Func&lt;INPUT, OUTPUT&gt; func)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> INPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> OUTPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork.Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">try</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            var output = InsideBehavior.Invoke(input, func);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Result = InsideBehavior.Result;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">return</span> output;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">catch</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">throw</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">finally</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _unitOfWork.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now to tell FubuMVC what to do, open up Global.asax.cs or your Bootstrapper, wherever you are doing the configuration of FubuMVC. In this project we are doing it in Global.asax.cs Add a new behavior to the section that starts with “x.ByDefault.EveryControllerAction” so it looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">x.ByDefault.EveryControllerAction(d =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">       d.Will&lt;execute_the_result&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">       d.Will&lt;access_the_database_through_a_unit_of_work&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">   });</pre>
  </div>
</div>

<p>The last thing we will do is open up HomeController and add this code above the Index action:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">private</span> IRepository _repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> HomeController(IRepository repository)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository = repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>I also modified the Index action to go ahead and create some database values before we query for them just so we can test this thing. (I only did this for the tutorial, I swear) So modify the Index action to look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> IndexViewModel Index(IndexSetupViewModel inModel)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod1 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct1&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod1);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod2 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct2&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod2);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var outModel = <span style="color: #0000ff">new</span> IndexViewModel();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var productList = _repository.Query&lt;Product&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    outModel.Products = productList.ToList().Select(x =&gt; <span style="color: #0000ff">new</span> ProductDisplay(x));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">return</span> outModel;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now with you should be able to run your web app and browse to /home and all this will be executed and you will be on your way to creating a whole bunch of persist able objects for use with your FubuMVC app.</p>

<p>As always, feedback is welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>What I Really, Really, Really Like About Using Fluent NHibernate</title>
		<link>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-i-really-really-really-like-about-using-fluent-nhibernate</link>
		<comments>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 00:32:10 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/</guid>
		<description><![CDATA[Reusable Mappings Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files: Catalog: &#60;class name="Catalog" table="`Catalog`" optimistic-lock="version"&#62; &#60;id name="Id" column="Id" type="Guid"&#62; &#60;generator class="guid.comb" /&#62; &#60;/id&#62; &#60;version column="Version" name="Version" /&#62; &#60;property name="Name" length="100" type="String"&#62; &#60;column [...]]]></description>
			<content:encoded><![CDATA[<h3>Reusable Mappings</h3>
Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files:
<h4>Catalog:</h4>
<pre class="csharpcode">  <span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="Catalog"</span> <span class="attr">table</span><span class="kwrd">="`Catalog`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>
    ...
  <span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategory:</h4>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="CatalogCategory"</span> <span class="attr">table</span><span class="kwrd">="`CatalogCategory`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>

   ...

<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Suppose we have the luxury to choose how we deal with the database in our project. I do realize that's not always feasible because at some point in our careers, we've all seen what database masochism can do. Anyway, if would be making the shots, I would like to use a surrogate key for every table and I would recommend sequential GUIDs (called <a href="http://www.informit.com/articles/article.aspx?p=25862" target="_blank">COMBs</a>) for that.

The mapping of the classes shown above both have the same configuration for the <em>Id</em> and <em>Version</em> properties. These properties typically live in a <em>DomainEntity</em> base class of some sort because we don't want to repeat that tedious code of putting those in every entity of our domain over and over again. It would be nice if we could somehow do the same for the NHibernate mapping files, which we can't (or at least, not that I know of).

Using <a href="http://fluentnhibernate.org/" target="_blank">Fluent NHibernate</a> we can create an abstract mapping class for our <em>DomainEntity</em> from which we derive all entity mapping classes. The following code would give us the same result as the XML mapping files shown above:
<h4>DomainEntityMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> DomainEntityMapping&lt;TDomainEntity&gt; :
ClassMap&lt;TDomainEntity&gt; <span class="kwrd">where</span> TDomainEntity : DomainEntity
{
    <span class="kwrd">protected</span> DomainEntityMapping()
    {
        Id(entity =&gt; entity.Id, <span class="str">"Id"</span>)
            .GeneratedBy.GuidComb();
        OptimisticLock.Version();
        Version(entity =&gt; entity.Version)
            .TheColumnNameIs(<span class="str">"Version"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogMapping : DomainEntityMapping&lt;Catalog&gt;
{
  <span class="kwrd">public</span> CatalogMapping()
  {
      Map(catalog=&gt; catalog.Name, <span class="str">"Name"</span>);
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategoryMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogCategoryMapping : DomainEntityMapping&lt;CatalogCategory&gt;
{
    <span class="kwrd">public</span> CatalogCategoryMapping()
    {
        Map(category=&gt; category.Description, <span class="str">"Name"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

This way,  I can specify the mapping configuration for <em>Id</em> and <em>Version</em> in a single place which is really nice.
<h3>Mapping Tests</h3>
This is probably my most favorite feature of Fluent NHibernate. Given the fluent mapping configuration for the Category class shown earlier, the following test checks whether this mapping is valid or not:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> When_verifying_the_class_mapping_of_a_catalog
    : NHibernateSpecification
{
  [Test]
  <span class="kwrd">public</span> <span class="kwrd">void</span> Then_a_catalog_object_should_be_persistable()
  {
      <span class="kwrd">new</span> PersistenceSpecification&lt;Catalog&gt;(Session)
          .VerifyTheMappings();
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Running this test results in the following SQL statements being executed to an in-memory SQLite database:

NHibernate: INSERT INTO "Catalog" (Version, Name, Id) VALUES (@p0, @p1, @p2); @p0 = '1', @p1 = '', @p2 = 'c52126cb-f11e-47e4-a481-9bc600134d39'
NHibernate: SELECT catalog0_.Id as Id1_0_, catalog0_.Version as Version1_0_, catalog0_.Name as Name1_0_ FROM "Catalog" catalog0_ WHERE catalog0_.Id=@p0; @p0 = 'c52126cb-f11e-47e4-a481-9bc600134d39'

The following code shows the NHibernateSpecification base class for all my database tests. The code in this class deals with setting up the in-memory SQLite database and building the required schema based on the mappings. The Fluent NHibernate framework gives some really nice support for this as well:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> NHibernateSpecification
    : Specification
{
    <span class="kwrd">protected</span> ISession Session { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Establish_context()
    {
        var config = <span class="kwrd">new</span> SQLiteConfiguration()
            .InMemory()
            .ShowSql()
            .ToProperties();

        var sessionSource = <span class="kwrd">new</span> SessionSource(config,
            <span class="kwrd">new</span> RetailerPersistenceModel());

        Session = sessionSource.CreateSession();
        sessionSource.BuildSchema(Session);

        ProvideInitialData(Session);

        Session.Flush();
        Session.Clear();
   }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Dispose_context()
    {
        <span class="kwrd">if</span>(<span class="kwrd">null</span> != Session)
        {
            Session.Dispose();
            Session = <span class="kwrd">null</span>;
        }
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Fluent NHibernate really lowers the barrier for configuring NHibernate, which is a really big thing in my book. I must admit that I was a bit sceptical at first, but now I noticed that I'm having a hard time going back to the standard XML mapping configuration of NHibernate itself. As Fluent NHibernate is still in an early development stage, some issues can come up. But my personal experience so far is that the <a href="http://groups.google.com/group/fluent-nhibernate?hl=en" target="_blank">user group</a> is very responsive.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Stored Procedure Patterns</title>
		<link>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-3-0-queryover</link>
		<comments>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 19:23:53 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/</guid>
		<description><![CDATA[One of the personal reasons that I had for co-founding Guild 3 was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in Avoiding (Or Recovering From) Burnout. For me the age old adage of “a change is as good as a rest” [...]]]></description>
			<content:encoded><![CDATA[One of the personal reasons that I had for co-founding <a href="http://guild3.com">Guild 3</a> was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in <a href="http://davybrion.com/blog/2009/09/avoiding-or-recovering-from-burnout/">Avoiding (Or Recovering From) Burnout</a>. For me the age old adage of “a change is as good as a rest” has proven to be an extremely successful strategy.

One of the things that I had stopped doing was keeping an eye on various OSS projects to see what was on the horizon. Yesterday I (finally!) started to experiment with NHibernate 3.0. The first thing that caught my eye was <a href="http://fabiomaulo.blogspot.com/2009/06/criteria-on-nh300.html">QueryOver</a>. As Fabio explained there are a <a href="http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html">lot of different ways of executing queries in NH</a>. Certainly in the pre-LINQ days ICriteria was the predominantly recommended option because it has elements of type safety to it and its fluent-ish API broke everything down into small pieces and avoided string concatenation hell.

QueryOver is fluent a layer on top of ICriteria. It looks very LINQesque but it’s a very different animal, not least of all because there are some concepts in ICriteria that do not have a LINQ equivalent (caching etc.). In the short to medium term I suspect that it will become my de facto approach for NHibernate queries (I’ve used NHibernate LINQ and it’s great for simple queries but I’ve experienced significant issues with more complicated ones).

What I haven’t figured out yet is how, if at all, this will affect my data access testing strategy. Historically I’ve favored smoke tests that have really been doing little more than verifying that a given ICriteria query was semantically valid. Typically I only resorted to actually worrying about the results in specific cases (mainly due to the burden of maintaining test data for each possible scenario). Then again this sounds like a topic for another post doesn’t it…]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; NHibernate</title>
	<atom:link href="http://elegantcode.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NHibernate, HttpModules, ASP.NET JSON Web Services and Database Transactions</title>
		<link>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions</link>
		<comments>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:08:03 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/</guid>
		<description><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play: An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor ) NHibernate The use [...]]]></description>
			<content:encoded><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play:
<ol>
	<li>An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor )</li>
	<li>NHibernate</li>
	<li>The use of an HttpModule to implement the <a href="https://www.hibernate.org/43.html">Open Session in View</a> pattern and provide a Unit of Work implementation (one ISession per HTTP request)</li>
	<li>ASP.NET web services returning JSON (might well be a problem if the result was XML as well)</li>
</ol>
In an “old fashioned” Web Forms scenario everything works perfectly. If some unexpected event occurs during a postback then an Exception is thrown and it bubbles up to the HttpModule which rolls back the transaction.

However with a JSON web service (we’re not using WCF so we just have an System.Web.Services.WebService flagged with a ScriptService attribute) when an unexpected event occurs the magic of the framework catches it and the ScriptMethod simply returns the Exception converted to JSON. This was being correctly handled in our UI but because an Exception is not thrown so the HttpModule thinks that everything is OK and incorrectly commits our transaction. And because processing didn’t necessarily finish and our data could therefore get into an inconsistent state (which is even worse than a plain wrong state).

In order to work around this feature I hooked into the EndRequest event in the HttpModule and check the StatusCode of the Repsonse. Here’s the method that I created:
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">bool</span> IsAjaxError()
{
  <span class="kwrd">return</span> HttpContext.Current.Response.StatusCode == 500 &amp;&amp; HttpContext.Current.Request.RawUrl.Contains(<span class="str">".asmx"</span>);
}</pre>
<!--.csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Obviously if an error is detected I can take the appropriate action. So if you’re using the above combination of technologies be careful because what you think is happening and what is happening might be two entirely different things.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database Change Management with Tarantino</title>
		<link>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=database-change-management-with-tarantino</link>
		<comments>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 10:17:12 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/</guid>
		<description><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard. Why [...]]]></description>
			<content:encoded><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard.
<h2>Why do we need DB Change management</h2>
Okay then Mr. Developer (or Mrs.), Why do we need source control? We need database change management for the same reason we need source control. To help manage the code and dependencies our applications rely on.
<h4>Recipes for Disaster:</h4>
<blockquote>Team of developers all working/developing off of a central database</blockquote>
<blockquote>Team of developers working/developing off of local database, with no way to keep changes in sync.</blockquote>
So what’s the problem with these recipes? As new features are developed for an application it is very likely that the database will need schema changes to support these features. These could be as simple as new tables or adding a column. If we have schema changes, we have to make sure that the code base for the app is deployed concurrently with the schema changes.

When deploying software to production, code files and libraries can usually be overwritten with a new version. Databases however must be updated intelligently.
<h2>So we’ve got a problem, do we have a solution?</h2>
Enter Tarantino. I was talking with Jarod F. and he said he had heard some stuff about it, but had never used it so I started digging around and found enough pieces to get it working. So first thing I did was head over to the <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino project</a> site. Starting reading around and then i did a little googling and managed to dig up enough information to really wet my appetite and to get it working. Then Eric Hexter was kind enough to give me some time via IM and cleared up several areas for me.

Before I get into the specifics of how to set all this up, I want to explain how Tarantino finds and keeps changes in sync.
<ol>
	<li>Tarantino is a forward only change management system, you can’t roll back.</li>
	<li>If you use Tarantino, you can’t go make changes by hand manually</li>
	<li>Tarantino uses RedGate SQL Compare if you want, to compare DB Schemas</li>
	<li>Tarantino makes a table in every DB it touches to keep track of what scripts have been executed on that DB</li>
</ol>
The system is based on a set of conventions which allow incremental changes to the database schema. The conventions are to create two subdirectories in your database scripts folder. Create– this is where your inital schema change scripts go. Update – this is where your change scripts should be placed. They should be named with the following convention ####SCRIPTNAME.sql where #### is the script number with leading zeros. This will ensure the first script 0001_first_schema_change.sql would be executed first. There is a third folder, ExistingSchema, that you will need if you are going to start using Tarantino on an app that is already in development or production.

There are two ways to use Tarantino, command line, and as a NAnt task. I will be showing the NAnt task method, as it has the most examples available on the net.
<h4>Project Setup</h4>
So what I have done, is created a very simple project. A class library with a Fluent NHibernate configuration, a test fixture to create the database schema, and two simple entities which I have listed below:
<div class="csharpcode">
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Order</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> OrderNumber { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> PONumber { get; set; }</pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;OrderItem&gt; OrderItems { get; set;}</pre>
<pre></pre>
<pre class="alt">   <span class="kwrd">public</span> Order()</pre>
<pre>   {</pre>
<pre class="alt">       OrderItems = <span class="kwrd">new</span> List&lt;OrderItem&gt;();</pre>
<pre>   }</pre>
<pre class="alt">}</pre>
<pre></pre>
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> OrderItem</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> ItemName { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Description { get; set; }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

and my NHibernate and Test Fixture setup:
<div class="csharpcode">
<pre class="alt">[TestFixture]</pre>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> DB_Setup_Fixture</pre>
<pre class="alt">{</pre>
<pre>    <span class="kwrd">private</span> Configuration _configuration;</pre>
<pre class="alt"></pre>
<pre>    [TestFixtureSetUp]</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> FixtureSetup()</pre>
<pre>    {</pre>
<pre class="alt"></pre>
<pre>        _configuration = Fluently.Configure()</pre>
<pre class="alt">            .Database(MsSqlConfiguration.MsSql2008</pre>
<pre>                          .ConnectionString(c =&gt;</pre>
<pre class="alt">                                            c.FromConnectionStringWithKey(<span class="str">"testData"</span>))</pre>
<pre>                          .UseReflectionOptimizer()</pre>
<pre class="alt">                          .ShowSql())</pre>
<pre>            .Mappings(m =&gt;</pre>
<pre class="alt">                      m.AutoMappings.Add(AutoMap.AssemblyOf&lt;Order&gt;()</pre>
<pre>                                             .Where(x =&gt; x.Namespace.EndsWith(<span class="str">"Domain"</span>))))</pre>
<pre class="alt">            .BuildConfiguration();</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre></pre>
<pre class="alt">    [Test, Explicit, Category(<span class="str">"DBSetup"</span>)]</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">void</span> Create_DB_Schema()</pre>
<pre class="alt">    {</pre>
<pre>        var exporter = <span class="kwrd">new</span> SchemaExport(_configuration);</pre>
<pre class="alt">        exporter.Create(<span class="kwrd">true</span>, <span class="kwrd">true</span>);</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --> <!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<h5>The one thing to make note of here, is that my test is set to explicit, and I gave it a category of “DBSetup”. This will be important once we start configuring NAnt and Tarantino.</h5>
So now we have a very simple application that we can use to play around with Tarantino. To get Tarantino working, I am using the modified NAnt files that CodeCampServer is using. I just copied over their NAnt folder they have checked into source control. There are two NAnt build files that are required to use Tarantino. Disclaimer: I am a total NAnt noob, this is not my usual build utility, but I figured out enough to make it work.
<h4>Common.build</h4>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database change management --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="rebuildDatabase"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="updateDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Update"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Create"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="dropDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Drop"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="attr">failonerror</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="manageSqlDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="${action}"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="${database.integrated}"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}"</span></pre>
<pre class="alt">      <span class="attr">username</span><span class="kwrd">="${database.username}"</span></pre>
<pre>      <span class="attr">password</span><span class="kwrd">="${database.password}"</span></pre>
<pre class="alt">        <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${action != 'Drop'}"</span><span class="kwrd">&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Current Database Version: ${usdDatabaseVersion}"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt"></pre>
<pre>  <span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">language</span><span class="kwrd">="C#"</span> <span class="attr">prefix</span><span class="kwrd">="migration"</span> <span class="kwrd">&gt;</span></pre>
<pre class="alt">    &lt;references&gt;</pre>
<pre>      &lt;include name=<span class="str">"System.IO.dll"</span> /&gt;</pre>
<pre class="alt">    &lt;/references&gt;</pre>
<pre>    &lt;code&gt;</pre>
<pre class="alt">      &lt;![CDATA[</pre>
<pre>              [Function(<span class="str">"next-migration-number"</span>)]</pre>
<pre class="alt">              <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> NextMigration(<span class="kwrd">string</span> path  ) {</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">string</span>[] files = System.IO.Directory.GetFiles(path);</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">int</span> count=1;</pre>
<pre></pre>
<pre class="alt">                    <span class="kwrd">if</span>(files.Length &gt; 0)</pre>
<pre>                    {</pre>
<pre class="alt">                        <span class="kwrd">string</span> filename = System.IO.Path.GetFileName(files[files.Length-1]);</pre>
<pre></pre>
<pre class="alt">                        <span class="kwrd">try</span></pre>
<pre>                        {</pre>
<pre class="alt">                            count = Convert.ToInt32(filename.Substring(0, 4));</pre>
<pre>                            count++;</pre>
<pre class="alt"></pre>
<pre>                            <span class="kwrd">if</span>(count%2 == 0)</pre>
<pre class="alt">                                count++;</pre>
<pre>                        }</pre>
<pre class="alt">                        <span class="kwrd">catch</span></pre>
<pre>                        {</pre>
<pre class="alt"></pre>
<pre>                        }</pre>
<pre class="alt">                    }</pre>
<pre>                  <span class="kwrd">return</span> <span class="kwrd">string</span>.Format(<span class="str">"{0:0000}"</span>, count);</pre>
<pre class="alt">              }</pre>
<pre>            ]]&gt;</pre>
<pre class="alt">    &lt;/code&gt;</pre>
<pre>  <span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

This file sets up most of the plumbing that hooks up the Tarantino NAnt targets for use in the nant.build file
<h4>NAnt.build</h4>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="rem">&lt;!--EXTERNAL_PROPERTIES: usdDatabaseVersion, CCNetLabel--&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">name</span><span class="kwrd">="TarantinoSample"</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre>      <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Initialization --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">buildfile</span><span class="kwrd">="common.build"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">loadtasks</span> <span class="attr">assembly</span><span class="kwrd">="lib\nant\Tarantino.Nant.Tasks.dll"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- Master Properties --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="company.name"</span> <span class="attr">value</span><span class="kwrd">="Tarantino Test"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="assembly.unittests"</span> <span class="attr">value</span><span class="kwrd">="TarantinoSample.dll"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- Version settings --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="project.config"</span> <span class="attr">value</span><span class="kwrd">="debug"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Folder references --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.solution"</span> <span class="attr">value</span><span class="kwrd">="src"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.build"</span> <span class="attr">value</span><span class="kwrd">="build"</span> <span class="attr">dynamic</span><span class="kwrd">="true"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Compilation settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="nant.settings.currentframework"</span> <span class="attr">value</span><span class="kwrd">="net-3.5"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="file.solution"</span> <span class="attr">value</span><span class="kwrd">="${dir.solution}/${project::get-name()}.sln"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database migration settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.script.directory"</span> <span class="attr">value</span><span class="kwrd">="dbChangeScripts"</span> <span class="kwrd">/&gt;</span></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.server"</span> <span class="attr">value</span><span class="kwrd">="localhost"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.name"</span> <span class="attr">value</span><span class="kwrd">="OrderData"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.integrated"</span> <span class="attr">value</span><span class="kwrd">="true"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.username"</span> <span class="attr">value</span><span class="kwrd">="dbuser"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.password"</span> <span class="attr">value</span><span class="kwrd">="P@ssword1"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Database Change Management Targets --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="reset-database"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">nunit2</span> <span class="attr">failonerror</span><span class="kwrd">="true"</span> <span class="attr">verbose</span><span class="kwrd">="true"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">formatter</span> <span class="attr">type</span><span class="kwrd">="Plain"</span> <span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">test</span> <span class="attr">assemblyname</span><span class="kwrd">="${dir.solution}/bin/${project.config}/${assembly.unittests}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">                <span class="kwrd">&lt;</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre>                    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">name</span><span class="kwrd">="DBSetup"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">                    <span class="kwrd">&lt;</span><span class="html">exclude</span> <span class="attr">name</span><span class="kwrd">="*"</span><span class="kwrd">/&gt;</span></pre>
<pre>                <span class="kwrd">&lt;/</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;/</span><span class="html">test</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">nunit2</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="db-migration"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">delete</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span>  <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="reset-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="create-versioned-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="migrationScriptName"</span> <span class="attr">value</span><span class="kwrd">="${migration::next-migration-number(database.script.directory+'/Update')}_AutoGeneratedMigration.sql"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">exec</span></pre>
<pre>              <span class="attr">program</span><span class="kwrd">="${redgate.exe}"</span></pre>
<pre class="alt">              <span class="attr">commandline</span><span class="kwrd">="/f /v /server1:${database.server} /server2:${database.server} /database1:${database.name} /database2:${database.name}Versioned /scriptfile:${database.script.directory}/Update/_New_Script.sql /exclude:Table:usd_AppliedDatabaseScript"</span></pre>
<pre>              <span class="attr">resultproperty</span><span class="kwrd">="execReturnCode"</span></pre>
<pre class="alt">              <span class="attr">failonerror</span> <span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>          <span class="kwrd">&lt;</span><span class="html">fail</span> <span class="attr">if</span><span class="kwrd">="${execReturnCode != '0' and execReturnCode != '63'}"</span> <span class="attr">message</span><span class="kwrd">="Redgate Compare Failed!"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql') == false}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">=""</span><span class="kwrd">/&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="---------------------"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="No Migration Required"</span><span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql')}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">move</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span> <span class="attr">tofile</span><span class="kwrd">="${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Created Migration File ${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">          <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="create-versioned-database"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">tstamp</span><span class="kwrd">&gt;&lt;/</span><span class="html">tstamp</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">action</span><span class="kwrd">="Drop"</span></pre>
<pre class="alt">            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre>            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre class="alt">            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre>            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre class="alt">            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="Create"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre class="alt">            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre>            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Inside this file there are a few pieces that need to be configured for use in your own app, these are all listed under “Master Properties”
<h4>Change script creation</h4>
So without going into too many details, what I have done is created my initial schema using my Unit Test. From here I could script this out to a text file, but what I did was use SQL Compare, comparing my initial schema to an empty database. This gave me my initial SQL script that creates my database.

Just to verify that my initial schema creation script is good, I will have Tarantino create the Versioned database for me using this command:
<blockquote>lib\nant\nant.exe –buildfile:nant.build create-versioned-database</blockquote>
Tarantino then looks in the ExistingSchema folder, runs any scripts found there, then does the same thing for the Updates folder.

In order to test out the change management features, I made a small change to my domain, adding a decimal of cost to the OrderItem class should do the trick. Rebuild my solution, then back to the command line to run my “Create Database Migration Script.bat” file. This file is just running the NAnt task.

What happens then, is NAnt goes out and runs my DBSetup test that is in my project. Which recreates the “OrderData” database. Tarantino then creates the “Versioned” database, then uses SQL Compare to find the changes between the two, and creates a file in the update folder for me named, <strong>0001_AutoGeneratedMigration.sql</strong> <span style="color: #ff0000;">NOTE: It is important to rename this file before it is committed to source control, or before you apply these changes to a database. Tarantino tracks the scripts that have been run by filename.</span>
<h4>Push to Production or Dev</h4>
So I have mentioned two databases so far in this post, OrderData and OrderDataVersioned. These two databases contain no data, that’s not to say that they couldn’t, if you had scripts that put it in. But that is not what they were designed to do. Our app.config is pointing to OrderData, so when we ask NHibernate to build the schema, it drops all the tables and recreates them, losing all data. OrderDataVersioned, is Tarantinos database that is used to keep changes in sync. In our development environment, every dev has a third database, we would call OrderDataProd for instance. This database could have sample data and other information in it. Let’s say, we don’t want to lose our data in this DB so what we do is let Tarantino handle the first two databases, then we deploy our schema changes to our development database. We use this same technique to push changes to production, after backups of course.

We can do this easily with the following command:
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build updateDatabase –D:database.name=OrderDataProd</span></blockquote>
Now when I tried to run this, my database OrderDataProd did not exist, so I went to SQL manager, created it, and then ran the command again, only to be slammed with another error. Since the database existed, Tarantino did not run the create schema script, only the update, which created another error. The next command will create the database I want, by executing the schema creation script, then applying any updates to it.
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build createDatabase –D:database.name=OrderDataProd</span></blockquote>
Bingo! I now have my 3rd database that I can dump data into and start developing on top of. Just to be sure that we can create another change script, and deploy it to this database, I will modify the domain again, by adding a quantity to the OrderItem object. I will leave this step up to you to try out.
<h4>Source Code</h4>
I know the blog formatting sometimes jacks up code samples a lot so I am making all the source for this post available two ways:
<ul>
	<li><a href="http://elegantcode.googlecode.com/files/TarantinoTest.zip" target="_blank">Zip File Download</a></li>
	<li><a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/RyanKelley/TarantinoTest" target="_blank">Elegant Code SVN Repository</a></li>
</ul>
<h3>Information Sources</h3>
<a href="http://www.lostechies.com/blogs/hex/default.aspx" target="_blank">Eric Hexter</a> – Huge Help

<a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a>

<a href="http://jeffreypalermo.com/blog/tarantino-project-rsquo-s-database-migrations-screencast/" target="_blank">Palermo and Hexter Screencast on Tarantino</a>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why Future&lt;T&gt; should be in your future</title>
		<link>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-futuret-should-be-in-your-future</link>
		<comments>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:50:24 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/</guid>
		<description><![CDATA[Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature [...]]]></description>
			<content:encoded><![CDATA[<p>Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature <a href="http://davybrion.com/blog/2009/01/nhibernate-and-future-queries/">here</a> so I am not going to rehash that. What I am going to focus on here if the why.</p>  <p>Building a UI can be a very database intensive operation in terms of the number of separate calls required. And for each one of those calls the time taken to actually execute the query can be a small compared to the duration of the roundtrip to request and fetch the data. The obvious solution is some type of batching and with Future&lt;T&gt; this happens automagically without any change to the consuming logic (for databases that support it). Let me restate that, you get a huge performance benefit with only a simple change to your query methods in the Repository. So why aren’t you using it?</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate 3.0 QueryOver</title>
		<link>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-3-0-queryover</link>
		<comments>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 19:23:53 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/</guid>
		<description><![CDATA[One of the personal reasons that I had for co-founding Guild 3 was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in Avoiding (Or Recovering From) Burnout. For me the age old adage of “a change is as good as a rest” [...]]]></description>
			<content:encoded><![CDATA[One of the personal reasons that I had for co-founding <a href="http://guild3.com">Guild 3</a> was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in <a href="http://davybrion.com/blog/2009/09/avoiding-or-recovering-from-burnout/">Avoiding (Or Recovering From) Burnout</a>. For me the age old adage of “a change is as good as a rest” has proven to be an extremely successful strategy.

One of the things that I had stopped doing was keeping an eye on various OSS projects to see what was on the horizon. Yesterday I (finally!) started to experiment with NHibernate 3.0. The first thing that caught my eye was <a href="http://fabiomaulo.blogspot.com/2009/06/criteria-on-nh300.html">QueryOver</a>. As Fabio explained there are a <a href="http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html">lot of different ways of executing queries in NH</a>. Certainly in the pre-LINQ days ICriteria was the predominantly recommended option because it has elements of type safety to it and its fluent-ish API broke everything down into small pieces and avoided string concatenation hell.

QueryOver is fluent a layer on top of ICriteria. It looks very LINQesque but it’s a very different animal, not least of all because there are some concepts in ICriteria that do not have a LINQ equivalent (caching etc.). In the short to medium term I suspect that it will become my de facto approach for NHibernate queries (I’ve used NHibernate LINQ and it’s great for simple queries but I’ve experienced significant issues with more complicated ones).

What I haven’t figured out yet is how, if at all, this will affect my data access testing strategy. Historically I’ve favored smoke tests that have really been doing little more than verifying that a given ICriteria query was semantically valid. Typically I only resorted to actually worrying about the results in specific cases (mainly due to the burden of maintaining test data for each possible scenario). Then again this sounds like a topic for another post doesn’t it…]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate 2.1 and Collection Event Listeners</title>
		<link>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-2-1-and-collection-event-listeners</link>
		<comments>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 00:21:37 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/</guid>
		<description><![CDATA[In a previous post, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to read about it first. Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">previous post</a>, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">read about it</a> first.</p> <p>Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't provide this feature or, as in my case, the database in question does support this feature but the DBA's don't want anything to do with it? In case of a parent domain object having a collection of many child objects, you still might want to have a one-shot delete capability instead of having separate DELETE statements for each child record. </p> <p>The <a href="http://nhforge.org/blogs/nhibernate/archive/2009/07/19/welcome-nh2-1-0.aspx">newly released NHibernate 2.1</a> (congratulations to the entire team for their efforts and hard work) comes to the rescue, which introduces a couple of new event listeners that deal with collections.</p> <p>First we need an example. Suppose we are building an auction web site and the domain has a class called <em>Item</em> which in turn has a collection of <em>Bid</em>s. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Item
{
    <span class="kwrd">private</span> ISet&lt;Bid&gt; _bids;    
    <span class="kwrd">public</span> Int64 Id { get; <span class="kwrd">private</span> set; }
    
    ...
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Bid
{
    <span class="kwrd">public</span> Double Amount { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> Sting Code { get; <span class="kwrd">private</span> set; }

    ...
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>The mapping for these classes looks something like this:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Item, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Item"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Int64"</span> <span class="attr">unsaved-value</span><span class="kwrd">="-1"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">sql-type</span><span class="kwrd">="integer"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="native"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    
    ...
    
    <span class="kwrd">&lt;</span><span class="html">set</span> <span class="attr">name</span><span class="kwrd">="Bids"</span> 
         <span class="attr">access</span><span class="kwrd">="field.camelcase-underscore"</span> 
         <span class="attr">lazy</span><span class="kwrd">="false"</span> 
         <span class="attr">cascade</span><span class="kwrd">="all-delete-orphan"</span> 
         <span class="attr">inverse</span><span class="kwrd">="true"</span> 
         <span class="attr">optimistic-lock</span><span class="kwrd">="false"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">key</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">one-to-many</span> <span class="attr">class</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">set</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Bid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">column</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">type</span><span class="kwrd">="Int64"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="Code"</span> 
                      <span class="attr">column</span><span class="kwrd">="Code"</span> 
                      <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
    
    ...
    
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Just to give you a general idea of the situation here. Now suppose we want to delete a quite popular <em>Item</em> object&nbsp; which has a numerous amount of <em>Bid</em>s. Because the collection of <em>Bids</em> is mapped as <em>inverse</em>, NHibernate will remove every record for a <em>Bid</em> with a separate DELETE statement for each row. </p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1001'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1003'</span>
...
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We could solve this by creating a collection event listener. The first thing we have to do is figure out how to issue a one-shot delete instead of those separate DELETE statements. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IOneShotDeleteHandler
{   
    Type ForEntity();
    Type[] ForChildEntities();
    <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> OneShotDeleteHandlerForItem : IOneShotDeleteHandler
{
    <span class="kwrd">public</span> Type ForEntity()
    {
        <span class="kwrd">return</span> <span class="kwrd">typeof</span>(Item);
    }

    <span class="kwrd">public</span> Type[] ForChildEntities()
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span>[] { <span class="kwrd">typeof</span>(Bid) };
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity)
    {
        var item = (Item)entity;

        session.CreateQuery(<span class="str">"delete Bid where ItemId = :itemId"</span>)
            .SetInt64(<span class="str">"itemId"</span>, item.Id)
            .ExecuteUpdate();
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We created an <em>IOneShotDeleteHandler</em> interface with one implementation for the <em>Item</em> class.&nbsp; The most notable aspect of this implementation is the use of the HQL delete statement that removes all <em>Bid</em>s for a particular <em>Item</em>.</p>
<p>Next step is to create a collection event listener that implements the <em>IPreCollectionRemoveEventListener</em> interface.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEventListener
{
    <span class="kwrd">void</span> ConfigureFor(Configuration configuration);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> CollectionRemoveEventListener 
    : IPreCollectionRemoveEventListener,
      IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;
    
    <span class="kwrd">public</span> CollectionRemoveEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> OnPreRemoveCollection(
        PreCollectionRemoveEvent @<span class="kwrd">event</span>)
    {
        var affectedOwner = @<span class="kwrd">event</span>.AffectedOwnerOrNull;
        <span class="kwrd">if</span>(<span class="kwrd">null</span> == affectedOwner)
            <span class="kwrd">return</span>;

        var oneShotDeleteHandler = 
            _oneShotDeleteHandlers.SingleOrDefault(handler =&gt;
            handler.ForEntity() == affectedOwner.GetType());

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler)
            <span class="kwrd">return</span>;

        oneShotDeleteHandler
            .GiveItAShot(@<span class="kwrd">event</span>.Session, affectedOwner);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration
            .SetListener(ListenerType.PreCollectionRemove, <span class="kwrd">this</span>);
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Don't worry about the IEventListener interface. Its just there for registering all NHibernate event listeners in an IoC container.By doing so, it enables us to inject a collection of <em>IOneShotDeleteHandler</em> objects into the constructor of our event listener. When the <em>OnPreRemoveCollection</em> method is called, we simply lookup whether there's a handler available for the type of entity that's going to be deleted and give it a shot at removing its child collection in one sweep. </p>
<p>Now we only have to register this event listener:</p><pre class="csharpcode">var eventListeners = _dependencyContainer
    .ResolveAll&lt;IEventListener&gt;();
eventListeners.ForEach(eventListener =&gt; eventListener
    .ConfigureFor(configuration));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now, if we would use this 'as is', NHibernate will give us the following error:</p>
<blockquote>
<p><em>Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)</em></p></blockquote>
<p>This is due to the fact that the collection event listener does its work and deletes all <em>Bid</em>s by executing the HQL statement, but NHibernate still tries to issue a DELETE statement for each <em>Bid</em>. This is the result of the <em>all-delete-orphan</em> cascading rule we imposed in the mapping. We could reduce it to <em>save-update</em>, but then no individual DELETE statements are executed when a singe <em>Bid</em> is removed from the collection. Now what?</p>
<p>Well, we could provide a regular delete event listener that allows individual DELETE statements for <em>Bid</em> entities as long as their parent <em>Item</em> is not removed.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> DeleteEventListener : 
    DefaultDeleteEventListener,
    IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;

    <span class="kwrd">public</span> DeleteEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> DeleteEntity(
        IEventSource session, <span class="kwrd">object</span> entity, 
        EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, 
        IEntityPersister persister, ISet transientEntities)
    {
        var oneShotDeleteHandler = _oneShotDeleteHandlers
            .SingleOrDefault(handler =&gt;
                handler.ForChildEntities()
                    .Contains(entity.GetType()));

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler ||
           !IsParentAlsoDeletedIn(
                session.PersistenceContext, 
                oneShotDeleteHandler.ForEntity())
            )
        {
            <span class="kwrd">base</span>.DeleteEntity(session, entity, entityEntry, 
                              isCascadeDeleteEnabled, persister, 
                              transientEntities);
            <span class="kwrd">return</span>;
        }

        CascadeBeforeDelete(session, persister, entity, 
                            entityEntry, transientEntities);
        CascadeAfterDelete(session, persister, entity, 
                           transientEntities);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration.SetListener(ListenerType.Delete, <span class="kwrd">this</span>);
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> Boolean IsParentAlsoDeletedIn(
        IPersistenceContext persistenceContext, Type typeOfParent)
    {
        <span class="kwrd">foreach</span>(DictionaryEntry entry <span class="kwrd">in</span> 
                                    persistenceContext.EntityEntries)
        {
            <span class="kwrd">if</span>(typeOfParent != entry.Key.GetType())
                <span class="kwrd">continue</span>;
            
            var entityEntry = (EntityEntry)entry.Value;
            <span class="kwrd">if</span>(Status.Deleted == entityEntry.Status)
                <span class="kwrd">return</span> <span class="kwrd">true</span>;
        }

        <span class="kwrd">return</span> <span class="kwrd">false</span>;
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>With both event listeners registered, deleting a single Bid results in a single DELETE statement as one would expect:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <span class="kwrd">AND</span> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>and&nbsp; removing an entire <em>Item</em> now results in a one-shot delete for all <em>Bid</em>s:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0; @p0 = 2
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Make sure you use this solution for one-shot deletes wisely and only if you have to. If you can use the CASCADE DELETE foreign key constraints, then by all means, this is the preferred option. If not, only resort to this kind of solution only if you must and that you can prove that its going to give you a tremendous performance benefit. Also take a look at the <a href="http://ayende.com/Blog/2006/09/16/BatchingSupportInNHibernate.aspx">batching support</a> that NHibernate provides (at the moment only SQL Server and Oracle are supported). </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using NHibernate for Legacy Databases</title>
		<link>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-nhibernate-for-legacy-databases</link>
		<comments>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:32:11 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/</guid>
		<description><![CDATA[One of the downsides of being confronted with a shared legacy database day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides of being confronted with a <a href="http://elegantcode.com/2009/03/28/about-a-shared-database/">shared legacy database</a> day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required for your application. These extra columns are specifically there to serve those other legacy applications. Heck, to make matters even worse, there are probably some new columns added specifically for your application as well. This is the fairy tale of shared legacy databases. </p> <p>Using NHibernate in these scenarios can be challenging sometimes but its built in flexibility and extensibility really helps you to deal with those cases. The problem I ran into last week was that we needed to store a domain object into a table that had a lot more columns than were actually required for our application. If it would be possible to store null values in these columns or if they had default values configured for them in the schema, then this would not be a problem. Instead, these unnecessary columns could not store null values and had no default values associated with them. </p> <p>First option would be to make some changes to the schema of the table. Alas, no luck there because the other legacy applications that are using the same table would break. Now what?</p> <p>We needed to insert the default values ourselves, but those columns are not known by NHibernate because they are not mapped to any members of the domain object. One way to solve this, is to pollute the domain object by adding private fields that are initialized to the required default values.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainEntity
{
    <span class="rem">// Legacy fields with no purpose for the domain but required </span>
    <span class="rem">// by the database.</span>
    <span class="kwrd">private</span> Int32 _legacyField1 = 2;
    <span class="kwrd">private</span> Boolean _legacyField2 = <span class="kwrd">false</span>;
    <span class="kwrd">private</span> String _legacyField3 = <span class="str">""</span>;
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably the simplest option, but imposes a <a href="http://en.wikipedia.org/wiki/Fixing_Broken_Windows">broken window</a> as infrastructure concerns are bleeding into the domain this way. In other words, this is not a viable solution. Keeping the legacy stuff isolated as much as possible, NHibernate provides some ways to deal with this by providing an extensive extensibility model.</p>
<p>After some snooping around in the source code of NHibernate, the solution we chose for dealing with this issue is by creating a custom access strategy. The built in property access strategies are probably already well known, but its also possible to write your own access strategy by implementing the <em>IPropertyAccessor</em> interface.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainObjectAccessor : IPropertyAccessor
{
    <span class="kwrd">private</span> IEnumerable&lt;IGetter&gt; _defaultValueGetters;
    
    <span class="kwrd">public</span> SomeDomainObjectAccessor()
    {
        _defaultValueGetters = <span class="kwrd">new</span> List&lt;IGetter&gt;()
        {
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Int32&gt;(<span class="str">"LegacyColumn1"</span>, 2) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Boolean&gt;(<span class="str">"LegacyColumn2"</span>, <span class="kwrd">false</span>) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;String&gt;(<span class="str">"LegacyColumn3"</span>, 2) }
        }
    }

    <span class="kwrd">public</span> IGetter GetGetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> _defaultValueGetters        
            .Where(getter =&gt; getter.PropertyName == propertyName)        
            .SingleOrDefault();
    }
    
    <span class="kwrd">public </span>ISetter GetSetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> NoopSetter();
    }

    <span class="kwrd">public</span> Boolean CanAccessTroughReflectionOptimizer
    {
        get { <span class="kwrd">return</span> <span class="kwrd">true</span>; }
    }
}

<span class="kwrd">private</span> <span class="kwrd">class</span> DefaultValueGetter&lt;T&gt; : IGetter
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> String _propertyName;
    <span class="kwrd">private</span> T Value { get; set; }

    <span class="kwrd">public</span> DefaultValueGetter(String propertyName, T <span class="kwrd">value</span>)
    {
        _propertyName = propertyName;
        Value = <span class="kwrd">value</span>;
    }

    <span class="kwrd">public</span> Object Get(Object target)
    {
        <span class="kwrd">return</span> Value;
    }

    <span class="kwrd">public</span> Type ReturnType
    {
        get { <span class="kwrd">return</span> <span class="kwrd">typeof</span>(T); }
    }

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> _propertyName; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get
        {
            var method = <span class="kwrd">typeof</span>(BasicPropertyAccessor)              
                .GetMethod(<span class="str">"GetGetterOrNull"</span>,
                           BindingFlags.Static | BindingFlags.NonPublic);

            var result = (BasicPropertyAccessor.BasicGetter)method              
                .Invoke(<span class="kwrd">null</span>, <span class="kwrd">new</span> Object[] { GetType(), <span class="str">"Value"</span> });

            <span class="kwrd">return</span> result.Method;
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> GetForInsert(Object owner, IDictionary mergeMap,
                               ISessionImplementor session)
    {
        <span class="kwrd">return</span> Get(owner);
    }
}

<span class="kwrd">private</span> <span class="kwrd">sealed</span> <span class="kwrd">class</span> NoopSetter : ISetter
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Set(Object target, Object <span class="kwrd">value</span>)
    {}

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This simply involves a <em>getter</em> for providing default values and a dummy <em>setter</em> as we're not interested in setting any values on the domain objects. The <em>DefaultValueGetter</em> class uses a trick so that we can keep using the reflection optimizer of NHibernate. This also seems to be necessary when using NHibernate Profiler. </p>
<p>Now we only have to provide some properties in the mapping of the domain object using our custom access strategy:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Int32"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Boolean"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="String"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably not the best solution, but it does the job and prevents polluting the domain objects as a result of database quirks like these. I'm interested in hearing feedback or any better approaches. </p>
<p>Anyway, the easy extensibility of NHibernate makes it the best data access solution around. This way, one can deal with all edge case scenarios that weren't anticipated by the framework builders. </p>
<p>Till next time&nbsp;&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Sybase working to improve NHibernate support</title>
		<link>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sybase-working-to-improve-nhibernate-support</link>
		<comments>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/#comments</comments>
		<pubDate>Fri, 15 May 2009 15:19:16 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Sybase]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/</guid>
		<description><![CDATA[If you also subscribe to Chris’ feed here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 [...]]]></description>
			<content:encoded><![CDATA[<p>If you also subscribe to <a href="http://elegantcode.com/author/brandsma/feed/">Chris’ feed</a> here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 – 10 production applications using this combination of technologies.</p>  <p>But support in NHibernate for Sybase is not great. The basics work but some of the more advanced features are not implemented at all or are done poorly. Fortunately we have an excellent relationship with our vendor and have had for many years. Earlier this week they contacted me and mentioned that they will be undertaking an effort to improve their support for NHibernate and were looking for some beta testers.</p>  <p>For this effort they can be commended. It’s also telling (and significant) that OR/M usage is becoming mainstream enough for them to consider this effort worthwhile.</p>  <p><a href="http://iablog.sybase.com/paulley">Glenn</a> is going to be leading the effort (as he did for the Java version last year). If you want to influence the direction that we go then please tell us what needs to be fixed.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FubuMVC From Scratch &#8211; Part 4 Persistence</title>
		<link>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fubumvc-from-scratch-part-4-persistence</link>
		<comments>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 21:22:00 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[FubuMVC]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/</guid>
		<description><![CDATA[Previous posts in series: FubuMVC From Scratch Part 1 – Basic project structure setup FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup FubuMVC From Scratch – Part 3 (Adding View to project) Persistence is a requirement in virtually every application that we write today, and now the time has come for us [...]]]></description>
			<content:encoded><![CDATA[<h3>Previous posts in series:</h3>  <ul>   <li><a href="http://elegantcode.com/2009/01/30/setting-up-a-fubumvc-project-from-scratch/" target="_blank">FubuMVC From Scratch Part 1 – Basic project structure setup</a> </li>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-2/" target="_blank">FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup</a></li>    <ul></ul>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-3-adding-view-to-project/" target="_blank">FubuMVC From Scratch – Part 3 (Adding View to project)</a></li> </ul>  <p>Persistence is a requirement in virtually every application that we write today, and now the time has come for us to add a persistence model to our <a href="http://fubumvc.googlecode.com/" target="_blank">FubuMVC</a> app. I know I mentioned moving my samples to the FubuCart source, but I am going to go ahead and implement the persistence piece in the same project the previous articles using, FubuSample. I have also added all the code for this series to the <a href="http://code.google.com/p/fubumvc-contrib/" target="_blank">FubuMVC-Contrib</a> project</p>  <p>We will be implementing our persistence model to use NHibernate, Fluent NHibernate, Repository Pattern and UnitOfWork pattern all on top of SQL Express or standard. This will also work for sqllite and other database servers.</p>  <p>Class and Interfaces we have to implement: </p>  <ul>   <li>ISessionSourceConfiguration </li>    <li>SQLServerSessionSourceConfiguration </li>    <li>IUnitOfWork </li>    <li>INHibernateUnitOfWork </li>    <li>NHibernateUnitOfWork </li>    <li>IRepository </li>    <li>NHibernateRepository </li>    <li>DomainEntity </li>    <li>IDomainQuery </li>    <li>FubuSamplePersistenceModel </li> </ul>  <p>Note: I put the first two in the list in my Core.Config namespace, and the rest in Core.Persistence</p>  <p>Before we start you probably want to go ahead and add the following references to your core project and Web project</p>  <p><a href="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref.jpg"><img title="FubuMVC-Persist-Ref" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="409" alt="FubuMVC-Persist-Ref" src="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref-thumb.jpg" width="274" border="0" /></a> </p>  <p>Let’s just work our way down the list and get these files implemented. </p>  <h3>ISessionSourceConfiguration</h3>  <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">   <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">     <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">bool</span> IsNewDatabase { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ISessionSource CreateSessionSource(PersistenceModel model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>SQLServerSessionSourceConfiguration</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 131.67%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 263px; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SQLServerSessionSourceConfiguration : ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#region</span> Implementation of ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsNewDatabase</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        get { <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISessionSource CreateSessionSource(PersistenceModel model)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        var properties = GetProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var source = <span style="color: #0000ff">new</span> SessionSource(properties, model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        create_schema_if_it_does_not_already_exist(source);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> create_schema_if_it_does_not_already_exist(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (IsNewDatabase) source.BuildSchema();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">protected</span> IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">string</span>&gt; GetProperties()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        MsSqlConfiguration config = MsSqlConfiguration.MsSql2005;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.ConnectionString.FromConnectionStringWithKey(“MYDBKEY”);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        config.ShowSql();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.UseOuterJoin();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> config.ToProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #cc6633">#endregion</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>A quick note here: We told SQLServerSessionSourceConfiguration to use a connection string from the app settings file (Web.Config) with a key of “MYDBKEY” This will be important later.</p>

<h3>IUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IUnitOfWork : IDisposable</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">void</span> Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>INHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> INHibernateUnitOfWork : IUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ISession CurrentSession { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateUnitOfWork : INHibernateUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> ITransaction _transaction;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isDisposed;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ISessionSource _source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isInitialized;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateUnitOfWork(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _source = source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Initialize()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        CurrentSession = _source.CreateSession();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isInitialized = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISession CurrentSession { get; <span style="color: #0000ff">private</span> set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Commit()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> begin_new_transaction()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_transaction != <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction = CurrentSession.BeginTransaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Rollback()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_not_currently_be_disposed()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ObjectDisposedException(GetType().Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_be_initialized_first()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (!_isInitialized) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> InvalidOperationException(<span style="color: #006080">&quot;Must initialize (call Initialize()) on NHibernateUnitOfWork before commiting or rolling back&quot;</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Dispose()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed || !_isInitialized) <span style="color: #0000ff">return</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        CurrentSession.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isDisposed = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ENTITY Load&lt;ENTITY&gt;(Guid id)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateRepository : IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> INHibernateUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateRepository(INHibernateUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.SaveOrUpdate(entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ENTITY Load&lt;ENTITY&gt;(Guid id) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Load&lt;ENTITY&gt;(id);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;() <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;().Where(whereQuery.Expression);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(entity);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var query = String.Format(<span style="color: #006080">&quot;from {0}&quot;</span>, <span style="color: #0000ff">typeof</span>(ENTITY).Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(query);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>DomainEntity</h3>

<p>Note: I put both of these classes in my Core.Domain namespace, also XML comments are in Source Repository</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DomainEntity : IEquatable&lt;DomainEntity&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> Guid ID { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">bool</span> Equals(DomainEntity other)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> other.ID.Equals(ID);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> Equals(<span style="color: #0000ff">object</span> obj)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (obj.GetType() != GetType()) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals((DomainEntity)obj);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> GetHashCode()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> ID.GetHashCode();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> ==(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> !=(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> !Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IDomainQuery</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IDomainQuery&lt;ENTITY&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    Expression&lt;Func&lt;ENTITY, <span style="color: #0000ff">bool</span>&gt;&gt; Expression { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>FubuSamplePersistenceModel</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Persistence</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FubuSamplePersistenceModel : PersistenceModel</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> FubuSamplePersistenceModel()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            addMappingsFromThisAssembly();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now that we have all the puzzle pieces in place we need to hook them up and make them do some work. Open up FubuSampleWebRegistry </p>

<p>And add the following code:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> configure()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSourceConfiguration&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;SQLServerSessionSourceConfiguration&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSource&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.ConstructedBy(ctx =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                                     ctx.GetInstance&lt;ISessionSourceConfiguration&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                                         .CreateSessionSource(<span style="color: #0000ff">new</span> FubuSamplePersistenceModel()));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;IUnitOfWork&gt;().TheDefault.Is.ConstructedBy(ctx =&gt; ctx.GetInstance&lt;INHibernateUnitOfWork&gt;());</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;INHibernateUnitOfWork&gt;().CacheBy(InstanceScope.Hybrid)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;NHibernateUnitOfWork&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;IRepository&gt;().TheDefault.Is.OfConcreteType&lt;NHibernateRepository&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>With a little luck and all of our mad skills, we should be able to talk to the database now. So we need to plumb our simple product object up to talk to NHibernate. We will do this with a mapping file, and since we are using FluentNHibernate we can do this strongly typed.</p>

<p>Make the following modifications to Product: Extend DomainEntity and get rid of Id Property, it is inherited</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Product : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> virtual <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual string</span> Description { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Add mapping file for Product:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Mapping</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ProductMap : ClassMap&lt;Product&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> ProductMap()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Id(e =&gt; e.ID).GeneratedBy.GuidComb();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Map(p =&gt; p.Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Map(p =&gt; p.Description);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>You may also want to setup your connection string at this point in the Web.Config file, mine looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&lt;add name=<span style="color: #006080">&quot;MYDBKEY&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=LOCALHOST;initial catalog=FubuSample;Integrated Security=SSPI;&quot;</span> providerName=<span style="color: #006080">&quot;System.Data.SqlClient&quot;</span> /&gt;</pre>
  </div>
</div>

<p>I also created an empty database in my local SQL Server instance called “FubuSample” And then I went back to SQLServerSessionSourceConfiguration and changed the value of IsNewDatabase to return true. This will create the schema for the database when the application first runs. There is a multitude of different ways that we can handle this, but this was easy for the tutorial purposes.</p>

<p>Next we will add a behavior to our core project and tell FubuMVC to apply that behavior to all or our ControllerActions.</p>

<p>Create a class named “access_the_database_through_a_unit_of_work” in your Core.Web.Behaviors namespace. The contents will look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> access_the_database_through_a_unit_of_work : IControllerActionBehavior</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> IUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> access_the_database_through_a_unit_of_work(IUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IControllerActionBehavior InsideBehavior { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IInvocationResult Result { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> OUTPUT Invoke&lt;INPUT, OUTPUT&gt;(INPUT input, Func&lt;INPUT, OUTPUT&gt; func)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> INPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> OUTPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork.Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">try</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            var output = InsideBehavior.Invoke(input, func);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Result = InsideBehavior.Result;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">return</span> output;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">catch</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">throw</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">finally</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _unitOfWork.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now to tell FubuMVC what to do, open up Global.asax.cs or your Bootstrapper, wherever you are doing the configuration of FubuMVC. In this project we are doing it in Global.asax.cs Add a new behavior to the section that starts with “x.ByDefault.EveryControllerAction” so it looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">x.ByDefault.EveryControllerAction(d =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">       d.Will&lt;execute_the_result&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">       d.Will&lt;access_the_database_through_a_unit_of_work&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">   });</pre>
  </div>
</div>

<p>The last thing we will do is open up HomeController and add this code above the Index action:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">private</span> IRepository _repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> HomeController(IRepository repository)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository = repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>I also modified the Index action to go ahead and create some database values before we query for them just so we can test this thing. (I only did this for the tutorial, I swear) So modify the Index action to look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> IndexViewModel Index(IndexSetupViewModel inModel)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod1 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct1&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod1);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod2 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct2&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod2);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var outModel = <span style="color: #0000ff">new</span> IndexViewModel();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var productList = _repository.Query&lt;Product&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    outModel.Products = productList.ToList().Select(x =&gt; <span style="color: #0000ff">new</span> ProductDisplay(x));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">return</span> outModel;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now with you should be able to run your web app and browse to /home and all this will be executed and you will be on your way to creating a whole bunch of persist able objects for use with your FubuMVC app.</p>

<p>As always, feedback is welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>What I Really, Really, Really Like About Using Fluent NHibernate</title>
		<link>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-i-really-really-really-like-about-using-fluent-nhibernate</link>
		<comments>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 00:32:10 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/</guid>
		<description><![CDATA[Reusable Mappings Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files: Catalog: &#60;class name="Catalog" table="`Catalog`" optimistic-lock="version"&#62; &#60;id name="Id" column="Id" type="Guid"&#62; &#60;generator class="guid.comb" /&#62; &#60;/id&#62; &#60;version column="Version" name="Version" /&#62; &#60;property name="Name" length="100" type="String"&#62; &#60;column [...]]]></description>
			<content:encoded><![CDATA[<h3>Reusable Mappings</h3>
Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files:
<h4>Catalog:</h4>
<pre class="csharpcode">  <span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="Catalog"</span> <span class="attr">table</span><span class="kwrd">="`Catalog`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>
    ...
  <span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategory:</h4>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="CatalogCategory"</span> <span class="attr">table</span><span class="kwrd">="`CatalogCategory`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>

   ...

<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Suppose we have the luxury to choose how we deal with the database in our project. I do realize that's not always feasible because at some point in our careers, we've all seen what database masochism can do. Anyway, if would be making the shots, I would like to use a surrogate key for every table and I would recommend sequential GUIDs (called <a href="http://www.informit.com/articles/article.aspx?p=25862" target="_blank">COMBs</a>) for that.

The mapping of the classes shown above both have the same configuration for the <em>Id</em> and <em>Version</em> properties. These properties typically live in a <em>DomainEntity</em> base class of some sort because we don't want to repeat that tedious code of putting those in every entity of our domain over and over again. It would be nice if we could somehow do the same for the NHibernate mapping files, which we can't (or at least, not that I know of).

Using <a href="http://fluentnhibernate.org/" target="_blank">Fluent NHibernate</a> we can create an abstract mapping class for our <em>DomainEntity</em> from which we derive all entity mapping classes. The following code would give us the same result as the XML mapping files shown above:
<h4>DomainEntityMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> DomainEntityMapping&lt;TDomainEntity&gt; :
ClassMap&lt;TDomainEntity&gt; <span class="kwrd">where</span> TDomainEntity : DomainEntity
{
    <span class="kwrd">protected</span> DomainEntityMapping()
    {
        Id(entity =&gt; entity.Id, <span class="str">"Id"</span>)
            .GeneratedBy.GuidComb();
        OptimisticLock.Version();
        Version(entity =&gt; entity.Version)
            .TheColumnNameIs(<span class="str">"Version"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogMapping : DomainEntityMapping&lt;Catalog&gt;
{
  <span class="kwrd">public</span> CatalogMapping()
  {
      Map(catalog=&gt; catalog.Name, <span class="str">"Name"</span>);
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategoryMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogCategoryMapping : DomainEntityMapping&lt;CatalogCategory&gt;
{
    <span class="kwrd">public</span> CatalogCategoryMapping()
    {
        Map(category=&gt; category.Description, <span class="str">"Name"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

This way,  I can specify the mapping configuration for <em>Id</em> and <em>Version</em> in a single place which is really nice.
<h3>Mapping Tests</h3>
This is probably my most favorite feature of Fluent NHibernate. Given the fluent mapping configuration for the Category class shown earlier, the following test checks whether this mapping is valid or not:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> When_verifying_the_class_mapping_of_a_catalog
    : NHibernateSpecification
{
  [Test]
  <span class="kwrd">public</span> <span class="kwrd">void</span> Then_a_catalog_object_should_be_persistable()
  {
      <span class="kwrd">new</span> PersistenceSpecification&lt;Catalog&gt;(Session)
          .VerifyTheMappings();
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Running this test results in the following SQL statements being executed to an in-memory SQLite database:

NHibernate: INSERT INTO "Catalog" (Version, Name, Id) VALUES (@p0, @p1, @p2); @p0 = '1', @p1 = '', @p2 = 'c52126cb-f11e-47e4-a481-9bc600134d39'
NHibernate: SELECT catalog0_.Id as Id1_0_, catalog0_.Version as Version1_0_, catalog0_.Name as Name1_0_ FROM "Catalog" catalog0_ WHERE catalog0_.Id=@p0; @p0 = 'c52126cb-f11e-47e4-a481-9bc600134d39'

The following code shows the NHibernateSpecification base class for all my database tests. The code in this class deals with setting up the in-memory SQLite database and building the required schema based on the mappings. The Fluent NHibernate framework gives some really nice support for this as well:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> NHibernateSpecification
    : Specification
{
    <span class="kwrd">protected</span> ISession Session { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Establish_context()
    {
        var config = <span class="kwrd">new</span> SQLiteConfiguration()
            .InMemory()
            .ShowSql()
            .ToProperties();

        var sessionSource = <span class="kwrd">new</span> SessionSource(config,
            <span class="kwrd">new</span> RetailerPersistenceModel());

        Session = sessionSource.CreateSession();
        sessionSource.BuildSchema(Session);

        ProvideInitialData(Session);

        Session.Flush();
        Session.Clear();
   }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Dispose_context()
    {
        <span class="kwrd">if</span>(<span class="kwrd">null</span> != Session)
        {
            Session.Dispose();
            Session = <span class="kwrd">null</span>;
        }
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Fluent NHibernate really lowers the barrier for configuring NHibernate, which is a really big thing in my book. I must admit that I was a bit sceptical at first, but now I noticed that I'm having a hard time going back to the standard XML mapping configuration of NHibernate itself. As Fluent NHibernate is still in an early development stage, some issues can come up. But my personal experience so far is that the <a href="http://groups.google.com/group/fluent-nhibernate?hl=en" target="_blank">user group</a> is very responsive.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Stored Procedure Patterns</title>
		<link>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-2-1-and-collection-event-listeners</link>
		<comments>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 00:21:37 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/</guid>
		<description><![CDATA[In a previous post, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to read about it first. Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">previous post</a>, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">read about it</a> first.</p> <p>Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't provide this feature or, as in my case, the database in question does support this feature but the DBA's don't want anything to do with it? In case of a parent domain object having a collection of many child objects, you still might want to have a one-shot delete capability instead of having separate DELETE statements for each child record. </p> <p>The <a href="http://nhforge.org/blogs/nhibernate/archive/2009/07/19/welcome-nh2-1-0.aspx">newly released NHibernate 2.1</a> (congratulations to the entire team for their efforts and hard work) comes to the rescue, which introduces a couple of new event listeners that deal with collections.</p> <p>First we need an example. Suppose we are building an auction web site and the domain has a class called <em>Item</em> which in turn has a collection of <em>Bid</em>s. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Item
{
    <span class="kwrd">private</span> ISet&lt;Bid&gt; _bids;    
    <span class="kwrd">public</span> Int64 Id { get; <span class="kwrd">private</span> set; }
    
    ...
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Bid
{
    <span class="kwrd">public</span> Double Amount { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> Sting Code { get; <span class="kwrd">private</span> set; }

    ...
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>The mapping for these classes looks something like this:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Item, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Item"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Int64"</span> <span class="attr">unsaved-value</span><span class="kwrd">="-1"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">sql-type</span><span class="kwrd">="integer"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="native"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    
    ...
    
    <span class="kwrd">&lt;</span><span class="html">set</span> <span class="attr">name</span><span class="kwrd">="Bids"</span> 
         <span class="attr">access</span><span class="kwrd">="field.camelcase-underscore"</span> 
         <span class="attr">lazy</span><span class="kwrd">="false"</span> 
         <span class="attr">cascade</span><span class="kwrd">="all-delete-orphan"</span> 
         <span class="attr">inverse</span><span class="kwrd">="true"</span> 
         <span class="attr">optimistic-lock</span><span class="kwrd">="false"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">key</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">one-to-many</span> <span class="attr">class</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">set</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Bid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">column</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">type</span><span class="kwrd">="Int64"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="Code"</span> 
                      <span class="attr">column</span><span class="kwrd">="Code"</span> 
                      <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
    
    ...
    
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Just to give you a general idea of the situation here. Now suppose we want to delete a quite popular <em>Item</em> object&nbsp; which has a numerous amount of <em>Bid</em>s. Because the collection of <em>Bids</em> is mapped as <em>inverse</em>, NHibernate will remove every record for a <em>Bid</em> with a separate DELETE statement for each row. </p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1001'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1003'</span>
...
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We could solve this by creating a collection event listener. The first thing we have to do is figure out how to issue a one-shot delete instead of those separate DELETE statements. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IOneShotDeleteHandler
{   
    Type ForEntity();
    Type[] ForChildEntities();
    <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> OneShotDeleteHandlerForItem : IOneShotDeleteHandler
{
    <span class="kwrd">public</span> Type ForEntity()
    {
        <span class="kwrd">return</span> <span class="kwrd">typeof</span>(Item);
    }

    <span class="kwrd">public</span> Type[] ForChildEntities()
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span>[] { <span class="kwrd">typeof</span>(Bid) };
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity)
    {
        var item = (Item)entity;

        session.CreateQuery(<span class="str">"delete Bid where ItemId = :itemId"</span>)
            .SetInt64(<span class="str">"itemId"</span>, item.Id)
            .ExecuteUpdate();
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We created an <em>IOneShotDeleteHandler</em> interface with one implementation for the <em>Item</em> class.&nbsp; The most notable aspect of this implementation is the use of the HQL delete statement that removes all <em>Bid</em>s for a particular <em>Item</em>.</p>
<p>Next step is to create a collection event listener that implements the <em>IPreCollectionRemoveEventListener</em> interface.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEventListener
{
    <span class="kwrd">void</span> ConfigureFor(Configuration configuration);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> CollectionRemoveEventListener 
    : IPreCollectionRemoveEventListener,
      IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;
    
    <span class="kwrd">public</span> CollectionRemoveEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> OnPreRemoveCollection(
        PreCollectionRemoveEvent @<span class="kwrd">event</span>)
    {
        var affectedOwner = @<span class="kwrd">event</span>.AffectedOwnerOrNull;
        <span class="kwrd">if</span>(<span class="kwrd">null</span> == affectedOwner)
            <span class="kwrd">return</span>;

        var oneShotDeleteHandler = 
            _oneShotDeleteHandlers.SingleOrDefault(handler =&gt;
            handler.ForEntity() == affectedOwner.GetType());

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler)
            <span class="kwrd">return</span>;

        oneShotDeleteHandler
            .GiveItAShot(@<span class="kwrd">event</span>.Session, affectedOwner);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration
            .SetListener(ListenerType.PreCollectionRemove, <span class="kwrd">this</span>);
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Don't worry about the IEventListener interface. Its just there for registering all NHibernate event listeners in an IoC container.By doing so, it enables us to inject a collection of <em>IOneShotDeleteHandler</em> objects into the constructor of our event listener. When the <em>OnPreRemoveCollection</em> method is called, we simply lookup whether there's a handler available for the type of entity that's going to be deleted and give it a shot at removing its child collection in one sweep. </p>
<p>Now we only have to register this event listener:</p><pre class="csharpcode">var eventListeners = _dependencyContainer
    .ResolveAll&lt;IEventListener&gt;();
eventListeners.ForEach(eventListener =&gt; eventListener
    .ConfigureFor(configuration));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now, if we would use this 'as is', NHibernate will give us the following error:</p>
<blockquote>
<p><em>Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)</em></p></blockquote>
<p>This is due to the fact that the collection event listener does its work and deletes all <em>Bid</em>s by executing the HQL statement, but NHibernate still tries to issue a DELETE statement for each <em>Bid</em>. This is the result of the <em>all-delete-orphan</em> cascading rule we imposed in the mapping. We could reduce it to <em>save-update</em>, but then no individual DELETE statements are executed when a singe <em>Bid</em> is removed from the collection. Now what?</p>
<p>Well, we could provide a regular delete event listener that allows individual DELETE statements for <em>Bid</em> entities as long as their parent <em>Item</em> is not removed.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> DeleteEventListener : 
    DefaultDeleteEventListener,
    IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;

    <span class="kwrd">public</span> DeleteEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> DeleteEntity(
        IEventSource session, <span class="kwrd">object</span> entity, 
        EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, 
        IEntityPersister persister, ISet transientEntities)
    {
        var oneShotDeleteHandler = _oneShotDeleteHandlers
            .SingleOrDefault(handler =&gt;
                handler.ForChildEntities()
                    .Contains(entity.GetType()));

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler ||
           !IsParentAlsoDeletedIn(
                session.PersistenceContext, 
                oneShotDeleteHandler.ForEntity())
            )
        {
            <span class="kwrd">base</span>.DeleteEntity(session, entity, entityEntry, 
                              isCascadeDeleteEnabled, persister, 
                              transientEntities);
            <span class="kwrd">return</span>;
        }

        CascadeBeforeDelete(session, persister, entity, 
                            entityEntry, transientEntities);
        CascadeAfterDelete(session, persister, entity, 
                           transientEntities);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration.SetListener(ListenerType.Delete, <span class="kwrd">this</span>);
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> Boolean IsParentAlsoDeletedIn(
        IPersistenceContext persistenceContext, Type typeOfParent)
    {
        <span class="kwrd">foreach</span>(DictionaryEntry entry <span class="kwrd">in</span> 
                                    persistenceContext.EntityEntries)
        {
            <span class="kwrd">if</span>(typeOfParent != entry.Key.GetType())
                <span class="kwrd">continue</span>;
            
            var entityEntry = (EntityEntry)entry.Value;
            <span class="kwrd">if</span>(Status.Deleted == entityEntry.Status)
                <span class="kwrd">return</span> <span class="kwrd">true</span>;
        }

        <span class="kwrd">return</span> <span class="kwrd">false</span>;
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>With both event listeners registered, deleting a single Bid results in a single DELETE statement as one would expect:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <span class="kwrd">AND</span> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>and&nbsp; removing an entire <em>Item</em> now results in a one-shot delete for all <em>Bid</em>s:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0; @p0 = 2
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Make sure you use this solution for one-shot deletes wisely and only if you have to. If you can use the CASCADE DELETE foreign key constraints, then by all means, this is the preferred option. If not, only resort to this kind of solution only if you must and that you can prove that its going to give you a tremendous performance benefit. Also take a look at the <a href="http://ayende.com/Blog/2006/09/16/BatchingSupportInNHibernate.aspx">batching support</a> that NHibernate provides (at the moment only SQL Server and Oracle are supported). </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; NHibernate</title>
	<atom:link href="http://elegantcode.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NHibernate, HttpModules, ASP.NET JSON Web Services and Database Transactions</title>
		<link>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions</link>
		<comments>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:08:03 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/</guid>
		<description><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play: An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor ) NHibernate The use [...]]]></description>
			<content:encoded><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play:
<ol>
	<li>An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor )</li>
	<li>NHibernate</li>
	<li>The use of an HttpModule to implement the <a href="https://www.hibernate.org/43.html">Open Session in View</a> pattern and provide a Unit of Work implementation (one ISession per HTTP request)</li>
	<li>ASP.NET web services returning JSON (might well be a problem if the result was XML as well)</li>
</ol>
In an “old fashioned” Web Forms scenario everything works perfectly. If some unexpected event occurs during a postback then an Exception is thrown and it bubbles up to the HttpModule which rolls back the transaction.

However with a JSON web service (we’re not using WCF so we just have an System.Web.Services.WebService flagged with a ScriptService attribute) when an unexpected event occurs the magic of the framework catches it and the ScriptMethod simply returns the Exception converted to JSON. This was being correctly handled in our UI but because an Exception is not thrown so the HttpModule thinks that everything is OK and incorrectly commits our transaction. And because processing didn’t necessarily finish and our data could therefore get into an inconsistent state (which is even worse than a plain wrong state).

In order to work around this feature I hooked into the EndRequest event in the HttpModule and check the StatusCode of the Repsonse. Here’s the method that I created:
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">bool</span> IsAjaxError()
{
  <span class="kwrd">return</span> HttpContext.Current.Response.StatusCode == 500 &amp;&amp; HttpContext.Current.Request.RawUrl.Contains(<span class="str">".asmx"</span>);
}</pre>
<!--.csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Obviously if an error is detected I can take the appropriate action. So if you’re using the above combination of technologies be careful because what you think is happening and what is happening might be two entirely different things.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database Change Management with Tarantino</title>
		<link>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=database-change-management-with-tarantino</link>
		<comments>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 10:17:12 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/</guid>
		<description><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard. Why [...]]]></description>
			<content:encoded><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard.
<h2>Why do we need DB Change management</h2>
Okay then Mr. Developer (or Mrs.), Why do we need source control? We need database change management for the same reason we need source control. To help manage the code and dependencies our applications rely on.
<h4>Recipes for Disaster:</h4>
<blockquote>Team of developers all working/developing off of a central database</blockquote>
<blockquote>Team of developers working/developing off of local database, with no way to keep changes in sync.</blockquote>
So what’s the problem with these recipes? As new features are developed for an application it is very likely that the database will need schema changes to support these features. These could be as simple as new tables or adding a column. If we have schema changes, we have to make sure that the code base for the app is deployed concurrently with the schema changes.

When deploying software to production, code files and libraries can usually be overwritten with a new version. Databases however must be updated intelligently.
<h2>So we’ve got a problem, do we have a solution?</h2>
Enter Tarantino. I was talking with Jarod F. and he said he had heard some stuff about it, but had never used it so I started digging around and found enough pieces to get it working. So first thing I did was head over to the <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino project</a> site. Starting reading around and then i did a little googling and managed to dig up enough information to really wet my appetite and to get it working. Then Eric Hexter was kind enough to give me some time via IM and cleared up several areas for me.

Before I get into the specifics of how to set all this up, I want to explain how Tarantino finds and keeps changes in sync.
<ol>
	<li>Tarantino is a forward only change management system, you can’t roll back.</li>
	<li>If you use Tarantino, you can’t go make changes by hand manually</li>
	<li>Tarantino uses RedGate SQL Compare if you want, to compare DB Schemas</li>
	<li>Tarantino makes a table in every DB it touches to keep track of what scripts have been executed on that DB</li>
</ol>
The system is based on a set of conventions which allow incremental changes to the database schema. The conventions are to create two subdirectories in your database scripts folder. Create– this is where your inital schema change scripts go. Update – this is where your change scripts should be placed. They should be named with the following convention ####SCRIPTNAME.sql where #### is the script number with leading zeros. This will ensure the first script 0001_first_schema_change.sql would be executed first. There is a third folder, ExistingSchema, that you will need if you are going to start using Tarantino on an app that is already in development or production.

There are two ways to use Tarantino, command line, and as a NAnt task. I will be showing the NAnt task method, as it has the most examples available on the net.
<h4>Project Setup</h4>
So what I have done, is created a very simple project. A class library with a Fluent NHibernate configuration, a test fixture to create the database schema, and two simple entities which I have listed below:
<div class="csharpcode">
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Order</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> OrderNumber { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> PONumber { get; set; }</pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;OrderItem&gt; OrderItems { get; set;}</pre>
<pre></pre>
<pre class="alt">   <span class="kwrd">public</span> Order()</pre>
<pre>   {</pre>
<pre class="alt">       OrderItems = <span class="kwrd">new</span> List&lt;OrderItem&gt;();</pre>
<pre>   }</pre>
<pre class="alt">}</pre>
<pre></pre>
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> OrderItem</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> ItemName { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Description { get; set; }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

and my NHibernate and Test Fixture setup:
<div class="csharpcode">
<pre class="alt">[TestFixture]</pre>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> DB_Setup_Fixture</pre>
<pre class="alt">{</pre>
<pre>    <span class="kwrd">private</span> Configuration _configuration;</pre>
<pre class="alt"></pre>
<pre>    [TestFixtureSetUp]</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> FixtureSetup()</pre>
<pre>    {</pre>
<pre class="alt"></pre>
<pre>        _configuration = Fluently.Configure()</pre>
<pre class="alt">            .Database(MsSqlConfiguration.MsSql2008</pre>
<pre>                          .ConnectionString(c =&gt;</pre>
<pre class="alt">                                            c.FromConnectionStringWithKey(<span class="str">"testData"</span>))</pre>
<pre>                          .UseReflectionOptimizer()</pre>
<pre class="alt">                          .ShowSql())</pre>
<pre>            .Mappings(m =&gt;</pre>
<pre class="alt">                      m.AutoMappings.Add(AutoMap.AssemblyOf&lt;Order&gt;()</pre>
<pre>                                             .Where(x =&gt; x.Namespace.EndsWith(<span class="str">"Domain"</span>))))</pre>
<pre class="alt">            .BuildConfiguration();</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre></pre>
<pre class="alt">    [Test, Explicit, Category(<span class="str">"DBSetup"</span>)]</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">void</span> Create_DB_Schema()</pre>
<pre class="alt">    {</pre>
<pre>        var exporter = <span class="kwrd">new</span> SchemaExport(_configuration);</pre>
<pre class="alt">        exporter.Create(<span class="kwrd">true</span>, <span class="kwrd">true</span>);</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --> <!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<h5>The one thing to make note of here, is that my test is set to explicit, and I gave it a category of “DBSetup”. This will be important once we start configuring NAnt and Tarantino.</h5>
So now we have a very simple application that we can use to play around with Tarantino. To get Tarantino working, I am using the modified NAnt files that CodeCampServer is using. I just copied over their NAnt folder they have checked into source control. There are two NAnt build files that are required to use Tarantino. Disclaimer: I am a total NAnt noob, this is not my usual build utility, but I figured out enough to make it work.
<h4>Common.build</h4>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database change management --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="rebuildDatabase"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="updateDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Update"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Create"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="dropDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Drop"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="attr">failonerror</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="manageSqlDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="${action}"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="${database.integrated}"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}"</span></pre>
<pre class="alt">      <span class="attr">username</span><span class="kwrd">="${database.username}"</span></pre>
<pre>      <span class="attr">password</span><span class="kwrd">="${database.password}"</span></pre>
<pre class="alt">        <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${action != 'Drop'}"</span><span class="kwrd">&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Current Database Version: ${usdDatabaseVersion}"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt"></pre>
<pre>  <span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">language</span><span class="kwrd">="C#"</span> <span class="attr">prefix</span><span class="kwrd">="migration"</span> <span class="kwrd">&gt;</span></pre>
<pre class="alt">    &lt;references&gt;</pre>
<pre>      &lt;include name=<span class="str">"System.IO.dll"</span> /&gt;</pre>
<pre class="alt">    &lt;/references&gt;</pre>
<pre>    &lt;code&gt;</pre>
<pre class="alt">      &lt;![CDATA[</pre>
<pre>              [Function(<span class="str">"next-migration-number"</span>)]</pre>
<pre class="alt">              <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> NextMigration(<span class="kwrd">string</span> path  ) {</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">string</span>[] files = System.IO.Directory.GetFiles(path);</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">int</span> count=1;</pre>
<pre></pre>
<pre class="alt">                    <span class="kwrd">if</span>(files.Length &gt; 0)</pre>
<pre>                    {</pre>
<pre class="alt">                        <span class="kwrd">string</span> filename = System.IO.Path.GetFileName(files[files.Length-1]);</pre>
<pre></pre>
<pre class="alt">                        <span class="kwrd">try</span></pre>
<pre>                        {</pre>
<pre class="alt">                            count = Convert.ToInt32(filename.Substring(0, 4));</pre>
<pre>                            count++;</pre>
<pre class="alt"></pre>
<pre>                            <span class="kwrd">if</span>(count%2 == 0)</pre>
<pre class="alt">                                count++;</pre>
<pre>                        }</pre>
<pre class="alt">                        <span class="kwrd">catch</span></pre>
<pre>                        {</pre>
<pre class="alt"></pre>
<pre>                        }</pre>
<pre class="alt">                    }</pre>
<pre>                  <span class="kwrd">return</span> <span class="kwrd">string</span>.Format(<span class="str">"{0:0000}"</span>, count);</pre>
<pre class="alt">              }</pre>
<pre>            ]]&gt;</pre>
<pre class="alt">    &lt;/code&gt;</pre>
<pre>  <span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

This file sets up most of the plumbing that hooks up the Tarantino NAnt targets for use in the nant.build file
<h4>NAnt.build</h4>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="rem">&lt;!--EXTERNAL_PROPERTIES: usdDatabaseVersion, CCNetLabel--&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">name</span><span class="kwrd">="TarantinoSample"</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre>      <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Initialization --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">buildfile</span><span class="kwrd">="common.build"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">loadtasks</span> <span class="attr">assembly</span><span class="kwrd">="lib\nant\Tarantino.Nant.Tasks.dll"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- Master Properties --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="company.name"</span> <span class="attr">value</span><span class="kwrd">="Tarantino Test"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="assembly.unittests"</span> <span class="attr">value</span><span class="kwrd">="TarantinoSample.dll"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- Version settings --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="project.config"</span> <span class="attr">value</span><span class="kwrd">="debug"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Folder references --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.solution"</span> <span class="attr">value</span><span class="kwrd">="src"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.build"</span> <span class="attr">value</span><span class="kwrd">="build"</span> <span class="attr">dynamic</span><span class="kwrd">="true"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Compilation settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="nant.settings.currentframework"</span> <span class="attr">value</span><span class="kwrd">="net-3.5"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="file.solution"</span> <span class="attr">value</span><span class="kwrd">="${dir.solution}/${project::get-name()}.sln"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database migration settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.script.directory"</span> <span class="attr">value</span><span class="kwrd">="dbChangeScripts"</span> <span class="kwrd">/&gt;</span></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.server"</span> <span class="attr">value</span><span class="kwrd">="localhost"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.name"</span> <span class="attr">value</span><span class="kwrd">="OrderData"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.integrated"</span> <span class="attr">value</span><span class="kwrd">="true"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.username"</span> <span class="attr">value</span><span class="kwrd">="dbuser"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.password"</span> <span class="attr">value</span><span class="kwrd">="P@ssword1"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Database Change Management Targets --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="reset-database"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">nunit2</span> <span class="attr">failonerror</span><span class="kwrd">="true"</span> <span class="attr">verbose</span><span class="kwrd">="true"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">formatter</span> <span class="attr">type</span><span class="kwrd">="Plain"</span> <span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">test</span> <span class="attr">assemblyname</span><span class="kwrd">="${dir.solution}/bin/${project.config}/${assembly.unittests}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">                <span class="kwrd">&lt;</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre>                    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">name</span><span class="kwrd">="DBSetup"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">                    <span class="kwrd">&lt;</span><span class="html">exclude</span> <span class="attr">name</span><span class="kwrd">="*"</span><span class="kwrd">/&gt;</span></pre>
<pre>                <span class="kwrd">&lt;/</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;/</span><span class="html">test</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">nunit2</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="db-migration"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">delete</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span>  <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="reset-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="create-versioned-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="migrationScriptName"</span> <span class="attr">value</span><span class="kwrd">="${migration::next-migration-number(database.script.directory+'/Update')}_AutoGeneratedMigration.sql"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">exec</span></pre>
<pre>              <span class="attr">program</span><span class="kwrd">="${redgate.exe}"</span></pre>
<pre class="alt">              <span class="attr">commandline</span><span class="kwrd">="/f /v /server1:${database.server} /server2:${database.server} /database1:${database.name} /database2:${database.name}Versioned /scriptfile:${database.script.directory}/Update/_New_Script.sql /exclude:Table:usd_AppliedDatabaseScript"</span></pre>
<pre>              <span class="attr">resultproperty</span><span class="kwrd">="execReturnCode"</span></pre>
<pre class="alt">              <span class="attr">failonerror</span> <span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>          <span class="kwrd">&lt;</span><span class="html">fail</span> <span class="attr">if</span><span class="kwrd">="${execReturnCode != '0' and execReturnCode != '63'}"</span> <span class="attr">message</span><span class="kwrd">="Redgate Compare Failed!"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql') == false}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">=""</span><span class="kwrd">/&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="---------------------"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="No Migration Required"</span><span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql')}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">move</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span> <span class="attr">tofile</span><span class="kwrd">="${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Created Migration File ${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">          <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="create-versioned-database"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">tstamp</span><span class="kwrd">&gt;&lt;/</span><span class="html">tstamp</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">action</span><span class="kwrd">="Drop"</span></pre>
<pre class="alt">            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre>            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre class="alt">            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre>            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre class="alt">            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="Create"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre class="alt">            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre>            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Inside this file there are a few pieces that need to be configured for use in your own app, these are all listed under “Master Properties”
<h4>Change script creation</h4>
So without going into too many details, what I have done is created my initial schema using my Unit Test. From here I could script this out to a text file, but what I did was use SQL Compare, comparing my initial schema to an empty database. This gave me my initial SQL script that creates my database.

Just to verify that my initial schema creation script is good, I will have Tarantino create the Versioned database for me using this command:
<blockquote>lib\nant\nant.exe –buildfile:nant.build create-versioned-database</blockquote>
Tarantino then looks in the ExistingSchema folder, runs any scripts found there, then does the same thing for the Updates folder.

In order to test out the change management features, I made a small change to my domain, adding a decimal of cost to the OrderItem class should do the trick. Rebuild my solution, then back to the command line to run my “Create Database Migration Script.bat” file. This file is just running the NAnt task.

What happens then, is NAnt goes out and runs my DBSetup test that is in my project. Which recreates the “OrderData” database. Tarantino then creates the “Versioned” database, then uses SQL Compare to find the changes between the two, and creates a file in the update folder for me named, <strong>0001_AutoGeneratedMigration.sql</strong> <span style="color: #ff0000;">NOTE: It is important to rename this file before it is committed to source control, or before you apply these changes to a database. Tarantino tracks the scripts that have been run by filename.</span>
<h4>Push to Production or Dev</h4>
So I have mentioned two databases so far in this post, OrderData and OrderDataVersioned. These two databases contain no data, that’s not to say that they couldn’t, if you had scripts that put it in. But that is not what they were designed to do. Our app.config is pointing to OrderData, so when we ask NHibernate to build the schema, it drops all the tables and recreates them, losing all data. OrderDataVersioned, is Tarantinos database that is used to keep changes in sync. In our development environment, every dev has a third database, we would call OrderDataProd for instance. This database could have sample data and other information in it. Let’s say, we don’t want to lose our data in this DB so what we do is let Tarantino handle the first two databases, then we deploy our schema changes to our development database. We use this same technique to push changes to production, after backups of course.

We can do this easily with the following command:
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build updateDatabase –D:database.name=OrderDataProd</span></blockquote>
Now when I tried to run this, my database OrderDataProd did not exist, so I went to SQL manager, created it, and then ran the command again, only to be slammed with another error. Since the database existed, Tarantino did not run the create schema script, only the update, which created another error. The next command will create the database I want, by executing the schema creation script, then applying any updates to it.
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build createDatabase –D:database.name=OrderDataProd</span></blockquote>
Bingo! I now have my 3rd database that I can dump data into and start developing on top of. Just to be sure that we can create another change script, and deploy it to this database, I will modify the domain again, by adding a quantity to the OrderItem object. I will leave this step up to you to try out.
<h4>Source Code</h4>
I know the blog formatting sometimes jacks up code samples a lot so I am making all the source for this post available two ways:
<ul>
	<li><a href="http://elegantcode.googlecode.com/files/TarantinoTest.zip" target="_blank">Zip File Download</a></li>
	<li><a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/RyanKelley/TarantinoTest" target="_blank">Elegant Code SVN Repository</a></li>
</ul>
<h3>Information Sources</h3>
<a href="http://www.lostechies.com/blogs/hex/default.aspx" target="_blank">Eric Hexter</a> – Huge Help

<a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a>

<a href="http://jeffreypalermo.com/blog/tarantino-project-rsquo-s-database-migrations-screencast/" target="_blank">Palermo and Hexter Screencast on Tarantino</a>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why Future&lt;T&gt; should be in your future</title>
		<link>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-futuret-should-be-in-your-future</link>
		<comments>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:50:24 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/</guid>
		<description><![CDATA[Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature [...]]]></description>
			<content:encoded><![CDATA[<p>Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature <a href="http://davybrion.com/blog/2009/01/nhibernate-and-future-queries/">here</a> so I am not going to rehash that. What I am going to focus on here if the why.</p>  <p>Building a UI can be a very database intensive operation in terms of the number of separate calls required. And for each one of those calls the time taken to actually execute the query can be a small compared to the duration of the roundtrip to request and fetch the data. The obvious solution is some type of batching and with Future&lt;T&gt; this happens automagically without any change to the consuming logic (for databases that support it). Let me restate that, you get a huge performance benefit with only a simple change to your query methods in the Repository. So why aren’t you using it?</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate 3.0 QueryOver</title>
		<link>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-3-0-queryover</link>
		<comments>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 19:23:53 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/</guid>
		<description><![CDATA[One of the personal reasons that I had for co-founding Guild 3 was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in Avoiding (Or Recovering From) Burnout. For me the age old adage of “a change is as good as a rest” [...]]]></description>
			<content:encoded><![CDATA[One of the personal reasons that I had for co-founding <a href="http://guild3.com">Guild 3</a> was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in <a href="http://davybrion.com/blog/2009/09/avoiding-or-recovering-from-burnout/">Avoiding (Or Recovering From) Burnout</a>. For me the age old adage of “a change is as good as a rest” has proven to be an extremely successful strategy.

One of the things that I had stopped doing was keeping an eye on various OSS projects to see what was on the horizon. Yesterday I (finally!) started to experiment with NHibernate 3.0. The first thing that caught my eye was <a href="http://fabiomaulo.blogspot.com/2009/06/criteria-on-nh300.html">QueryOver</a>. As Fabio explained there are a <a href="http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html">lot of different ways of executing queries in NH</a>. Certainly in the pre-LINQ days ICriteria was the predominantly recommended option because it has elements of type safety to it and its fluent-ish API broke everything down into small pieces and avoided string concatenation hell.

QueryOver is fluent a layer on top of ICriteria. It looks very LINQesque but it’s a very different animal, not least of all because there are some concepts in ICriteria that do not have a LINQ equivalent (caching etc.). In the short to medium term I suspect that it will become my de facto approach for NHibernate queries (I’ve used NHibernate LINQ and it’s great for simple queries but I’ve experienced significant issues with more complicated ones).

What I haven’t figured out yet is how, if at all, this will affect my data access testing strategy. Historically I’ve favored smoke tests that have really been doing little more than verifying that a given ICriteria query was semantically valid. Typically I only resorted to actually worrying about the results in specific cases (mainly due to the burden of maintaining test data for each possible scenario). Then again this sounds like a topic for another post doesn’t it…]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate 2.1 and Collection Event Listeners</title>
		<link>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-2-1-and-collection-event-listeners</link>
		<comments>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 00:21:37 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/</guid>
		<description><![CDATA[In a previous post, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to read about it first. Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">previous post</a>, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to <a href="http://elegantcode.com/2008/04/12/nhibernate-20-and-cascading-deletes-2/">read about it</a> first.</p> <p>Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't provide this feature or, as in my case, the database in question does support this feature but the DBA's don't want anything to do with it? In case of a parent domain object having a collection of many child objects, you still might want to have a one-shot delete capability instead of having separate DELETE statements for each child record. </p> <p>The <a href="http://nhforge.org/blogs/nhibernate/archive/2009/07/19/welcome-nh2-1-0.aspx">newly released NHibernate 2.1</a> (congratulations to the entire team for their efforts and hard work) comes to the rescue, which introduces a couple of new event listeners that deal with collections.</p> <p>First we need an example. Suppose we are building an auction web site and the domain has a class called <em>Item</em> which in turn has a collection of <em>Bid</em>s. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Item
{
    <span class="kwrd">private</span> ISet&lt;Bid&gt; _bids;    
    <span class="kwrd">public</span> Int64 Id { get; <span class="kwrd">private</span> set; }
    
    ...
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Bid
{
    <span class="kwrd">public</span> Double Amount { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> Sting Code { get; <span class="kwrd">private</span> set; }

    ...
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>The mapping for these classes looks something like this:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Item, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Item"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Int64"</span> <span class="attr">unsaved-value</span><span class="kwrd">="-1"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">sql-type</span><span class="kwrd">="integer"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="native"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    
    ...
    
    <span class="kwrd">&lt;</span><span class="html">set</span> <span class="attr">name</span><span class="kwrd">="Bids"</span> 
         <span class="attr">access</span><span class="kwrd">="field.camelcase-underscore"</span> 
         <span class="attr">lazy</span><span class="kwrd">="false"</span> 
         <span class="attr">cascade</span><span class="kwrd">="all-delete-orphan"</span> 
         <span class="attr">inverse</span><span class="kwrd">="true"</span> 
         <span class="attr">optimistic-lock</span><span class="kwrd">="false"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">key</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">one-to-many</span> <span class="attr">class</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">set</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="MyAuction.Bid, MyAuction"</span> 
       <span class="attr">table</span><span class="kwrd">="Bid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">column</span><span class="kwrd">="ItemId"</span> 
                      <span class="attr">type</span><span class="kwrd">="Int64"</span><span class="kwrd">/&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">key-property</span> <span class="attr">name</span><span class="kwrd">="Code"</span> 
                      <span class="attr">column</span><span class="kwrd">="Code"</span> 
                      <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">composite-id</span><span class="kwrd">&gt;</span>
    
    ...
    
<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Just to give you a general idea of the situation here. Now suppose we want to delete a quite popular <em>Item</em> object&nbsp; which has a numerous amount of <em>Bid</em>s. Because the collection of <em>Bids</em> is mapped as <em>inverse</em>, NHibernate will remove every record for a <em>Bid</em> with a separate DELETE statement for each row. </p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1001'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span>
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <font color="#0000ff">AND</font> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1003'</span>
...
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We could solve this by creating a collection event listener. The first thing we have to do is figure out how to issue a one-shot delete instead of those separate DELETE statements. </p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IOneShotDeleteHandler
{   
    Type ForEntity();
    Type[] ForChildEntities();
    <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> OneShotDeleteHandlerForItem : IOneShotDeleteHandler
{
    <span class="kwrd">public</span> Type ForEntity()
    {
        <span class="kwrd">return</span> <span class="kwrd">typeof</span>(Item);
    }

    <span class="kwrd">public</span> Type[] ForChildEntities()
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span>[] { <span class="kwrd">typeof</span>(Bid) };
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> GiveItAShot(ISession session, Object entity)
    {
        var item = (Item)entity;

        session.CreateQuery(<span class="str">"delete Bid where ItemId = :itemId"</span>)
            .SetInt64(<span class="str">"itemId"</span>, item.Id)
            .ExecuteUpdate();
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>We created an <em>IOneShotDeleteHandler</em> interface with one implementation for the <em>Item</em> class.&nbsp; The most notable aspect of this implementation is the use of the HQL delete statement that removes all <em>Bid</em>s for a particular <em>Item</em>.</p>
<p>Next step is to create a collection event listener that implements the <em>IPreCollectionRemoveEventListener</em> interface.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEventListener
{
    <span class="kwrd">void</span> ConfigureFor(Configuration configuration);
}

<span class="kwrd">public</span> <span class="kwrd">class</span> CollectionRemoveEventListener 
    : IPreCollectionRemoveEventListener,
      IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;
    
    <span class="kwrd">public</span> CollectionRemoveEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> OnPreRemoveCollection(
        PreCollectionRemoveEvent @<span class="kwrd">event</span>)
    {
        var affectedOwner = @<span class="kwrd">event</span>.AffectedOwnerOrNull;
        <span class="kwrd">if</span>(<span class="kwrd">null</span> == affectedOwner)
            <span class="kwrd">return</span>;

        var oneShotDeleteHandler = 
            _oneShotDeleteHandlers.SingleOrDefault(handler =&gt;
            handler.ForEntity() == affectedOwner.GetType());

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler)
            <span class="kwrd">return</span>;

        oneShotDeleteHandler
            .GiveItAShot(@<span class="kwrd">event</span>.Session, affectedOwner);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration
            .SetListener(ListenerType.PreCollectionRemove, <span class="kwrd">this</span>);
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Don't worry about the IEventListener interface. Its just there for registering all NHibernate event listeners in an IoC container.By doing so, it enables us to inject a collection of <em>IOneShotDeleteHandler</em> objects into the constructor of our event listener. When the <em>OnPreRemoveCollection</em> method is called, we simply lookup whether there's a handler available for the type of entity that's going to be deleted and give it a shot at removing its child collection in one sweep. </p>
<p>Now we only have to register this event listener:</p><pre class="csharpcode">var eventListeners = _dependencyContainer
    .ResolveAll&lt;IEventListener&gt;();
eventListeners.ForEach(eventListener =&gt; eventListener
    .ConfigureFor(configuration));</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Now, if we would use this 'as is', NHibernate will give us the following error:</p>
<blockquote>
<p><em>Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)</em></p></blockquote>
<p>This is due to the fact that the collection event listener does its work and deletes all <em>Bid</em>s by executing the HQL statement, but NHibernate still tries to issue a DELETE statement for each <em>Bid</em>. This is the result of the <em>all-delete-orphan</em> cascading rule we imposed in the mapping. We could reduce it to <em>save-update</em>, but then no individual DELETE statements are executed when a singe <em>Bid</em> is removed from the collection. Now what?</p>
<p>Well, we could provide a regular delete event listener that allows individual DELETE statements for <em>Bid</em> entities as long as their parent <em>Item</em> is not removed.</p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> DeleteEventListener : 
    DefaultDeleteEventListener,
    IEventListener
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IEnumerable&lt;IOneShotDeleteHandler&gt; 
        _oneShotDeleteHandlers;

    <span class="kwrd">public</span> DeleteEventListener(
        IEnumerable&lt;IOneShotDeleteHandler&gt; oneShotDeleteHandlers)
    {
        _oneShotDeleteHandlers = oneShotDeleteHandlers;
    }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> DeleteEntity(
        IEventSource session, <span class="kwrd">object</span> entity, 
        EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, 
        IEntityPersister persister, ISet transientEntities)
    {
        var oneShotDeleteHandler = _oneShotDeleteHandlers
            .SingleOrDefault(handler =&gt;
                handler.ForChildEntities()
                    .Contains(entity.GetType()));

        <span class="kwrd">if</span>(<span class="kwrd">null</span> == oneShotDeleteHandler ||
           !IsParentAlsoDeletedIn(
                session.PersistenceContext, 
                oneShotDeleteHandler.ForEntity())
            )
        {
            <span class="kwrd">base</span>.DeleteEntity(session, entity, entityEntry, 
                              isCascadeDeleteEnabled, persister, 
                              transientEntities);
            <span class="kwrd">return</span>;
        }

        CascadeBeforeDelete(session, persister, entity, 
                            entityEntry, transientEntities);
        CascadeAfterDelete(session, persister, entity, 
                           transientEntities);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> ConfigureFor(Configuration configuration)
    {
        configuration.SetListener(ListenerType.Delete, <span class="kwrd">this</span>);
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> Boolean IsParentAlsoDeletedIn(
        IPersistenceContext persistenceContext, Type typeOfParent)
    {
        <span class="kwrd">foreach</span>(DictionaryEntry entry <span class="kwrd">in</span> 
                                    persistenceContext.EntityEntries)
        {
            <span class="kwrd">if</span>(typeOfParent != entry.Key.GetType())
                <span class="kwrd">continue</span>;
            
            var entityEntry = (EntityEntry)entry.Value;
            <span class="kwrd">if</span>(Status.Deleted == entityEntry.Status)
                <span class="kwrd">return</span> <span class="kwrd">true</span>;
        }

        <span class="kwrd">return</span> <span class="kwrd">false</span>;
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>With both event listeners registered, deleting a single Bid results in a single DELETE statement as one would expect:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0 <span class="kwrd">AND</span> Code=@p1 ;@p0 = 2, @p1 = <span class="str">'F1002'</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>and&nbsp; removing an entire <em>Item</em> now results in a one-shot delete for all <em>Bid</em>s:</p><pre class="csharpcode"><span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Bid <span class="kwrd">WHERE</span> ItemId=@p0; @p0 = 2
<span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> Item <span class="kwrd">WHERE</span> Id = @p0; @p0 = 2</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>Make sure you use this solution for one-shot deletes wisely and only if you have to. If you can use the CASCADE DELETE foreign key constraints, then by all means, this is the preferred option. If not, only resort to this kind of solution only if you must and that you can prove that its going to give you a tremendous performance benefit. Also take a look at the <a href="http://ayende.com/Blog/2006/09/16/BatchingSupportInNHibernate.aspx">batching support</a> that NHibernate provides (at the moment only SQL Server and Oracle are supported). </p>
<p>Till next time</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using NHibernate for Legacy Databases</title>
		<link>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-nhibernate-for-legacy-databases</link>
		<comments>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:32:11 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/</guid>
		<description><![CDATA[One of the downsides of being confronted with a shared legacy database day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides of being confronted with a <a href="http://elegantcode.com/2009/03/28/about-a-shared-database/">shared legacy database</a> day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required for your application. These extra columns are specifically there to serve those other legacy applications. Heck, to make matters even worse, there are probably some new columns added specifically for your application as well. This is the fairy tale of shared legacy databases. </p> <p>Using NHibernate in these scenarios can be challenging sometimes but its built in flexibility and extensibility really helps you to deal with those cases. The problem I ran into last week was that we needed to store a domain object into a table that had a lot more columns than were actually required for our application. If it would be possible to store null values in these columns or if they had default values configured for them in the schema, then this would not be a problem. Instead, these unnecessary columns could not store null values and had no default values associated with them. </p> <p>First option would be to make some changes to the schema of the table. Alas, no luck there because the other legacy applications that are using the same table would break. Now what?</p> <p>We needed to insert the default values ourselves, but those columns are not known by NHibernate because they are not mapped to any members of the domain object. One way to solve this, is to pollute the domain object by adding private fields that are initialized to the required default values.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainEntity
{
    <span class="rem">// Legacy fields with no purpose for the domain but required </span>
    <span class="rem">// by the database.</span>
    <span class="kwrd">private</span> Int32 _legacyField1 = 2;
    <span class="kwrd">private</span> Boolean _legacyField2 = <span class="kwrd">false</span>;
    <span class="kwrd">private</span> String _legacyField3 = <span class="str">""</span>;
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably the simplest option, but imposes a <a href="http://en.wikipedia.org/wiki/Fixing_Broken_Windows">broken window</a> as infrastructure concerns are bleeding into the domain this way. In other words, this is not a viable solution. Keeping the legacy stuff isolated as much as possible, NHibernate provides some ways to deal with this by providing an extensive extensibility model.</p>
<p>After some snooping around in the source code of NHibernate, the solution we chose for dealing with this issue is by creating a custom access strategy. The built in property access strategies are probably already well known, but its also possible to write your own access strategy by implementing the <em>IPropertyAccessor</em> interface.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainObjectAccessor : IPropertyAccessor
{
    <span class="kwrd">private</span> IEnumerable&lt;IGetter&gt; _defaultValueGetters;
    
    <span class="kwrd">public</span> SomeDomainObjectAccessor()
    {
        _defaultValueGetters = <span class="kwrd">new</span> List&lt;IGetter&gt;()
        {
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Int32&gt;(<span class="str">"LegacyColumn1"</span>, 2) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Boolean&gt;(<span class="str">"LegacyColumn2"</span>, <span class="kwrd">false</span>) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;String&gt;(<span class="str">"LegacyColumn3"</span>, 2) }
        }
    }

    <span class="kwrd">public</span> IGetter GetGetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> _defaultValueGetters        
            .Where(getter =&gt; getter.PropertyName == propertyName)        
            .SingleOrDefault();
    }
    
    <span class="kwrd">public </span>ISetter GetSetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> NoopSetter();
    }

    <span class="kwrd">public</span> Boolean CanAccessTroughReflectionOptimizer
    {
        get { <span class="kwrd">return</span> <span class="kwrd">true</span>; }
    }
}

<span class="kwrd">private</span> <span class="kwrd">class</span> DefaultValueGetter&lt;T&gt; : IGetter
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> String _propertyName;
    <span class="kwrd">private</span> T Value { get; set; }

    <span class="kwrd">public</span> DefaultValueGetter(String propertyName, T <span class="kwrd">value</span>)
    {
        _propertyName = propertyName;
        Value = <span class="kwrd">value</span>;
    }

    <span class="kwrd">public</span> Object Get(Object target)
    {
        <span class="kwrd">return</span> Value;
    }

    <span class="kwrd">public</span> Type ReturnType
    {
        get { <span class="kwrd">return</span> <span class="kwrd">typeof</span>(T); }
    }

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> _propertyName; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get
        {
            var method = <span class="kwrd">typeof</span>(BasicPropertyAccessor)              
                .GetMethod(<span class="str">"GetGetterOrNull"</span>,
                           BindingFlags.Static | BindingFlags.NonPublic);

            var result = (BasicPropertyAccessor.BasicGetter)method              
                .Invoke(<span class="kwrd">null</span>, <span class="kwrd">new</span> Object[] { GetType(), <span class="str">"Value"</span> });

            <span class="kwrd">return</span> result.Method;
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> GetForInsert(Object owner, IDictionary mergeMap,
                               ISessionImplementor session)
    {
        <span class="kwrd">return</span> Get(owner);
    }
}

<span class="kwrd">private</span> <span class="kwrd">sealed</span> <span class="kwrd">class</span> NoopSetter : ISetter
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Set(Object target, Object <span class="kwrd">value</span>)
    {}

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This simply involves a <em>getter</em> for providing default values and a dummy <em>setter</em> as we're not interested in setting any values on the domain objects. The <em>DefaultValueGetter</em> class uses a trick so that we can keep using the reflection optimizer of NHibernate. This also seems to be necessary when using NHibernate Profiler. </p>
<p>Now we only have to provide some properties in the mapping of the domain object using our custom access strategy:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Int32"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Boolean"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="String"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably not the best solution, but it does the job and prevents polluting the domain objects as a result of database quirks like these. I'm interested in hearing feedback or any better approaches. </p>
<p>Anyway, the easy extensibility of NHibernate makes it the best data access solution around. This way, one can deal with all edge case scenarios that weren't anticipated by the framework builders. </p>
<p>Till next time&nbsp;&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Sybase working to improve NHibernate support</title>
		<link>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sybase-working-to-improve-nhibernate-support</link>
		<comments>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/#comments</comments>
		<pubDate>Fri, 15 May 2009 15:19:16 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Sybase]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/</guid>
		<description><![CDATA[If you also subscribe to Chris’ feed here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 [...]]]></description>
			<content:encoded><![CDATA[<p>If you also subscribe to <a href="http://elegantcode.com/author/brandsma/feed/">Chris’ feed</a> here at Elegant Code you might have determined that we work for the same organization. We both regularly use Sybase databases, in particular ASA and Ultralite, which, unlike Chris, I can spell correctly :). We’re also both NHibernate users and have been for several years. We have 5 – 10 production applications using this combination of technologies.</p>  <p>But support in NHibernate for Sybase is not great. The basics work but some of the more advanced features are not implemented at all or are done poorly. Fortunately we have an excellent relationship with our vendor and have had for many years. Earlier this week they contacted me and mentioned that they will be undertaking an effort to improve their support for NHibernate and were looking for some beta testers.</p>  <p>For this effort they can be commended. It’s also telling (and significant) that OR/M usage is becoming mainstream enough for them to consider this effort worthwhile.</p>  <p><a href="http://iablog.sybase.com/paulley">Glenn</a> is going to be leading the effort (as he did for the Java version last year). If you want to influence the direction that we go then please tell us what needs to be fixed.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/05/15/sybase-working-to-improve-nhibernate-support/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FubuMVC From Scratch &#8211; Part 4 Persistence</title>
		<link>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fubumvc-from-scratch-part-4-persistence</link>
		<comments>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 21:22:00 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[FubuMVC]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/</guid>
		<description><![CDATA[Previous posts in series: FubuMVC From Scratch Part 1 – Basic project structure setup FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup FubuMVC From Scratch – Part 3 (Adding View to project) Persistence is a requirement in virtually every application that we write today, and now the time has come for us [...]]]></description>
			<content:encoded><![CDATA[<h3>Previous posts in series:</h3>  <ul>   <li><a href="http://elegantcode.com/2009/01/30/setting-up-a-fubumvc-project-from-scratch/" target="_blank">FubuMVC From Scratch Part 1 – Basic project structure setup</a> </li>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-2/" target="_blank">FubuMVC From Scratch Part 2 – FubuMVC configuration and Controller setup</a></li>    <ul></ul>    <li><a href="http://elegantcode.com/2009/03/08/fubumvc-from-scratch-part-3-adding-view-to-project/" target="_blank">FubuMVC From Scratch – Part 3 (Adding View to project)</a></li> </ul>  <p>Persistence is a requirement in virtually every application that we write today, and now the time has come for us to add a persistence model to our <a href="http://fubumvc.googlecode.com/" target="_blank">FubuMVC</a> app. I know I mentioned moving my samples to the FubuCart source, but I am going to go ahead and implement the persistence piece in the same project the previous articles using, FubuSample. I have also added all the code for this series to the <a href="http://code.google.com/p/fubumvc-contrib/" target="_blank">FubuMVC-Contrib</a> project</p>  <p>We will be implementing our persistence model to use NHibernate, Fluent NHibernate, Repository Pattern and UnitOfWork pattern all on top of SQL Express or standard. This will also work for sqllite and other database servers.</p>  <p>Class and Interfaces we have to implement: </p>  <ul>   <li>ISessionSourceConfiguration </li>    <li>SQLServerSessionSourceConfiguration </li>    <li>IUnitOfWork </li>    <li>INHibernateUnitOfWork </li>    <li>NHibernateUnitOfWork </li>    <li>IRepository </li>    <li>NHibernateRepository </li>    <li>DomainEntity </li>    <li>IDomainQuery </li>    <li>FubuSamplePersistenceModel </li> </ul>  <p>Note: I put the first two in the list in my Core.Config namespace, and the rest in Core.Persistence</p>  <p>Before we start you probably want to go ahead and add the following references to your core project and Web project</p>  <p><a href="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref.jpg"><img title="FubuMVC-Persist-Ref" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="409" alt="FubuMVC-Persist-Ref" src="http://elegantcode.com/wp-content/uploads/2009/03/fubumvcpersistref-thumb.jpg" width="274" border="0" /></a> </p>  <p>Let’s just work our way down the list and get these files implemented. </p>  <h3>ISessionSourceConfiguration</h3>  <div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">   <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">     <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">bool</span> IsNewDatabase { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ISessionSource CreateSessionSource(PersistenceModel model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>SQLServerSessionSourceConfiguration</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 131.67%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 263px; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> SQLServerSessionSourceConfiguration : ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #cc6633">#region</span> Implementation of ISessionSourceConfiguration</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsNewDatabase</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        get { <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISessionSource CreateSessionSource(PersistenceModel model)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        var properties = GetProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var source = <span style="color: #0000ff">new</span> SessionSource(properties, model);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        create_schema_if_it_does_not_already_exist(source);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> create_schema_if_it_does_not_already_exist(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (IsNewDatabase) source.BuildSchema();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">protected</span> IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">string</span>&gt; GetProperties()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        MsSqlConfiguration config = MsSqlConfiguration.MsSql2005;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.ConnectionString.FromConnectionStringWithKey(“MYDBKEY”);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        config.ShowSql();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        config.UseOuterJoin();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> config.ToProperties();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #cc6633">#endregion</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>A quick note here: We told SQLServerSessionSourceConfiguration to use a connection string from the app settings file (Web.Config) with a key of “MYDBKEY” This will be important later.</p>

<h3>IUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IUnitOfWork : IDisposable</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">void</span> Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>INHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> INHibernateUnitOfWork : IUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ISession CurrentSession { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateUnitOfWork</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateUnitOfWork : INHibernateUnitOfWork</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> ITransaction _transaction;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isDisposed;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ISessionSource _source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">bool</span> _isInitialized;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateUnitOfWork(ISessionSource source)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _source = source;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Initialize()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        CurrentSession = _source.CreateSession();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isInitialized = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ISession CurrentSession { get; <span style="color: #0000ff">private</span> set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Commit()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> begin_new_transaction()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_transaction != <span style="color: #0000ff">null</span>)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction = CurrentSession.BeginTransaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Rollback()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        should_not_currently_be_disposed();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        should_be_initialized_first();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _transaction.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        begin_new_transaction();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_not_currently_be_disposed()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ObjectDisposedException(GetType().Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> should_be_initialized_first()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (!_isInitialized) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> InvalidOperationException(<span style="color: #006080">&quot;Must initialize (call Initialize()) on NHibernateUnitOfWork before commiting or rolling back&quot;</span>);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Dispose()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (_isDisposed || !_isInitialized) <span style="color: #0000ff">return</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _transaction.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        CurrentSession.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _isDisposed = <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ENTITY Load&lt;ENTITY&gt;(Guid id)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> ENTITY : DomainEntity;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>NHibernateRepository</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateRepository : IRepository</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> INHibernateUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> NHibernateRepository(INHibernateUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Save&lt;ENTITY&gt;(ENTITY entity) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.SaveOrUpdate(entity);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> ENTITY Load&lt;ENTITY&gt;(Guid id) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Load&lt;ENTITY&gt;(id);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;() <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IQueryable&lt;ENTITY&gt; Query&lt;ENTITY&gt;(IDomainQuery&lt;ENTITY&gt; whereQuery) <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> _unitOfWork.CurrentSession.Linq&lt;ENTITY&gt;().Where(whereQuery.Expression);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Delete&lt;ENTITY&gt;(ENTITY entity)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(entity);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> DeleteAll&lt;ENTITY&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        var query = String.Format(<span style="color: #006080">&quot;from {0}&quot;</span>, <span style="color: #0000ff">typeof</span>(ENTITY).Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        _unitOfWork.CurrentSession.Delete(query);   </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>DomainEntity</h3>

<p>Note: I put both of these classes in my Core.Domain namespace, also XML comments are in Source Repository</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DomainEntity : IEquatable&lt;DomainEntity&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> Guid ID { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">bool</span> Equals(DomainEntity other)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, other)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> other.ID.Equals(ID);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> Equals(<span style="color: #0000ff">object</span> obj)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">null</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">if</span> (ReferenceEquals(<span style="color: #0000ff">this</span>, obj)) <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">if</span> (obj.GetType() != GetType()) <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals((DomainEntity)obj);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> GetHashCode()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> ID.GetHashCode();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> ==(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">return</span> Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> <span style="color: #0000ff">operator</span> !=(DomainEntity left, DomainEntity right)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">return</span> !Equals(left, right);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>IDomainQuery</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IDomainQuery&lt;ENTITY&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">where</span> ENTITY : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    Expression&lt;Func&lt;ENTITY, <span style="color: #0000ff">bool</span>&gt;&gt; Expression { get; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<h3>FubuSamplePersistenceModel</h3>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Persistence</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FubuSamplePersistenceModel : PersistenceModel</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> FubuSamplePersistenceModel()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            addMappingsFromThisAssembly();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now that we have all the puzzle pieces in place we need to hook them up and make them do some work. Open up FubuSampleWebRegistry </p>

<p>And add the following code:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> configure()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSourceConfiguration&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;SQLServerSessionSourceConfiguration&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;ISessionSource&gt;().AsSingletons()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        .TheDefault.Is.ConstructedBy(ctx =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">                                     ctx.GetInstance&lt;ISessionSourceConfiguration&gt;()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">                                         .CreateSessionSource(<span style="color: #0000ff">new</span> FubuSamplePersistenceModel()));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;IUnitOfWork&gt;().TheDefault.Is.ConstructedBy(ctx =&gt; ctx.GetInstance&lt;INHibernateUnitOfWork&gt;());</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    ForRequestedType&lt;INHibernateUnitOfWork&gt;().CacheBy(InstanceScope.Hybrid)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        .TheDefault.Is.OfConcreteType&lt;NHibernateUnitOfWork&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    ForRequestedType&lt;IRepository&gt;().TheDefault.Is.OfConcreteType&lt;NHibernateRepository&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>With a little luck and all of our mad skills, we should be able to talk to the database now. So we need to plumb our simple product object up to talk to NHibernate. We will do this with a mapping file, and since we are using FluentNHibernate we can do this strongly typed.</p>

<p>Make the following modifications to Product: Extend DomainEntity and get rid of Id Property, it is inherited</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Product : DomainEntity</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> virtual <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual string</span> Description { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Add mapping file for Product:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">namespace</span> FubuSample.Core.Domain.Mapping</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ProductMap : ClassMap&lt;Product&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">public</span> ProductMap()</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Id(e =&gt; e.ID).GeneratedBy.GuidComb();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            Map(p =&gt; p.Name);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Map(p =&gt; p.Description);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            </pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>You may also want to setup your connection string at this point in the Web.Config file, mine looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&lt;add name=<span style="color: #006080">&quot;MYDBKEY&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=LOCALHOST;initial catalog=FubuSample;Integrated Security=SSPI;&quot;</span> providerName=<span style="color: #006080">&quot;System.Data.SqlClient&quot;</span> /&gt;</pre>
  </div>
</div>

<p>I also created an empty database in my local SQL Server instance called “FubuSample” And then I went back to SQLServerSessionSourceConfiguration and changed the value of IsNewDatabase to return true. This will create the schema for the database when the application first runs. There is a multitude of different ways that we can handle this, but this was easy for the tutorial purposes.</p>

<p>Next we will add a behavior to our core project and tell FubuMVC to apply that behavior to all or our ControllerActions.</p>

<p>Create a class named “access_the_database_through_a_unit_of_work” in your Core.Web.Behaviors namespace. The contents will look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> access_the_database_through_a_unit_of_work : IControllerActionBehavior</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> IUnitOfWork _unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> access_the_database_through_a_unit_of_work(IUnitOfWork unitOfWork)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork = unitOfWork;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    <span style="color: #0000ff">public</span> IControllerActionBehavior InsideBehavior { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> IInvocationResult Result { get; set; }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">public</span> OUTPUT Invoke&lt;INPUT, OUTPUT&gt;(INPUT input, Func&lt;INPUT, OUTPUT&gt; func)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">where</span> INPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">where</span> OUTPUT : <span style="color: #0000ff">class</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">    {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        _unitOfWork.Initialize();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">try</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            var output = InsideBehavior.Invoke(input, func);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            Result = InsideBehavior.Result;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Commit();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            <span style="color: #0000ff">return</span> output;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        <span style="color: #0000ff">catch</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">            _unitOfWork.Rollback();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            <span style="color: #0000ff">throw</span>;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">        <span style="color: #0000ff">finally</span></pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">            _unitOfWork.Dispose();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">        }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    }</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now to tell FubuMVC what to do, open up Global.asax.cs or your Bootstrapper, wherever you are doing the configuration of FubuMVC. In this project we are doing it in Global.asax.cs Add a new behavior to the section that starts with “x.ByDefault.EveryControllerAction” so it looks like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">x.ByDefault.EveryControllerAction(d =&gt;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">   {</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">       d.Will&lt;execute_the_result&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">       d.Will&lt;access_the_database_through_a_unit_of_work&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">   });</pre>
  </div>
</div>

<p>The last thing we will do is open up HomeController and add this code above the Index action:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">private</span> IRepository _repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> HomeController(IRepository repository)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository = repository;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>I also modified the Index action to go ahead and create some database values before we query for them just so we can test this thing. (I only did this for the tutorial, I swear) So modify the Index action to look like this:</p>

<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
  <div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">public</span> IndexViewModel Index(IndexSetupViewModel inModel)</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">{</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod1 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct1&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod1);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var prod2 = <span style="color: #0000ff">new</span> Product {Name = <span style="color: #006080">&quot;TestProduct2&quot;</span>, Description = <span style="color: #006080">&quot;This is a test product&quot;</span>};</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    _repository.Save(prod2);</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var outModel = <span style="color: #0000ff">new</span> IndexViewModel();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    var productList = _repository.Query&lt;Product&gt;();</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    outModel.Products = productList.ToList().Select(x =&gt; <span style="color: #0000ff">new</span> ProductDisplay(x));</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&#160;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none">    <span style="color: #0000ff">return</span> outModel;</pre>

    <pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">}</pre>
  </div>
</div>

<p>Now with you should be able to run your web app and browse to /home and all this will be executed and you will be on your way to creating a whole bunch of persist able objects for use with your FubuMVC app.</p>

<p>As always, feedback is welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/25/fubumvc-from-scratch-part-4-persistence/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>What I Really, Really, Really Like About Using Fluent NHibernate</title>
		<link>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-i-really-really-really-like-about-using-fluent-nhibernate</link>
		<comments>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 00:32:10 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/</guid>
		<description><![CDATA[Reusable Mappings Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files: Catalog: &#60;class name="Catalog" table="`Catalog`" optimistic-lock="version"&#62; &#60;id name="Id" column="Id" type="Guid"&#62; &#60;generator class="guid.comb" /&#62; &#60;/id&#62; &#60;version column="Version" name="Version" /&#62; &#60;property name="Name" length="100" type="String"&#62; &#60;column [...]]]></description>
			<content:encoded><![CDATA[<h3>Reusable Mappings</h3>
Something that slightly bothered me with the XML mappings of NHibernate, is the fact that some things need to be configured over and over again. Behold the following two mapping files:
<h4>Catalog:</h4>
<pre class="csharpcode">  <span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="Catalog"</span> <span class="attr">table</span><span class="kwrd">="`Catalog`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>
    ...
  <span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategory:</h4>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">class</span> <span class="attr">name</span><span class="kwrd">="CatalogCategory"</span> <span class="attr">table</span><span class="kwrd">="`CatalogCategory`"</span> <span class="attr">optimistic-lock</span><span class="kwrd">="version"</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">id</span> <span class="attr">name</span><span class="kwrd">="Id"</span> <span class="attr">column</span><span class="kwrd">="Id"</span> <span class="attr">type</span><span class="kwrd">="Guid"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">generator</span> <span class="attr">class</span><span class="kwrd">="guid.comb"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">id</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">version</span> <span class="attr">column</span><span class="kwrd">="Version"</span> <span class="attr">name</span><span class="kwrd">="Version"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="attr">length</span><span class="kwrd">="100"</span> <span class="attr">type</span><span class="kwrd">="String"</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">column</span> <span class="attr">name</span><span class="kwrd">="Description"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">property</span><span class="kwrd">&gt;</span>

   ...

<span class="kwrd">&lt;/</span><span class="html">class</span><span class="kwrd">&gt;</span></pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Suppose we have the luxury to choose how we deal with the database in our project. I do realize that's not always feasible because at some point in our careers, we've all seen what database masochism can do. Anyway, if would be making the shots, I would like to use a surrogate key for every table and I would recommend sequential GUIDs (called <a href="http://www.informit.com/articles/article.aspx?p=25862" target="_blank">COMBs</a>) for that.

The mapping of the classes shown above both have the same configuration for the <em>Id</em> and <em>Version</em> properties. These properties typically live in a <em>DomainEntity</em> base class of some sort because we don't want to repeat that tedious code of putting those in every entity of our domain over and over again. It would be nice if we could somehow do the same for the NHibernate mapping files, which we can't (or at least, not that I know of).

Using <a href="http://fluentnhibernate.org/" target="_blank">Fluent NHibernate</a> we can create an abstract mapping class for our <em>DomainEntity</em> from which we derive all entity mapping classes. The following code would give us the same result as the XML mapping files shown above:
<h4>DomainEntityMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> DomainEntityMapping&lt;TDomainEntity&gt; :
ClassMap&lt;TDomainEntity&gt; <span class="kwrd">where</span> TDomainEntity : DomainEntity
{
    <span class="kwrd">protected</span> DomainEntityMapping()
    {
        Id(entity =&gt; entity.Id, <span class="str">"Id"</span>)
            .GeneratedBy.GuidComb();
        OptimisticLock.Version();
        Version(entity =&gt; entity.Version)
            .TheColumnNameIs(<span class="str">"Version"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogMapping : DomainEntityMapping&lt;Catalog&gt;
{
  <span class="kwrd">public</span> CatalogMapping()
  {
      Map(catalog=&gt; catalog.Name, <span class="str">"Name"</span>);
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->
<h4>CatalogCategoryMapping:</h4>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CatalogCategoryMapping : DomainEntityMapping&lt;CatalogCategory&gt;
{
    <span class="kwrd">public</span> CatalogCategoryMapping()
    {
        Map(category=&gt; category.Description, <span class="str">"Name"</span>);
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

This way,  I can specify the mapping configuration for <em>Id</em> and <em>Version</em> in a single place which is really nice.
<h3>Mapping Tests</h3>
This is probably my most favorite feature of Fluent NHibernate. Given the fluent mapping configuration for the Category class shown earlier, the following test checks whether this mapping is valid or not:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> When_verifying_the_class_mapping_of_a_catalog
    : NHibernateSpecification
{
  [Test]
  <span class="kwrd">public</span> <span class="kwrd">void</span> Then_a_catalog_object_should_be_persistable()
  {
      <span class="kwrd">new</span> PersistenceSpecification&lt;Catalog&gt;(Session)
          .VerifyTheMappings();
  }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Running this test results in the following SQL statements being executed to an in-memory SQLite database:

NHibernate: INSERT INTO "Catalog" (Version, Name, Id) VALUES (@p0, @p1, @p2); @p0 = '1', @p1 = '', @p2 = 'c52126cb-f11e-47e4-a481-9bc600134d39'
NHibernate: SELECT catalog0_.Id as Id1_0_, catalog0_.Version as Version1_0_, catalog0_.Name as Name1_0_ FROM "Catalog" catalog0_ WHERE catalog0_.Id=@p0; @p0 = 'c52126cb-f11e-47e4-a481-9bc600134d39'

The following code shows the NHibernateSpecification base class for all my database tests. The code in this class deals with setting up the in-memory SQLite database and building the required schema based on the mappings. The Fluent NHibernate framework gives some really nice support for this as well:
<pre class="csharpcode">[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> NHibernateSpecification
    : Specification
{
    <span class="kwrd">protected</span> ISession Session { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Establish_context()
    {
        var config = <span class="kwrd">new</span> SQLiteConfiguration()
            .InMemory()
            .ShowSql()
            .ToProperties();

        var sessionSource = <span class="kwrd">new</span> SessionSource(config,
            <span class="kwrd">new</span> RetailerPersistenceModel());

        Session = sessionSource.CreateSession();
        sessionSource.BuildSchema(Session);

        ProvideInitialData(Session);

        Session.Flush();
        Session.Clear();
   }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Dispose_context()
    {
        <span class="kwrd">if</span>(<span class="kwrd">null</span> != Session)
        {
            Session.Dispose();
            Session = <span class="kwrd">null</span>;
        }
    }
}</pre>
<!-- .csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
-->

Fluent NHibernate really lowers the barrier for configuring NHibernate, which is a really big thing in my book. I must admit that I was a bit sceptical at first, but now I noticed that I'm having a hard time going back to the standard XML mapping configuration of NHibernate itself. As Fluent NHibernate is still in an early development stage, some issues can come up. But my personal experience so far is that the <a href="http://groups.google.com/group/fluent-nhibernate?hl=en" target="_blank">user group</a> is very responsive.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/03/07/what-i-really-really-really-like-about-using-fluent-nhibernate/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Stored Procedure Patterns</title>
		<link>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-nhibernate-for-legacy-databases</link>
		<comments>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:32:11 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/</guid>
		<description><![CDATA[One of the downsides of being confronted with a shared legacy database day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides of being confronted with a <a href="http://elegantcode.com/2009/03/28/about-a-shared-database/">shared legacy database</a> day in and day out is that you have to map your domain objects to database tables that are also used by other applications. A typical scenario in this case is that those database tables contain more columns than those that are required for your application. These extra columns are specifically there to serve those other legacy applications. Heck, to make matters even worse, there are probably some new columns added specifically for your application as well. This is the fairy tale of shared legacy databases. </p> <p>Using NHibernate in these scenarios can be challenging sometimes but its built in flexibility and extensibility really helps you to deal with those cases. The problem I ran into last week was that we needed to store a domain object into a table that had a lot more columns than were actually required for our application. If it would be possible to store null values in these columns or if they had default values configured for them in the schema, then this would not be a problem. Instead, these unnecessary columns could not store null values and had no default values associated with them. </p> <p>First option would be to make some changes to the schema of the table. Alas, no luck there because the other legacy applications that are using the same table would break. Now what?</p> <p>We needed to insert the default values ourselves, but those columns are not known by NHibernate because they are not mapped to any members of the domain object. One way to solve this, is to pollute the domain object by adding private fields that are initialized to the required default values.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainEntity
{
    <span class="rem">// Legacy fields with no purpose for the domain but required </span>
    <span class="rem">// by the database.</span>
    <span class="kwrd">private</span> Int32 _legacyField1 = 2;
    <span class="kwrd">private</span> Boolean _legacyField2 = <span class="kwrd">false</span>;
    <span class="kwrd">private</span> String _legacyField3 = <span class="str">""</span>;
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably the simplest option, but imposes a <a href="http://en.wikipedia.org/wiki/Fixing_Broken_Windows">broken window</a> as infrastructure concerns are bleeding into the domain this way. In other words, this is not a viable solution. Keeping the legacy stuff isolated as much as possible, NHibernate provides some ways to deal with this by providing an extensive extensibility model.</p>
<p>After some snooping around in the source code of NHibernate, the solution we chose for dealing with this issue is by creating a custom access strategy. The built in property access strategies are probably already well known, but its also possible to write your own access strategy by implementing the <em>IPropertyAccessor</em> interface.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeDomainObjectAccessor : IPropertyAccessor
{
    <span class="kwrd">private</span> IEnumerable&lt;IGetter&gt; _defaultValueGetters;
    
    <span class="kwrd">public</span> SomeDomainObjectAccessor()
    {
        _defaultValueGetters = <span class="kwrd">new</span> List&lt;IGetter&gt;()
        {
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Int32&gt;(<span class="str">"LegacyColumn1"</span>, 2) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;Boolean&gt;(<span class="str">"LegacyColumn2"</span>, <span class="kwrd">false</span>) }
            { <span class="kwrd">new</span> DefaultValueGetter&lt;String&gt;(<span class="str">"LegacyColumn3"</span>, 2) }
        }
    }

    <span class="kwrd">public</span> IGetter GetGetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> _defaultValueGetters        
            .Where(getter =&gt; getter.PropertyName == propertyName)        
            .SingleOrDefault();
    }
    
    <span class="kwrd">public </span>ISetter GetSetter(Type type, String propertyName)
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> NoopSetter();
    }

    <span class="kwrd">public</span> Boolean CanAccessTroughReflectionOptimizer
    {
        get { <span class="kwrd">return</span> <span class="kwrd">true</span>; }
    }
}

<span class="kwrd">private</span> <span class="kwrd">class</span> DefaultValueGetter&lt;T&gt; : IGetter
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> String _propertyName;
    <span class="kwrd">private</span> T Value { get; set; }

    <span class="kwrd">public</span> DefaultValueGetter(String propertyName, T <span class="kwrd">value</span>)
    {
        _propertyName = propertyName;
        Value = <span class="kwrd">value</span>;
    }

    <span class="kwrd">public</span> Object Get(Object target)
    {
        <span class="kwrd">return</span> Value;
    }

    <span class="kwrd">public</span> Type ReturnType
    {
        get { <span class="kwrd">return</span> <span class="kwrd">typeof</span>(T); }
    }

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> _propertyName; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get
        {
            var method = <span class="kwrd">typeof</span>(BasicPropertyAccessor)              
                .GetMethod(<span class="str">"GetGetterOrNull"</span>,
                           BindingFlags.Static | BindingFlags.NonPublic);

            var result = (BasicPropertyAccessor.BasicGetter)method              
                .Invoke(<span class="kwrd">null</span>, <span class="kwrd">new</span> Object[] { GetType(), <span class="str">"Value"</span> });

            <span class="kwrd">return</span> result.Method;
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> GetForInsert(Object owner, IDictionary mergeMap,
                               ISessionImplementor session)
    {
        <span class="kwrd">return</span> Get(owner);
    }
}

<span class="kwrd">private</span> <span class="kwrd">sealed</span> <span class="kwrd">class</span> NoopSetter : ISetter
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Set(Object target, Object <span class="kwrd">value</span>)
    {}

    <span class="kwrd">public</span> String PropertyName
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }

    <span class="kwrd">public</span> MethodInfo Method
    {
        get { <span class="kwrd">return</span> <span class="kwrd">null</span>; }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This simply involves a <em>getter</em> for providing default values and a dummy <em>setter</em> as we're not interested in setting any values on the domain objects. The <em>DefaultValueGetter</em> class uses a trick so that we can keep using the reflection optimizer of NHibernate. This also seems to be necessary when using NHibernate Profiler. </p>
<p>Now we only have to provide some properties in the mapping of the domain object using our custom access strategy:</p><pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn1"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Int32"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn2"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="Boolean"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span>
          
<span class="kwrd">&lt;</span><span class="html">property</span> 
    <span class="attr">name</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">column</span><span class="kwrd">="LegacyColumn3"</span>
    <span class="attr">not-null</span><span class="kwrd">="true"</span>
    <span class="attr">type</span><span class="kwrd">="String"</span>
    <span class="attr">access</span><span class="kwrd">="SomeNamespace.SomeDomainObjectAccessor, SomeAssembly"</span><span class="kwrd">/&gt;</span></pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>This is probably not the best solution, but it does the job and prevents polluting the domain objects as a result of database quirks like these. I'm interested in hearing feedback or any better approaches. </p>
<p>Anyway, the easy extensibility of NHibernate makes it the best data access solution around. This way, one can deal with all edge case scenarios that weren't anticipated by the framework builders. </p>
<p>Till next time&nbsp;&nbsp; </p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/13/using-nhibernate-for-legacy-databases/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; NHibernate</title>
	<atom:link href="http://elegantcode.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NHibernate, HttpModules, ASP.NET JSON Web Services and Database Transactions</title>
		<link>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions</link>
		<comments>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:08:03 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/</guid>
		<description><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play: An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor ) NHibernate The use [...]]]></description>
			<content:encoded><![CDATA[First of all sorry for the frankly appalling title which is actually longer than some of my posts. In order to explain let’s start with a problem definition. These are the elements that were in play:
<ol>
	<li>An ASP.NET 3.5 Web Forms based website (although #3 below is really the key factor )</li>
	<li>NHibernate</li>
	<li>The use of an HttpModule to implement the <a href="https://www.hibernate.org/43.html">Open Session in View</a> pattern and provide a Unit of Work implementation (one ISession per HTTP request)</li>
	<li>ASP.NET web services returning JSON (might well be a problem if the result was XML as well)</li>
</ol>
In an “old fashioned” Web Forms scenario everything works perfectly. If some unexpected event occurs during a postback then an Exception is thrown and it bubbles up to the HttpModule which rolls back the transaction.

However with a JSON web service (we’re not using WCF so we just have an System.Web.Services.WebService flagged with a ScriptService attribute) when an unexpected event occurs the magic of the framework catches it and the ScriptMethod simply returns the Exception converted to JSON. This was being correctly handled in our UI but because an Exception is not thrown so the HttpModule thinks that everything is OK and incorrectly commits our transaction. And because processing didn’t necessarily finish and our data could therefore get into an inconsistent state (which is even worse than a plain wrong state).

In order to work around this feature I hooked into the EndRequest event in the HttpModule and check the StatusCode of the Repsonse. Here’s the method that I created:
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">bool</span> IsAjaxError()
{
  <span class="kwrd">return</span> HttpContext.Current.Response.StatusCode == 500 &amp;&amp; HttpContext.Current.Request.RawUrl.Contains(<span class="str">".asmx"</span>);
}</pre>
<!--.csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Obviously if an error is detected I can take the appropriate action. So if you’re using the above combination of technologies be careful because what you think is happening and what is happening might be two entirely different things.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/18/nhibernate-httpmodules-asp-net-json-web-services-and-database-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Database Change Management with Tarantino</title>
		<link>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=database-change-management-with-tarantino</link>
		<comments>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 10:17:12 +0000</pubDate>
		<dc:creator>Ryan Kelley</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/</guid>
		<description><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard. Why [...]]]></description>
			<content:encoded><![CDATA[Seeing as how I have not graced these pages with my presence in quite some time. I figured I better get something going. A little while back my team started working with some outside contractors and our make shift way of keeping changes in sync and under control was simply not cutting the mustard.
<h2>Why do we need DB Change management</h2>
Okay then Mr. Developer (or Mrs.), Why do we need source control? We need database change management for the same reason we need source control. To help manage the code and dependencies our applications rely on.
<h4>Recipes for Disaster:</h4>
<blockquote>Team of developers all working/developing off of a central database</blockquote>
<blockquote>Team of developers working/developing off of local database, with no way to keep changes in sync.</blockquote>
So what’s the problem with these recipes? As new features are developed for an application it is very likely that the database will need schema changes to support these features. These could be as simple as new tables or adding a column. If we have schema changes, we have to make sure that the code base for the app is deployed concurrently with the schema changes.

When deploying software to production, code files and libraries can usually be overwritten with a new version. Databases however must be updated intelligently.
<h2>So we’ve got a problem, do we have a solution?</h2>
Enter Tarantino. I was talking with Jarod F. and he said he had heard some stuff about it, but had never used it so I started digging around and found enough pieces to get it working. So first thing I did was head over to the <a href="http://code.google.com/p/tarantino/" target="_blank">Tarantino project</a> site. Starting reading around and then i did a little googling and managed to dig up enough information to really wet my appetite and to get it working. Then Eric Hexter was kind enough to give me some time via IM and cleared up several areas for me.

Before I get into the specifics of how to set all this up, I want to explain how Tarantino finds and keeps changes in sync.
<ol>
	<li>Tarantino is a forward only change management system, you can’t roll back.</li>
	<li>If you use Tarantino, you can’t go make changes by hand manually</li>
	<li>Tarantino uses RedGate SQL Compare if you want, to compare DB Schemas</li>
	<li>Tarantino makes a table in every DB it touches to keep track of what scripts have been executed on that DB</li>
</ol>
The system is based on a set of conventions which allow incremental changes to the database schema. The conventions are to create two subdirectories in your database scripts folder. Create– this is where your inital schema change scripts go. Update – this is where your change scripts should be placed. They should be named with the following convention ####SCRIPTNAME.sql where #### is the script number with leading zeros. This will ensure the first script 0001_first_schema_change.sql would be executed first. There is a third folder, ExistingSchema, that you will need if you are going to start using Tarantino on an app that is already in development or production.

There are two ways to use Tarantino, command line, and as a NAnt task. I will be showing the NAnt task method, as it has the most examples available on the net.
<h4>Project Setup</h4>
So what I have done, is created a very simple project. A class library with a Fluent NHibernate configuration, a test fixture to create the database schema, and two simple entities which I have listed below:
<div class="csharpcode">
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Order</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> OrderNumber { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> PONumber { get; set; }</pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> IList&lt;OrderItem&gt; OrderItems { get; set;}</pre>
<pre></pre>
<pre class="alt">   <span class="kwrd">public</span> Order()</pre>
<pre>   {</pre>
<pre class="alt">       OrderItems = <span class="kwrd">new</span> List&lt;OrderItem&gt;();</pre>
<pre>   }</pre>
<pre class="alt">}</pre>
<pre></pre>
<pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> OrderItem</pre>
<pre>{</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Id { get; set; }</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">string</span> ItemName { get; set; }</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Description { get; set; }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

and my NHibernate and Test Fixture setup:
<div class="csharpcode">
<pre class="alt">[TestFixture]</pre>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> DB_Setup_Fixture</pre>
<pre class="alt">{</pre>
<pre>    <span class="kwrd">private</span> Configuration _configuration;</pre>
<pre class="alt"></pre>
<pre>    [TestFixtureSetUp]</pre>
<pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> FixtureSetup()</pre>
<pre>    {</pre>
<pre class="alt"></pre>
<pre>        _configuration = Fluently.Configure()</pre>
<pre class="alt">            .Database(MsSqlConfiguration.MsSql2008</pre>
<pre>                          .ConnectionString(c =&gt;</pre>
<pre class="alt">                                            c.FromConnectionStringWithKey(<span class="str">"testData"</span>))</pre>
<pre>                          .UseReflectionOptimizer()</pre>
<pre class="alt">                          .ShowSql())</pre>
<pre>            .Mappings(m =&gt;</pre>
<pre class="alt">                      m.AutoMappings.Add(AutoMap.AssemblyOf&lt;Order&gt;()</pre>
<pre>                                             .Where(x =&gt; x.Namespace.EndsWith(<span class="str">"Domain"</span>))))</pre>
<pre class="alt">            .BuildConfiguration();</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre></pre>
<pre class="alt">    [Test, Explicit, Category(<span class="str">"DBSetup"</span>)]</pre>
<pre>    <span class="kwrd">public</span> <span class="kwrd">void</span> Create_DB_Schema()</pre>
<pre class="alt">    {</pre>
<pre>        var exporter = <span class="kwrd">new</span> SchemaExport(_configuration);</pre>
<pre class="alt">        exporter.Create(<span class="kwrd">true</span>, <span class="kwrd">true</span>);</pre>
<pre></pre>
<pre class="alt">    }</pre>
<pre>}</pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --> <!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<h5>The one thing to make note of here, is that my test is set to explicit, and I gave it a category of “DBSetup”. This will be important once we start configuring NAnt and Tarantino.</h5>
So now we have a very simple application that we can use to play around with Tarantino. To get Tarantino working, I am using the modified NAnt files that CodeCampServer is using. I just copied over their NAnt folder they have checked into source control. There are two NAnt build files that are required to use Tarantino. Disclaimer: I am a total NAnt noob, this is not my usual build utility, but I figured out enough to make it work.
<h4>Common.build</h4>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database change management --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="rebuildDatabase"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="updateDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Update"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Create"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="dropDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="action"</span> <span class="attr">value</span><span class="kwrd">="Drop"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="manageSqlDatabase"</span> <span class="attr">failonerror</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="manageSqlDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="${action}"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="${database.integrated}"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}"</span></pre>
<pre class="alt">      <span class="attr">username</span><span class="kwrd">="${database.username}"</span></pre>
<pre>      <span class="attr">password</span><span class="kwrd">="${database.password}"</span></pre>
<pre class="alt">        <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${action != 'Drop'}"</span><span class="kwrd">&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Current Database Version: ${usdDatabaseVersion}"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt"></pre>
<pre>  <span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">language</span><span class="kwrd">="C#"</span> <span class="attr">prefix</span><span class="kwrd">="migration"</span> <span class="kwrd">&gt;</span></pre>
<pre class="alt">    &lt;references&gt;</pre>
<pre>      &lt;include name=<span class="str">"System.IO.dll"</span> /&gt;</pre>
<pre class="alt">    &lt;/references&gt;</pre>
<pre>    &lt;code&gt;</pre>
<pre class="alt">      &lt;![CDATA[</pre>
<pre>              [Function(<span class="str">"next-migration-number"</span>)]</pre>
<pre class="alt">              <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> NextMigration(<span class="kwrd">string</span> path  ) {</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">string</span>[] files = System.IO.Directory.GetFiles(path);</pre>
<pre></pre>
<pre class="alt">                  <span class="kwrd">int</span> count=1;</pre>
<pre></pre>
<pre class="alt">                    <span class="kwrd">if</span>(files.Length &gt; 0)</pre>
<pre>                    {</pre>
<pre class="alt">                        <span class="kwrd">string</span> filename = System.IO.Path.GetFileName(files[files.Length-1]);</pre>
<pre></pre>
<pre class="alt">                        <span class="kwrd">try</span></pre>
<pre>                        {</pre>
<pre class="alt">                            count = Convert.ToInt32(filename.Substring(0, 4));</pre>
<pre>                            count++;</pre>
<pre class="alt"></pre>
<pre>                            <span class="kwrd">if</span>(count%2 == 0)</pre>
<pre class="alt">                                count++;</pre>
<pre>                        }</pre>
<pre class="alt">                        <span class="kwrd">catch</span></pre>
<pre>                        {</pre>
<pre class="alt"></pre>
<pre>                        }</pre>
<pre class="alt">                    }</pre>
<pre>                  <span class="kwrd">return</span> <span class="kwrd">string</span>.Format(<span class="str">"{0:0000}"</span>, count);</pre>
<pre class="alt">              }</pre>
<pre>            ]]&gt;</pre>
<pre class="alt">    &lt;/code&gt;</pre>
<pre>  <span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

This file sets up most of the plumbing that hooks up the Tarantino NAnt targets for use in the nant.build file
<h4>NAnt.build</h4>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->
<div class="csharpcode">
<pre class="alt"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="utf-8"</span>?<span class="kwrd">&gt;</span></pre>
<pre><span class="rem">&lt;!--EXTERNAL_PROPERTIES: usdDatabaseVersion, CCNetLabel--&gt;</span></pre>
<pre class="alt"><span class="kwrd">&lt;</span><span class="html">project</span> <span class="attr">name</span><span class="kwrd">="TarantinoSample"</span> <span class="attr">xmlns</span><span class="kwrd">="http://nant.sf.net/schemas/nant.xsd"</span><span class="kwrd">&gt;</span></pre>
<pre>      <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Initialization --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ************** --&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">buildfile</span><span class="kwrd">="common.build"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">loadtasks</span> <span class="attr">assembly</span><span class="kwrd">="lib\nant\Tarantino.Nant.Tasks.dll"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- Master Properties --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- ***************** --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="company.name"</span> <span class="attr">value</span><span class="kwrd">="Tarantino Test"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="assembly.unittests"</span> <span class="attr">value</span><span class="kwrd">="TarantinoSample.dll"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">    <span class="rem">&lt;!-- Version settings --&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="project.config"</span> <span class="attr">value</span><span class="kwrd">="debug"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Folder references --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.solution"</span> <span class="attr">value</span><span class="kwrd">="src"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="dir.build"</span> <span class="attr">value</span><span class="kwrd">="build"</span> <span class="attr">dynamic</span><span class="kwrd">="true"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Compilation settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="nant.settings.currentframework"</span> <span class="attr">value</span><span class="kwrd">="net-3.5"</span> <span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="file.solution"</span> <span class="attr">value</span><span class="kwrd">="${dir.solution}/${project::get-name()}.sln"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- Database migration settings --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.script.directory"</span> <span class="attr">value</span><span class="kwrd">="dbChangeScripts"</span> <span class="kwrd">/&gt;</span></pre>
<pre>      <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.server"</span> <span class="attr">value</span><span class="kwrd">="localhost"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.name"</span> <span class="attr">value</span><span class="kwrd">="OrderData"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.integrated"</span> <span class="attr">value</span><span class="kwrd">="true"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.username"</span> <span class="attr">value</span><span class="kwrd">="dbuser"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>    <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="database.password"</span> <span class="attr">value</span><span class="kwrd">="P@ssword1"</span> <span class="attr">overwrite</span><span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="rem">&lt;!-- Database Change Management Targets --&gt;</span></pre>
<pre>    <span class="rem">&lt;!-- ********************************** --&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="reset-database"</span> <span class="attr">depends</span><span class="kwrd">="dropDatabase, createDatabase"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">nunit2</span> <span class="attr">failonerror</span><span class="kwrd">="true"</span> <span class="attr">verbose</span><span class="kwrd">="true"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">formatter</span> <span class="attr">type</span><span class="kwrd">="Plain"</span> <span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">test</span> <span class="attr">assemblyname</span><span class="kwrd">="${dir.solution}/bin/${project.config}/${assembly.unittests}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">                <span class="kwrd">&lt;</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre>                    <span class="kwrd">&lt;</span><span class="html">include</span> <span class="attr">name</span><span class="kwrd">="DBSetup"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">                    <span class="kwrd">&lt;</span><span class="html">exclude</span> <span class="attr">name</span><span class="kwrd">="*"</span><span class="kwrd">/&gt;</span></pre>
<pre>                <span class="kwrd">&lt;/</span><span class="html">categories</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;/</span><span class="html">test</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">nunit2</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="db-migration"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">delete</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span>  <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="reset-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">call</span> <span class="attr">target</span><span class="kwrd">="create-versioned-database"</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="migrationScriptName"</span> <span class="attr">value</span><span class="kwrd">="${migration::next-migration-number(database.script.directory+'/Update')}_AutoGeneratedMigration.sql"</span><span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files (x86)\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists('c:\program files\red gate\SQL Compare 8\SQLCompare.exe')}"</span> <span class="kwrd">&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">property</span> <span class="attr">name</span><span class="kwrd">="redgate.exe"</span> <span class="attr">value</span><span class="kwrd">="c:\program files\red gate\SQL Compare 8\SQLCompare.exe"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">exec</span></pre>
<pre>              <span class="attr">program</span><span class="kwrd">="${redgate.exe}"</span></pre>
<pre class="alt">              <span class="attr">commandline</span><span class="kwrd">="/f /v /server1:${database.server} /server2:${database.server} /database1:${database.name} /database2:${database.name}Versioned /scriptfile:${database.script.directory}/Update/_New_Script.sql /exclude:Table:usd_AppliedDatabaseScript"</span></pre>
<pre>              <span class="attr">resultproperty</span><span class="kwrd">="execReturnCode"</span></pre>
<pre class="alt">              <span class="attr">failonerror</span> <span class="kwrd">="false"</span><span class="kwrd">/&gt;</span></pre>
<pre>          <span class="kwrd">&lt;</span><span class="html">fail</span> <span class="attr">if</span><span class="kwrd">="${execReturnCode != '0' and execReturnCode != '63'}"</span> <span class="attr">message</span><span class="kwrd">="Redgate Compare Failed!"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql') == false}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">=""</span><span class="kwrd">/&gt;</span></pre>
<pre>              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="---------------------"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">              <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="No Migration Required"</span><span class="kwrd">/&gt;</span></pre>
<pre>        <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">if</span> <span class="attr">test</span><span class="kwrd">="${file::exists(database.script.directory + '/Update/_New_Script.sql')}"</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">            <span class="kwrd">&lt;</span><span class="html">move</span> <span class="attr">file</span><span class="kwrd">="${database.script.directory}/Update/_New_Script.sql"</span> <span class="attr">tofile</span><span class="kwrd">="${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre>            <span class="kwrd">&lt;</span><span class="html">echo</span> <span class="attr">message</span><span class="kwrd">="Created Migration File ${database.script.directory}/Update/${migrationScriptName}"</span><span class="kwrd">/&gt;</span></pre>
<pre class="alt">          <span class="kwrd">&lt;/</span><span class="html">if</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre></pre>
<pre class="alt">      <span class="kwrd">&lt;</span><span class="html">target</span> <span class="attr">name</span><span class="kwrd">="create-versioned-database"</span><span class="kwrd">&gt;</span></pre>
<pre>        <span class="kwrd">&lt;</span><span class="html">tstamp</span><span class="kwrd">&gt;&lt;/</span><span class="html">tstamp</span><span class="kwrd">&gt;</span></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">action</span><span class="kwrd">="Drop"</span></pre>
<pre class="alt">            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre>            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre class="alt">            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre>            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre class="alt">            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre></pre>
<pre class="alt">        <span class="kwrd">&lt;</span><span class="html">manageSqlDatabase</span></pre>
<pre>            <span class="attr">scriptDirectory</span><span class="kwrd">="${database.script.directory}"</span></pre>
<pre class="alt">            <span class="attr">action</span><span class="kwrd">="Create"</span></pre>
<pre>            <span class="attr">server</span><span class="kwrd">="${database.server}"</span></pre>
<pre class="alt">            <span class="attr">integratedAuthentication</span><span class="kwrd">="true"</span></pre>
<pre>            <span class="attr">database</span><span class="kwrd">="${database.name}Versioned"</span></pre>
<pre class="alt">            <span class="attr">username</span><span class="kwrd">=""</span></pre>
<pre>            <span class="attr">password</span><span class="kwrd">=""</span> <span class="kwrd">/&gt;</span></pre>
<pre class="alt"></pre>
<pre>      <span class="kwrd">&lt;/</span><span class="html">target</span><span class="kwrd">&gt;</span></pre>
<pre class="alt"></pre>
<pre></pre>
<pre class="alt"><span class="kwrd">&lt;/</span><span class="html">project</span><span class="kwrd">&gt;</span></pre>
</div>
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->

Inside this file there are a few pieces that need to be configured for use in your own app, these are all listed under “Master Properties”
<h4>Change script creation</h4>
So without going into too many details, what I have done is created my initial schema using my Unit Test. From here I could script this out to a text file, but what I did was use SQL Compare, comparing my initial schema to an empty database. This gave me my initial SQL script that creates my database.

Just to verify that my initial schema creation script is good, I will have Tarantino create the Versioned database for me using this command:
<blockquote>lib\nant\nant.exe –buildfile:nant.build create-versioned-database</blockquote>
Tarantino then looks in the ExistingSchema folder, runs any scripts found there, then does the same thing for the Updates folder.

In order to test out the change management features, I made a small change to my domain, adding a decimal of cost to the OrderItem class should do the trick. Rebuild my solution, then back to the command line to run my “Create Database Migration Script.bat” file. This file is just running the NAnt task.

What happens then, is NAnt goes out and runs my DBSetup test that is in my project. Which recreates the “OrderData” database. Tarantino then creates the “Versioned” database, then uses SQL Compare to find the changes between the two, and creates a file in the update folder for me named, <strong>0001_AutoGeneratedMigration.sql</strong> <span style="color: #ff0000;">NOTE: It is important to rename this file before it is committed to source control, or before you apply these changes to a database. Tarantino tracks the scripts that have been run by filename.</span>
<h4>Push to Production or Dev</h4>
So I have mentioned two databases so far in this post, OrderData and OrderDataVersioned. These two databases contain no data, that’s not to say that they couldn’t, if you had scripts that put it in. But that is not what they were designed to do. Our app.config is pointing to OrderData, so when we ask NHibernate to build the schema, it drops all the tables and recreates them, losing all data. OrderDataVersioned, is Tarantinos database that is used to keep changes in sync. In our development environment, every dev has a third database, we would call OrderDataProd for instance. This database could have sample data and other information in it. Let’s say, we don’t want to lose our data in this DB so what we do is let Tarantino handle the first two databases, then we deploy our schema changes to our development database. We use this same technique to push changes to production, after backups of course.

We can do this easily with the following command:
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build updateDatabase –D:database.name=OrderDataProd</span></blockquote>
Now when I tried to run this, my database OrderDataProd did not exist, so I went to SQL manager, created it, and then ran the command again, only to be slammed with another error. Since the database existed, Tarantino did not run the create schema script, only the update, which created another error. The next command will create the database I want, by executing the schema creation script, then applying any updates to it.
<blockquote><span style="background-color: #ffffff;">lib\nant\nant.exe –buildfile:nant.build createDatabase –D:database.name=OrderDataProd</span></blockquote>
Bingo! I now have my 3rd database that I can dump data into and start developing on top of. Just to be sure that we can create another change script, and deploy it to this database, I will modify the domain again, by adding a quantity to the OrderItem object. I will leave this step up to you to try out.
<h4>Source Code</h4>
I know the blog formatting sometimes jacks up code samples a lot so I am making all the source for this post available two ways:
<ul>
	<li><a href="http://elegantcode.googlecode.com/files/TarantinoTest.zip" target="_blank">Zip File Download</a></li>
	<li><a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/RyanKelley/TarantinoTest" target="_blank">Elegant Code SVN Repository</a></li>
</ul>
<h3>Information Sources</h3>
<a href="http://www.lostechies.com/blogs/hex/default.aspx" target="_blank">Eric Hexter</a> – Huge Help

<a href="http://code.google.com/p/codecampserver/" target="_blank">CodeCampServer</a>

<a href="http://jeffreypalermo.com/blog/tarantino-project-rsquo-s-database-migrations-screencast/" target="_blank">Palermo and Hexter Screencast on Tarantino</a>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/28/database-change-management-with-tarantino/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why Future&lt;T&gt; should be in your future</title>
		<link>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-futuret-should-be-in-your-future</link>
		<comments>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 17:50:24 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/</guid>
		<description><![CDATA[Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature [...]]]></description>
			<content:encoded><![CDATA[<p>Even though NHibernate 3.0 is on the way I am going to highlight a feature that was introduced in v2.1 but that relatively few people seem to be using (at least in the code bases I have seen recently). Former ECer Davy Brion, as always, does an excellent job of demonstrating usage of the feature <a href="http://davybrion.com/blog/2009/01/nhibernate-and-future-queries/">here</a> so I am not going to rehash that. What I am going to focus on here if the why.</p>  <p>Building a UI can be a very database intensive operation in terms of the number of separate calls required. And for each one of those calls the time taken to actually execute the query can be a small compared to the duration of the roundtrip to request and fetch the data. The obvious solution is some type of batching and with Future&lt;T&gt; this happens automagically without any change to the consuming logic (for databases that support it). Let me restate that, you get a huge performance benefit with only a simple change to your query methods in the Repository. So why aren’t you using it?</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/16/why-futuret-should-be-in-your-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NHibernate 3.0 QueryOver</title>
		<link>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-3-0-queryover</link>
		<comments>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 19:23:53 +0000</pubDate>
		<dc:creator>Jason Grundy</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/</guid>
		<description><![CDATA[One of the personal reasons that I had for co-founding Guild 3 was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in Avoiding (Or Recovering From) Burnout. For me the age old adage of “a change is as good as a rest” [...]]]></description>
			<content:encoded><![CDATA[One of the personal reasons that I had for co-founding <a href="http://guild3.com">Guild 3</a> was for me to re-discover my passion for software. I was suffering from something similar to what Davy Brion (quite bravely) outlined in <a href="http://davybrion.com/blog/2009/09/avoiding-or-recovering-from-burnout/">Avoiding (Or Recovering From) Burnout</a>. For me the age old adage of “a change is as good as a rest” has proven to be an extremely successful strategy.

One of the things that I had stopped doing was keeping an eye on various OSS projects to see what was on the horizon. Yesterday I (finally!) started to experiment with NHibernate 3.0. The first thing that caught my eye was <a href="http://fabiomaulo.blogspot.com/2009/06/criteria-on-nh300.html">QueryOver</a>. As Fabio explained there are a <a href="http://fabiomaulo.blogspot.com/2009/09/nhibernate-queries.html">lot of different ways of executing queries in NH</a>. Certainly in the pre-LINQ days ICriteria was the predominantly recommended option because it has elements of type safety to it and its fluent-ish API broke everything down into small pieces and avoided string concatenation hell.

QueryOver is fluent a layer on top of ICriteria. It looks very LINQesque but it’s a very different animal, not least of all because there are some concepts in ICriteria that do not have a LINQ equivalent (caching etc.). In the short to medium term I suspect that it will become my de facto approach for NHibernate queries (I’ve used NHibernate LINQ and it’s great for simple queries but I’ve experienced significant issues with more complicated ones).

What I haven’t figured out yet is how, if at all, this will affect my data access testing strategy. Historically I’ve favored smoke tests that have really been doing little more than verifying that a given ICriteria query was semantically valid. Typically I only resorted to actually worrying about the results in specific cases (mainly due to the burden of maintaining test data for each possible scenario). Then again this sounds like a topic for another post doesn’t it…]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/12/nhibernate-3-0-queryover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate 2.1 and Collection Event Listeners</title>
		<link>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nhibernate-2-1-and-collection-event-listeners</link>
		<comments>http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 00:21:37 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/19/nhibernate-2-1-and-collection-event-listeners/</guid>
		<description><![CDATA[In a previous post, I talked about cascading deletes being a new feature introduced by NHibernate 2.0. If you haven't heard about this before, then you'd probably be interested to read about it first. Cascading deletes are all great if your database of choice supports CASCADE DELETE foreign key constraints. But what if it doesn't [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://elegantcode.com/2008/04/12/nhibernate-20-
