<?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>Calle Arnesten&#039;s Blog &#187; NHibernate</title>
	<atom:link href="http://www.arnesten.com/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arnesten.com</link>
	<description>Web Development, Test Driven Development, NHibernate, ASP.NET MVC, jQuery and more..</description>
	<lastBuildDate>Sun, 10 Jan 2010 18:35:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Testing your NHibernate data access code using SQLite</title>
		<link>http://www.arnesten.com/2008/11/25/testing-your-nhibernate-data-access-code-using-sqlite/</link>
		<comments>http://www.arnesten.com/2008/11/25/testing-your-nhibernate-data-access-code-using-sqlite/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 21:28:54 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/?p=30</guid>
		<description><![CDATA[Ever since I started working professionally at a software company (which is roughly 3 years ago) I’ve been doing unit testing. I really consider automatic testing one of the most (if not THE most) important trends of software development the last decade. But, despite that, I’ve let code close to the data access be left [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I started working professionally at a software company (which is roughly 3 years ago) I’ve been doing unit testing. I really consider automatic testing one of the most (if not THE most) important trends of software development the last decade. </p>
<p>But, despite that, I’ve let code close to the data access be left untested. This has been due to the pain I thought it would be to create and maintain those tests. And also I’ve thought that those tests would be way to slow to have among my other unit tests (which we want to be fast, so that we run them often).</p>
<p>For the first two years of my career this was not a big problem. We rarely had bugs in our data access code as it mostly consisted of simple, straight forward code. So I hadn’t really given it much thought. Until lately that is.</p>
<p>Then we started to get performance issues as we got some really large organizations to use or software. To resolve the performance issues we made our data access smarter so it could make more efficient queries to the database. As smarter often means more code, or at least added complexity, I saw the need of automatic tests to guarantee the quality of the code.</p>
<p>So, I read some articles on using SQLite with NHibernate. After some pain setting it all up, I accomplished my goal of easy data access testing. So say that I want to test GetCustomerByName in the following class (in real life I wouldn&#8217;t test methods that are this simple, but it&#8217;s just to show the idea):</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">CustomerDao</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">INHibernateManager</span> _sessionManager;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> CustomerDao(<span style="color: #2b91af;">INHibernateManager</span> sessionManager)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; _sessionManager = sessionManager;
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">Customer</span> GetCustomerByName(<span style="color: blue;">string</span> name)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> session = _sessionManager.CurrentSession;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> session.CreateQuery(<span style="color: #a31515;">&quot;from Customer where Name = :name&quot;</span>)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .SetParameter(<span style="color: #a31515;">&quot;name&quot;</span>, name)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .UniqueResult&lt;<span style="color: #2b91af;">Customer</span>&gt;();
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>Then I can write a test like this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">CustomerDaoTest</span> : <span style="color: #2b91af;">BaseTest</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; [<span style="color: #2b91af;">Test</span>]
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> CanGetCustomerByName()
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; Database.Init();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> customerA = <span style="color: blue;">new</span> <span style="color: #2b91af;">Customer</span> { Name = <span style="color: #a31515;">&quot;A&quot;</span> };
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> customerB = <span style="color: blue;">new</span> <span style="color: #2b91af;">Customer</span> { Name = <span style="color: #a31515;">&quot;B&quot;</span> };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; Database.Save(customerA, customerB);&nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> customerDao = <span style="color: blue;">new</span> <span style="color: #2b91af;">CustomerDao</span>(Database);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> result = customerDao.GetCustomerByName(<span style="color: #a31515;">&quot;B&quot;</span>);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">Assert</span>.That(result, <span style="color: #2b91af;">Is</span>.EqualTo(customerB));
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>For this to work I have a class called TestDatabase with the following code:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">TestDatabase</span> : <span style="color: #2b91af;">INHibernateManager</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">ISessionFactory</span> m_sessionFactory;&nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">SchemaExport</span> SchemaExport;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: #2b91af;">ISession</span> m_session;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Init()
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (m_sessionFactory == <span style="color: blue;">null</span>)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">Configuration</span> config = CreateNHibernateConfig();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_sessionFactory = config.BuildSessionFactory();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SchemaExport = <span style="color: blue;">new</span> <span style="color: #2b91af;">SchemaExport</span>(config);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (m_session != <span style="color: blue;">null</span>)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_session.Close();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; m_session = m_sessionFactory.OpenSession();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; m_session.BeginTransaction();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> connection = m_session.Connection;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; SchemaExport.Execute(<span style="color: blue;">false</span>, <span style="color: blue;">true</span>, <span style="color: blue;">false</span>, <span style="color: blue;">false</span>, connection, <span style="color: blue;">null</span>);
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">Configuration</span> CreateNHibernateConfig()
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">Configuration</span> config = <span style="color: blue;">new</span> <span style="color: #2b91af;">Configuration</span>();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.Properties.Clear();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.connection.driver_class&quot;</span>, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a31515;">&quot;NHibernate.Driver.SQLite20Driver&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.dialect&quot;</span>, <span style="color: #a31515;">&quot;NHibernate.Dialect.SQLiteDialect&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.connection.connection_string&quot;</span>, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a31515;">&quot;Data Source=:memory:;Version=3;New=True;&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.cache.use_second_level_cache&quot;</span>, <span style="color: #a31515;">&quot;false&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.hbm2ddl.auto&quot;</span>, <span style="color: #a31515;">&quot;create&quot;</span>);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.AddAssembly(<span style="color: #a31515;">&quot;MyAssembly&quot;</span>); <span style="color: green;">// Change this to your assembly</span>
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> config;
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ISession</span> Session
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> m_session; }
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Save(<span style="color: blue;">params</span> <span style="color: blue;">object</span>[] entities)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> entity <span style="color: blue;">in</span> entities)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session.Save(entity);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; Session.Flush();
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: #2b91af;">ISession</span> <span style="color: #2b91af;">INHibernateManager</span>.CurrentSession
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> m_session; }
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>And I’ve also added the following field to my BaseTest class:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;">[<span style="color: #2b91af;">TestFixture</span>]
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">BaseTest</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">TestDatabase</span> Database = <span style="color: blue;">new</span> <span style="color: #2b91af;">TestDatabase</span>();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: green;">// More code here...</span>
<p style="margin: 0px;">}
</div>
</pre>
<p>So, in summary, I&#8217;ve shown a pretty easy way of doing database testing. Even though the method under test was really simple in the example, the same technique could be used for much more complex scenarios.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/11/25/testing-your-nhibernate-data-access-code-using-sqlite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using NHibernate 1.2 with Cuyahoga</title>
		<link>http://www.arnesten.com/2007/05/25/using-nhibernate-12-with-cuyahoga/</link>
		<comments>http://www.arnesten.com/2007/05/25/using-nhibernate-12-with-cuyahoga/#comments</comments>
		<pubDate>Fri, 25 May 2007 15:20:55 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Cuyahoga]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/2007/05/25/using-nhibernate-12-with-cuyahoga/</guid>
		<description><![CDATA[One of the things I missed when I started to look at Cuyahoga two weeks ago was that it didn&#8217;t use NHibernate 1.2. As I was used to NHibernate 1.2 from my work with its support for generics, I wanted to use that with Cuyahoga as well. So I decided to make a patch that [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things I missed when I started to look at Cuyahoga two weeks ago was that it didn&#8217;t use NHibernate 1.2. As I was used to NHibernate 1.2 from my work with its support for generics, I wanted to use that with Cuyahoga as well. So I decided to make a patch that would enable me to use it.</p>
<p>Besides using the files from the NHibernate 1.2 release, I had to download the subversion source code for Castle.Facilities.NHibernateIntegration.dll. Then I had to make a few adjustments to the code for it to compile against NHibernate 1.2.</p>
<p>After I had upgraded the dll&#8217;s, the Cuyahoga compiled. But when I run it NHibernate complained on some things like missing virtuals and wrong mapping scheme (urn:nhibernate-mapping-2.0 instead of urn:nhibernate-mapping-2.2). After that it seems to be working nice.</p>
<p>You can download the patch for Cuyahoga 1.5 source code <a href='http://www.arnesten.com/wordpress/wp-content/patch-cuyahoga-150-nhibernate-12.zip' title='Patch-Cuyahoga-1.5.0-NHibernate 1.2.zip'>here.</a> And for Cuyahoga&#8217;s latest trunk <a href='http://www.arnesten.com/wordpress/wp-content/patch-cuyahoga-trunk-nhibernate-12.zip' title='Patch-Cuyahoga-trunk-NHibernate-1.2.zip'>here</a>. </p>
<p>Unfortunately, I&#8217;ve been having some problem applying the entire patches. It complains about some strange character in the beginning of some of the repository files, so you might have to manually edit some files. (The patch file is actually quite human-readable so it can be of help in the manual process). But for those that start from scratch I&#8217;ve also created a ready package of Cuyahoga 1.5 with patch applied. Download it <a href='http://www.arnesten.com/wordpress/wp-content/cuyahoga-150-with-nhibernate-12.zip' title='Cuyahoga-1.5.0-with-NHibernate-1.2.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2007/05/25/using-nhibernate-12-with-cuyahoga/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

