<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Valeriu Caraulean &#187; NHibernate</title>
	<atom:link href="http://blog.caraulean.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.caraulean.com</link>
	<description>Keep it simple. It will work...</description>
	<lastBuildDate>Fri, 10 Feb 2012 04:38:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.caraulean.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Valeriu Caraulean &#187; NHibernate</title>
		<link>http://blog.caraulean.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.caraulean.com/osd.xml" title="Valeriu Caraulean" />
	<atom:link rel='hub' href='http://blog.caraulean.com/?pushpress=hub'/>
		<item>
		<title>NHibernate 3.0 will be a very nice release</title>
		<link>http://blog.caraulean.com/2009/12/21/nhibernate-3-0-will-be-a-very-nice-release/</link>
		<comments>http://blog.caraulean.com/2009/12/21/nhibernate-3-0-will-be-a-very-nice-release/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 14:05:10 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://varely.wordpress.com/2009/12/21/nhibernate-3-0-will-be-a-very-nice-release/</guid>
		<description><![CDATA[First of all, Steve Strong made a Christmas gift to community by presenting a new Linq implementation for NHibernate, which is now incorporated in the trunk. Kudos for a such great work! Another valuable addition to NHibernate is the QueryOver&#60;T&#62; API that wrapped ICriteria API and made it easier to use. Thanks to Richard Brown. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=82&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>First of all, <a href="http://blogs.imeta.co.uk/sstrong/Default.aspx">Steve Strong</a> made a Christmas gift to community by presenting a <a href="http://nhforge.org/blogs/nhibernate/archive/2009/12/17/linq-to-nhibernate-progress-report-a-christmas-gift.aspx">new Linq implementation</a> for <a href="http://nhforge.org/">NHibernate</a>, which is now incorporated in the trunk. Kudos for a such great work!</p>
<p>Another valuable addition to NHibernate is the <a href="http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx">QueryOver&lt;T&gt; API</a> that wrapped ICriteria API and made it easier to use. Thanks to <a href="http://broloco.blogspot.com/">Richard Brown</a>.</p>
<p>Both features are not crucial for the success of NHibernate, but they are very nice improvements that will attract new users and will keep the old ones. It&#8217;s easier to advocate for NHibernate when you can point not only at his advanced features. The hype around Linq makes difference when features are compared. Having a provider for NHibernate will be a strong decision argument.</p>
<p>I hope that soon after New Year&#8217;s holidays I&#8217;ll try to switch to NHibernate 3.0. If migration of our project will be painless and all tests will still green, I think we&#8217;ll run with the trunk. It&#8217;s the least we can do to help make this project better.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/varely.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/varely.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/varely.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/varely.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/varely.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/varely.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/varely.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/varely.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/varely.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/varely.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/varely.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/varely.wordpress.com/82/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/varely.wordpress.com/82/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/varely.wordpress.com/82/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=82&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.caraulean.com/2009/12/21/nhibernate-3-0-will-be-a-very-nice-release/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d96a7814c830eb5ee8e12b677212cae?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">varely</media:title>
		</media:content>
	</item>
		<item>
		<title>Clean code: Register additional event listeners in NHibernate</title>
		<link>http://blog.caraulean.com/2009/09/02/clean-code-register-additional-event-listeners-in-nhibernate/</link>
		<comments>http://blog.caraulean.com/2009/09/02/clean-code-register-additional-event-listeners-in-nhibernate/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 08:49:02 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://varely.wordpress.com/2009/09/02/clean-code-register-additional-event-listeners-in-nhibernate/</guid>
		<description><![CDATA[&#8220;why is this a problem?&#8221; may ask you&#8230; Let&#8217;s review the ways that NHibernate lets you register your own event listeners. You can do it in configuration file: &#60;listener class=&#8221;NHTest.Listeners.CustomSaveEventListener, NHTest&#8221; type=&#8221;save&#8221; /&#62; &#8230; or in code: Configuration cfg = new Configuration();cfg.EventListeners.SaveEventListeners = &#160;&#160;&#160; new ISaveOrUpdateEventListener[] {new CustomSaveEventListener() }; Nice, so far&#8230; It&#8217;s not if [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=69&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>&#8220;why is this a problem?&#8221; may ask you&#8230;</p>
<p>Let&#8217;s review the ways that <a href="http://nhforge.org/">NHibernate</a> lets you register your own event listeners. You can do it in configuration file:</p>
<p>&lt;listener class=&#8221;NHTest.Listeners.CustomSaveEventListener, NHTest&#8221; type=&#8221;save&#8221; /&gt;</p>
<p>&#8230; or in code:</p>
<p>Configuration cfg = new Configuration();<br />cfg.EventListeners.SaveEventListeners = <br />&nbsp;&nbsp;&nbsp; new ISaveOrUpdateEventListener[] {new CustomSaveEventListener() };
<p>Nice, so far&#8230;</p>
<p>It&#8217;s not if you want to <strong>add</strong> a listener, not to replace them. Both registrations are just replacing default event listeners with your own implementations. Very often it&#8217;s not want you want!</p>
<p>In our case, we want default listeners do the job and just add few our own listeners to run the business. No problem, let&#8217;s code:</p>
<pre class="code"><span style="color:blue;">var </span>listeners = configuration.EventListeners;
listeners.SaveEventListeners =
    <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">ISaveOrUpdateEventListener</span>&gt;(listeners.SaveEventListeners)
    {<span style="color:blue;">new </span><span style="color:#2b91af;">CustomSaveEventListener</span>()}
    .ToArray();
</pre>
<p>&nbsp;</p>
<p>Since I wrote that code some day I hated it. Too verbose, too obscure, hard to read &amp; understand. But event registration infrastructure doesn&#8217;t offer an easy and simple way to add a listener. Few times I tried to come with a nice solution for that, even tried to modify NH&#8217;s code. </p>
<p>Today, I&#8217;ve done it:</p>
<pre class="code">configuration.AddListener(el =&gt; el.SaveEventListeners, <span style="color:blue;">new </span><span style="color:#2b91af;">CustomSaveEventListener</span>());</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Some expression&#8217;s magic and we end up with a very <strong>clean &amp; readable code</strong>.</p>
<p>Implementation:</p>
<pre class="code"><span style="color:blue;">public static class </span><span style="color:#2b91af;">NHibernateConfigurationExtensions
</span>{
    <span style="color:blue;">public static void </span>AddListener&lt;TListener&gt;(
        <span style="color:blue;">this </span><span style="color:#2b91af;">Configuration </span>configuration,
        <span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">EventListeners</span>, TListener[]&gt;&gt; expression,
        TListener listenerImpl)
    {
        <span style="color:blue;">var </span>propertyInfo = <span style="color:#2b91af;">ReflectionHelper</span>.GetProperty(expression);
        <span style="color:blue;">var </span>existentListeners = (TListener[]) propertyInfo.GetValue(configuration.EventListeners, <span style="color:blue;">null</span>);
        <span style="color:blue;">var </span>newListeners = <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&lt;TListener&gt;(existentListeners) { listenerImpl }.ToArray();
        propertyInfo.SetValue(configuration.EventListeners, newListeners, <span style="color:blue;">null</span>);
    }

    <span style="color:blue;">public static void </span>AddListeners&lt;TListener&gt;(
        <span style="color:blue;">this </span><span style="color:#2b91af;">Configuration </span>configuration,
        <span style="color:#2b91af;">Expression</span>&lt;<span style="color:#2b91af;">Func</span>&lt;<span style="color:#2b91af;">EventListeners</span>, TListener[]&gt;&gt; expression,
        <span style="color:#2b91af;">IEnumerable</span>&lt;TListener&gt; listeners)
    {
        <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>listener <span style="color:blue;">in </span>listeners)
        {
            configuration.AddListener(expression, listener);
        }
    }
}</pre>
<pre class="code">Have a nice day...</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/varely.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/varely.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/varely.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/varely.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/varely.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/varely.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/varely.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/varely.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/varely.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/varely.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/varely.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/varely.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/varely.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/varely.wordpress.com/69/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=69&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.caraulean.com/2009/09/02/clean-code-register-additional-event-listeners-in-nhibernate/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d96a7814c830eb5ee8e12b677212cae?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">varely</media:title>
		</media:content>
	</item>
		<item>
		<title>NHibernate Mapping</title>
		<link>http://blog.caraulean.com/2009/04/12/nhibernate-mapping/</link>
		<comments>http://blog.caraulean.com/2009/04/12/nhibernate-mapping/#comments</comments>
		<pubDate>Sun, 12 Apr 2009 10:42:46 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://varely.wordpress.com/2009/04/12/nhibernate-mapping/</guid>
		<description><![CDATA[Ayende is running a series of posts detailing and explaining how NHibernate maps entity classes to database. Posts include XML mappings, code and SQL queries generated when saving or querying the database so that you have everything to understand what NHibernate do to persist your data. The series is a nice addition to NHibernate Reference [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=49&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://ayende.com/Blog/Default.aspx">Ayende</a> is running a series of posts detailing and explaining how NHibernate maps entity classes to database. Posts include XML mappings, code and SQL queries generated when saving or querying the database so that you have everything to understand what NHibernate do to persist your data. The series is a nice addition to <a href="http://nhforge.org/doc/nh/en/index.html">NHibernate Reference Documentation</a>.</p>
<p>NHibernate Mapping:</p>
<ul>
<li><a href="http://ayende.com/Blog/archive/2009/04/07/nhibernate-mapping-ltpropertygt.aspx">Properties &lt;property/&gt;</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/08/nhibernate-mapping-ltcomponentgt.aspx">Components &lt;component/&gt;</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/09/nhibernate-mapping-ltmany-to-onegt.aspx">Many to one relationships &lt;many-to-one/&gt;</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/10/nhibernate-mapping-ndash-inheritance.aspx">Inheritance</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/11/nhibernate-mapping-ltdynamic-componentgt.aspx">Dynamic components &lt;dynamic-component/&gt;</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/13/nhibernate-mapping-ltsetgt.aspx">Collections &lt;set/&gt;</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/15/nhibernate-mapping-concurrency.aspx">Concurrency</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/16/nhibernate-mapping-ltdatabase-objectgt.aspx">Database objects &lt;database-object/&gt;</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/17/nhibernate-mapping-ndash-named-queries-ltquerygt-and-ltsql-querygt.aspx">Named queries &lt;query/&gt; &amp; &lt;sql-query/&gt;</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/19/nhibernate-mapping-ltone-to-onegt.aspx">One to one relations &lt;one-to-one/&gt;</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/20/nhibernate-mapping-ltjoingt.aspx">Joined classes &lt;join/&gt;</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/21/nhibernate-mapping-ltanygt.aspx">Any type mappings &lt;any/&gt;</a>
<li><a href="http://ayende.com/Blog/archive/2009/04/22/nhibernate-mapping-ltmany-to-anygt.aspx">Many to any relationships &lt;many-to-any/&gt;</a></li>
</ul>
<p>&nbsp;</p>
<p>It was a nice reading. I&#8217;ve refreshed few things in my memory and learned few new tricks. </p>
<p>This post will be updated as new chunks will be published (I hope it will) by Ayende. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/varely.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/varely.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/varely.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/varely.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/varely.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/varely.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/varely.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/varely.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/varely.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/varely.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/varely.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/varely.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/varely.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/varely.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=49&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.caraulean.com/2009/04/12/nhibernate-mapping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d96a7814c830eb5ee8e12b677212cae?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">varely</media:title>
		</media:content>
	</item>
		<item>
		<title>NHibernate&#8217;s niceties: hbm2ddl auto mode in unit tests</title>
		<link>http://blog.caraulean.com/2008/12/18/nhibernates-niceties-hbm2ddl-auto-mode-in-unit-tests/</link>
		<comments>http://blog.caraulean.com/2008/12/18/nhibernates-niceties-hbm2ddl-auto-mode-in-unit-tests/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 17:33:17 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://varely.wordpress.com/2008/12/18/nhibernates-niceties-hbm2ddl-auto-mode-in-unit-tests/</guid>
		<description><![CDATA[Very often you&#8217;re going over a description of some function or new feature and just notice in your mind, &#8220;nice feature&#8221; without seeing a real application for it. Later, you&#8217;re coming with scenario where the trick you&#8217;ve read days ago will fit perfectly. That was the case when I&#8217;ve seen earlier in Tuna Toksoz&#8216;s weblog [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=40&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Very often you&#8217;re going over a description of some function or new feature and just notice in your mind, &#8220;nice feature&#8221; without seeing a real application for it. Later, you&#8217;re coming with scenario where the trick you&#8217;ve read days ago will fit perfectly.</p>
<p>That was the case when I&#8217;ve seen earlier in <a href="http://www.tunatoksoz.com/">Tuna Toksoz</a>&#8216;s weblog a <a href="http://www.tunatoksoz.com/post/NHibernate-hbm2ddl.aspx">description of &#8220;hbm2ddl.auto&#8221; property</a>. Few days ago, I&#8217;ve put it to work in a way that makes me feel better. By the way, the particular reason that made me happy was that I&#8217;ve deleted a lot of repetitive code from our code base.</p>
<h4>How you can use hbm2ddl.auto property in unit tests</h4>
<p>Our integration tests are run over a real database. For each test we&#8217;re re-creating the database, so that each test will be ran in separation and without interfering with results of previous tests. Also, things are a bit complicated since we&#8217;re working with few databases at the same time, each with his own schema. Before, each test had to use SchemaExport class that exported the database schema built from NHibernate configuration on the way deleting your old database/schema/data. So, each integration test class had same method to call. Redundant and repetitive. Yes, you can make a base class that will do that, but you have to inherit then all tests from that class.</p>
<p><strong>How it works now:</strong> in session configuration (usually in app.config for integration test project) you put:</p>
<blockquote><p>&lt;property name=&#8221;hbm2ddl.auto&#8221;&gt;create&lt;/property&gt;</p>
</blockquote>
<p>&#8230; and you&#8217;re set. Each time your code will build a new SessionFactory, NHibernate will call:</p>
<blockquote><p>new SchemaExport(cfg).Create(false, true);</p>
</blockquote>
<p>This way, before the SessionFactory will be built, your database will be refreshed with a new schema and tests will run in a clean database.</p>
<p>One line of code instead of tens or hundreds. Isn&#8217;t that a good trick?!</p>
<p>&nbsp;</p>
<p>A word of warning &#8211; don&#8217;t let this code escape to your production code. It may be dangerous&#8230; But, you know that, we&#8217;re running with scissors&#8230;</p>
<p><a href="http://altdotnet.org/"><img height="180" alt="RWS2-Big" src="http://varely.files.wordpress.com/2008/12/rws2-big.png?w=468&#038;h=180" width="468"></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/varely.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/varely.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/varely.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/varely.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/varely.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/varely.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/varely.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/varely.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/varely.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/varely.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/varely.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/varely.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/varely.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/varely.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=40&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.caraulean.com/2008/12/18/nhibernates-niceties-hbm2ddl-auto-mode-in-unit-tests/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d96a7814c830eb5ee8e12b677212cae?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">varely</media:title>
		</media:content>

		<media:content url="http://varely.files.wordpress.com/2008/12/rws2-big.png" medium="image">
			<media:title type="html">RWS2-Big</media:title>
		</media:content>
	</item>
		<item>
		<title>Additional identity column with NHibernate</title>
		<link>http://blog.caraulean.com/2008/08/26/additional-identity-column-with-nhibernate/</link>
		<comments>http://blog.caraulean.com/2008/08/26/additional-identity-column-with-nhibernate/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 09:51:49 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://varely.wordpress.com/2008/08/26/additional-identity-column-with-nhibernate/</guid>
		<description><![CDATA[As always, business requirement went over existing code base to ask for a minor change that wasn&#8217;t compatible with our existing infrastructure: all surrogate keys are unique identifiers (Guid) and all infrastructure was built around that. The business people asked to add to an existing domain entity a natural key &#8211; integer identity number (auto [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=30&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As always, business requirement went over existing code base to ask for a minor change that wasn&#8217;t compatible with our existing infrastructure: all surrogate keys are unique identifiers (Guid) and all infrastructure was built around that. The business people asked to add to an existing domain entity a natural key &#8211; integer identity number (auto increment field), to serve as a reference number.</p>
<p>We had two choices: </p>
<ul>
<li>to generate the identity from business code</li>
<li>to let database manage it for us.</li>
</ul>
<p>The second approach it preferable for us because it&#8217;s the database&#8217;s job to generate the keys.</p>
<p>Out of the box NHibernate doesn&#8217;t support such a thing. Answers to my question in <a href="http://groups.google.com/group/nhusers?hl=en">NHibernate Users discussion group</a> led to the next solution.</p>
<p>Given the class:</p>
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">Entity
</span>{
    <span style="color:blue;">public int </span>Id { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
    <span style="color:blue;">public int </span>Id2 { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }
}
</pre>
<p>First &#8220;Id&#8221; is the primary key, the second is the key that we want to map to identity column. To have that done we should do two things:</p>
<ul>
<li>Map &#8220;Id2&#8243; property as &#8220;generated&#8221;</li>
<li>Use &#8220;database-object&#8221; to drop the column generated by NHibernate &amp; create new one, with IDENTITY set on.</li>
</ul>
<p>The complete mapping:</p>
<pre class="code"><span style="color:blue;">&lt;</span><span style="color:#a31515;">hibernate-mapping </span><span style="color:red;">xmlns:xsi</span><span style="color:blue;">=</span>"<span style="color:blue;">http://www.w3.org/2001/XMLSchema-instance</span>" <span style="color:red;">xmlns:xsd</span><span style="color:blue;">=</span>"<span style="color:blue;">http://www.w3.org/2001/XMLSchema</span>"
    <span style="color:red;">namespace</span><span style="color:blue;">=</span>"<span style="color:blue;">NHibernate.Playground</span>"
    <span style="color:red;">assembly</span><span style="color:blue;">=</span>"<span style="color:blue;">NHibernate.Playground</span>"
    <span style="color:red;">default-lazy</span><span style="color:blue;">=</span>"<span style="color:blue;">false</span>" <span style="color:red;">xmlns</span><span style="color:blue;">=</span>"<span style="color:blue;">urn:nhibernate-mapping-2.2</span>"<span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">class </span><span style="color:red;">name</span><span style="color:blue;">=</span>"<span style="color:blue;">Entity</span>"<span style="color:blue;">&gt;
    &lt;</span><span style="color:#a31515;">id </span><span style="color:red;">name</span><span style="color:blue;">=</span>"<span style="color:blue;">Id</span>"<span style="color:blue;">&gt;
      &lt;</span><span style="color:#a31515;">generator </span><span style="color:red;">class</span><span style="color:blue;">=</span>"<span style="color:blue;">increment</span>" <span style="color:blue;">/&gt;
    &lt;/</span><span style="color:#a31515;">id</span><span style="color:blue;">&gt;
    &lt;</span><span style="color:#a31515;">property </span><span style="color:red;">name</span><span style="color:blue;">=</span>"<span style="color:blue;">Id2</span>" <span style="color:red;">type</span><span style="color:blue;">=</span>"<span style="color:blue;">int</span>" <span style="color:red;">generated</span><span style="color:blue;">=</span>"<span style="color:blue;">always</span>" <span style="color:red;">insert</span><span style="color:blue;">=</span>"<span style="color:blue;">false</span>" <span style="color:blue;">/&gt;
  &lt;/</span><span style="color:#a31515;">class</span><span style="color:blue;">&gt;
  &lt;</span><span style="color:#a31515;">database-object</span><span style="color:blue;">&gt;
    &lt;</span><span style="color:#a31515;">create</span><span style="color:blue;">&gt;
      </span>ALTER TABLE Entity DROP COLUMN Id2
      ALTER TABLE Entity ADD Id2 INT IDENTITY
    <span style="color:blue;">&lt;/</span><span style="color:#a31515;">create</span><span style="color:blue;">&gt;
    &lt;</span><span style="color:#a31515;">drop</span><span style="color:blue;">&gt;
      </span>ALTER TABLE Entity DROP COLUMN Id2
    <span style="color:blue;">&lt;/</span><span style="color:#a31515;">drop</span><span style="color:blue;">&gt;
  &lt;/</span><span style="color:#a31515;">database-object</span><span style="color:blue;">&gt;
&lt;/</span><span style="color:#a31515;">hibernate-mapping</span><span style="color:blue;">&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>I like the flexibility offered by NHibernate. Even if you don&#8217;t have required feature, you can always use naked ADO.NET or raw SQL.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/varely.wordpress.com/30/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/varely.wordpress.com/30/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/varely.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/varely.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/varely.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/varely.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/varely.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/varely.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/varely.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/varely.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/varely.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/varely.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/varely.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/varely.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/varely.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/varely.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=30&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.caraulean.com/2008/08/26/additional-identity-column-with-nhibernate/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d96a7814c830eb5ee8e12b677212cae?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">varely</media:title>
		</media:content>
	</item>
		<item>
		<title>What integration tests should you write first</title>
		<link>http://blog.caraulean.com/2008/06/18/what-integration-tests-should-you-write-first/</link>
		<comments>http://blog.caraulean.com/2008/06/18/what-integration-tests-should-you-write-first/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 10:18:10 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Testing]]></category>

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

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

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