<?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>Alkampfer&#039;s Place&#187; Nhibernate</title>
	<atom:link href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codewrecks.com/blog</link>
	<description>Wrecks of code floating in the sea of Internet By Ricci Gian Maria</description>
	<lastBuildDate>Wed, 08 Feb 2012 09:29:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Unit test NHibernate query to verify N+1</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/12/30/unit-test-nhibernate-query-to-verify-n1/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/12/30/unit-test-nhibernate-query-to-verify-n1/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 15:11:39 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Nhibernate]]></category>
		<category><![CDATA[Nunit]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/12/30/unit-test-nhibernate-query-to-verify-n1/</guid>
		<description><![CDATA[When you work with ORM like nhibernate, having a tool like nhprof is the key of success. But even with NHProfiler you could not prevent people of doing wrong stuff because they do not use it . I have a simple scenario, a developer changed a method on the server lazily fetching a property of [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F12%2F30%2Funit-test-nhibernate-query-to-verify-n1%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F12%2F30%2Funit-test-nhibernate-query-to-verify-n1%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Nhibernate,Nunit,Testing&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>When you work with ORM like <a target="_blank" title="nhibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">nhibernate</a>, having a tool like <a href="http://nhprof.com/">nhprof</a> is the key of success. But even with NHProfiler you could not prevent people of doing wrong stuff because they do not use it <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . I have a simple scenario, a developer changed a method on the server lazily fetching a property of a large resultset. The effect is that the service call, that usually responded in milliseconds starts to respond in 10 seconds.</p>
<p>The reason is really simple, the function loaded about 200 entities from the database and if you are sure you want to access for all 200 entities a property, you should do eager fetching because issuing ~200 queries to the database is not usually a good idea. Then after some time the function started to do timeout, so I inspected again the code and did not find anything strange, but NHprof reveals me that the query was actually fetching another related <a target="_blank" title="entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">entity</a>, from a table with millions of row causing timeout. This is due to a modification to a mapping, someone disabled proxy for that class, so NH decided to do a join with the table with millions of row, slowing the method again .</p>
<p>After the problem was fixed, I created some helpers that permits me to write a <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a> that will alert me immediately in the future if such a problem is comeback again.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> [Test]</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> verify_xxxx()</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>     var sut = <span style="color: rgb(0, 0, 255);">new</span> cccccService();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>     sut.DoSomething(1);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>     <span style="color: rgb(0, 0, 255);">this</span>.NhibernateQueryCount().Should().Be.EqualTo(1);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>     String query = <span style="color: rgb(0, 0, 255);">this</span>.NhibernateGetQueries().First();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>     query.Should().Not.Contain(<span style="color: rgb(0, 96, 128);">&quot;relatedlin1_.Id&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>This <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a> is not really a UnitTest, it is more an integration one, but it is able to verify that calling a function on a service class only one query is issued to the database and the query should not eager fetch data from the other table (relatedlin…). This is much more an integration test than a unit test, but it is quite interesting because it permits me to write assertion on number and text of SQL generated by <a title="NHibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">NHibernate</a>, a feature that is really interesting especially if you know that production database is quite big. To achieve this is really simple.</p>
<p>I wrote this simple <a href="http://www.codewrecks.com/blog/index.php/2010/12/29/test-helper-for-a-single-method/">test helper</a> based on my infrastructure.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> InterceptNhQueriesHelper : ITestHelper</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>  {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>      <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">const</span> <span style="color: rgb(0, 0, 255);">string</span> nhQueries = <span style="color: rgb(0, 96, 128);">&quot;DFCDE96A-ADF7-4C46-A55B-219381364B7F&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>      <span style="color: rgb(0, 0, 255);">private</span> Dictionary&lt;String, Level&gt; _oldLevel = <span style="color: rgb(0, 0, 255);">new</span> Dictionary&lt;<span style="color: rgb(0, 0, 255);">string</span>, Level&gt;();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>      <span style="color: rgb(204, 102, 51);">#region</span> ITestHelper Members</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>      <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> FixtureSetUp(BaseTestFixture fixture)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>      {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>          ILogger loggerToForceInitializationOfLoggingSystem = IoC.Resolve&lt;ILogger&gt;();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum11">  11:</span>          var repository = LogManager.GetAllRepositories();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum12">  12:</span>          Log4NetLogEventSourceAppender interceptorAppender = <span style="color: rgb(0, 0, 255);">new</span> Log4NetLogEventSourceAppender();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum13">  13:</span>          <span style="color: rgb(0, 0, 255);">foreach</span> (var loggerRepository <span style="color: rgb(0, 0, 255);">in</span> repository)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum14">  14:</span>          {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum15">  15:</span>              Hierarchy hierarchy = (Hierarchy)loggerRepository;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum16">  16:</span>              <span style="color: rgb(0, 0, 255);">if</span> (hierarchy.GetAppenders()</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum17">  17:</span>                      .Count(appender =&gt; appender.GetType() == <span style="color: rgb(0, 0, 255);">typeof</span>(Log4NetLogEventSourceAppender)) == 0)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum18">  18:</span>              {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum19">  19:</span>                  hierarchy.Root.AddAppender(interceptorAppender);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum20">  20:</span>                  hierarchy.RaiseConfigurationChanged(EventArgs.Empty);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum21">  21:</span>              }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum22">  22:</span>              _oldLevel[hierarchy.Name] = hierarchy.Root.Level;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum23">  23:</span>              hierarchy.Root.Level = Level.Debug;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum24">  24:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum25">  25:</span>              var loggers = loggerRepository.GetCurrentLoggers();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum26">  26:</span>              <span style="color: rgb(0, 0, 255);">foreach</span> (var logger <span style="color: rgb(0, 0, 255);">in</span> loggers)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum27">  27:</span>              {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum28">  28:</span>                  Logger realLogger = logger <span style="color: rgb(0, 0, 255);">as</span> Logger;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum29">  29:</span>                  <span style="color: rgb(0, 0, 255);">if</span> (realLogger.Name.IndexOf(<span style="color: rgb(0, 96, 128);">&quot;<a title="NHIBERNATE" href="http://www.codewrecks.com/blog/index.php/tag/nhibernate/">NHIBERNATE</a>&quot;</span>, StringComparison.InvariantCultureIgnoreCase) &gt;= 0)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum30">  30:</span>                  {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum31">  31:</span>                      _oldLevel[realLogger.Name] = realLogger.Level;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum32">  32:</span>                      realLogger.Level = Level.Debug;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum33">  33:</span>                      <span style="color: rgb(0, 0, 255);">if</span> (!realLogger.Appenders.OfType&lt;IAppender&gt;().Any(appender =&gt; appender.GetType() == <span style="color: rgb(0, 0, 255);">typeof</span>(Log4NetLogEventSourceAppender)))</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum34">  34:</span>                      {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum35">  35:</span>                          <span style="color: rgb(0, 128, 0);">//non ho appender intercettori</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum36">  36:</span>                          realLogger.AddAppender(interceptorAppender);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum37">  37:</span>                      }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum38">  38:</span>                  }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum39">  39:</span>              }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum40">  40:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum41">  41:</span>              hierarchy.RaiseConfigurationChanged(EventArgs.Empty);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum42">  42:</span>          }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum43">  43:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum44">  44:</span>          Log4NetLogEventSourceAppender.OnLog += Log4NetLogEventSourceAppender_OnLog;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum45">  45:</span>          loggerToForceInitializationOfLoggingSystem.Debug(<span style="color: rgb(0, 96, 128);">&quot;TESTLOG DEBUG&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum46">  46:</span>          loggerToForceInitializationOfLoggingSystem.Info(<span style="color: rgb(0, 96, 128);">&quot;TESTLOG Info&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum47">  47:</span>          loggerToForceInitializationOfLoggingSystem.Error(<span style="color: rgb(0, 96, 128);">&quot;TESTLOG Error&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum48">  48:</span>      }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum49">  49:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum50">  50:</span>      <span style="color: rgb(0, 0, 255);">private</span> BaseTestFixture currentFixture;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum51">  51:</span>      <span style="color: rgb(0, 0, 255);">private</span> List&lt;String&gt; SqlInstructions = <span style="color: rgb(0, 0, 255);">new</span> List&lt;<span style="color: rgb(0, 0, 255);">string</span>&gt;();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum52">  52:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum53">  53:</span>      <span style="color: rgb(0, 0, 255);">void</span> Log4NetLogEventSourceAppender_OnLog(<span style="color: rgb(0, 0, 255);">object</span> sender, OnLog4NetLogEventArgs e)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum54">  54:</span>      {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum55">  55:</span>          <span style="color: rgb(0, 0, 255);">if</span> (e.LoggingEvent.LoggerName.Equals(<span style="color: rgb(0, 96, 128);">&quot;<a title="nhibernate" href="http://www.codewrecks.com/blog/index.php/tag/nhibernate/">nhibernate</a>.sql&quot;</span>, StringComparison.OrdinalIgnoreCase))</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum56">  56:</span>          {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum57">  57:</span>              SqlInstructions.Add(e.LoggingEvent.MessageObject <span style="color: rgb(0, 0, 255);">as</span> <span style="color: rgb(0, 0, 255);">string</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum58">  58:</span>          }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum59">  59:</span>      }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum60">  60:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum61">  61:</span>      <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> <a title="SetUp" href="http://www.codewrecks.com/blog/index.php/tag/setup/">SetUp</a>(BaseTestFixture fixture)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum62">  62:</span>      {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum63">  63:</span>          currentFixture = fixture;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum64">  64:</span>          fixture.SetIntoTestContext(nhQueries, SqlInstructions);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum65">  65:</span>          SqlInstructions.Clear();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum66">  66:</span>      }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum67">  67:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum68">  68:</span>      <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> TearDown(BaseTestFixture fixture)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum69">  69:</span>      {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum70">  70:</span>          currentFixture = <span style="color: rgb(0, 0, 255);">null</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum71">  71:</span>      }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum72">  72:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum73">  73:</span>      <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> FixtureTearDown(BaseTestFixture fixture)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum74">  74:</span>      {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum75">  75:</span>          var repository = LogManager.GetAllRepositories();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum76">  76:</span>          <span style="color: rgb(0, 0, 255);">foreach</span> (var loggerRepository <span style="color: rgb(0, 0, 255);">in</span> repository)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum77">  77:</span>          {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum78">  78:</span>              Hierarchy hierarchy = (Hierarchy)loggerRepository;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum79">  79:</span>              hierarchy.Root.Level = _oldLevel[hierarchy.Name];</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum80">  80:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum81">  81:</span>              var loggers = loggerRepository.GetCurrentLoggers();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum82">  82:</span>              <span style="color: rgb(0, 0, 255);">foreach</span> (var logger <span style="color: rgb(0, 0, 255);">in</span> loggers)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum83">  83:</span>              {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum84">  84:</span>                  Logger realLogger = logger <span style="color: rgb(0, 0, 255);">as</span> Logger;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum85">  85:</span>                  <span style="color: rgb(0, 0, 255);">if</span> (realLogger.Name.IndexOf(<span style="color: rgb(0, 96, 128);">&quot;NHIBERNATE&quot;</span>, StringComparison.InvariantCultureIgnoreCase) &gt;= 0 &amp;&amp;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum86">  86:</span>                      _oldLevel.ContainsKey(realLogger.Name))</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum87">  87:</span>                  {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum88">  88:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum89">  89:</span>                      realLogger.Level = _oldLevel[realLogger.Name];</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum90">  90:</span>                  }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum91">  91:</span>              }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum92">  92:</span>              hierarchy.RaiseConfigurationChanged(EventArgs.Empty);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum93">  93:</span>          }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum94">  94:</span>      }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum95">  95:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum96">  96:</span>      <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">int</span> Priority</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum97">  97:</span>      {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum98">  98:</span>          get { <span style="color: rgb(0, 0, 255);">return</span> 1; }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum99">  99:</span>      }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum100"> 100:</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum101"> 101:</span>      <span style="color: rgb(204, 102, 51);">#endregion</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum102"> 102:</span>  }</pre>
<p><!--CRLF--></div>
</div>
<p>this helpers seems complex, but basically it simply add during FixtureSetup an appender to <a title="log4net" href="http://www.codewrecks.com/blog/index.php/tag/log4net/">log4net</a>, this appender basically raises an event whenever it receives a log.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> Log4NetLogEventSourceAppender : AppenderSkeleton</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>    {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>        <span style="color: rgb(0, 0, 255);">private</span> Object _syncRoot;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>        <span style="color: rgb(0, 0, 255);">public</span> Log4NetLogEventSourceAppender()</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>            _syncRoot = <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 255);">object</span>();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>        <span style="color: rgb(0, 128, 0);">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum11">  11:</span>        <span style="color: rgb(0, 128, 0);">/// Occurs when [on log].</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum12">  12:</span>        <span style="color: rgb(0, 128, 0);">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum13">  13:</span>        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 255);">event</span> EventHandler&lt;OnLog4NetLogEventArgs&gt; OnLog;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum14">  14:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum15">  15:</span>        <span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">override</span> <span style="color: rgb(0, 0, 255);">void</span> Append(LoggingEvent loggingEvent)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum16">  16:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum17">  17:</span>            EventHandler&lt;OnLog4NetLogEventArgs&gt; temp = OnLog;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum18">  18:</span>            <span style="color: rgb(0, 0, 255);">if</span> (temp != <span style="color: rgb(0, 0, 255);">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum19">  19:</span>            {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum20">  20:</span>                <span style="color: rgb(0, 0, 255);">lock</span> (_syncRoot)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum21">  21:</span>                {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum22">  22:</span>                    temp(<span style="color: rgb(0, 0, 255);">null</span>, <span style="color: rgb(0, 0, 255);">new</span> OnLog4NetLogEventArgs(loggingEvent));</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum23">  23:</span>                }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum24">  24:</span>            }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum25">  25:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum26">  26:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum27">  27:</span>    }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum28">  28:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum29">  29:</span>    <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> OnLog4NetLogEventArgs : EventArgs</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum30">  30:</span>    {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum31">  31:</span>        <span style="color: rgb(0, 0, 255);">public</span> LoggingEvent LoggingEvent { get; <span style="color: rgb(0, 0, 255);">private</span> set; }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum32">  32:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum33">  33:</span>        <span style="color: rgb(0, 0, 255);">public</span> OnLog4NetLogEventArgs(LoggingEvent loggingEvent)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum34">  34:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum35">  35:</span>            LoggingEvent = loggingEvent;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum36">  36:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum37">  37:</span>    }</pre>
<p><!--CRLF--></div>
</div>
<p>This appender permits the helper to intercept all logs and since Nhibernate raise a log with name nhibernate.sql with the SQL Code whenever it raises a query to the database, the helper can filter for those kind of messages and store each query inside a standard String collection. </p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 255);">class</span> InterceptNhQueriesHelperMethods</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>     <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> Int32 NhibernateQueryCount(<span style="color: rgb(0, 0, 255);">this</span> BaseTestFixtureWithHelper fixture)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>         <span style="color: rgb(0, 0, 255);">return</span> fixture.GetFromTestContext&lt;List&lt;String&gt;&gt;(InterceptNhQueriesHelper.nhQueries).Count;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>     <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> List&lt;String&gt; NhibernateGetQueries(<span style="color: rgb(0, 0, 255);">this</span> BaseTestFixtureWithHelper fixture)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>         <span style="color: rgb(0, 0, 255);">return</span> fixture.GetFromTestContext&lt;List&lt;String&gt;&gt;(InterceptNhQueriesHelper.nhQueries);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum11">  11:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum12">  12:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Now I can simply decorate my Test Fixture with a&#160; specific attribute and let the magic happens.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> [InterceptNhQueries]</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>    <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> XxxFixture : Test.Utilities.BaseTestFixtureWithHelper</pre>
<p><!--CRLF--></div>
</div>
<p>Inside a test I can use NhibernateQueryCount() to know the number of the queries issued by NH during the test and NhibernateGetQueries() to grab the whole list and assert on how NH interacted with the database during a test.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/12/30/unit-test-nhibernate-query-to-verify-n1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom XML Serialization</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/12/01/custom-xml-serialization/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/12/01/custom-xml-serialization/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 08:10:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Nhibernate]]></category>
		<category><![CDATA[NoSql]]></category>
		<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/12/01/custom-xml-serialization/</guid>
		<description><![CDATA[Another advantage of storing properties of entities into a state object based on a Dictionary, is the ability to easily serialize objects in custom formats. As an example I create an XML serializer that is capable to serialize an entity in a custom XML format. I used this simple serializer to create a NHibernate User [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F12%2F01%2Fcustom-xml-serialization%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F12%2F01%2Fcustom-xml-serialization%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Nhibernate,NoSql,Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Another advantage of sto<a target="_blank" href="http://www.codewrecks.com/blog/index.php/2011/06/13/leverage-the-concept-of-state-of-your-entities/">ring properties of entities into a state object</a> based on a Dictionary, is the ability to easily serialize objects in custom formats. As an example I create an XML serializer that is capable to serialize an <a title="entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">entity</a> in a custom XML format. </p>
<p>I used this simple serializer to create a <a target="_blank" title="NHibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">NHibernate</a> User Type that permits me to save a child <a title="entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">entity</a> in a single XML column of <a target="_blank" title="SQL Server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">SQL Server</a>, a feature useful when you need to save objects which schema changes quite often and you do not want to keep database schema updated, or you need to store dynamic data into the DB. I now that all of you are screaming “USE NO SQL DB”, like <a href="http://ravendb.net/">Raven</a>, but it is not simple to introduce new technologies into existing projects, and only to justify the need to save the 2% of objects.</p>
<p>Thanks to the custom serializer and the ability to do <a href="http://www.codewrecks.com/blog/index.php/2011/11/29/using-a-state-object-to-store-object-property-values/">DeepCloning</a>, writing such a User Type is really simple. First of all, I’ve implemented a method called EquivalentTo that permits me to compare two entities based on their state, this makes trivial writing the Equals Method of the UserType</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">bool</span> IUserType.Equals(<span style="color: rgb(0, 0, 255);">object</span> x, <span style="color: rgb(0, 0, 255);">object</span> y)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">{</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">if</span> (ReferenceEquals(x, y)) <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 255);">true</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">if</span> (x == <span style="color: rgb(0, 0, 255);">null</span> || y == <span style="color: rgb(0, 0, 255);">null</span>) <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 255);">false</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">if</span> (x <span style="color: rgb(0, 0, 255);">is</span> BaseEntity)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        <span style="color: rgb(0, 0, 255);">return</span> ((BaseEntity)x).IsEquivalentTo(y <span style="color: rgb(0, 0, 255);">as</span> BaseEntity);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">return</span> x.Equals(y);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">}</pre>
<p><!--CRLF--></div>
</div>
<p>Same consideration for the IUserType.DeepCopy method, based on the Clone method of the base class. Saving and loading the object is just a matter of using the BaseEntity serialization methods.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">object</span> NullSafeGet(System.Data.IDataReader rs, <span style="color: rgb(0, 0, 255);">string</span>[] names, <span style="color: rgb(0, 0, 255);">object</span> owner)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">{</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    Int32 index = rs.GetOrdinal(names[0]);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">if</span> (rs.IsDBNull(index))</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 255);">null</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    String databaseContent = (String)rs[index];</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">return</span> Deserialize(databaseContent);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">}</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">internal</span> Object Deserialize(String content)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">{</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    XElement element =  XElement.Parse(content);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">return</span> element.DeserializeToBaseEntity();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">}</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> NullSafeSet(System.Data.IDbCommand cmd, <span style="color: rgb(0, 0, 255);">object</span> <span style="color: rgb(0, 0, 255);">value</span>, <span style="color: rgb(0, 0, 255);">int</span> index)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">{</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">if</span> (<span style="color: rgb(0, 0, 255);">value</span> == <span style="color: rgb(0, 0, 255);">null</span> || <span style="color: rgb(0, 0, 255);">value</span> == DBNull.Value)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        NHibernateUtil.String.NullSafeSet(cmd, <span style="color: rgb(0, 0, 255);">null</span>, index);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">else</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        NHibernateUtil.String.Set(cmd, Serialize(<span style="color: rgb(0, 0, 255);">value</span>), index);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">}</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">internal</span> String Serialize(Object obj)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">{</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">if</span> (!(obj <span style="color: rgb(0, 0, 255);">is</span> BaseEntity))</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        <span style="color: rgb(0, 0, 255);">throw</span> <span style="color: rgb(0, 0, 255);">new</span> ArgumentException(<span style="color: rgb(0, 96, 128);">&quot;Only BaseEntity based entities could be serialized with this usertype&quot;</span>, <span style="color: rgb(0, 96, 128);">&quot;obj&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">return</span> (obj <span style="color: rgb(0, 0, 255);">as</span> BaseEntity).SerializeToXml().ToString();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">}</pre>
<p><!--CRLF--></div>
</div>
<p>The advantage of this approach, is that I have another base entity class called BaseExpandoEntity that permits to store into state object property by name, it is like a dynamic object, but I used it in .NET 3.5 where dynamics still does not exists. This kind of entity is clearly not really <a target="_blank" title="OOP" href="http://en.wikipedia.org/wiki/Object-oriented_programming">OOP</a>, it is not well encapsulated, because you can set property of any name from external code and it is used mainly as a DataClass, just to store information in database without the need to be schema or class bounded. Now suppose to have a class called Father that has a property of type SpecificNotes, based on this BaseExpandoEntity and saved in database with the above User Type, you can write this code.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">Father d1 = <span style="color: rgb(0, 0, 255);">new</span> Father () {...};</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">d1.SpecificNotes.SetProperty(<span style="color: rgb(0, 96, 128);">&quot;Ciao&quot;</span>, <span style="color: rgb(0, 96, 128);">&quot;mondo&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">d1.SpecificNotes.SetProperty(<span style="color: rgb(0, 96, 128);">&quot;Age&quot;</span>, 80);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">Repository.Father.Save(d1);</pre>
<p><!--CRLF--></div>
</div>
<p>In the above code, the Father class has a property called SpecificNotes mapped as XML in database, I can store two new properties with the SetProperty and this is what is saved to database.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">INSERT <span style="color: rgb(0, 0, 255);">INTO</span> Father</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            (Id,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">             xxx,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">             yyy,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">             SpecificNotes)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">VALUES</span>      (1,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">             ...,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">             ...,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">             &lt;SpecificNotes fname=&quot;Myproject.Entities.SpecificNotes, Myproject.Entities&quot; &gt;&lt;Ciao&gt;mondo&lt;/Ciao&gt;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">  &lt;Age type=&quot;System.Int32&quot;&gt;80&lt;/Age&gt;&lt;/SpecificNotes&gt;' )</pre>
<p><!--CRLF--></div>
</div>
<p>Now suppose you want to retrieve from the database all Father objects that have a SpecificNote with a&#160; property named <em>Ciao</em> with value ‘<em>mondo’</em>, how could you issue the query since the object is stored as XML? The solution is creating a custom Criteria Operator. I do not want to bother you with the details, but the key method is something like this</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">override</span> <a title="NHibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">NHibernate</a>.SqlCommand.SqlString ToSqlString(</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <a title="NHibernate" href="http://www.codewrecks.com/blog/index.php/tag/nhibernate/">NHibernate</a>.<a title="ICriteria" href="http://www.codewrecks.com/blog/index.php/tag/icriteria/">ICriteria</a> criteria, </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    ICriteriaQuery criteriaQuery, </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    IDictionary&lt;<span style="color: rgb(0, 0, 255);">string</span>, <a title="NHibernate" href="http://www.codewrecks.com/blog/index.php/tag/nhibernate/">NHibernate</a>.IFilter&gt; enabledFilters)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">{</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">string</span> objectPropertyColumnName = criteriaQuery</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        .GetColumnsUsingProjection(criteria, PropertyName)[0];</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    StringBuilder criteriaText = <span style="color: rgb(0, 0, 255);">new</span> StringBuilder();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    criteriaText.Append(objectPropertyColumnName);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    criteriaText.Append(<span style="color: rgb(0, 96, 128);">&quot;.value('(/*/&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    criteriaText.Append(XmlCriterionParameters.ChildPropertyName);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    criteriaText.Append(<span style="color: rgb(0, 96, 128);">&quot;)[1]', 'nvarchar(max)')&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">switch</span> (XmlCriterionParameters.CriteriaOperator)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 0, 255);">case</span> CriteriaOperator.Equal:</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">            criteriaText.Append(<span style="color: rgb(0, 96, 128);">&quot;=&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            <span style="color: rgb(0, 0, 255);">break</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        <span style="color: rgb(0, 0, 255);">case</span> CriteriaOperator.Like:</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            criteriaText.Append(<span style="color: rgb(0, 96, 128);">&quot; like &quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">            <span style="color: rgb(0, 0, 255);">break</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 0, 255);">default</span> :</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">            <span style="color: rgb(0, 0, 255);">throw</span> <span style="color: rgb(0, 0, 255);">new</span> NotSupportedException(<span style="color: rgb(0, 96, 128);">&quot;Still not supported operator&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    criteriaText.AppendFormat(<span style="color: rgb(0, 96, 128);">&quot;'{0}'&quot;</span>, Value);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 255);">new</span> SqlString(criteriaText.ToString());</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&#160;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">}</pre>
<p><!--CRLF--></div>
</div>
<p>In this example I’ve implemented only “=” and “like” operators, but it is enough for this sample. Now I can issue the following query.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">Query query = Query.CreateXml(<span style="color: rgb(0, 96, 128);">&quot;SpecificNotes&quot;</span>, <span style="color: rgb(0, 96, 128);">&quot;Ciao&quot;</span>, CriteriaOperator.Equal, <span style="color: rgb(0, 96, 128);">&quot;mondo&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">var result = Repository.Father.GetByCriteria(query);</pre>
<p><!--CRLF--></div>
</div>
<p>This snippet used a Query Model to specify the query, but as you can see the important aspect is that I’m able to create an XML criterion on the “SpecificNotes” sub object, where the expando property “Ciao” is Equal to the value “mondo”. Here is the query that was issued to the DB</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">SELECT</span> ...</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">FROM</span>   Father this_</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">WHERE</span>  this_.SpecificNotes.<span style="color: rgb(0, 0, 255);">value</span>(<span style="color: rgb(0, 96, 128);">'(/*/Ciao)[1]'</span>, <span style="color: rgb(0, 96, 128);">'nvarchar(max)'</span>) = <span style="color: rgb(0, 96, 128);">'mondo'</span></pre>
<p><!--CRLF--></div>
</p></div>
</p>
<p>This is quite a primitive query, because of the /*/ that basically mean I’m searching for a property Ciao contained in any subobject, but you can modify your Custom Criterion to adapt the XPath to your need; the important aspect is that I’m now able to store and retrieve transparently my expando objects from a standard <a title="SQL server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">SQL server</a> thanks to the great flexibility of NHibernate.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/12/01/custom-xml-serialization/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Use SQLite to test NH&#8211;DateAdd function</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/08/29/use-sqlite-to-test-nhdatediff-function/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/08/29/use-sqlite-to-test-nhdatediff-function/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 15:52:33 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Nhibernate]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/08/29/use-sqlite-to-test-nhdatediff-function/</guid>
		<description><![CDATA[1: DateAdd(dd, T.AnalysisFrequence, :refdate) Quite often I use SQLite to create Unit Tests of NH queries with a fast In-Memory database, and then run the queries against a standard Sql Server database. Today I have this problem, an HQL query uses the DateAdd Sql Server function 1: DateDiff(dd, T.AnalysisFrequence, :refdate) In this query with the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F08%2F29%2Fuse-sqlite-to-test-nhdatediff-function%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F08%2F29%2Fuse-sqlite-to-test-nhdatediff-function%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Nhibernate&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> DateAdd(dd, T.AnalysisFrequence, :refdate)</pre>
<p><!--CRLF--></div>
</p></div>
<p>Quite often I use <a title="SQLite" href="http://www.codewrecks.com/blog/index.php/tag/sqlite/">SQLite</a> to create Unit Tests of NH queries with a fast In-Memory database, and then run the queries against a standard <a title="Sql Server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">Sql Server</a> database.</p>
<p>Today I have this problem, an HQL query uses the DateAdd <a title="Sql Server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">Sql Server</a> function </p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> DateDiff(dd, T.AnalysisFrequence, :refdate)</pre>
<p><!--CRLF--></div>
</div>
<p>In this query with the DateAdd function I want to obtain a date that is calculated adding an amount of days from a reference date. Running this query against a <a title="SQLite" href="http://www.codewrecks.com/blog/index.php/tag/sqlite/">SQLite</a> database gave me an error, clearly because there is no DateAdd function in SQLite. </p>
<p>The solution is simple, you can use the julianday if you are using the â€œddâ€ constant in the DateAdd function (you are subtracting days and not seconds or other time value). You can add this function to your custom dialect to create a DateAdd function in the SQLite dialect.</p>
<p>RegisterFunction(&quot;DateAdd&quot;, new SQLFunctionTemplate(NHibernateUtil.Date, &quot;julianday(?3) + ?2&quot;));</p>
<p>This is valid only if you use the DateAdd(â€œddâ€, xxx, yy) but since this is my situation, this solves my problems.</p>
<p>Alk.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/08/29/use-sqlite-to-test-nhdatediff-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using schema with NHibernate and SqlLite</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/08/02/using-schema-with-nhibernate-and-sqllite/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/08/02/using-schema-with-nhibernate-and-sqllite/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 18:02:28 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Nhibernate]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/08/02/using-schema-with-nhibernate-and-sqllite/</guid>
		<description><![CDATA[Some time ago I blogged about a technique to use SqLite in Unit testing when you have nhibernate mapping that targets Sql Server tables in a schema different from dbo. The problem was: if you specify the schema name in the mapping of a class, then you are not able to execute the test in [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F08%2F02%2Fusing-schema-with-nhibernate-and-sqllite%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F08%2F02%2Fusing-schema-with-nhibernate-and-sqllite%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Nhibernate,Testing&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Some time ago I <a href="http://www.codewrecks.com/blog/index.php/2009/07/24/manage-in-memory-nhibernate-test-with-sqlite-and-database-schema/">blogged about a</a> technique to use <a title="SqLite" href="http://www.codewrecks.com/blog/index.php/tag/sqlite/">SqLite</a> in Unit <a title="testing" href="http://www.codewrecks.com/blog/index.php/category/programming/testing/">testing</a> when you have <a title="nhibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">nhibernate</a> mapping that targets <a title="Sql Server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">Sql Server</a> tables in a schema different from dbo. The problem was: if you specify the schema name in the mapping of a class, then you are not able to execute the <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a> in <a title="SqLite" href="http://www.codewrecks.com/blog/index.php/tag/sqlite/">SqLite</a> database.</p>
<p>Ex. if you have this mapping.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/08/image.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/08/image_thumb.png" width="314" height="82" /></a></p>
<p>This is a mapping for a table called EngagementView in a schema called <em>report</em> and when u try to run some <a target="_blank" title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a> on this <a title="entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">entity</a> with SqLite you got an error telling that there is no report database. The solution is to use the schema element of the mapping. The right schema should be</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">class</span> <span style="color: rgb(255, 0, 0);">name</span><span style="color: rgb(0, 0, 255);">=&quot;xxxxx&quot;</span>  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>     <span style="color: rgb(255, 0, 0);">schema</span><span style="color: rgb(0, 0, 255);">=&quot;report&quot;</span> <span style="color: rgb(255, 0, 0);">table</span><span style="color: rgb(0, 0, 255);">=&quot;EngagementView&quot;</span> <span style="color: rgb(0, 0, 255);">/&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>With this mapping <a title="NHibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">NHibernate</a> understand that the table EngagementView is on the schema report and the SqLite dialect is able to handle this without the need of the trick described in my old post. If you now issue a query against the EngagementView object you can verify that the query is issued again a table called report_EngagementView. This happens because SqLite has no concept of schema and the dialect can correctly generate the table name prepending the Table name with Schema and an underscore.</p>
<p>Alk.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/08/02/using-schema-with-nhibernate-and-sqllite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A strange bug in NHibernate 2.1</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/07/28/a-strange-bug-in-nhibernate-2-1/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/07/28/a-strange-bug-in-nhibernate-2-1/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 12:46:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Nhibernate]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/07/28/a-strange-bug-in-nhibernate-2-1/</guid>
		<description><![CDATA[In a previous post I described a technique to insert query hints into NHibernate query with the use of comments. Testing this code in a real project lead to a strange exception when I issue queries with ICriteria The query should start with &#8216;SELECT&#8217; or &#8216;SELECT DISTINCT&#8217; This happens because ICriteria queries inserts comments inside [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F07%2F28%2Fa-strange-bug-in-nhibernate-2-1%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F07%2F28%2Fa-strange-bug-in-nhibernate-2-1%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Nhibernate&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In a <a target="_blank" href="http://www.codewrecks.com/blog/index.php/2011/07/23/use-sql-server-query-hints-with-nhibernate-hql-and-icriteria/">previous post</a> I described a technique to insert query hints into <a href="http://nhforge.org/Default.aspx">NHibernate</a> query with the use of comments. <a target="_blank" title="Testing" href="http://www.codewrecks.com/blog/index.php/category/programming/testing/">Testing</a> this code in a real project lead to a strange exception when I issue queries with <a href="http://knol.google.com/k/nhibernate-chapter-13-criteria-queries#">ICriteria</a></p>
<blockquote><p>The query should start with &#8216;SELECT&#8217; or &#8216;SELECT DISTINCT&#8217;</p>
</blockquote>
<p>This happens because <a title="ICriteria" href="http://www.codewrecks.com/blog/index.php/tag/icriteria/">ICriteria</a> queries inserts comments inside the query and if you enable comments to flow into the query with the setting use_sql_comments something weird happens when you use SetMaxResults to issue a paginated query. If you look into the <a title="NHibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">NHibernate</a> code that is throwing the exception you find this function in the Sql Dialect</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 255);">int</span> GetAfterSelectInsertPoint(SqlString sql)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>     <span style="color: rgb(0, 0, 255);">if</span> (sql.StartsWithCaseInsensitive(<span style="color: rgb(0, 96, 128);">&quot;select distinct&quot;</span>))</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>         <span style="color: rgb(0, 0, 255);">return</span> 15;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>     <span style="color: rgb(0, 0, 255);">else</span> <span style="color: rgb(0, 0, 255);">if</span> (sql.StartsWithCaseInsensitive(<span style="color: rgb(0, 96, 128);">&quot;select&quot;</span>))</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>         <span style="color: rgb(0, 0, 255);">return</span> 6;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum11">  11:</span>     <span style="color: rgb(0, 0, 255);">throw</span> <span style="color: rgb(0, 0, 255);">new</span> NotSupportedException(<span style="color: rgb(0, 96, 128);">&quot;The query should start with 'SELECT' or 'SELECT DISTINCT'&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum12">  12:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>As the name of the function states, the purpose of this code is finding the insert point in the query immediately after the Select part of the query and is used when you call SetMaxResult to insert the TOP keyword immediately after the select clause. Since using <a title="ICriteria" href="http://www.codewrecks.com/blog/index.php/tag/icriteria/">ICriteria</a> can generate comments in the SQL like<em><strong> /* criteria query */</strong></em>, this lead to an obvious exception because the query does not start with a&#160; standard select or select distinct. But having comment does not harms in any way the generation of a paginated query, so I changed the function in this way</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 255);">int</span> GetAfterSelectInsertPoint(SqlString sql)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>     Int32 selectPosition = 0;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>     <span style="color: rgb(0, 0, 255);">if</span> ((selectPosition = sql.IndexOfCaseInsensitive(<span style="color: rgb(0, 96, 128);">&quot;select distinct&quot;</span>)) &gt;= 0)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>         <span style="color: rgb(0, 0, 255);">return</span> selectPosition + 15; <span style="color: rgb(0, 128, 0);">// &quot;select distinct&quot;.Length;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>     <span style="color: rgb(0, 0, 255);">else</span> <span style="color: rgb(0, 0, 255);">if</span> ((selectPosition = sql.IndexOfCaseInsensitive(<span style="color: rgb(0, 96, 128);">&quot;select&quot;</span>)) &gt;= 0)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>         <span style="color: rgb(0, 0, 255);">return</span> selectPosition + 6; <span style="color: rgb(0, 128, 0);">// &quot;select&quot;.Length;</span></pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum11">  11:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum12">  12:</span>     <span style="color: rgb(0, 0, 255);">throw</span> <span style="color: rgb(0, 0, 255);">new</span> NotSupportedException(<span style="color: rgb(0, 96, 128);">&quot;The query should start with 'SELECT' or 'SELECT DISTINCT'&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum13">  13:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Using InexOfCaseInsensitive function permits me to find the insertion point after the select clause even if some comments are present in the query. Now everything works as expected as you can verify from <a target="_blank" href="http://nhprof.com/">NhibernateProfiler</a>. I run all the NH tests and they are all green, excepts one that verify that an exception is thrown if the query begins with /* criteria query */, but this is the bug I want to fix <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  so I do not care about it <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Now I run the query again with my custom recompiled version of <a title="nhibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">nhibernate</a> and I got.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/image8.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/image_thumb8.png" width="151" height="121" /></a></p>
<p><strong>Figure 1: </strong><em>The top 5 clause was correctly inserted in the query even if there is a comment present in the query</em></p>
<p>Gotcha: the bug is solved, top 5 was correctly inserted even if there are comments on top of the query. Having the source of a library is invaluable if you need to fix bug <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>alk.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/07/28/a-strange-bug-in-nhibernate-2-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use Sql Server Query Hints with NHibernate HQL and ICriteria</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/07/23/use-sql-server-query-hints-with-nhibernate-hql-and-icriteria/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/07/23/use-sql-server-query-hints-with-nhibernate-hql-and-icriteria/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 09:01:38 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Nhibernate]]></category>
		<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[HQL]]></category>
		<category><![CDATA[ICriteria]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/07/23/use-sql-server-query-hints-with-nhibernate-hql-and-icriteria/</guid>
		<description><![CDATA[When you work with Big databases with many records and not uniform distribution of data into columns used for join or where conditions, you can have really bad performance problem due to Query Plan caching. I do not want to give a deep explanation of this problem, you can find information here, but I want [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F07%2F23%2Fuse-sql-server-query-hints-with-nhibernate-hql-and-icriteria%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F07%2F23%2Fuse-sql-server-query-hints-with-nhibernate-hql-and-icriteria%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=HQL,ICriteria,Nhibernate,Performance,Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>When you work with Big databases with many records and <em>not uniform distribution of data into columns used for join or where conditions</em>, you can have really bad <a target="_blank" title="performance" href="http://www.codewrecks.com/blog/index.php/tag/performance/">performance</a> problem due to Query Plan caching. I do not want to give a deep explanation of this problem, you can find information <a href="http://legeronline.blogspot.com/2009/03/evils-of-slow-paramaterized-query-plans.html">here</a>, but I want to outline the <a title="general" href="http://www.codewrecks.com/blog/index.php/category/general/">general</a> problem to make clear what is happening.</p>
<p>We can argue a lot why this problem happens in a database engine, but basically I have a really bad situation where the db stores data of multiple customers with really non uniform distribution of data (some of them have ~1000 rows for each table, others have ~100.000 rows in some of the tables). </p>
<p><strong>The problem is the following one</strong>: the software issue a query (two tables in join) filtered with a customer Id that has few rows on these tables; SQL-Server creates a plan where tables are joined with nested loops and the query executes in 2 milliseconds.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/Untitled2.jpg"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="Untitled2" border="0" alt="Untitled2" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/Untitled2_thumb.jpg" width="545" height="282" /></a></p>
<p><strong>Figure 1: </strong><em>Query with a value of parameter that filters for a customer that has ~100 rows in the two tables used in the join (elapsed time = 2ms).</em></p>
<p>As you can see the execution time is almost zero, now I execute the very same query, changing the value of the parameter to filter records for a customer that has nearly 100.000 records, since the query is the same, <a title="sql server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">sql server</a> uses the same execution plan. </p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/Untitled3.jpg"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="Untitled3" border="0" alt="Untitled3" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/Untitled3_thumb.jpg" width="492" height="281" /></a></p>
<p><strong>Figure 2: </strong><em>Query gets executed filtering for a customer that has ~100.000 rows in the two table that joins together (elapsed time = 2824 ms).</em></p>
<p>The query is three order of magnitude slower (2.824 ms vs 2 ms) and for query that are more complex (three tables in join), sometimes the execution time is more than 5 minutes and the software gives a TimeoutException. As you can see from <strong>Figure 2</strong>, the gray arrow from the bottom table is really wider respect of the arrows of <strong>Figure 1</strong>, because this customer has more rows in database. If you issue a <a target="_blank" href="http://msdn.microsoft.com/it-it/library/ms174283.aspx">DBCC FREEPROCCACHE</a> command to<em> clear all cached query plans</em> and execute again the query of <strong>Figure 2, </strong>you got an execution time of 28 ms (100 times faster than before XD).</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/Untitlewerwerd.jpg"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="Untitlewerwerd" border="0" alt="Untitlewerwerd" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/Untitlewerwerd_thumb.jpg" width="444" height="189" /></a>&#160;</p>
<p><strong>Figure 3: </strong><em>Join is done with an hash match, a technique that is more efficient for joining two tables with a lot of rows. (elapsed time = 28 ms).</em></p>
<p>In figure 3 you can see that if you <em>clear the query plan cache</em> and execute again the query, <a title="SQL Server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">SQL Server</a> check the table, and since this customer has a lot of rows it uses HASH MATCH join and not a nested loop. Now if you execute the query for the customer used in <strong>Figure 1</strong> that has few rows, the execution time is 28 ms, higher than 2 ms because an hash match is less efficient for few rows. </p>
<p><strong>This lead to a nasty problem due to order of execution of the query</strong>: if I issue the same query, first for the customer with a lot of rows and then for the customer with few rows, I got an execution time of 28 ms for each query, if I reverse the order of the query I got 2 ms and 2800 ms thus the system is much slower . This happens because the Hash Match is not good for few number of rows, (28 ms against 2 ms), but the loss of <a title="performance" href="http://www.codewrecks.com/blog/index.php/tag/performance/">performance</a> when we have a small set of record is not so bad; at the contrary, the Nested Loop is a <strong>KILLER for big resultset </strong>and this can even cause execution Timeout. Having such a problem in production is like sitting on a bomb ready to explode. (A customer calls telling us that the software is completely unresponsive, you verify that this is true, but for other customers everything is quite good <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/Untitled.jpg"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="Untitled" border="0" alt="Untitled" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/Untitled_thumb.jpg" width="250" height="172" /></a></p>
<p>The only solution to this approach is using the <a target="_blank" href="http://sqltutorials.blogspot.com/2008/03/with-recompile-re-compile-execution.html">OPTION (NORECOMPILE)</a> for all queries that present this problem, or you can instruct the query governor to prefer some join option (using <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms181714(v=SQL.90).aspx">OPTION (HASH JOIN)</a> for example) if you already know that this is the right execution plan for all data distribution. All these techniques are called<strong><em> </em></strong><a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms181714(v=SQL.90).aspx"><strong><em>â€œquery hintâ€</em></strong></a> and are the only way to solve bad performance problem of parameterized query when you have non uniform data distribution.</p>
<p>Now the problem seems solved, but, wait!!!!!, <strong><em>most of the queries are issued by <a title="nhibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">nhibernate</a></em></strong>, and I need to find a way to add query hints to <a title="nhibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">nhibernate</a> query, a task that is not supported natively by NH. Solving this problem is a two phase process, first of all you need to find a way to insert text into <a title="nhibernate" href="http://www.codewrecks.com/blog/index.php/tag/nhibernate/">nhibernate</a> generated SQL, a task that can easily solved by an interceptor.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> [Serializable]</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> QueryHintInterceptor : EmptyInterceptor</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>     <span style="color: rgb(0, 0, 255);">internal</span> <span style="color: rgb(0, 0, 255);">const</span> <span style="color: rgb(0, 0, 255);">string</span> QueryHintHashJoinCommentString = <span style="color: rgb(0, 96, 128);">&quot;queryhint-option-hash&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>     <span style="color: rgb(0, 0, 255);">internal</span> <span style="color: rgb(0, 0, 255);">const</span> <span style="color: rgb(0, 0, 255);">string</span> QueryHintRecompileCommentString = <span style="color: rgb(0, 96, 128);">&quot;queryhint-recompile&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>     <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">override</span> <a title="NHibernate" href="http://www.codewrecks.com/blog/index.php/tag/nhibernate/">NHibernate</a>.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>         <span style="color: rgb(0, 0, 255);">if</span> (sql.ToString().Contains(QueryHintHashJoinCommentString))</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum11">  11:</span>             <span style="color: rgb(0, 0, 255);">return</span> sql.Insert(sql.Length, <span style="color: rgb(0, 96, 128);">&quot; option(HASH JOIN)&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum12">  12:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum13">  13:</span>         <span style="color: rgb(0, 0, 255);">if</span> (sql.ToString().Contains(QueryHintRecompileCommentString))</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum14">  14:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum15">  15:</span>             <span style="color: rgb(0, 0, 255);">return</span> sql.Insert(sql.Length, <span style="color: rgb(0, 96, 128);">&quot; option(RECOMPILE)&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum16">  16:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum17">  17:</span>         <span style="color: rgb(0, 0, 255);">return</span> <span style="color: rgb(0, 0, 255);">base</span>.OnPrepareStatement(sql);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum18">  18:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum19">  19:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>As you can see the code is really simple, the interceptor inherits from EmptyInterceptor and override the OnPrepareStatement(), <strong>adding the right Query Hint to the end of the query</strong>. This is probably not 100% production ready code, because Iâ€™m not 100% sure that for complex query, inserting the hint at the end of the query is the right choice, but for my specific problem is enough and it is a good starting point.</p>
<p>If you look at the code you can verify that Iâ€™m checking for certain string in query text to add the appropriate hint, but how can you add these strings to the query to enable query hint to be generated? The answer is â€œcommentsâ€. First of all I add this interceptor to NH configuration, so it got attached to every session.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> cfg.SetInterceptor(<span style="color: rgb(0, 0, 255);">new</span> QueryHintInterceptor());</pre>
<p><!--CRLF--></div>
</div>
<p>But I also enable comments in SQL code in NH configuration setting the property <strong><em>use_sql_comments </em></strong>to true, now I only need a couple of extension methods like this one.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> IQuery QueryHintRecompile(<span style="color: rgb(0, 0, 255);">this</span> IQuery query)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>  {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>      query.SetComment(QueryHintInterceptor.QueryHintRecompileCommentString);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>      <span style="color: rgb(0, 0, 255);">return</span> query;</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>  }</pre>
<p><!--CRLF--></div>
</div>
<p>That enables me to write this HQL query.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> session.CreateQuery(<span style="color: rgb(0, 96, 128);">&quot;Select y from LinkResult y where ClieId = :param&quot;</span>)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>     .QueryHintRecompile()</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>     .SetInt32(<span style="color: rgb(0, 96, 128);">&quot;param&quot;</span>, 11)</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>     .List();</pre>
<p><!--CRLF--></div>
</div>
<p>As you can see I have now the QueryHintRecompile() method that permits me to specify for each single HQL Query (you can add similar extension method to <a title="ICriteria" href="http://www.codewrecks.com/blog/index.php/tag/icriteria/">ICriteria</a>) the query hint to use. Running this query output this query.</p>
<p></p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/23-07-2011-10-36-27.jpg"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="23-07-2011 10-36-27" border="0" alt="23-07-2011 10-36-27" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/07/23-07-2011-10-36-27_thumb.jpg" width="524" height="199" /></a></p>
<p><strong>Figure 4:</strong><em> The query with the query hint specified by the QueryHintRecompile()</em></p>
<p>Thanks to incredible number of extension points offered by NHibernate, adding Query Hint to queries is really a breeze.</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/NHibernate" rel="tag">NHibernate</a> <a target="_blank" href="http://technorati.com/tag/Sql Server" rel="tag">Sql Server</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/07/23/use-sql-server-query-hints-with-nhibernate-hql-and-icriteria/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Entity Framework, a super dataset?</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/06/09/entity-framework-a-super-dataset/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/06/09/entity-framework-a-super-dataset/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 08:26:21 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Nhibernate]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/06/09/entity-framework-a-super-dataset/</guid>
		<description><![CDATA[Entity Framework is quite a good product, but in my opinion still misses some point to be called an ORM. I must admit that I never used the 4.1 Code first in real project, but there are some stuff that still does not convince me when using EF. When I decide to use EF (and [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F06%2F09%2Fentity-framework-a-super-dataset%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F06%2F09%2Fentity-framework-a-super-dataset%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Entity+Framework,Nhibernate,ORM&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a target="_blank" title="Entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">Entity</a> Framework is quite a good product, but in my opinion still misses some point to be called an ORM. I must admit that I never used the 4.1 Code first in real project, but there are some stuff that still does not convince me when using EF.</p>
<h3>When I decide to use EF (and not NH) </h3>
<p>Sometimes I need to access small legacy databases to do a small amount of operations and I must admit that adding a EF Model from <a target="_blank" title="Visual Studio" href="http://blogs.msdn.com/b/visualstudio/">Visual Studio</a> is a matter of seconds and the <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">LINQ</a> provider of EF is quite good so it is a valuable alternative to create a quick DAL. I have a simple database (four tables), I created an EF model, and then issue this query.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image7.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb7.png" width="301" height="93" /></a></p>
<p><strong>Figure 1: </strong><em>A very simple piece of code that query database with EF</em></p>
<p>This is a very super simple piece of code, in the first line I grab an instance of Term from the database (actually I grab the first one, in real code I have a complex query that returns a specific term). Then I want to grab all instance of Verbs that are related to this term, so I issue the second query, what will be the result?</p>
<p><img src="http://t0.gstatic.com/images?q=tbn:ANd9GcSdyTdE26j7X1A04CUg2PNg9VAWxnxfX6mIRYKgv_6jpdSKN7cCEQ&amp;t=1" /></p>
<p>This image exceptionally capture my face the first time I ran this query.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/SNAGHTML4bb74e.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="SNAGHTML4bb74e" border="0" alt="SNAGHTML4bb74e" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/SNAGHTML4bb74e_thumb.png" width="548" height="150" /></a></p>
<p><strong>Figure 2: </strong><em>Strange error in the query.</em></p>
<p>Iâ€™m really disappointed from this exception for many reasons. First of all I do not know why it does not work, because this is a perfectly and reasonable query to issue. </p>
<h3>Fix the query to make it works </h3>
<p>To verify that the query is indeed ok lets modify it in this way.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image8.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb8.png" width="293" height="71" /></a></p>
<p><strong>Figure 3: </strong><em>Modified version of the query, now it works.</em></p>
<p>This version works simply changing the order of the <em>Where</em> and <em>ToList</em> operators. It now works because it <em>loads the entire Verbs table in memory</em> and then filters out with <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">LINQ</a> to Object. This clearly makes me sick, because this creates a big memory pressure. The second issue with the above error is the error message, that is really obscure. <em>Unable to create a constraint value of type xxxxxxxx</em>. If you read with caution you could eventually understand that you cannot put a condition based on equality of objects, but only primitive type. </p>
<p>This error could have a good reason to exists: if the Term <a target="_blank" title="entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">entity</a> overrides the Equality method, it could be impossible to create an equivalent query in SQL to mimic the same behavior by the EF <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/tag/linq/">LINQ</a> provider. Suppose that the Term overrides the Equal operator comparing two entities by checking for equality of the <strong>Value</strong> and <strong>Type</strong> properties, how could the EF provider understand this behavior to issue an equivalent query to the database? I can agree with this reason, but only if the error would be something like â€œCould not issue a comparison between two entities in a EF query because it is impossible to mimic the semantic of equals operator.. bla bla blaâ€. </p>
<h3>A real solution, but you need to make your hands dirty</h3>
<p>But working with objects in <a target="_blank" title="DDD" href="http://www.domaindrivendesign.org/">DDD</a> or <a title="Domain Model" href="http://www.codewrecks.com/blog/index.php/tag/domain-model/">Domain Model</a> or even with Active Record pattern, the programmer in 99.99% of the situation, translate the condition <strong><em>V.Term == term</em></strong> with: â€œ<em>gives me all <strong>verbs</strong> that are <strong>connected</strong> with <strong>term</strong>â€. </em>This because we are used to the fact that a reference from Verb entity to Term entity is part of the object graph and this can easily be translated with a comparison between identity operators. Given that, you can rewrite the above query in this way.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image9.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2011/06/image_thumb9.png" width="407" height="77" /></a></p>
<p><strong>Figure 3: </strong><em>The right way to issue the EF query</em></p>
<p>Now the query runs fine, but you can have two completely different opinion about it. </p>
<p>The first opinion is negative, because I need to understand the name of the property/properties used as id by the ORM. I know that by convention you can use a property named <strong>Id </strong>for every entity, but sometimes, especially with legacy database you can have entities with composite key or identity property named differently. <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/tag/linq/">LINQ</a> is great to express query in the object model, but in this situation I need to be aware of details of the ORM Mapping to issue the right query and this is bad.</p>
<p><img src="http://www.mydswa.org/wp-content/uploads/2010/09/negative-attitude.gif" /></p>
<p>The second type of opinion is positive, because this query does not suffer from mismatch between the LINQ to entities counterpart when someone redefines the equals operator for the entity. </p>
<p><img src="http://www.tigweb.org/images/express/panorama/articles/26121.jpg" width="153" height="153" /></p>
<h3>Conclusion</h3>
<p>I tend to agree with the dislike part, because I want my ORM to permit me to express query without being aware of the details. So I tend to consider EF not a real ORM but something like a <strong><em>SuperDataset</em></strong>, it is quick to use with database first approach, it permits you to create a DAL quickly, but it forces you to know the detail of the database, like a dataset. Clearly EF has <strong>really a lot of interesting feature respect to a dataset</strong>, but it still miss something to be considered an ORM.</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/ORM" rel="tag">ORM</a> <a target="_blank" href="http://technorati.com/tag/Entity Framework" rel="tag">Entity Framework</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/06/09/entity-framework-a-super-dataset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

