<?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>Web Dev Bros &#187; nhibernate</title>
	<atom:link href="http://www.webdevbros.net/category/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webdevbros.net</link>
	<description>hot talk about web development</description>
	<lastBuildDate>Thu, 20 Jan 2011 19:55:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Nhibernate returns duplicate results on paged data sets &#8211; work around</title>
		<link>http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/</link>
		<comments>http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 12:02:13 +0000</pubDate>
		<dc:creator>Dai Bok</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=909</guid>
		<description><![CDATA[There is a problem with Nhibernate, when paging record sets, using SQLServer 2008. This offers a work around, to fix this problem.]]></description>
			<content:encoded><![CDATA[<p>Recently, while implementing a page-able data grid with <a href="http://nhforge.org/">nHibernate </a>and  <a href="http://mvccontrib.codeplex.com/">MVC Contrib Grid</a>, I came across a strange problem. My result set had duplicates, and the strange thing was that it would only happen when paging my record set. Anyway, I thought I would write a little post about how I solved the problem, just in case someone else comes across it. </p>
<p>Firstly, lets look at simplified description of the problem. Those using MySQL have the luxury of limit, which makes paging data sets a breeze, but in SQL Server(and Oracle) things get a bit more “tricky”. The trick is to count the rows on the result set, using RowCount over something, and mix that in with a sub query, but there is a problem with nHibernate T-SQL 2005 Dialect. The RowCount was being used on the sub-query, and not the parent query. Now that I had discovered the problem.</p>
<blockquote><p><strong> &#8220;ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row&#8221;</strong> is in the wrong place!</p></blockquote>
<p>After some research, I found that other people were having this problem too.<a href="http://www.daczkowski.net/2010/09/07/rows-duplication-for-certain-nhibernate-queries-%E2%80%93-workaround/"> Marcin Daczkowski has an excellent work-around, that he blogged about</a>, he also describes the problem with some NH generated SQL examples, I won’t repeat myself here, so have a look at his post if you are not sure and check his <a href="http://216.121.112.228/browse/NH-2214">bug report here</a>. </p>
<p><a href="http://julianjelfs.wordpress.com/2009/04/03/nhibernate-removing-duplicates-combined-with-paging/">I found a another solution here too- not sure this one works though</a>, ultimately I had to come up with a solution that suited my project.</p>
<p>There are some reasons why I can’t use Marcin Daczkowski solution.<br />
<br />
<strong> &#8211; Firstly</strong>, it does not look like the guys at nHibernate be able to release the patched version of nHibernate any time soon,I guess they are very busy working hard on version 3.0! Can&#8217;t wait for that realease!  <a href="http://216.121.112.228/browse/NH-2214?focusedCommentId=20075&amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20075">see the comments here.</a><br />
<br />
<strong> &#8211; Secondly</strong>, if I build my own version of nHibernate, I will need to also rebuild all my dependencies, linking them with Marcins patched version. That means <strong>FluentNHibernate</strong> needs to be rebuilt, <strong>NHibernate.Caches.SysCache</strong> needs to be rebuilt, <strong>NHibernate.ByteCode.Castle</strong> needs to be rebuilt, you get the picture?</p>
<p>So after some thought and source code investigation, I came up with the idea of making a customised Dialect, and just use Darcins patched files. Now, I don’t need to build a patched version of my all my open source dependencies, as I have my own SQL dialect set up in the fluent configuration.</p>
<p><code><br />
        _sessionFactory = Fluently.<span style="color:#3399CC">Configure</span>()<br />
 &nbsp; &nbsp;  &nbsp; .Database(<span style="color:#3399CC">MsSqlConfiguration</span><br />
 &nbsp; &nbsp;  &nbsp; .MsSql2008<br />
 &nbsp; &nbsp;  &nbsp; .ConnectionString(connectionString)<br />
 &nbsp; &nbsp;  &nbsp; .CurrentSessionContext(<span style="color:#CC3300">"web"</span>)<br />
 &nbsp; &nbsp;  &nbsp; .Dialect(<span style="color:#3333CC">CustomSQL2008Dialect</span>)<br />
                )<br />
</code></p>
<p> My Dialect classes are set up like this:</p>
<p><code><br />
<span style="color:#3333CC"> public class </span> <span style="color:#3399CC"> CustomSQL2008Dialect: CustomSQL2005Dialect </span> {<br />
 &nbsp; &nbsp;  &nbsp; <span style="color:#3333CC">public </span> <span style="color:#3399CC">  GWMsSql2008Dialect</span> () {<br />
 &nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp;  &nbsp;  <span style="color:#347C17"> // Duplicate of the contents of MsSsql2008Dialect constructor  goes here </span><br />
&nbsp; &nbsp;  &nbsp;   }<br />
 }</p>
<p><span style="color:#3333CC"> public class </span> <span style="color:#3399CC"> CustomSQL2005Dialect: MsSql2000Dialect </span> {<br />
 &nbsp; &nbsp;  &nbsp; <span style="color:#347C17"> // the contents Darcins MsSql2005Dialect file goes here.<br />
 &nbsp; &nbsp;  &nbsp; // <a href="http://216.121.112.228/browse/NH-2214"> MsSql2005Dialect at NHibernate JIRA</a></span><br />
 }<br />
</code></p>
<p>Luckily, I have unit tests set up for all my repository methods, and after seeing the green bar in NUnit, I was more than satisfied with the custom dialect. I hope the guys at NHibernate manage to get things working in their next release, keep up the good work guys! And a special thanks to<br />
to Darcin, for writing the patch. </p>
<p>
Here is a copy <a href='http://www.webdevbros.net/wp-content/uploads/2011/01/CustomSql2008Dialect.cs_.zip'>the Custom Sql 2008 Dialect</a>. </p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/&amp;t=Nhibernate+returns+duplicate+results+on+paged+data+sets+-+work+around&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/&amp;title=Nhibernate+returns+duplicate+results+on+paged+data+sets+-+work+around&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/&amp;title=Nhibernate+returns+duplicate+results+on+paged+data+sets+-+work+around&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Nhibernate+returns+duplicate+results+on+paged+data+sets+-+work+around;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>FluentNhibernate and Stored Procedures</title>
		<link>http://www.webdevbros.net/2009/11/25/fluentnhibernate-and-stored-procedures/</link>
		<comments>http://www.webdevbros.net/2009/11/25/fluentnhibernate-and-stored-procedures/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 10:59:27 +0000</pubDate>
		<dc:creator>Dai Bok</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[general stuff]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[Fluent NHibernate]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=808</guid>
		<description><![CDATA[I am evaluating FluentNHibernate (FNH), to see if it is suitable for a project I am working on. Disappointingly, FNH does not support Store procedures of the box. Of course, FNH is under the BSD licence, so I am sure those who are confident enough can implement this for the rest of us! This post [...]]]></description>
			<content:encoded><![CDATA[<p>I am evaluating <a href="http://fluentnhibernate.org/">FluentNHibernate </a> (FNH), to see if it is suitable for a project I am working on. Disappointingly, FNH does not support Store procedures of the box. Of course, FNH is under the BSD licence, so I am sure those who are confident enough can implement this for the rest of us! This post will show how I got FNH to work with stored procedures, and can hopefully be followed as a working example.</p>
<p>FNH extends NHibernate, and automagically generates XML mapping files for your objects. Unfortunately, to get stored procedures to work, you need to take a step backwards, and create good old fashioned hbm.xml files, doing the mappings manually.</p>
<p>Firstly , let us look at the results  of the stored procedure that we want to map.</p>
<table border="0" align="center">
<tbody>
<tr>
<td>ID</td>
<td>enDescription</td>
<td>cyDescription</td>
<td>IsActive</td>
</tr>
<tr>
<td>1</td>
<td>Swansea</td>
<td>Abertawe</td>
<td>True</td>
</tr>
<tr>
<td>2</td>
<td>Cardiff</td>
<td>Caerdydd</td>
<td>True</td>
</tr>
<tr>
<td>3</td>
<td>Newport</td>
<td>Cas Newydd</td>
<td>False</td>
</tr>
</tbody>
</table>
<p>The class that will use this data is called lookup.</p>
<p>The code for this class is:</p>
<div style="font-family: Courier New;font-size: 10pt;color: black;background: white">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color: blue">namespace</span> Entities {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">Lookup</span>&nbsp; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">int</span> Id { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> EnDescription { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">string</span> CyDescription { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">virtual</span> <span style="color: blue">bool</span> IsActive { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;}</p>
</div>
<p>&nbsp; <br />
This object will be used to populate a simple drop down list, so that a user can select their county.</p>
<p>When I started using FluentHNibernate, I wanted to totally avoid using XML mappings, so I skipped<a href="http://www.amazon.co.uk/gp/product/193239415X?ie=UTF8&amp;tag=daisramblin-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=193239415X"> chapters 3 and 6 of Hibernate in Action.</a><img class=" luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs pcshjncfdcdawbvrvmch pcshjncfdcdawbvrvmch" style="border:none !important;margin:0px !important" src="http://www.assoc-amazon.co.uk/e/ir?t=daisramblin-21&amp;l=as2&amp;o=2&amp;a=193239415X" border="0" alt="" width="1" height="1" /> My first mistake! So for those attempting this, it may be worth your while understanding Hibernate mappings before you proceed. (You may also ask why I have the Java Book and my code is in C#, that is because I am quite used to working in different programming languages, so those who prefer examples in .Net examples check <a href="http://www.amazon.co.uk/gp/product/1932394923?ie=UTF8&amp;tag=daisramblin-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=1932394923">NHibernate in Action</a><img class=" luqmsfcfqseksqyogrfs" style="border:none !important;margin:0px !important" src="http://www.assoc-amazon.co.uk/e/ir?t=daisramblin-21&amp;l=as2&amp;o=2&amp;a=1932394923" border="0" alt="" width="1" height="1" />.)</p>
<p>Let’s move on to creating the mapping file.</p>
<p><em>IMPORTANT:</em> When you add the mapping file to your project, make sure you set the Build Action to Embedded Resource!</p>
<p>I have created a Lookup.hbm.xml file, and the source is below:</p>
<div style="font-family: Courier New;font-size: 10pt;color: black;background: white">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;<span style="color: blue">&lt;?</span><span style="color: #a31515">xml</span><span style="color: blue"> </span><span style="color: red">version</span><span style="color: blue">=</span>&quot;<span style="color: blue">1.0</span>&quot;<span style="color: blue"> </span><span style="color: red">encoding</span><span style="color: blue">=</span>&quot;<span style="color: blue">utf-8</span>&quot;<span style="color: blue"> ?&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color: blue">&lt;</span><span style="color: #a31515">hibernate-mapping</span><span style="color: blue"> </span><span style="color: red">xmlns</span><span style="color: blue">=</span>&quot;<span style="color: blue">urn:nhibernate-mapping-2.2</span>&quot;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </span><span style="color: red">namespace</span><span style="color: blue">=</span>&quot;<span style="color: blue">Entities</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">class</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Lookup</span>&quot;<span style="color: blue"> </span><span style="color: red">table</span><span style="color: blue">=</span>&quot;<span style="color: blue">dbo.sp_GetLookups</span>&quot;<span style="color: blue"> &gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">id</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Id</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">Id</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">generator</span><span style="color: blue"> </span><span style="color: red">class</span><span style="color: blue">=</span>&quot;<span style="color: blue">native</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/</span><span style="color: #a31515">id</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">EnDescription</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">enDescription</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">CyDescription</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">cyDescription</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;10</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">IsActive</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">IsActive</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;11</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">loader</span><span style="color: blue"> </span><span style="color: red">query-ref</span><span style="color: blue">=</span>&quot;<span style="color: blue">dbo.sp_GetLookups</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;12</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color: #a31515">class</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;13</span>&nbsp;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;14</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">sql-query</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">dbo.sp_GetLookups</span>&quot;<span style="color: blue"> &gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;15</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">return</span><span style="color: blue"> </span><span style="color: red">alias</span><span style="color: blue">=</span>&quot;<span style="color: blue">dbo.sp_GetLookups</span>&quot;<span style="color: blue"> </span><span style="color: red">class</span><span style="color: blue">=</span>&quot;<span style="color: blue">Lookup</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;16</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">return-property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Id</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">Id</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;17</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">return-property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">EnDescription</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">enDescription</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;18</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">return-property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">CyDescription</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">cyDescription</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;19</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">return-property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">IsActive</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">IsActive</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;20</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/</span><span style="color: #a31515">return</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;21</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exec dbo.sp_GetLookups</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;22</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color: #a31515">sql-query</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;23</span>&nbsp;<span style="color: blue">&lt;/</span><span style="color: #a31515">hibernate-mapping</span><span style="color: blue">&gt; </span></p>
</div>
<p>&nbsp; </p>
<p>To put it quite simply, lines 5 to 13 map my Lookup class to the columns in the stored procedure, while lines 16 to 20 map the results from the stored procedure my lookup class. Line 22 names the stored procedure. I am not sure if this is the best way to achieve the mappings, so any feedback would be appreciated.</p>
<p>Once your object is nicely  mapped, you then need to update your fluent configuration. All you need to do is tell FNH to load hbmMappings from the current assembly. See the snippet below:</p>
<div style="font-family: Courier New;font-size: 10pt;color: black;background: white">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp; .Mappings(m =&gt; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; m.HbmMappings.AddFromAssembly(<span style="color: #2b91af">Assembly</span>.GetExecutingAssembly());</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  m.FluentMappings.AddFromAssembly(<span style="color: #2b91af">Assembly</span>.GetExecutingAssembly());</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; })</p>
</div>
<p> &nbsp; </p>
<p>To retrieve the list of lookups, I do the following, which populates my results variable with a list of all my lookups.</p>
<div style="font-family: Courier New;font-size: 10pt;color: black;background: white">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> sessionfactory = CreateSessionFactory();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> session = sessionfactory.OpenSession();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> results = session.GetNamedQuery(<span style="color: #a31515">&quot;dbo.sp_GetLookups&quot;</span>).List();</p>
</div>
<p> &nbsp; </p>
<p>And that is it, the results variable now contains the list of lookups that I can use to populate my list control.</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2009/11/25/fluentnhibernate-and-stored-procedures/&amp;t=FluentNhibernate+and+Stored+Procedures&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2009/11/25/fluentnhibernate-and-stored-procedures/&amp;title=FluentNhibernate+and+Stored+Procedures&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2009/11/25/fluentnhibernate-and-stored-procedures/&amp;title=FluentNhibernate+and+Stored+Procedures&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=FluentNhibernate+and+Stored+Procedures;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2009/11/25/fluentnhibernate-and-stored-procedures/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2009/11/25/fluentnhibernate-and-stored-procedures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nhibernate and the Null object pattern</title>
		<link>http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/</link>
		<comments>http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 04:41:29 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[default value]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[if null]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[null reference]]></category>
		<category><![CDATA[pattern]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=349</guid>
		<description><![CDATA[Is this dialog (exception) familiar to you? Okay, maybe you haven&#8217;t seen this screen in particular but apart from the troubleshooting tips you should have seen it. Few days ago I have discovered the Null object pattern (NOP) and its usefulness. Kick me, punch me or even stone me to death cause I haven&#8217;t known [...]]]></description>
			<content:encoded><![CDATA[<p>Is this dialog (exception) familiar to you?</p>
<p><img src="http://www.webdevbros.net/wp-content/uploads/2009/07/null-object-pattern.png" alt="null-object-pattern" title="null-object-pattern" width="402" height="233" style="border:0px" class="size-full wp-image-350" /></p>
<p>Okay, maybe you haven&#8217;t seen this screen in particular but apart from the troubleshooting tips you should have seen it.<br />
<span id="more-349"></span><br />
Few days ago I have discovered the <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">Null object pattern (NOP)</a> and its usefulness. Kick me, punch me or even stone me to death cause I haven&#8217;t known it so far :) Still I would like to write a quick note about it as not everyone might be aware of it.</p>
<blockquote><p>
If it&#8217;s possible for a variable to be null, you have to remember to surround it with null test code so you&#8217;ll do the right thing if a null is present. Often the right thing is same in many contexts, so you end up writing similar code in lots of places &#8211; committing the sin of code duplication. &#8211; <a href="http://martinfowler.com/eaaCatalog/specialCase.html">Martin Fowler</a>
</p></blockquote>
<p>What does that exactly mean? It means the following:</p>
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">if</span> (Product.Creator == <span style="color: blue;">null</span>) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; Debug.WriteLine(<span style="color: maroon;">"unknown"</span>);</pre>
<pre style="margin: 0px;">} <span style="color: blue;">else</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; Debug.WriteLine(Product.Creator);</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p>If we have a <code>Creator</code> property of a <code>Product</code> and it may be null (to indicate an unknown creator) we need to do a null check everywhere we wanna display the creator. I would call it boring code duplication. And that&#8217;s where the NOP joins our game.</p>
<p>Let&#8217;s create a user instance which represents the actual &#8220;unknown&#8221; user so we can get rid of the odd null check. Best to achieve that is to sub class our <code>User</code> into an <code>Unknown</code> user. The following diagram illustrates this simple approach. btw see Customers equal to users :))</p>
<p><img src="http://www.webdevbros.net/wp-content/uploads/2009/07/nullobjectpatternclassdiagramm.gif" alt="nullobjectpatternclassdiagramm" title="nullobjectpatternclassdiagramm" width="244" height="189" class="aligncenter size-full wp-image-361" /><br />
<a href="http://martinfowler.com/eaaCatalog/specialCase.html">Figure 1 &#8211; Patterns of Enterprise Application Architecture, Martin Fowler</a></p>
<p>As you can see we are even able to provide different types of &#8220;null&#8221; users such as &#8220;Missing&#8221; and &#8220;Unknown&#8221;. That allows us to assign a human readable name to those users (e.g. assign in the sub class&#8217; constructor) and we are still able to check if a given user is unknown (if necessary):</p>
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">if</span> (Product.Creator <span style="color: blue;">is</span> UnknownUser)</pre>
</div>
<p>Okay, enough general theory about the pattern itself. You might ask how does the whole thing fit into NHibernate? Persistence is the magic word. If our users are persisted then the &#8220;unknown&#8221; users gets persisted as well. That&#8217;s probably not what we want. We wanna keep our data storage clean containing only &#8220;real&#8221; users. Michael Cromwell points that out in one of his articles:</p>
<blockquote><p>This then has its own problems because if it’s a user editable object you don’t really want them to be able to change/delete this data so it becomes a special case that will need to be locked for editing/deleting. &#8211; <a href="http://journalofasoftwaredev.wordpress.com/2009/01/17/nhibernate-null-object-pattern-the-options/">NHibernate &#038; Null Object Pattern: The Options, Michael Cromwell</a></p></blockquote>
<p>Correct! We don&#8217;t want to exclude this user in all our queries and take care of these &#8220;special case users&#8221;. What he suggests is to handle that case directly in the getter and setter of the property. In case a null is assigned he returns an &#8220;NotAssigned User&#8221;. In our particular example the solution would look the following:</p>
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">protected</span> <span style="color: teal;">User</span> _creator;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: teal;">User</span> Creator {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> _creator ?? <span style="color: blue;">new</span> UnknownUser();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">value</span> <span style="color: blue;">is</span> UnknownUser)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _creator = <span style="color: blue;">null</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _creator = <span style="color: blue;">value</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p>I am happy! This looks like a nice OO solution to me. The &#8220;unknown user&#8221; won&#8217;t be persisted, a null reference will be stored instead. <strong>Important:</strong> This works fine for one &#8220;unknown user type&#8221;. However, keep in mind that if we have more than one &#8220;unknown user type&#8221; (see Figure 1) than it might be necessary to persist those users in order to be able to differentiate between them.</p>
<p>Haven&#8217;t used it so far but will in my next projects. Anyone who is interested into more practical details might read <a href="http://stackoverflow.com/questions/271526/how-to-avoid-null-statements-in-java">How to avoid “!= null” statements in Java?</a>.</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/&amp;t=Nhibernate+and+the+Null+object+pattern&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/&amp;title=Nhibernate+and+the+Null+object+pattern&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/&amp;title=Nhibernate+and+the+Null+object+pattern&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Nhibernate+and+the+Null+object+pattern;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create a multi languaged domain model with NHibernate and C#</title>
		<link>http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/</link>
		<comments>http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 16:40:47 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[culture]]></category>
		<category><![CDATA[DAO]]></category>
		<category><![CDATA[globalization]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[localisation]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[mapping]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=290</guid>
		<description><![CDATA[Recently I&#8217;ve been developing an enterprise asp.net application with NHibernate which was required to fully support multi language (localization) on UI side as well as on data side. Whereas the former is easily implemented with .net resources the latter is not that straightforward as it seems. That articles talks about it and the solution I [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been developing an enterprise asp.net application with NHibernate which was required to fully support multi language (localization) on UI side as well as on data side. Whereas the former is easily implemented with .net resources the latter is not that straightforward as it seems. That articles talks about it and the solution I have chosen as I have never done it with NHibernate before. <span id="more-290"></span></p>
<p>Let&#8217;s start with a brief description of the problem before digging through the possible solutions. The applications architecture is based on the <a href="http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx">NHibernate Best Practices with ASP.NET, 1.2nd Ed.</a> by Billy McCafferty. I guess it&#8217;s a must read for all who deal with Nhibernate and asp.net. So please read it before moving on, as it helps you to understand the approach I am talking about.</p>
<p><strong>Problem:</strong></p>
<ol>
<li>Many domain objects have different properties which are in need of translations. E.g. A <code>Product</code> may contain a <code>Name</code> and a  <code>Category</code> property which needs to be translated into the applications supported languages.</li>
<li>It should be easily possible to update the translations of an object before persisting it.</li>
<li>After getting a persistent entity it should be possible to get a given property in the applications current language.</li>
<li>Querying entities according to the translation must not be a big problem. E.g. Getting a list of products ordered by its name in the current selected language.</li>
</ol>
<p><strong>1st Solution:</strong></p>
<p>The first solution I tried was the approach from Ayende (<a href="http://ayende.com/Blog/archive/2006/12/26/LocalizingNHibernateContextualParameters.aspx">Localizing NHibernate: Contextual Parameters</a>) &#8211; whose Blog I strongly recommend for all NHibernaters. The approach he describes is having a simple string property on your domain object and mapping it using a filter with the current selected language (culture).</p>
<p>Pros:<br />
- quickly to implement<br />
- Domain objects property stays unchanged (still a string)</p>
<p>Cons:<br />
- Each property would result in an own table<br />
- &#8220;ugly&#8221; sql queries in your mapping files<br />
- Can&#8217;t easily access all translations<br />
- Querying entities is not straightforward (e.g. fulltext search)</p>
<p><strong>2nd Solution (my preferred):</strong></p>
<p>Due to the cons I could not really use the 1st approach within the project and decided to build an own solution. Here is my suggestions&#8230;<br />
We create a Domain object called <code>PhraseDictionary</code> acts as a generic container for translation phrases. It can be bound to any property of any of our domain objects. I have added its class diagram to show its members:</p>
<p><img src="http://www.webdevbros.net/wp-content/uploads/2009/06/PhraseDictionary.png" alt="PhraseDictionary" title="PhraseDictionary" width="315" height="355" class="aligncenter size-full wp-image-298" /></p>
<p>That type can be used now for each of our domain properties which require translation. The <code>Name</code> property helps us to differentiate all the different phrases we will have in the DB (you can skip it if you want &#8211; though it&#8217;s nice if you browse the data directly). The <code>SetPhrase()</code> method sets a phrase for a given LCID and <code>getPhrase()</code> gets a phrase translation for the requested LCID. Both methods return the instance itself to make <a href="http://martinfowler.com/dslwip/MethodChaining.html">method chaining</a> possible.<br />
For easier usage there is a <code>Phrase</code> property which does the same but always for the current domain culture (Domain culture could be handled in a <code>DomainCulture</code> class &#8211; which gives us the ability to get the current culture and/or switch it). All Phrases are stored in a Dictionary with LCID as key and the actual translation as the value.</p>
<p>As we are going to persist it, thats the resulting mapping file:</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">&lt;?</span><span style="color: maroon;">xml</span><span style="color: blue;"> </span><span style="color: red;">version</span><span style="color: blue;">=</span>"<span style="color: blue;">1.0</span>"<span style="color: blue;"> </span><span style="color: red;">encoding</span><span style="color: blue;">=</span>"<span style="color: blue;">utf-8</span>"<span style="color: blue;"> ?&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: maroon;">hibernate-mapping</span><span style="color: blue;"> </span><span style="color: red;">xmlns</span><span style="color: blue;">=</span>"<span style="color: blue;">urn:nhibernate-mapping-2.2</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;</span><span style="color: maroon;">class</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">MyProject.Core.Domain.PhraseDictionary, MyProject.Core</span>"<span style="color: blue;"> </span><span style="color: red;">table</span><span style="color: blue;">=</span>"<span style="color: blue;">dictionary</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: maroon;">id</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">ID</span>"<span style="color: blue;"> </span><span style="color: red;">unsaved-value</span><span style="color: blue;">=</span>"<span style="color: blue;">0</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: maroon;">generator</span><span style="color: blue;"> </span><span style="color: red;">class</span><span style="color: blue;">=</span>"<span style="color: blue;">identity</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: maroon;">id</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: maroon;">property</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">Name</span>"<span style="color: blue;">&gt;&lt;/</span><span style="color: maroon;">property</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: maroon;">map</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">Phrases</span>"<span style="color: blue;"> </span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; </span><span style="color: red;">access</span><span style="color: blue;">=</span>"<span style="color: blue;">nosetter.camelcase-underscore</span>"<span style="color: blue;"> </span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; </span><span style="color: red;">table</span><span style="color: blue;">=</span>"<span style="color: blue;">phrase</span>"<span style="color: blue;"> </span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; </span><span style="color: red;">cascade</span><span style="color: blue;">=</span>"<span style="color: blue;">all-delete-orphan</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: maroon;">key</span><span style="color: blue;"> </span><span style="color: red;">column</span><span style="color: blue;">=</span>"<span style="color: blue;">dictionary_id</span>"<span style="color: blue;">&gt;&lt;/</span><span style="color: maroon;">key</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: maroon;">index</span><span style="color: blue;"> </span><span style="color: red;">column</span><span style="color: blue;">=</span>"<span style="color: blue;">culture_id</span>"<span style="color: blue;"> </span><span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">Int32</span>"<span style="color: blue;">&gt;&lt;/</span><span style="color: maroon;">index</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: maroon;">element</span><span style="color: blue;"> </span><span style="color: red;">column</span><span style="color: blue;">=</span>"<span style="color: blue;">phrase</span>"<span style="color: blue;"> </span><span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">String</span>"<span style="color: blue;">&gt;&lt;/</span><span style="color: maroon;">element</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: maroon;">map</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;/</span><span style="color: maroon;">class</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&lt;/</span><span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">&gt;</span></pre>
</div>
</div>
<p>The most important part here is the Dictionary (map) which stores the LCID as key and the translation as value. Thoses &#8220;phrases&#8221; are fully cascaded as it makes no sense for them to live without the <code>PhraseDictionary</code>. </p>
<p>Okay lets dig into the actual implementation&#8230;</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">using</span> System;</pre>
<pre style="margin: 0px;"><span style="color: blue;">using</span> System.Collections.Generic;</pre>
<pre style="margin: 0px;"><span style="color: blue;">using</span> System.Text;</pre>
<pre style="margin: 0px;"><span style="color: blue;">using</span> System.Threading;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">namespace</span> MyProject.Core.Domain {</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: teal;">PhraseDictionary</span> : <span style="color: teal;">DomainObject</span>&lt;<span style="color: blue;">int</span>&gt; {</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> required for NHiberante</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> PhraseDictionary()</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; : <span style="color: blue;">base</span>() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Phrases = <span style="color: blue;">new</span> <span style="color: teal;">Dictionary</span>&lt;<span style="color: blue;">int</span>, <span style="color: blue;">string</span>&gt;();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="name"&gt;</span><span style="color: green;">internal name only</span><span style="color: gray;">&lt;/param&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> PhraseDictionary(<span style="color: blue;">string</span> name)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; : <span style="color: blue;">this</span>() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Name = name;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: teal;">IDictionary</span>&lt;<span style="color: blue;">int</span>, <span style="color: blue;">string</span>&gt; _phrases;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> _name;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Name of the dictionary. </span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> - only for internal use. Eases browsing data directly in database</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> Name {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> _name; </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _name = <span style="color: blue;">value</span>; </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Stores all phrases for a lcid</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: teal;">IDictionary</span>&lt;<span style="color: blue;">int</span>, <span style="color: blue;">string</span>&gt; Phrases {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> _phrases; </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">set</span> { </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _phrases = <span style="color: blue;">value</span>; </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Adds a phrase for a given lcid</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> - if the lcid exists then the phrase is updated</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> - empty strings are considered as "not available". </span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;">&nbsp;&nbsp;&nbsp;  Thus adding an empty phrase might remove an existing if already existed</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="lcid"&gt;&lt;/param&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="phrase"&gt;&lt;/param&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;returns&gt;</span><span style="color: green;">Returns itself. Useful for method chaining.</span><span style="color: gray;">&lt;/returns&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: teal;">PhraseDictionary</span> SetPhrase(<span style="color: blue;">string</span> phrase, <span style="color: blue;">int</span> lcid) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (Phrases.ContainsKey(lcid)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">string</span>.IsNullOrEmpty(phrase)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Phrases.Remove(lcid);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <span style="color: blue;">else</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Phrases[lcid] = phrase;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <span style="color: blue;">else</span> <span style="color: blue;">if</span> (!<span style="color: blue;">string</span>.IsNullOrEmpty(phrase)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Phrases.Add(lcid, phrase);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: teal;">PhraseDictionary</span> SetPhrase(<span style="color: blue;">string</span> phrase) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> SetPhrase(phrase, <span style="color: teal;">Thread</span>.CurrentThread.CurrentUICulture.LCID);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Gets a phrase for a given lcid</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> - returns empty string if it does not exist</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="lcid"&gt;&lt;/param&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;returns&gt;&lt;/returns&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> GetPhrase(<span style="color: blue;">int</span> lcid) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (Phrases.ContainsKey(lcid)) <span style="color: blue;">return</span> Phrases[lcid];</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">string</span>.Empty;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> GetPhrase() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> GetPhrase(<span style="color: teal;">Thread</span>.CurrentThread.CurrentUICulture.LCID);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Gets the phrase for the current UI culrute</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> Phrase {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> GetPhrase();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SetPhrase(<span style="color: blue;">value</span>);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">int</span> GetHashCode() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> Phrases.GetHashCode();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">object</span> Copy() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: teal;">PhraseDictionary</span> d = <span style="color: blue;">new</span> <span style="color: teal;">PhraseDictionary</span>();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; d.Name = Name;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; d.Phrases = <span style="color: blue;">new</span> <span style="color: teal;">Dictionary</span>&lt;<span style="color: blue;">int</span>, <span style="color: blue;">string</span>&gt;(Phrases);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> d;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">string</span> ToString() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> Phrase;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
<p>And here is how we would hook it up onto e.g. the <code>Name</code> property of a <code>Product</code> domain object.</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: teal;">IProduct</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: green;">// contains all name translations</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: green;">// AllNames.Phrase gets/sets the translation for the current domain culture</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: teal;">PhraseDictionary</span> AllNames { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
<p>This would allow us to set/receive the translation of name property conveniently as follows:</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: green;">// set the culture to US english</span></pre>
<pre style="margin: 0px;"><span style="color: teal;">Thread</span>.CurrentThread.CurrentUICulture = <span style="color: blue;">new</span> CultureInfo(<span style="color: maroon;">"en-US"</span>);</pre>
<pre style="margin: 0px;">IProduct p = <span style="color: blue;">new</span> Product();</pre>
<pre style="margin: 0px;"><span style="color: green;">// sets product name in english</span></pre>
<pre style="margin: 0px;">p.AllNames.Phrase = <span style="color: maroon;">"Book"</span>;</pre>
<pre style="margin: 0px;"><span style="color: green;">// switch domain culture to german</span></pre>
<pre style="margin: 0px;"><span style="color: teal;">Thread</span>.CurrentThread.CurrentUICulture = <span style="color: blue;">new</span> CultureInfo(<span style="color: maroon;">"de-DE"</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// sets product name in german</span></pre>
<pre style="margin: 0px;">p.AllNames.Phrase = <span style="color: maroon;">"Buch"</span>;</pre>
<pre style="margin: 0px;"><span style="color: green;">// both translations have been stored</span></pre>
<pre style="margin: 0px;">Debug.Assert(p.AllNames == 2);</pre>
<pre style="margin: 0px;"><span style="color: green;">// the current one should be german</span></pre>
<pre style="margin: 0px;">Debug.Assert(p.AllNames.Phrase == <span style="color: maroon;">"Buch"</span>);</pre>
</div>
</div>
<p>To keep everything together. Here is the part you require in your Product mapping file:</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: maroon;">many-to-one</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">AllNames</span>"</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; </span><span style="color: red;">cascade</span><span style="color: blue;">=</span>"<span style="color: blue;">all-delete-orphan</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: maroon;">many-to-one</span><span style="color: blue;">&gt;</span></pre>
</div>
</div>
<p>One requirement was an easy use of the translations within HQL query. Here is an example of how we would fet all products sorted by the name of the current culture:</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: green;">// Gets all products sorted by the name of the current culture</span></pre>
<pre style="margin: 0px;"><span style="color: teal;">IList</span>&lt;Product&gt; GetAllSortedByName() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> query = <span style="color: maroon;">@"</span></pre>
<pre style="margin: 0px;"><span style="color: maroon;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; from Product product</span></pre>
<pre style="margin: 0px;"><span style="color: maroon;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; left outer join product.AllNames allNames </span></pre>
<pre style="margin: 0px;"><span style="color: maroon;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; where index(allNames) = :lcid </span></pre>
<pre style="margin: 0px;"><span style="color: maroon;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; order by allNames"</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; IQuery q = NHibernateSession.CreateQuery(query);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; q.SetParameter(<span style="color: maroon;">"lcid"</span>, <span style="color: teal;">Thread</span>.CurrentThread.CurrentUICulture.LCID);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> q.List&lt;Product&gt;();</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
<p>That was it. Thanks for having the read and I am looking forward to lots of feedback which could improve that approach. </p>
<p><strong>Here are some quick remarks:</strong><br />
- I strongly recommend creating a <code>DomainCulture</code> class which encapsulates the current culture and how it is stored.<br />
- Probably it would be useful to make the <code>PhraseDictionary.Phrases</code> readonly to the public.</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/&amp;t=Create+a+multi+languaged+domain+model+with+NHibernate+and+C%23&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/&amp;title=Create+a+multi+languaged+domain+model+with+NHibernate+and+C%23&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/&amp;title=Create+a+multi+languaged+domain+model+with+NHibernate+and+C%23&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Create+a+multi+languaged+domain+model+with+NHibernate+and+C%23;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

