<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://nhforge.org/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>How to</title><link>http://nhforge.org/wikis/howtonh/default.aspx</link><description>Quick starts, tutorials, code snippets, custom user types, application blocks and more...</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Changing Values in NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/0.aspx</link><pubDate>Fri, 06 Jul 2012 12:34:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:89</guid><dc:creator>David Gardiner</dc:creator><description>Current revision posted to How to by David Gardiner on 06/07/2012 09:34:37 a.m.&lt;br /&gt;
&lt;h2&gt;Changing Values in NHibernate Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;events&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;listener&lt;/span&gt;&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; Although It may work in some cases and
is sometimes referred to as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;&lt;/p&gt;
&lt;p&gt;There is also a bug report about that.&lt;/p&gt;
&lt;h2&gt;Why OnPreUpdate Triggers May Fail&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;There is a reason for that. NHibernate does the &lt;span style="text-decoration: line-through; color: red;"&gt;folowing&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;following&lt;/span&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When flushing, the &lt;a href="https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Event/IFlushEntityEventListener.cs"&gt;FlushEntityEventListener&lt;/a&gt; is called for each entity in the session.&lt;/li&gt;
&lt;li&gt;It determines the dirty properties.&lt;/li&gt;
&lt;li&gt;Based on the dirty properties, it determines if the entity needs updating&lt;/li&gt;
&lt;li&gt;It calls PreUpdate triggers&lt;/li&gt;
&lt;li&gt;Based on the dirty properties it updates the corresponding tables.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last point is the critical one. If your PreUpdate trigger changes properties that are not found dirty before and which are not in the same table as the dirty properties, it won&amp;#39;t be updated in the database. The same happens when dynamic-update is used.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&amp;lt;class name=&amp;quot;Foo&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;id ...&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;property name=&amp;quot;Name&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;join table=&amp;quot;AnotherTable&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;key name=&amp;quot;id&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Value&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;/pre&gt;
&lt;p&gt;Now, when you change Name in the session (it gets dirty) and Value in the trigger, the table AnotherTable won&amp;#39;t be updated.&lt;/p&gt;
&lt;h2&gt;How To Implement It The Correct Way&lt;/h2&gt;
&lt;p&gt;The correct way is to use the &lt;b&gt;FlushEntityEventListener &lt;/b&gt;to implement property changes.The important difference is that you can provide a list of changed properties in this event listener, and therefore tell NHibernate what to store.&lt;/p&gt;
&lt;p&gt;There is an &lt;a href="https://github.com/Buthrakaur/NHListenerTests/blob/master/NHListenerTest/SetModificationTimeFlushEntityEventListener.cs"&gt;example by Buthrakaur&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;History&lt;/h2&gt;
&lt;p&gt;There is a solution to reproduce the code by Scott Findlater&lt;span class="fontsize2 author"&gt;&lt;span style="color:#00681C;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;here&lt;/a&gt;. The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;. This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state. An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;. This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h3&gt;References to the problem&lt;br /&gt;&lt;/h3&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is also &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the resolution of &amp;quot;not an issue&amp;quot; because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;p&gt;There are some discussions and blog posts related to this problem:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/forum/#!topic/nhusers/0kugoXFPeGw/discussion"&gt;audit listener using IFlushEntityEventListener (continuation of NH-2596)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/10d7c2542e3849af"&gt;Bizarre error when using NHibernate, IPreInsertEventListener, Oracle, batching and sequences &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;Clarification on NH-2596 of PreUpdate vs OnSaveOrUpdate semantics &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Changing Values in NHibernate Events</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/11.aspx</link><pubDate>Mon, 08 Aug 2011 15:20:19 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:535</guid><dc:creator>Stefan Steinegger</dc:creator><description>Revision 11 posted to How to by Stefan Steinegger on 08/08/2011 12:20:19 p.m.&lt;br /&gt;
&lt;h2&gt;Changing Values in &lt;span style="text-decoration: line-through; color: red;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; Events&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: events, listener&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; Although It may work in some cases and
is sometimes referred to as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;&lt;/p&gt;
&lt;p&gt;There is also a bug report about that.&lt;/p&gt;
&lt;h2&gt;Why OnPreUpdate Triggers May Fail&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;There is a reason for that. NHibernate does the folowing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When flushing, the FlushEntityEventListener is called for each entity in the session.&lt;/li&gt;
&lt;li&gt;It determines the dirty properties.&lt;/li&gt;
&lt;li&gt;Based on the dirty properties, it determines if the entity needs updating&lt;/li&gt;
&lt;li&gt;It calls PreUpdate triggers&lt;/li&gt;
&lt;li&gt;Based on the dirty properties it updates the corresponding tables.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The last point is the critical one. If your PreUpdate trigger changes properties that are not found dirty before and which are not in the same table as the dirty properties, it won&amp;#39;t be updated in the database. The same happens when dynamic-update is used.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;p&gt;&amp;lt;class name=&amp;quot;Foo&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;id ...&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;property name=&amp;quot;Name&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;join table=&amp;quot;AnotherTable&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;key name=&amp;quot;id&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;property name=&amp;quot;Value&amp;quot;/&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/join&amp;gt;&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;/p&gt;
&lt;p&gt;Now, when you change Name in the session (it gets dirty) and Value in the trigger, the table AnotherTable won&amp;#39;t be updated.&lt;/p&gt;
&lt;h2&gt;How To Implement It The Correct Way&lt;/h2&gt;
&lt;p&gt;The correct way is to use the &lt;b&gt;FlushEntityEventListener &lt;/b&gt;to implement property &lt;span style="text-decoration: line-through; color: red;"&gt;changes&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;changes.The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;important&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;difference&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provide&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;list&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;event&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;listener&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;therefore&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tell&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;what&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;store&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;There is an &lt;a href="https://github.com/Buthrakaur/NHListenerTests/blob/master/NHListenerTest/SetModificationTimeFlushEntityEventListener.cs"&gt;example by Buthrakaur&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;History&lt;/h2&gt;
&lt;p&gt;There is a solution to reproduce the code by Scott Findlater&lt;span class="fontsize2 author"&gt;&lt;span style="color:#00681C;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;here&lt;/a&gt;. The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h3&gt;References to the problem&lt;br /&gt;&lt;/h3&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is also &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the resolution of &amp;quot;not an issue&amp;quot; because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;p&gt;There are some discussions and blog posts related to this problem:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/forum/#!topic/nhusers/0kugoXFPeGw/discussion"&gt;audit listener using IFlushEntityEventListener (continuation of NH-2596)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/10d7c2542e3849af"&gt;Bizarre error when using NHibernate, IPreInsertEventListener, Oracle, batching and sequences &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;Clarification on NH-2596 of PreUpdate vs OnSaveOrUpdate semantics &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Changing Values in OnPreUpdate Events</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/10.aspx</link><pubDate>Mon, 08 Aug 2011 15:17:25 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:506</guid><dc:creator>Stefan Steinegger</dc:creator><description>Revision 10 posted to How to by Stefan Steinegger on 08/08/2011 12:17:25 p.m.&lt;br /&gt;
&lt;h2&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Audit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;trails&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OnPreInsert&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Changing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Values&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; OnPreUpdate &lt;span style="text-decoration: line-through; color: red;"&gt;event&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;listeners&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;vs&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Save&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Update&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;event&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;listeners&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Events&lt;/span&gt;&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Audit&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;log&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; events, listener&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;While&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;does&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;works&lt;/span&gt; &amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Although&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;may&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;work&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cases&lt;/span&gt; and
is &lt;span style="text-decoration: line-through; color: red;"&gt;widely&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;documented&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sometimes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;referred&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;veto&amp;quot;.&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Code&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;veto&amp;quot;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;also&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bug&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;report&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;about&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style="background: SpringGreen;"&gt;Why&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Triggers&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;May&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fail&lt;/span&gt;&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reason&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;does&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;folowing&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;When&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;flushing&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FlushEntityEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;called&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;determines&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Based&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;determines&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;if&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;needs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updating&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;calls&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;triggers&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;Based&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updates&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;corresponding&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tables&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;span style="text-decoration: line-through; color: red;"&gt;full&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt; &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;last&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;point&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;critical&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;If&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;trigger&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;found&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;before&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;same&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;won&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;t&lt;/span&gt; be &lt;span style="text-decoration: line-through; color: red;"&gt;downloaded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updated&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;same&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;happens&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dynamic-update&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Example&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Foo&amp;quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;id&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;..&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Name&amp;quot;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;join&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;AnotherTable&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;key&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;id&amp;quot;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Value&amp;quot;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;&amp;lt;/join&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/class&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;change&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;gets&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;dirty&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Value&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;trigger&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;AnotherTable&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;won&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;t&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updated&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style="background: SpringGreen;"&gt;How&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;To&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;It&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Correct&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Way&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;correct&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;way&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;FlushEntityEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;implement&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;property&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changes&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;a href="https://github.com/Buthrakaur/NHListenerTests/blob/master/NHListenerTest/SetModificationTimeFlushEntityEventListener.cs"&gt;&lt;span style="background: SpringGreen;"&gt;example&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Buthrakaur&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;span style="background: SpringGreen;"&gt;History&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reproduce&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Scott&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Findlater&lt;/span&gt;&lt;span class="fontsize2 author"&gt;&lt;span style="color:#00681C;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;here&lt;/a&gt;. The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;May&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;politely&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ask&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;bear&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;mind&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;aim&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;issue&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;inheritance&lt;/span&gt;&lt;/b&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;they&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;been&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;over&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;simplified&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;auditing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;mind&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;demonstrate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;inheritance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;issues&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;within&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;..&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;commercial&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;design&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;pattern&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;History&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;section&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;history&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;arriving&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;at&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;post&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;Please&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;read&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;spirit&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;sharing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;attempting&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;contribute&lt;/span&gt; &lt;/p&gt;
&lt;h3&gt;&lt;span style="background: SpringGreen;"&gt;References&lt;/span&gt; to &lt;span style="text-decoration: line-through; color: red;"&gt;documentation&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;problem&lt;/span&gt;&lt;br /&gt;&lt;/h3&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;And&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;there&lt;/span&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; is &lt;span style="text-decoration: line-through; color: red;"&gt;no&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;doubt&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;all&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;these&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solutions&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;work&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;using&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;concept&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;changing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;state&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;before&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;pre)&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;database&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;insertion&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;or&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;updating&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;adopted&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OnPreInsert&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;approach&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;auditing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;within&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;inheritance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;architected&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;when&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;found&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;what&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;thought&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;bug&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;subsequently&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ended&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;up&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;also&lt;/span&gt; &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the &lt;span style="text-decoration: line-through; color: red;"&gt;unexpected&lt;/span&gt;
resolution of &amp;quot;not an issue&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;.&amp;nbsp;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;proposed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;use&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Save&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Update&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;event&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;listeners&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;obviously&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;works&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;but&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;unexpected&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reason&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;as&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;why&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;report&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;bug&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt; because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Conclusion&lt;/span&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;note&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Fabio&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;clearly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;states&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;usage&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OnPre&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;events&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;and&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;herewith&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; some &lt;span style="text-decoration: line-through; color: red;"&gt;documentation&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;However&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;from&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;perspective&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;developer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;learning&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;feel&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;this&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;contradicted&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;key&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;NHibernate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;community&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;people&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;am&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;yet&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;discussions&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;blog&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;posts&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;related&lt;/span&gt; to &lt;span style="text-decoration: line-through; color: red;"&gt;truly&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;understand&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;why&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;variety&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;scenarios&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;demonstrated&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; this &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;considered&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;bug&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;Maybe&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;there&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;should&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;some&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;explicit&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;enforcing&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;being&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;able&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;change&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;state&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;these&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;OnPre&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;events&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;A&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;detailed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;example&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;from&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Fabio&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;?&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;There&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;seem&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;alot&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;cries&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;help&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;but&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;with&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;no&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;conclusive&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;working&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;examples&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;problem&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/forum/#!topic/nhusers/0kugoXFPeGw/discussion"&gt;audit listener using IFlushEntityEventListener (continuation of NH-2596)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/10d7c2542e3849af"&gt;Bizarre error when using NHibernate, IPreInsertEventListener, Oracle, batching and sequences &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;Clarification on NH-2596 of PreUpdate vs OnSaveOrUpdate semantics &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;And&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Fabio&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;delivers&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;blog&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;post&lt;/span&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://fabiomaulo.blogspot.com/2011/05/nhibernate-bizarre-audit.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;http://fabiomaulo.blogspot.com/2011/05/nhibernate-bizarre-audit.html&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/9.aspx</link><pubDate>Thu, 12 May 2011 08:47:48 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:505</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 9 posted to How to by Scott Findlater on 12/05/2011 05:47:48 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;History&lt;br /&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;This section is the history of arriving at this
post.&amp;nbsp; Please read this with the spirit
of sharing and attempting to contribute to documentation.&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And there is no doubt all of these solutions work
using the concept of changing the state of the entity before (pre) database
insertion or updating.&lt;/p&gt;
&lt;p&gt;I adopted the OnPreInsert and OnPreUpdate approach
for an auditing solution within an inheritance architected solution when I found,
what I thought, was an NHibernate bug.&amp;nbsp; I
subsequently ended up at &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the unexpected
resolution of &amp;quot;not an issue&amp;quot;.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The solution proposed was to use the Save/ Update
event listeners, which obviously works, but the unexpected reason as to why
this report was not a bug was because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;I note Fabio clearly states the usage for the OnPre*
events and herewith is some documentation.&amp;nbsp;
However, from the perspective of a developer learning NHibernate, I feel
this is contradicted by key NHibernate community people.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I am yet to truly understand why, with the variety
of scenarios demonstrated, this is a not considered a bug.&amp;nbsp; Maybe there should be some explicit enforcing
of not being able to change entity state in these OnPre* events.&lt;/p&gt;
&lt;h3&gt;A detailed example from Fabio?&lt;/h3&gt;
&lt;p&gt;There seem to be alot of cries for help but with no conclusive working examples;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/forum/#!topic/nhusers/0kugoXFPeGw/discussion"&gt;audit listener using IFlushEntityEventListener (continuation of NH-2596)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/10d7c2542e3849af"&gt;Bizarre error when using NHibernate, IPreInsertEventListener, Oracle, batching and sequences &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;Clarification on NH-2596 of PreUpdate vs OnSaveOrUpdate semantics &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;&lt;span style="background: SpringGreen;"&gt;And&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fabio&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;delivers&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;blog&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://fabiomaulo.blogspot.com/2011/05/nhibernate-bizarre-audit.html"&gt;&lt;span style="background: SpringGreen;"&gt;http://fabiomaulo.blogspot.com/2011/05/nhibernate-bizarre-audit.html&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/8.aspx</link><pubDate>Wed, 11 May 2011 12:29:26 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:464</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 8 posted to How to by Scott Findlater on 11/05/2011 09:29:26 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;History&lt;br /&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;This section is the history of arriving at this
post.&amp;nbsp; Please read this with the spirit
of sharing and attempting to contribute to documentation.&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And there is no doubt all of these solutions work
using the concept of changing the state of the entity before (pre) database
insertion or updating.&lt;/p&gt;
&lt;p&gt;I adopted the OnPreInsert and OnPreUpdate approach
for an auditing solution within an inheritance architected solution when I found,
what I thought, was an NHibernate bug.&amp;nbsp; I
subsequently ended up at &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the unexpected
resolution of &amp;quot;not an issue&amp;quot;.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The solution proposed was to use the Save/ Update
event listeners, which obviously works, but the unexpected reason as to why
this report was not a bug was because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;I note Fabio clearly states the usage for the OnPre*
events and herewith is some documentation.&amp;nbsp;
However, from the perspective of a developer learning NHibernate, I feel
this is contradicted by key NHibernate community people.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I am yet to truly understand why, with the variety
of scenarios demonstrated, this is a not considered a bug.&amp;nbsp; Maybe there should be some explicit enforcing
of not being able to change entity state in these OnPre* events.&lt;/p&gt;
&lt;h3&gt;&lt;span style="background: SpringGreen;"&gt;A&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;detailed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;example&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fabio&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;?&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;There&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;seem&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;alot&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cries&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;help&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;no&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;conclusive&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;working&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;examples&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://groups.google.com/forum/#!topic/nhusers/0kugoXFPeGw/discussion"&gt;&lt;span style="background: SpringGreen;"&gt;audit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;listener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IFlushEntityEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;continuation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NH-2596&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/10d7c2542e3849af"&gt;&lt;span style="background: SpringGreen;"&gt;Bizarre&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;error&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IPreInsertEventListener&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Oracle&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;batching&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sequences&lt;/span&gt; &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;&lt;span style="background: SpringGreen;"&gt;Clarification&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NH-2596&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;PreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;vs&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnSaveOrUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;semantics&lt;/span&gt; &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/7.aspx</link><pubDate>Wed, 27 Apr 2011 05:32:27 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:463</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 7 posted to How to by Scott Findlater on 27/04/2011 02:32:27 a.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;h2&gt;Abstract&lt;/h2&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;History&lt;br /&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;This section is the history of arriving at this
post.&amp;nbsp; Please read this with the spirit
of sharing and attempting to contribute to documentation.&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model are well
documented;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;Ayende&amp;#39;s
NHibernate IPreUpdateEventListener &amp;amp; IPreInsertEventListener blog post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;Graham
Bunce&amp;#39;s Creating an Audit Log using NHibernate Events on this WIKI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; And most recently Jason &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;Dentler&amp;#39;s
NHibernate 3.0 cookbook&lt;/a&gt;, chapter 7, Creating and
changing stamping entities recipe. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And there is no doubt all of these solutions work
using the concept of changing the state of the entity before (pre) database
insertion or updating.&lt;/p&gt;
&lt;p&gt;I adopted the OnPreInsert and OnPreUpdate approach
for an auditing solution within an inheritance architected solution when I found,
what I thought, was an NHibernate bug.&amp;nbsp; I
subsequently ended up at &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;this
NHibernate bug report - changes made in IPreInsert/UpdateEventListener are not
persisted into DB on inherited classes&lt;/a&gt; with the unexpected
resolution of &amp;quot;not an issue&amp;quot;.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The solution proposed was to use the Save/ Update
event listeners, which obviously works, but the unexpected reason as to why
this report was not a bug was because &amp;quot;&lt;i&gt;pre-insert
and pre-update listeners are not intended to be used to change the values of
the entity. Instead they should be used to check-values (for that reason they
return &amp;quot;veto&amp;quot;).&amp;quot;&lt;/i&gt; (&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;see
in context&lt;/a&gt;) &lt;/p&gt;
&lt;p&gt;This contradiction of intended usage regarding the
changing of entity state was further discussed on this &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;NHUser
Group thread&lt;/a&gt;, with the same answer.&lt;/p&gt;
&lt;h2&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Conclusion&lt;/span&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;note&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fabio&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;clearly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;states&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;usage&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPre&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;*&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;events&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;herewith&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;documentation&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp;
&lt;span style="background: SpringGreen;"&gt;However&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;perspective&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;developer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;learning&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;feel&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;contradicted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;key&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;community&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;people&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;am&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;yet&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;truly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;understand&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;why&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;variety&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;scenarios&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;demonstrated&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;considered&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bug&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Maybe&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;there&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;explicit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;enforcing&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;being&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;able&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;change&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;these&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPre&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;*&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;events&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/6.aspx</link><pubDate>Tue, 26 Apr 2011 23:42:34 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:459</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 6 posted to How to by Scott Findlater on 26/04/2011 08:42:34 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Project - 03Save&lt;/b&gt;.&amp;nbsp; Uses the same entity inheritance hierarchy
from the &lt;b&gt;02OnPreEventsFailing&lt;/b&gt; project
to demonstrate a working version using the Save/ Insert events.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;History&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;section&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;history&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;arriving&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Please&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;read&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;spirit&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sharing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;attempting&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;contribute&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;documentation&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Audit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;trails&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;event&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;model&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;well&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;documented&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;ul class="unIndentedList"&gt;
&lt;li&gt; &lt;a href="http://ayende.com/Blog/archive/2009/04/29/nhibernate-ipreupdateeventlistener-amp-ipreinserteventlistener.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;Ayende&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IPreUpdateEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;amp;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IPreInsertEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;blog&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;a href="/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx"&gt;&lt;span style="background: SpringGreen;"&gt;Graham&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Bunce&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Creating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Audit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Log&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Events&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;WIKI&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt; &lt;span style="background: SpringGreen;"&gt;And&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;most&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recently&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Jason&lt;/span&gt; &lt;a href="http://www.packtpub.com/nhibernate-3-0-cookbook/book"&gt;&lt;span style="background: SpringGreen;"&gt;Dentler&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;3.0&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cookbook&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;chapter&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;7&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Creating&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;changing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;stamping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entities&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;recipe&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;And&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;there&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;no&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;doubt&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;all&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;these&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solutions&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;work&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;concept&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;before&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;pre)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;insertion&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;or&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;updating&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;adopted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreInsert&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;approach&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;auditing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;within&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inheritance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;architected&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;when&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;found&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;what&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;thought&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bug&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;subsequently&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ended&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;up&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;at&lt;/span&gt; &lt;a href="http://216.121.112.228/browse/NH-2596"&gt;&lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bug&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;report&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changes&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;made&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;IPreInsert/UpdateEventListener&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;persisted&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;into&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;DB&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inherited&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;classes&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unexpected&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;resolution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;.&amp;nbsp;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;proposed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Save&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Update&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;event&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;listeners&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;obviously&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;works&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;unexpected&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reason&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;why&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;report&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bug&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;because&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;i&gt;&lt;span style="background: SpringGreen;"&gt;pre-insert&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;pre-update&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;listeners&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;intended&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;change&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;values&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Instead&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;they&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;check-values&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reason&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;they&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;&amp;quot;veto&amp;quot;).&amp;quot;&lt;/span&gt;&lt;/i&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;a href="http://216.121.112.228/browse/NH-2596?focusedCommentId=20776&amp;amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20776"&gt;&lt;span style="background: SpringGreen;"&gt;see&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;context&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;contradiction&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;intended&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;usage&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;regarding&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;changing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;further&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;discussed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b6807c2a486073d8"&gt;&lt;span style="background: SpringGreen;"&gt;NHUser&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Group&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;thread&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;same&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;answer&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/5.aspx</link><pubDate>Tue, 26 Apr 2011 23:40:44 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:458</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 5 posted to How to by Scott Findlater on 26/04/2011 08:40:44 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 02OnPreEventsFailing&lt;/b&gt;.&amp;nbsp; This project shows how an inheritance entity
hierarchy using the OnPreInsert and OnPreUpdate events to modify entity state
fail to persist the changed entity state.&amp;nbsp;
Here, for whatever design decision, the &lt;b&gt;Entity&lt;/b&gt; base class which originally provide identification and
common auditing has been split into &lt;b&gt;Entity&lt;/b&gt;
and &lt;b&gt;AuditableEntity&lt;/b&gt;.&amp;nbsp; Now the &lt;b&gt;Category&lt;/b&gt;
class inherits from &lt;b&gt;AuditableEntity&lt;/b&gt;.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;03Save&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Uses&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;same&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inheritance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hierarchy&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;02OnPreEventsFailing&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;demonstrate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;working&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;version&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Save&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Insert&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;events&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/4.aspx</link><pubDate>Tue, 26 Apr 2011 23:40:12 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:457</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 4 posted to How to by Scott Findlater on 26/04/2011 08:40:12 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Code&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;The full solution &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;can be downloaded here&lt;/a&gt;.&amp;nbsp; The &lt;b&gt;tests&lt;/b&gt;
folder in each project highlights successes/ issues (by failing tests) in each
project.&amp;nbsp; May I politely ask you to bear
in mind the aim of this code is the issue of &lt;b&gt;inheritance&lt;/b&gt; and they have been over simplified with auditing in
mind to demonstrate inheritance issues within NHibernate ... this is not a
commercial design pattern/ solution.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
Project - 01OnPreEvents&lt;/b&gt;.&amp;nbsp; This project shows a typical, working,
approach to audit trails using the OnPreInsert and OnPreUpdate to modify the
entity state.&amp;nbsp; An &lt;b&gt;Entity&lt;/b&gt; base class which is inherited by a &lt;b&gt;Category&lt;/b&gt; class.&amp;nbsp; The base &lt;b&gt;Entity&lt;/b&gt; provides identification and
common auditing properties.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
&lt;span style="background: SpringGreen;"&gt;Project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;02OnPreEventsFailing&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;shows&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;how&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inheritance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;hierarchy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreInsert&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;events&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;modify&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;fail&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;persist&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;changed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp;
&lt;span style="background: SpringGreen;"&gt;Here&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;whatever&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;design&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;decision&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Entity&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;base&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;originally&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;provide&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;identification&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;common&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;auditing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;has&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;been&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;split&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;into&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Entity&lt;/span&gt;&lt;/b&gt;
&lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;AuditableEntity&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Now&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt;&lt;/b&gt;
&lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inherits&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;AuditableEntity&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/3.aspx</link><pubDate>Tue, 26 Apr 2011 23:39:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:456</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 3 posted to How to by Scott Findlater on 26/04/2011 08:39:37 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Code&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;full&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt; &lt;a href="http://www.scottfindlater.co.uk/blog/nhibernate-audit-trails-and-onpreinsert-onpreupdate-event-listeners-vs-save-update-event-listeners"&gt;&lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;downloaded&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;tests&lt;/span&gt;&lt;/b&gt;
&lt;span style="background: SpringGreen;"&gt;folder&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;highlights&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;successes&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issues&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;failing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;tests&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;May&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;politely&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ask&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;bear&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mind&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;aim&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issue&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;inheritance&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;they&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;been&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;over&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;simplified&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;auditing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;mind&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;demonstrate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inheritance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;issues&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;within&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;..&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;commercial&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;design&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;pattern&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;solution&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;
&lt;span style="background: SpringGreen;"&gt;Project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;01OnPreEvents&lt;/span&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;shows&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;typical&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;working&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;approach&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;audit&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;trails&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;using&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreInsert&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;OnPreUpdate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;modify&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;entity&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;state&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;An&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Entity&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;base&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;inherited&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Category&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;base&lt;/span&gt; &lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Entity&lt;/span&gt;&lt;/b&gt; &lt;span style="background: SpringGreen;"&gt;provides&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;identification&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;common&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;auditing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;properties&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/2.aspx</link><pubDate>Tue, 26 Apr 2011 23:37:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:455</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 2 posted to How to by Scott Findlater on 26/04/2011 08:37:53 p.m.&lt;br /&gt;
&lt;h2&gt;NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Audit, log, events, listener&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>NHibernate, Audit trails and OnPreInsert/ OnPreUpdate event listeners vs Save/ Update event listeners</title><link>http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events/revision/1.aspx</link><pubDate>Tue, 26 Apr 2011 23:37:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:454</guid><dc:creator>Scott Findlater</dc:creator><description>Revision 1 posted to How to by Scott Findlater on 26/04/2011 08:37:53 p.m.&lt;br /&gt;
&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Audit trails using NHibernate&amp;#39;s event model often
use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state
of the entity.&amp;nbsp; While this does works and
is widely documented as a solution, it should be noted the OnPreInsert and
OnPreUpdate events are not intended to be used to change the values of the
entity and instead they should be used to check values and for that reason they
return &amp;quot;veto&amp;quot;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>WCF + NHibernate: Entity Serialization</title><link>http://nhforge.org/wikis/howtonh/wcf-nhibernate-entity-serialization/revision/0.aspx</link><pubDate>Sat, 28 Jan 2012 22:29:15 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:97</guid><dc:creator>awgneo</dc:creator><description>Current revision posted to How to by awgneo on 28/01/2012 07:29:15 p.m.&lt;br /&gt;
&lt;h2&gt;WCF + NHibernate: Entity Serialization&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;loading&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Nhibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FluentNH&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;I found many examples online covering how to serialize NHibernate entities for use over a WCF channel, while still using lazy and eager loading. In my application, my database models and my SOA models needed to be exactly the same; therefore, I wanted to avoid the use of DTOs.&amp;nbsp;However, none of the examples I found quite worked for me.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Examples I found included:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IDataContractSurrogate (&lt;a href="http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx"&gt;http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx&lt;/a&gt;)&amp;nbsp;&lt;/strong&gt;that allows WCF to attempt serialization by walking over the object graph (has a bug that does not allow proper XML generation: &amp;nbsp;&lt;a href="http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null"&gt;http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard Object Graph Walking&lt;/strong&gt; (&lt;a href="http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/"&gt;http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/&lt;/a&gt;) this example did not work for me at all, but it was close to my final solution. Additionally, this example does not support collections.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;My custom object graph walker (I hacked this out of some integrated code so it may need some tweaking):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;using&amp;nbsp;System;
using&amp;nbsp;System.Collections.Generic;
using&amp;nbsp;System.Collections;
using&amp;nbsp;System.Reflection;
using&amp;nbsp;System.Data;
using&amp;nbsp;System.Linq;
using&amp;nbsp;System.Configuration;
using&amp;nbsp;FluentNHibernate.Cfg;
using&amp;nbsp;FluentNHibernate.Cfg.Db;
using&amp;nbsp;NHibernate;
using&amp;nbsp;NHibernate.Cfg;
using&amp;nbsp;NHibernate.Tool.hbm2ddl;
using&amp;nbsp;FluentNHibernate.Automapping;
using&amp;nbsp;Radiant.RQS.Services.Types;
using&amp;nbsp;Radiant.RQS.Services.Core.Conventions;
using&amp;nbsp;Radiant.RQS.Superbank;
using&amp;nbsp;NHibernate.Proxy;
using&amp;nbsp;NHibernate.Collection;
using&amp;nbsp;NHibernate.Metadata;
using&amp;nbsp;NHibernate.Type;
 
namespace&amp;nbsp;Radiant.RQS.Services.Core
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;class&amp;nbsp;Resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;List&amp;lt;T&amp;gt;&amp;nbsp;ResolveList&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt;&amp;nbsp;entityList,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityListIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityListIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityList.Count;&amp;nbsp;entityListIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityList[entityListIndex]&amp;nbsp;=&amp;nbsp;Resolve&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);&lt;/span&gt;
 
&lt;span style="background: SpringGreen;"&gt;&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;session,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;resolvedEntities)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityList;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T[]&amp;nbsp;ResolveArray&amp;lt;T&amp;gt;(T[]&amp;nbsp;entityArray,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityArrayIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityArrayIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityArray.Length;&amp;nbsp;entityArrayIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityArray[entityArrayIndex]&amp;nbsp;=&amp;nbsp;Resolve&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);&lt;/span&gt;
 
&lt;span style="background: SpringGreen;"&gt;&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;session,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;resolvedEntities)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityArray;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;forward&amp;nbsp;to&amp;nbsp;resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;Resolve&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;T&amp;gt;(entity,&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;(),&amp;nbsp;session);&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;T&amp;gt;(entity,&amp;nbsp;session,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;List&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Object&amp;gt;());&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;List&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;CHECKS&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;is&amp;nbsp;null,&amp;nbsp;just&amp;nbsp;skip&amp;nbsp;it
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entity&amp;nbsp;==&amp;nbsp;null)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;we&amp;nbsp;have&amp;nbsp;already&amp;nbsp;resolved&amp;nbsp;it,&amp;nbsp;return&amp;nbsp;that
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(resolvedEntities.Contains(entity))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;RESOLVE&amp;nbsp;ENTITY&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;now&amp;nbsp;lets&amp;nbsp;go&amp;nbsp;ahead&amp;nbsp;and&amp;nbsp;make&amp;nbsp;sure&amp;nbsp;everything&amp;nbsp;is&amp;nbsp;unproxied
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;(T)session.GetSessionImplementation().PersistenceContext.Unproxy(entity);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;add&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;list&amp;nbsp;of&amp;nbsp;resolved&amp;nbsp;entities
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resolvedEntities.Add(resolvedEntity);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;GET&amp;nbsp;TYPE&amp;nbsp;INFO&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IClassMetadata&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;entityType&amp;nbsp;=&amp;nbsp;entity.GetType();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;meta&amp;nbsp;data&amp;nbsp;from&amp;nbsp;the&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;session.SessionFactory.GetClassMetadata(entityType);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;PERFORM&amp;nbsp;PROPERTY&amp;nbsp;DIVE&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;propertyName;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object&amp;nbsp;propertyValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IType&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListInternalType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;properties&amp;nbsp;for&amp;nbsp;this&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PropertyInfo[]&amp;nbsp;propertyInfos&amp;nbsp;=&amp;nbsp;entityType.GetProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;source&amp;nbsp;properties&amp;nbsp;&amp;amp;&amp;nbsp;compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&amp;nbsp;(PropertyInfo&amp;nbsp;propertyInfo&amp;nbsp;in&amp;nbsp;propertyInfos)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyName&amp;nbsp;=&amp;nbsp;propertyInfo.Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;entityMetadata.GetPropertyType(propertyName);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;continue;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyValue&amp;nbsp;=&amp;nbsp;propertyInfo.GetValue(entity,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;these&amp;nbsp;are&amp;nbsp;not&amp;nbsp;the&amp;nbsp;good&amp;nbsp;kind&amp;nbsp;of&amp;nbsp;bags&amp;nbsp;:P
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsCollectionType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;first&amp;nbsp;get&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;#39;s&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListInternalType&amp;nbsp;=&amp;nbsp;propertyInfo.PropertyType.GetGenericArguments()[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;new&amp;nbsp;array&amp;nbsp;type&amp;nbsp;based&amp;nbsp;on&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListType&amp;nbsp;=&amp;nbsp;typeof(List&amp;lt;&amp;gt;).MakeGenericType(propertyListInternalType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;new&amp;nbsp;property&amp;nbsp;list&amp;nbsp;of&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;nbsp;propertyList&amp;nbsp;=&amp;nbsp;(IList)Activator.CreateInstance(propertyListType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;nbsp;in&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;propertyList,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;enumerator&amp;nbsp;for&amp;nbsp;this&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerator&amp;nbsp;enumerator&amp;nbsp;=&amp;nbsp;((IEnumerable)propertyValue).GetEnumerator();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;items&amp;nbsp;to&amp;nbsp;also&amp;nbsp;perform&amp;nbsp;resolution
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(enumerator.MoveNext())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyList.Add(Resolve(enumerator.Current,&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; resolvedEntities));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;destroy&amp;nbsp;hibernate&amp;nbsp;proxies
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsEntityType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;of&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;child&amp;nbsp;beneath&amp;nbsp;us
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;Resolve(propertyValue&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session&lt;/span&gt;,&amp;nbsp;resolvedEntities),&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;return&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;:)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;resolvedEntity;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
&lt;p&gt;This works FLAWLESSLY for me. When I lazy load or eager load sub-entities before calling Resolve() it perfectly serializes only what I want and does not attempt to lazy load the entire DB.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;Example&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;computer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;var&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;computerQuery&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;from&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;c&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;context.Session.Query&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Types.Computer&amp;gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;where&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;c.ID&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;computerID&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;select&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;c&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;computer&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;return&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Resolver.Resolve&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;Types.Computer&amp;gt;(computerQuery.SingleOrDefault(),&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Let me know if this doesn&amp;#39;t work for you :).&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Thanks,&lt;br /&gt;Alex G. &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;awgneo@xbetanet.com)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>WCF + NHibernate: Entity Serialization</title><link>http://nhforge.org/wikis/howtonh/wcf-nhibernate-entity-serialization/revision/4.aspx</link><pubDate>Sat, 28 Jan 2012 22:23:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:532</guid><dc:creator>awgneo</dc:creator><description>Revision 4 posted to How to by awgneo on 28/01/2012 07:23:22 p.m.&lt;br /&gt;
&lt;h2&gt;WCF + NHibernate: Entity Serialization&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: lazy loading, Nhibernate, FluentNH&lt;/div&gt;

&lt;p&gt;I found many examples online covering how to serialize NHibernate entities for use over a WCF channel, while still using lazy and eager loading. In my application, my database models and my SOA models needed to be exactly the same; therefore, I wanted to avoid the use of DTOs.&amp;nbsp;However, none of the examples I found quite worked for me.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Examples I found included:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IDataContractSurrogate (&lt;a href="http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx"&gt;http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx&lt;/a&gt;)&amp;nbsp;&lt;/strong&gt;that allows WCF to attempt serialization by walking over the object graph (has a bug that does not allow proper XML generation: &amp;nbsp;&lt;a href="http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null"&gt;http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard Object Graph Walking&lt;/strong&gt; (&lt;a href="http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/"&gt;http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/&lt;/a&gt;) this example did not work for me at all, but it was close to my final solution. Additionally, this example does not support collections&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;My custom object graph walker (I hacked this out of some integrated code so it may need some tweaking):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;using&amp;nbsp;System;
using&amp;nbsp;System.Collections.Generic;
using&amp;nbsp;System.Collections;
using&amp;nbsp;System.Reflection;
using&amp;nbsp;System.Data;
using&amp;nbsp;System.Linq;
using&amp;nbsp;System.Configuration;
using&amp;nbsp;FluentNHibernate.Cfg;
using&amp;nbsp;FluentNHibernate.Cfg.Db;
using&amp;nbsp;NHibernate;
using&amp;nbsp;NHibernate.Cfg;
using&amp;nbsp;NHibernate.Tool.hbm2ddl;
using&amp;nbsp;FluentNHibernate.Automapping;
using&amp;nbsp;Radiant.RQS.Services.Types;
using&amp;nbsp;Radiant.RQS.Services.Core.Conventions;
using&amp;nbsp;Radiant.RQS.Superbank;
using&amp;nbsp;NHibernate.Proxy;
using&amp;nbsp;NHibernate.Collection;
using&amp;nbsp;NHibernate.Metadata;
using&amp;nbsp;NHibernate.Type;
 
namespace&amp;nbsp;Radiant.RQS.Services.Core
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;class&amp;nbsp;Resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;List&amp;lt;T&amp;gt;&amp;nbsp;ResolveList&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt;&amp;nbsp;entityList,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityListIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityListIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityList.Count;&amp;nbsp;entityListIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityList[entityListIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityList;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T[]&amp;nbsp;ResolveArray&amp;lt;T&amp;gt;(T[]&amp;nbsp;entityArray,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityArrayIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityArrayIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityArray.Length;&amp;nbsp;entityArrayIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityArray[entityArrayIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityArray;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;forward&amp;nbsp;to&amp;nbsp;resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entity,&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;(),&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;CHECKS&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;is&amp;nbsp;null,&amp;nbsp;just&amp;nbsp;skip&amp;nbsp;it
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entity&amp;nbsp;==&amp;nbsp;null)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;we&amp;nbsp;have&amp;nbsp;already&amp;nbsp;resolved&amp;nbsp;it,&amp;nbsp;return&amp;nbsp;that
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(resolvedEntities.Contains(entity))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;RESOLVE&amp;nbsp;ENTITY&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;now&amp;nbsp;lets&amp;nbsp;go&amp;nbsp;ahead&amp;nbsp;and&amp;nbsp;make&amp;nbsp;sure&amp;nbsp;everything&amp;nbsp;is&amp;nbsp;unproxied
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;(T)session.GetSessionImplementation().PersistenceContext.Unproxy(entity);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;add&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;list&amp;nbsp;of&amp;nbsp;resolved&amp;nbsp;entities
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resolvedEntities.Add(resolvedEntity);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;GET&amp;nbsp;TYPE&amp;nbsp;INFO&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IClassMetadata&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;entityType&amp;nbsp;=&amp;nbsp;entity.GetType();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;meta&amp;nbsp;data&amp;nbsp;from&amp;nbsp;the&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;session.SessionFactory.GetClassMetadata(entityType);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;PERFORM&amp;nbsp;PROPERTY&amp;nbsp;DIVE&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;propertyName;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object&amp;nbsp;propertyValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IType&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListInternalType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;properties&amp;nbsp;for&amp;nbsp;this&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PropertyInfo[]&amp;nbsp;propertyInfos&amp;nbsp;=&amp;nbsp;entityType.GetProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;source&amp;nbsp;properties&amp;nbsp;&amp;amp;&amp;nbsp;compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&amp;nbsp;(PropertyInfo&amp;nbsp;propertyInfo&amp;nbsp;in&amp;nbsp;propertyInfos)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyName&amp;nbsp;=&amp;nbsp;propertyInfo.Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;entityMetadata.GetPropertyType(propertyName);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;continue;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyValue&amp;nbsp;=&amp;nbsp;propertyInfo.GetValue(entity,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;these&amp;nbsp;are&amp;nbsp;not&amp;nbsp;the&amp;nbsp;good&amp;nbsp;kind&amp;nbsp;of&amp;nbsp;bags&amp;nbsp;:P
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsCollectionType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;first&amp;nbsp;get&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;#39;s&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListInternalType&amp;nbsp;=&amp;nbsp;propertyInfo.PropertyType.GetGenericArguments()[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;new&amp;nbsp;array&amp;nbsp;type&amp;nbsp;based&amp;nbsp;on&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListType&amp;nbsp;=&amp;nbsp;typeof(List&amp;lt;&amp;gt;).MakeGenericType(propertyListInternalType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;new&amp;nbsp;property&amp;nbsp;list&amp;nbsp;of&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;nbsp;propertyList&amp;nbsp;=&amp;nbsp;(IList)Activator.CreateInstance(propertyListType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;nbsp;in&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;propertyList,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;enumerator&amp;nbsp;for&amp;nbsp;this&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerator&amp;nbsp;enumerator&amp;nbsp;=&amp;nbsp;((IEnumerable)propertyValue).GetEnumerator();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;items&amp;nbsp;to&amp;nbsp;also&amp;nbsp;perform&amp;nbsp;resolution
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(enumerator.MoveNext())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyList.Add(Resolve(enumerator.Current,&amp;nbsp;resolvedEntities));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;destroy&amp;nbsp;hibernate&amp;nbsp;proxies
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsEntityType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;of&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;child&amp;nbsp;beneath&amp;nbsp;us
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;Resolve(propertyValue,&amp;nbsp;resolvedEntities),&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;return&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;:)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;resolvedEntity;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
&lt;pre&gt;This works FLAWLESSLY for me. When I lazy load or eager load sub-entities before calling Resolve() it perfectly serializes only what I want and does not attempt to lazy load the entire DB.&lt;/pre&gt;
&lt;pre&gt;Let me know if this doesn&amp;#39;t work for you :).&lt;/pre&gt;
&lt;pre&gt;Thanks,&lt;/pre&gt;
&lt;pre&gt;Alex G.&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>WCF + NHibernate: Entity Serialization</title><link>http://nhforge.org/wikis/howtonh/wcf-nhibernate-entity-serialization/revision/3.aspx</link><pubDate>Sat, 28 Jan 2012 22:22:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:531</guid><dc:creator>awgneo</dc:creator><description>Revision 3 posted to How to by awgneo on 28/01/2012 07:22:37 p.m.&lt;br /&gt;
&lt;h2&gt;WCF + NHibernate: Entity Serialization&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: lazy loading, Nhibernate, FluentNH&lt;/div&gt;

&lt;p&gt;I found many examples online &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;covering&lt;/span&gt; how to serialize NHibernate entities for use over a WCF channel, while still using lazy and eager loading. In my application, my database models and my SOA models needed to be exactly the same; therefore, I wanted to avoid the use of DTOs.&amp;nbsp;However, none of the examples I found quite worked for me.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Examples I found included:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IDataContractSurrogate (&lt;a href="http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx"&gt;http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx&lt;/a&gt;)&amp;nbsp;&lt;/strong&gt;that allows WCF to attempt serialization by walking over the object graph (has a bug that does not allow proper XML generation: &amp;nbsp;&lt;a href="http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null"&gt;http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard Object Graph Walking&lt;/strong&gt; (&lt;a href="http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/"&gt;http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/&lt;/a&gt;) this example did not work for me at all, but it was close to my final solution. Additionally, this example does not support collections&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;My custom object graph walker (I hacked this out of some integrated code so it may need some tweaking):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;using&amp;nbsp;System;
using&amp;nbsp;System.Collections.Generic;
using&amp;nbsp;System.Collections;
using&amp;nbsp;System.Reflection;
using&amp;nbsp;System.Data;
using&amp;nbsp;System.Linq;
using&amp;nbsp;System.Configuration;
using&amp;nbsp;FluentNHibernate.Cfg;
using&amp;nbsp;FluentNHibernate.Cfg.Db;
using&amp;nbsp;NHibernate;
using&amp;nbsp;NHibernate.Cfg;
using&amp;nbsp;NHibernate.Tool.hbm2ddl;
using&amp;nbsp;FluentNHibernate.Automapping;
using&amp;nbsp;Radiant.RQS.Services.Types;
using&amp;nbsp;Radiant.RQS.Services.Core.Conventions;
using&amp;nbsp;Radiant.RQS.Superbank;
using&amp;nbsp;NHibernate.Proxy;
using&amp;nbsp;NHibernate.Collection;
using&amp;nbsp;NHibernate.Metadata;
using&amp;nbsp;NHibernate.Type;
 
namespace&amp;nbsp;Radiant.RQS.Services.Core
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;class&amp;nbsp;Resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;List&amp;lt;T&amp;gt;&amp;nbsp;ResolveList&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt;&amp;nbsp;entityList,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityListIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityListIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityList.Count;&amp;nbsp;entityListIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityList[entityListIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityList;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T[]&amp;nbsp;ResolveArray&amp;lt;T&amp;gt;(T[]&amp;nbsp;entityArray,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityArrayIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityArrayIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityArray.Length;&amp;nbsp;entityArrayIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityArray[entityArrayIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityArray;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;forward&amp;nbsp;to&amp;nbsp;resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entity,&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;(),&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;CHECKS&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;is&amp;nbsp;null,&amp;nbsp;just&amp;nbsp;skip&amp;nbsp;it
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entity&amp;nbsp;==&amp;nbsp;null)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;we&amp;nbsp;have&amp;nbsp;already&amp;nbsp;resolved&amp;nbsp;it,&amp;nbsp;return&amp;nbsp;that
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(resolvedEntities.Contains(entity))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;RESOLVE&amp;nbsp;ENTITY&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;now&amp;nbsp;lets&amp;nbsp;go&amp;nbsp;ahead&amp;nbsp;and&amp;nbsp;make&amp;nbsp;sure&amp;nbsp;everything&amp;nbsp;is&amp;nbsp;unproxied
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;(T)session.GetSessionImplementation().PersistenceContext.Unproxy(entity);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;add&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;list&amp;nbsp;of&amp;nbsp;resolved&amp;nbsp;entities
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resolvedEntities.Add(resolvedEntity);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;GET&amp;nbsp;TYPE&amp;nbsp;INFO&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IClassMetadata&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;entityType&amp;nbsp;=&amp;nbsp;entity.GetType();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;meta&amp;nbsp;data&amp;nbsp;from&amp;nbsp;the&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;session.SessionFactory.GetClassMetadata(entityType);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;PERFORM&amp;nbsp;PROPERTY&amp;nbsp;DIVE&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;propertyName;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object&amp;nbsp;propertyValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IType&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListInternalType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;properties&amp;nbsp;for&amp;nbsp;this&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PropertyInfo[]&amp;nbsp;propertyInfos&amp;nbsp;=&amp;nbsp;entityType.GetProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;source&amp;nbsp;properties&amp;nbsp;&amp;amp;&amp;nbsp;compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&amp;nbsp;(PropertyInfo&amp;nbsp;propertyInfo&amp;nbsp;in&amp;nbsp;propertyInfos)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyName&amp;nbsp;=&amp;nbsp;propertyInfo.Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;entityMetadata.GetPropertyType(propertyName);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;continue;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyValue&amp;nbsp;=&amp;nbsp;propertyInfo.GetValue(entity,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;these&amp;nbsp;are&amp;nbsp;not&amp;nbsp;the&amp;nbsp;good&amp;nbsp;kind&amp;nbsp;of&amp;nbsp;bags&amp;nbsp;:P
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsCollectionType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;first&amp;nbsp;get&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;#39;s&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListInternalType&amp;nbsp;=&amp;nbsp;propertyInfo.PropertyType.GetGenericArguments()[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;new&amp;nbsp;array&amp;nbsp;type&amp;nbsp;based&amp;nbsp;on&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListType&amp;nbsp;=&amp;nbsp;typeof(List&amp;lt;&amp;gt;).MakeGenericType(propertyListInternalType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;new&amp;nbsp;property&amp;nbsp;list&amp;nbsp;of&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;nbsp;propertyList&amp;nbsp;=&amp;nbsp;(IList)Activator.CreateInstance(propertyListType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;nbsp;in&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;propertyList,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;enumerator&amp;nbsp;for&amp;nbsp;this&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerator&amp;nbsp;enumerator&amp;nbsp;=&amp;nbsp;((IEnumerable)propertyValue).GetEnumerator();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;items&amp;nbsp;to&amp;nbsp;also&amp;nbsp;perform&amp;nbsp;resolution
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(enumerator.MoveNext())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyList.Add(Resolve(enumerator.Current,&amp;nbsp;resolvedEntities));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;destroy&amp;nbsp;hibernate&amp;nbsp;proxies
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsEntityType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;of&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;child&amp;nbsp;beneath&amp;nbsp;us
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;Resolve(propertyValue,&amp;nbsp;resolvedEntities),&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;return&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;:)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;resolvedEntity;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
&lt;pre&gt;This works FLAWLESSLY for me. When I lazy load or eager load sub-entities before calling Resolve() it perfectly serializes only what I want and does not attempt to lazy load the entire DB.&lt;/pre&gt;
&lt;pre&gt;Let me know if this doesn&amp;#39;t work for you :).&lt;/pre&gt;
&lt;pre&gt;Thanks,&lt;/pre&gt;
&lt;pre&gt;Alex G.&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>WCF + NHibernate: Entity Serialization</title><link>http://nhforge.org/wikis/howtonh/wcf-nhibernate-entity-serialization/revision/2.aspx</link><pubDate>Sat, 28 Jan 2012 22:22:19 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:530</guid><dc:creator>awgneo</dc:creator><description>Revision 2 posted to How to by awgneo on 28/01/2012 07:22:19 p.m.&lt;br /&gt;
&lt;h2&gt;WCF + NHibernate: Entity Serialization&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: lazy loading, Nhibernate, FluentNH&lt;/div&gt;

&lt;p&gt;I found many &lt;span style="text-decoration: line-through; color: red;"&gt;example&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;examples&lt;/span&gt; online on how to serialize NHibernate entities for use over a WCF channel, while still using lazy and eager loading. In my application, my database models and my SOA models needed to be exactly the same; therefore, I wanted to avoid the use of DTOs.&amp;nbsp;However, none of the examples I found quite worked for me.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Examples I found included:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;IDataContractSurrogate (&lt;a href="http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx"&gt;http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx&lt;/a&gt;)&amp;nbsp;&lt;/strong&gt;that allows WCF to attempt serialization by walking over the object graph (has a bug that does not allow proper XML generation: &amp;nbsp;&lt;a href="http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null"&gt;http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard Object Graph Walking&lt;/strong&gt; (&lt;a href="http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/"&gt;http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/&lt;/a&gt;) this example did not work for me at all, but it was close to my final solution. Additionally, this example does not support collections&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;My custom object graph walker (I hacked this out of some integrated code so it may need some tweaking):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;using&amp;nbsp;System;
using&amp;nbsp;System.Collections.Generic;
using&amp;nbsp;System.Collections;
using&amp;nbsp;System.Reflection;
using&amp;nbsp;System.Data;
using&amp;nbsp;System.Linq;
using&amp;nbsp;System.Configuration;
using&amp;nbsp;FluentNHibernate.Cfg;
using&amp;nbsp;FluentNHibernate.Cfg.Db;
using&amp;nbsp;NHibernate;
using&amp;nbsp;NHibernate.Cfg;
using&amp;nbsp;NHibernate.Tool.hbm2ddl;
using&amp;nbsp;FluentNHibernate.Automapping;
using&amp;nbsp;Radiant.RQS.Services.Types;
using&amp;nbsp;Radiant.RQS.Services.Core.Conventions;
using&amp;nbsp;Radiant.RQS.Superbank;
using&amp;nbsp;NHibernate.Proxy;
using&amp;nbsp;NHibernate.Collection;
using&amp;nbsp;NHibernate.Metadata;
using&amp;nbsp;NHibernate.Type;
 
namespace&amp;nbsp;Radiant.RQS.Services.Core
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;class&amp;nbsp;Resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;List&amp;lt;T&amp;gt;&amp;nbsp;ResolveList&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt;&amp;nbsp;entityList,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityListIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityListIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityList.Count;&amp;nbsp;entityListIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityList[entityListIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityList;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T[]&amp;nbsp;ResolveArray&amp;lt;T&amp;gt;(T[]&amp;nbsp;entityArray,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityArrayIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityArrayIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityArray.Length;&amp;nbsp;entityArrayIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityArray[entityArrayIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityArray;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;forward&amp;nbsp;to&amp;nbsp;resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entity,&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;(),&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;CHECKS&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;is&amp;nbsp;null,&amp;nbsp;just&amp;nbsp;skip&amp;nbsp;it
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entity&amp;nbsp;==&amp;nbsp;null)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;we&amp;nbsp;have&amp;nbsp;already&amp;nbsp;resolved&amp;nbsp;it,&amp;nbsp;return&amp;nbsp;that
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(resolvedEntities.Contains(entity))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;RESOLVE&amp;nbsp;ENTITY&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;now&amp;nbsp;lets&amp;nbsp;go&amp;nbsp;ahead&amp;nbsp;and&amp;nbsp;make&amp;nbsp;sure&amp;nbsp;everything&amp;nbsp;is&amp;nbsp;unproxied
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;(T)session.GetSessionImplementation().PersistenceContext.Unproxy(entity);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;add&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;list&amp;nbsp;of&amp;nbsp;resolved&amp;nbsp;entities
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resolvedEntities.Add(resolvedEntity);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;GET&amp;nbsp;TYPE&amp;nbsp;INFO&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IClassMetadata&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;entityType&amp;nbsp;=&amp;nbsp;entity.GetType();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;meta&amp;nbsp;data&amp;nbsp;from&amp;nbsp;the&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;session.SessionFactory.GetClassMetadata(entityType);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;PERFORM&amp;nbsp;PROPERTY&amp;nbsp;DIVE&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;propertyName;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object&amp;nbsp;propertyValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IType&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListInternalType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;properties&amp;nbsp;for&amp;nbsp;this&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PropertyInfo[]&amp;nbsp;propertyInfos&amp;nbsp;=&amp;nbsp;entityType.GetProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;source&amp;nbsp;properties&amp;nbsp;&amp;amp;&amp;nbsp;compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&amp;nbsp;(PropertyInfo&amp;nbsp;propertyInfo&amp;nbsp;in&amp;nbsp;propertyInfos)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyName&amp;nbsp;=&amp;nbsp;propertyInfo.Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;entityMetadata.GetPropertyType(propertyName);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;continue;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyValue&amp;nbsp;=&amp;nbsp;propertyInfo.GetValue(entity,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;these&amp;nbsp;are&amp;nbsp;not&amp;nbsp;the&amp;nbsp;good&amp;nbsp;kind&amp;nbsp;of&amp;nbsp;bags&amp;nbsp;:P
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsCollectionType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;first&amp;nbsp;get&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;#39;s&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListInternalType&amp;nbsp;=&amp;nbsp;propertyInfo.PropertyType.GetGenericArguments()[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;new&amp;nbsp;array&amp;nbsp;type&amp;nbsp;based&amp;nbsp;on&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListType&amp;nbsp;=&amp;nbsp;typeof(List&amp;lt;&amp;gt;).MakeGenericType(propertyListInternalType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;new&amp;nbsp;property&amp;nbsp;list&amp;nbsp;of&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;nbsp;propertyList&amp;nbsp;=&amp;nbsp;(IList)Activator.CreateInstance(propertyListType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;nbsp;in&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;propertyList,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;enumerator&amp;nbsp;for&amp;nbsp;this&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerator&amp;nbsp;enumerator&amp;nbsp;=&amp;nbsp;((IEnumerable)propertyValue).GetEnumerator();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;items&amp;nbsp;to&amp;nbsp;also&amp;nbsp;perform&amp;nbsp;resolution
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(enumerator.MoveNext())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyList.Add(Resolve(enumerator.Current,&amp;nbsp;resolvedEntities));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;destroy&amp;nbsp;hibernate&amp;nbsp;proxies
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsEntityType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;of&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;child&amp;nbsp;beneath&amp;nbsp;us
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;Resolve(propertyValue,&amp;nbsp;resolvedEntities),&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;return&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;:)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;resolvedEntity;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
&lt;pre&gt;This works FLAWLESSLY for me. When I lazy load or eager load sub-entities before calling Resolve() it perfectly serializes only what I want and does not attempt to lazy load the entire DB.&lt;/pre&gt;
&lt;pre&gt;Let me know if this doesn&amp;#39;t work for you :).&lt;/pre&gt;
&lt;pre&gt;Thanks,&lt;/pre&gt;
&lt;pre&gt;Alex G.&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>WCF + NHibernate: Entity Serialization</title><link>http://nhforge.org/wikis/howtonh/wcf-nhibernate-entity-serialization/revision/1.aspx</link><pubDate>Sat, 28 Jan 2012 22:21:48 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:529</guid><dc:creator>awgneo</dc:creator><description>Revision 1 posted to How to by awgneo on 28/01/2012 07:21:48 p.m.&lt;br /&gt;
&lt;p&gt;I found many example online on how to serialize NHibernate entities for use over a WCF channel, while still using lazy and eager loading. In my application, my database models and my SOA models needed to be exactly the same; therefore, I wanted to avoid the use of DTOs.&amp;nbsp;However, none of the examples I found quite worked for me.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Examples I found included:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;IDataContractSurrogate (&lt;a href="http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx"&gt;http://www.timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx&lt;/a&gt;)&amp;nbsp;&lt;/b&gt;that allows WCF to attempt serialization by walking over the object graph (has a bug that does not allow proper XML generation: &amp;nbsp;&lt;a href="http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null"&gt;http://connect.microsoft.com/wcf/feedback/details/404241/method-object-getobjecttoserialize-object-obj-type-targettype-of-idatacontractsurrogate-can-not-return-null&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Standard Object Graph Walking&lt;/b&gt; (&lt;a href="http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/"&gt;http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/&lt;/a&gt;) this example did not work for me at all, but it was close to my final solution. Additionally, this example does not support collections&lt;/li&gt;
&lt;/ul&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;My custom object graph walker (I hacked this out of some integrated code so it may need some tweaking):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
&lt;pre&gt;using&amp;nbsp;System;
using&amp;nbsp;System.Collections.Generic;
using&amp;nbsp;System.Collections;
using&amp;nbsp;System.Reflection;
using&amp;nbsp;System.Data;
using&amp;nbsp;System.Linq;
using&amp;nbsp;System.Configuration;
using&amp;nbsp;FluentNHibernate.Cfg;
using&amp;nbsp;FluentNHibernate.Cfg.Db;
using&amp;nbsp;NHibernate;
using&amp;nbsp;NHibernate.Cfg;
using&amp;nbsp;NHibernate.Tool.hbm2ddl;
using&amp;nbsp;FluentNHibernate.Automapping;
using&amp;nbsp;Radiant.RQS.Services.Types;
using&amp;nbsp;Radiant.RQS.Services.Core.Conventions;
using&amp;nbsp;Radiant.RQS.Superbank;
using&amp;nbsp;NHibernate.Proxy;
using&amp;nbsp;NHibernate.Collection;
using&amp;nbsp;NHibernate.Metadata;
using&amp;nbsp;NHibernate.Type;
 
namespace&amp;nbsp;Radiant.RQS.Services.Core
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;class&amp;nbsp;Resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;List&amp;lt;T&amp;gt;&amp;nbsp;ResolveList&amp;lt;T&amp;gt;(List&amp;lt;T&amp;gt;&amp;nbsp;entityList,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityListIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityListIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityList.Count;&amp;nbsp;entityListIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityList[entityListIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityList[entityListIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityList;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T[]&amp;nbsp;ResolveArray&amp;lt;T&amp;gt;(T[]&amp;nbsp;entityArray,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;resolved&amp;nbsp;entities&amp;nbsp;list&amp;nbsp;for&amp;nbsp;peace&amp;nbsp;and&amp;nbsp;sharing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities&amp;nbsp;=&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;elements
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for&amp;nbsp;(int&amp;nbsp;entityArrayIndex&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;entityArrayIndex&amp;nbsp;&amp;lt;&amp;nbsp;entityArray.Length;&amp;nbsp;entityArrayIndex++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;entityArray[entityArrayIndex]&amp;nbsp;=&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entityArray[entityArrayIndex],&amp;nbsp;resolvedEntities,&amp;nbsp;session);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;done
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entityArray;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;forward&amp;nbsp;to&amp;nbsp;resolver
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;Resolve&amp;lt;T&amp;gt;(entity,&amp;nbsp;new&amp;nbsp;List&amp;lt;Object&amp;gt;(),&amp;nbsp;session);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static&amp;nbsp;T&amp;nbsp;Resolve&amp;lt;T&amp;gt;(T&amp;nbsp;entity,&amp;nbsp;List&amp;lt;Object&amp;gt;&amp;nbsp;resolvedEntities,&amp;nbsp;ISession&amp;nbsp;session)&amp;nbsp;where&amp;nbsp;T&amp;nbsp;:&amp;nbsp;class
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;CHECKS&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;is&amp;nbsp;null,&amp;nbsp;just&amp;nbsp;skip&amp;nbsp;it
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entity&amp;nbsp;==&amp;nbsp;null)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;if&amp;nbsp;we&amp;nbsp;have&amp;nbsp;already&amp;nbsp;resolved&amp;nbsp;it,&amp;nbsp;return&amp;nbsp;that
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(resolvedEntities.Contains(entity))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;entity;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;RESOLVE&amp;nbsp;ENTITY&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;default(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;now&amp;nbsp;lets&amp;nbsp;go&amp;nbsp;ahead&amp;nbsp;and&amp;nbsp;make&amp;nbsp;sure&amp;nbsp;everything&amp;nbsp;is&amp;nbsp;unproxied
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;resolvedEntity&amp;nbsp;=&amp;nbsp;(T)session.GetSessionImplementation().PersistenceContext.Unproxy(entity);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;add&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;list&amp;nbsp;of&amp;nbsp;resolved&amp;nbsp;entities
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;resolvedEntities.Add(resolvedEntity);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;GET&amp;nbsp;TYPE&amp;nbsp;INFO&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IClassMetadata&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;entityType&amp;nbsp;=&amp;nbsp;entity.GetType();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;entity&amp;nbsp;meta&amp;nbsp;data&amp;nbsp;from&amp;nbsp;the&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityMetadata&amp;nbsp;=&amp;nbsp;session.SessionFactory.GetClassMetadata(entityType);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;return&amp;nbsp;default(T);&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;PERFORM&amp;nbsp;PROPERTY&amp;nbsp;DIVE&amp;nbsp;//
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String&amp;nbsp;propertyName;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object&amp;nbsp;propertyValue;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IType&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;null;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type&amp;nbsp;propertyListInternalType;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;properties&amp;nbsp;for&amp;nbsp;this&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PropertyInfo[]&amp;nbsp;propertyInfos&amp;nbsp;=&amp;nbsp;entityType.GetProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;source&amp;nbsp;properties&amp;nbsp;&amp;amp;&amp;nbsp;compare
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&amp;nbsp;(PropertyInfo&amp;nbsp;propertyInfo&amp;nbsp;in&amp;nbsp;propertyInfos)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;name
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyName&amp;nbsp;=&amp;nbsp;propertyInfo.Name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try&amp;nbsp;{&amp;nbsp;entityPropertyType&amp;nbsp;=&amp;nbsp;entityMetadata.GetPropertyType(propertyName);&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;catch&amp;nbsp;(Exception&amp;nbsp;ex)&amp;nbsp;{&amp;nbsp;continue;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyValue&amp;nbsp;=&amp;nbsp;propertyInfo.GetValue(entity,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;these&amp;nbsp;are&amp;nbsp;not&amp;nbsp;the&amp;nbsp;good&amp;nbsp;kind&amp;nbsp;of&amp;nbsp;bags&amp;nbsp;:P
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsCollectionType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;first&amp;nbsp;get&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;#39;s&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListInternalType&amp;nbsp;=&amp;nbsp;propertyInfo.PropertyType.GetGenericArguments()[0];
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;new&amp;nbsp;array&amp;nbsp;type&amp;nbsp;based&amp;nbsp;on&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyListType&amp;nbsp;=&amp;nbsp;typeof(List&amp;lt;&amp;gt;).MakeGenericType(propertyListInternalType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;create&amp;nbsp;a&amp;nbsp;new&amp;nbsp;property&amp;nbsp;list&amp;nbsp;of&amp;nbsp;the&amp;nbsp;internal&amp;nbsp;type
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;nbsp;propertyList&amp;nbsp;=&amp;nbsp;(IList)Activator.CreateInstance(propertyListType);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;list&amp;nbsp;in&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;object
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;propertyList,&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;get&amp;nbsp;the&amp;nbsp;enumerator&amp;nbsp;for&amp;nbsp;this&amp;nbsp;property&amp;nbsp;value
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerator&amp;nbsp;enumerator&amp;nbsp;=&amp;nbsp;((IEnumerable)propertyValue).GetEnumerator();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;loop&amp;nbsp;over&amp;nbsp;items&amp;nbsp;to&amp;nbsp;also&amp;nbsp;perform&amp;nbsp;resolution
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while&amp;nbsp;(enumerator.MoveNext())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyList.Add(Resolve(enumerator.Current,&amp;nbsp;resolvedEntities));
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;destroy&amp;nbsp;hibernate&amp;nbsp;proxies
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&amp;nbsp;if&amp;nbsp;(entityPropertyType.IsEntityType)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;set&amp;nbsp;the&amp;nbsp;property&amp;nbsp;of&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;to&amp;nbsp;the&amp;nbsp;child&amp;nbsp;beneath&amp;nbsp;us
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;propertyInfo.SetValue(resolvedEntity,&amp;nbsp;Resolve(propertyValue,&amp;nbsp;resolvedEntities),&amp;nbsp;null);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;return&amp;nbsp;the&amp;nbsp;resolved&amp;nbsp;entity&amp;nbsp;:)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&amp;nbsp;resolvedEntity;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
&lt;pre&gt;This works FLAWLESSLY for me. When I lazy load or eager load sub-entities before calling Resolve() it perfectly serializes only what I want and does not attempt to lazy load the entire DB.&lt;/pre&gt;
&lt;pre&gt;Let me know if this doesn&amp;#39;t work for you :).&lt;/pre&gt;
&lt;pre&gt;Thanks,&lt;/pre&gt;
&lt;pre&gt;Alex G.&lt;/pre&gt;
&lt;/p&gt;</description></item><item><title>Mapping tables</title><link>http://nhforge.org/wikis/howtonh/mapping-tables/revision/0.aspx</link><pubDate>Wed, 23 Nov 2011 15:01:29 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:96</guid><dc:creator>oceantrain</dc:creator><description>Current revision posted to How to by oceantrain on 23/11/2011 12:01:29 p.m.&lt;br /&gt;
&lt;p&gt;I&amp;#39;m new to NHibernate, So I&amp;#39;m not sure if I this is even the place to ask questions. But here goes.&lt;/p&gt;
&lt;p&gt; This is my problem:&lt;/p&gt;
&lt;p&gt;I am building a website using Orchard CMS. &amp;nbsp;All the Orchard tables are mapped by NHibernate. I also have tables in the same database that are NOT mapped by NHibernate. These tables were provided by the client. I need to query these tables and display data on the front end using Orchard. I must keep the table schemas in tact. I have a tutorial on how to map tables using NHibernate, however everything I see in the tutorials, requires the tables to have an ID column as the Primary key. None of the tables provided to me by the client have ID as the first/Primary key column. Is it possible to map to these tables without using the ID method? Or a variation of the ID method?&lt;/p&gt;
&lt;p&gt;Id(x =&amp;gt; x.Id);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Instead can I map to the table using:&lt;/p&gt;
&lt;p&gt;MyColumnName/Primarykey(x =&amp;gt; x.Id);&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;Id(x =&amp;gt; x.MyColumnname/PrimaryKey);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Again, at this point I cannot migrate the data into new tables mapped by NHibernate and also the table schema must remain in tact. As the Client is updating these tables on a nightly bases.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Any help is appreciated.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Oceantrain&lt;/p&gt;
&lt;p&gt;:&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/0.aspx</link><pubDate>Sun, 20 Nov 2011 23:29:17 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:65</guid><dc:creator>Steve Bohlen</dc:creator><description>Current revision posted to How to by Steve Bohlen on 20/11/2011 08:29:17 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;Mapping&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Nhibernate&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;IIdentifierGenerator&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;IPersistentIdentifierGenerator&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;TableGenerator&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;FluentNH&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator.&lt;/code&gt;&amp;nbsp; &amp;nbsp;I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Source&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://packshot360.wordpress.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;packshot&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;360&lt;/span&gt;&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/10.aspx</link><pubDate>Sun, 20 Nov 2011 22:56:44 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:528</guid><dc:creator>marcusix</dc:creator><description>Revision 10 posted to How to by marcusix on 20/11/2011 07:56:44 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&lt;a href="http://www.essaymojo.co.uk/buy-essay.php"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt;&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;a href="http://www.essaymojo.co.uk/write-my-essay.php"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Write&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;My&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;For&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Me&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Cur&lt;/span&gt;&lt;a href="http://ideal-escapes.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;rent&lt;/span&gt;&lt;/a&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;ly&lt;/span&gt; &lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Currently&lt;/span&gt; I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Hill&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;About&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Woodfield&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;a href="http://www.essayinn.co.uk"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;UK&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Rochville&lt;/span&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Adisonhighschool&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Corllins&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Review&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;a href="http://www.essaymojo.co.uk/essay-service.php"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Service&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator.&lt;a href="http://www.mightydesigners.com"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Logo&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Contests&lt;/span&gt;&lt;/code&gt;&amp;nbsp; &amp;nbsp;I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Source&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://packshot360.wordpress.com/"&gt;&lt;span style="background: SpringGreen;"&gt;packshot&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;360&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/9.aspx</link><pubDate>Wed, 12 Oct 2011 10:43:32 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:527</guid><dc:creator>smithwilsonrogard</dc:creator><description>Revision 9 posted to How to by smithwilsonrogard on 12/10/2011 07:43:32 a.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&lt;a href="http://www.essaymojo.co.uk/buy-essay.php"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt;&lt;/a&gt;&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;a href="http://www.essaymojo.co.uk/write-my-essay.php"&gt;&lt;span style="background: SpringGreen;"&gt;Write&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;My&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;For&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Me&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Cur&lt;a href="http://ideal-escapes.com/"&gt;rent&lt;/a&gt;ly I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.  &lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;
Hill University Accreditation&lt;/a&gt; AND &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
About Woodfield University&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;a href="http://www.essayinn.co.uk"&gt;&lt;span style="background: SpringGreen;"&gt;UK&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate. &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;Rochville
 University&lt;/a&gt; AND &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
Adisonhighschool&lt;/a&gt; AND &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;
Corllins University Review&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;a href="http://www.essaymojo.co.uk/essay-service.php"&gt;&lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Service&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&lt;a href="http://www.mightydesigners.com"&gt;&lt;span style="background: SpringGreen;"&gt;Logo&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Contests&lt;/span&gt;&lt;/a&gt;&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/8.aspx</link><pubDate>Wed, 06 Apr 2011 16:58:15 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:520</guid><dc:creator>suedallin</dc:creator><description>Revision 8 posted to How to by suedallin on 06/04/2011 01:58:15 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Currently&lt;/span&gt; &lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Cur&lt;/span&gt;&lt;a href="http://ideal-escapes.com/"&gt;&lt;span style="background: SpringGreen;"&gt;rent&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;ly&lt;/span&gt; I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.  &lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;
Hill University Accreditation&lt;/a&gt; AND &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
About Woodfield University&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate. &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;Rochville
 University&lt;/a&gt; AND &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
Adisonhighschool&lt;/a&gt; AND &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;
Corllins University Review&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/7.aspx</link><pubDate>Wed, 30 Mar 2011 11:46:10 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:389</guid><dc:creator>markweee</dc:creator><description>Revision 7 posted to How to by markweee on 30/03/2011 08:46:10 a.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.  &lt;a href="http://www.reviewonlineuniversity.com/universities/hill-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Hill&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Accreditation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://reviewonlineuniversity.com/universities/woodfield-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;About&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Woodfield&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate. &lt;a href="http://www.reviewonlineuniversity.com/universities/rochieville-university.asp"&gt;&lt;span style="background: SpringGreen;"&gt;Rochville&lt;/span&gt;
 &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/adison-highschool.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Adisonhighschool&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.reviewonlineuniversity.com/universities/corllins-university.asp"&gt;
&lt;span style="background: SpringGreen;"&gt;Corllins&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Review&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/6.aspx</link><pubDate>Sat, 03 Apr 2010 18:14:59 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:383</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 6 posted to How to by Chris Nicola on 03/04/2010 03:14:59 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://www.lucisferre.net"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;posted&lt;/a&gt; about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the DDL to generate the table to store the next id value for my custom id is
only created once and does not check the column name value for each class.&amp;nbsp; For my id generator there is a separate column used for
each table in the database, so this was a problem.&amp;nbsp; For example, the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;, but I was only getting one column (this appears to be a &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;problem with hilo and SchemaExport&lt;/a&gt;, you can currently only have one column per database)&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net/post/2009/08/05/Strings-are-immutable-stupid!.aspx"&gt;last post &lt;/a&gt; and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/5.aspx</link><pubDate>Fri, 25 Dec 2009 00:31:36 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:313</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 5 posted to How to by Chris Nicola on 24/12/2009 09:31:36 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://coding.lucisferre.org"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;posted &lt;/a&gt;
about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the &lt;span style="text-decoration: line-through; color: red;"&gt;code&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;DDL&lt;/span&gt; to generate the table &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;store&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;next&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;id&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt; for &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;my&lt;/span&gt; custom id is
only &lt;span style="text-decoration: line-through; color: red;"&gt;run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;created&lt;/span&gt; once &lt;span style="text-decoration: line-through; color: red;"&gt;per&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;class&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;(&lt;/span&gt;and &lt;span style="text-decoration: line-through; color: red;"&gt;then&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;only&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;on&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;does&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;check&lt;/span&gt; the &lt;span style="text-decoration: line-through; color: red;"&gt;base&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;each&lt;/span&gt; class&lt;span style="text-decoration: line-through; color: red;"&gt;if&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;inheriting)&lt;/span&gt;.&amp;nbsp; For my id generator there is a separate column used for
each table in the database&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;So&lt;/span&gt;, &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;so&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;problem&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;For&lt;/span&gt; example&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/i&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;but&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;was&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;getting&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;this&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;appears&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;a href="http://stackoverflow.com/questions/1944927/schemaexport-vs-hilo-algorithm"&gt;&lt;span style="background: SpringGreen;"&gt;problem&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;hilo&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SchemaExport&lt;/span&gt;&lt;/a&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;currently&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;only&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;per&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;database&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;last post &lt;/a&gt;and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/4.aspx</link><pubDate>Fri, 25 Dec 2009 00:25:51 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:304</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 4 posted to How to by Chris Nicola on 24/12/2009 09:25:51 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://coding.lucisferre.org"&gt;&lt;/a&gt;&lt;a href="http://www.lucisferre.net"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;posted &lt;/a&gt;
about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the code to generate the table for a custom id is
only run once per class (and then only on the base class if you are
inheriting).&amp;nbsp; For my id generator there is a separate column used for
each table in the database.&amp;nbsp; So, for example the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;last post &lt;/a&gt;and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/3.aspx</link><pubDate>Fri, 25 Dec 2009 00:18:06 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:303</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 3 posted to How to by Chris Nicola on 24/12/2009 09:18:06 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.net/post/2009/07/13/Implementing-a-custom-ID-generator-for-nHibernate.aspx"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.net/post/2009/08/15/Generate-Custom-DDL-for-your-Custom-Id-Generator-with-nHibernate.aspx"&gt;here &lt;/a&gt;on my &lt;a href="http://coding.lucisferre.org"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;posted &lt;/a&gt;
about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;As I discovered the code to generate the table for a custom id is
only run once per class (and then only on the base class if you are
inheriting).&amp;nbsp; For my id generator there is a separate column used for
each table in the database.&amp;nbsp; So, for example the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;last post &lt;/a&gt;and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/2.aspx</link><pubDate>Thu, 03 Sep 2009 16:42:06 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:302</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 2 posted to How to by Chris Nicola on 03/09/2009 01:42:06 p.m.&lt;br /&gt;
&lt;h2&gt;Creating a custom id generator for nHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: Mapping, Nhibernate, IIdentifierGenerator, IPersistentIdentifierGenerator, TableGenerator, FluentNH&lt;/div&gt;

&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/15/generate-custom-ddl-for-your-custom-id-generator-with-nhibernate"&gt;here &lt;/a&gt;on my &lt;a href="http://coding.lucisferre.org"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--{12519950541020}--&amp;gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;posted &lt;/a&gt;
about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;!--{12519956184740}--&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;As I discovered the code to generate the table for a custom id is
only run once per class (and then only on the base class if you are
inheriting).&amp;nbsp; For my id generator there is a separate column used for
each table in the database.&amp;nbsp; So, for example the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;last post &lt;/a&gt;and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/revision/1.aspx</link><pubDate>Thu, 03 Sep 2009 16:41:21 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:293</guid><dc:creator>Chris Nicola</dc:creator><description>Revision 1 posted to How to by Chris Nicola on 03/09/2009 01:41:21 p.m.&lt;br /&gt;
&lt;p&gt;I originally blogged about this &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;here &lt;/a&gt;and &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/15/generate-custom-ddl-for-your-custom-id-generator-with-nhibernate"&gt;here &lt;/a&gt;on my &lt;a href="http://coding.lucisferre.org"&gt;blog&lt;/a&gt; but Fabio suggested I add these posts to the Forge, so here I am.&amp;nbsp; I have to say I am a little exited to make my first contribution to the Wiki&amp;nbsp;[H].&amp;nbsp; I will try to add a bit more to them and show some examples that do not use the &lt;code&gt;TableGenerator &lt;/code&gt;class but directly implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;.  Please let me know if there are any mistakes or anything else you want to see.&lt;/p&gt;
&lt;p&gt;Before beginning I recommend that you download the NH source code and take a quick look at the &lt;code&gt;TableGenerator &lt;/code&gt;, &lt;code&gt;IIdentifierGenerator and &lt;/code&gt;&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; classes as you will be working with these classes and interfaces to implement your custom generator.&lt;/p&gt;
&lt;h2&gt;Part 1: Inheriting from TableGenerator class&lt;br /&gt;&lt;/h2&gt;
&lt;p&gt;Currently I am doing some work developing a utility that imports
securities data into a fairly old legacy database. It is still SQL but
the database and software havn&amp;#39;t been updated in about 15 years (maybe
more) and never will be again.&lt;/p&gt;
&lt;p&gt;The original application for this database uses a primitive
hilo-style ID generator mechanism. Tt simply queries an integer value
from a table in the database and increments it, then it adds a fixed
value to it and uses that as the next unique ID. It doesn&amp;#39;t seem very
optimal, but that&amp;#39;s how it works and there&amp;#39;s no changing it.&lt;/p&gt;
&lt;p&gt;It isn&amp;#39;t complex, but it also isn&amp;#39;t one of the identity strategies available in nHibernate and I fully intend to use nHibernate.&lt;/p&gt;
&lt;p&gt;So what to do?  One option would be to set &lt;code&gt;generator=&amp;quot;assigned&amp;quot;&lt;/code&gt; and do it all myself, assigning the ID using &lt;code&gt;ADO.Net &lt;/code&gt;directly
or something similar... but what fun would that be? No, I&amp;#39;ve decided to
take a peak at the nHibernate code and learn to extend it. Read on to
find out how to create a custom class that inherits from &lt;code&gt;IIdentifierGenerator &lt;/code&gt;to implement your own custom ID generator strategy for nHibernate.&lt;/p&gt;
&amp;lt;!--{12519950541020}--&amp;gt;
&lt;p&gt;One of the beautiful things about nHibernate the ability to extend
it. You can extend its functionality without ever touching their source
code.&lt;/p&gt;
&lt;p&gt;It was very easy to locate the classes responsible for ID generation in the &lt;code&gt;NHibernate.ID &lt;/code&gt;namespace.  It was also clear immediately that most classes inherited from &lt;code&gt;IIdentifierGenerator&lt;/code&gt;.&amp;nbsp;  I also knew that my strategy was similar to the procedure that hilo implements so I started with &lt;code&gt;TableHiLoGenerator &lt;/code&gt;which inherts from &lt;code&gt;TableGenerator&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After playing around with this for a while and trying different things, it was clear that &lt;code&gt;TableGenerator&lt;/code&gt;, basically takes a &lt;code&gt;tablename &lt;/code&gt;and &lt;code&gt;columnname &lt;/code&gt;from
the configuration, reads a value, increments it and passes it on as an
ID. Voila! That was almost exactly what I needed to do with little else
to change. I was quickly able to create my own class inheriting from
TableGenerator&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;TableGenerator&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;const&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Int32&lt;/span&gt; SeedValue = 1048576;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ILog&lt;/span&gt; Log = &lt;span style="color:#2b91af;"&gt;LogManager&lt;/span&gt;.GetLogger(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;override&lt;/span&gt; &lt;span style="color:blue;"&gt;object&lt;/span&gt; Generate(&lt;span style="color:#2b91af;"&gt;ISessionImplementor&lt;/span&gt; session, &lt;span style="color:blue;"&gt;object&lt;/span&gt; obj)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;int&lt;/span&gt; counter = &lt;span style="color:#2b91af;"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:blue;"&gt;base&lt;/span&gt;.Generate(session, obj));&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; counter + SeedValue + 1;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Not much to that is there? Now that is a fairly simple case, but
even getting more complex isn&amp;#39;t much harder than that. You can
implement &lt;code&gt;IIdentifierGenerator &lt;/code&gt;directly and create
something far more customized. TableGenerator actually implements
&lt;code&gt;IPersistentIdentifierGenerator&lt;/code&gt; which is an interface for identifiers
which need to write and updated value to the database. However, since
nHibernate provides so many versatile and inheritable base class
generators that you will likely get by with inheriting one of those and
extending it as I did.&lt;/p&gt;
&lt;p&gt;In order to use this custom generator class I setup my configuration to use it.  Using Fluent I wrote a mapping like this:&lt;/p&gt;
&lt;div style="margin:5px;padding:5px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;InvestSpecMap&lt;/span&gt; : &lt;span style="color:#2b91af;"&gt;FDPEntityBaseMap&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;InvestSpec&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;{&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap()&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(tablename + &lt;span style="color:#a31515;"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(FDPGenerator.&lt;span style="color:#2b91af;"&gt;FDPSequence&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//Name of the ID counter table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column for this table&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AnnualDividend).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AnnlzdDiv&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.AssetClass);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CurrencyType);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.CusipNumber).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;CusipNum&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.DividendFreq).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;DivFrequency&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.FullName);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.MaturityDate).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;MaturityDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.PriceAsOf).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;PriceAsOfDt&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.ShortName).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;AbbrName&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TaxTreatment);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.TickerSymbol);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.Type);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Map(x =&amp;gt; x.UnitPrice);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can do the same with XML mappings just declare the assembly and
class as you usually would in XML. This is actually the first time I&amp;#39;ve
used FluentNH.&lt;/p&gt;
&lt;p&gt;What is great about this is that even when testing my mappings with SQLLite, &lt;code&gt;SchemaExport &lt;/code&gt;is
actually able to create the identity table based on the tablename and
columnname properties in the nHibernate configuration. The main benefit
here however is that identity generation remains transparent to the
rest of my application. Also doing something cool with nHibernate,
that&amp;#39;s priceless of course&lt;/p&gt;
&lt;h2&gt;Part 2: Stealing from TableGenerator and controlling DDL generation&lt;/h2&gt;
&lt;p&gt;A while ago I &lt;a href="http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1"&gt;posted &lt;/a&gt;
about writing a custom id generator using nHibernate and extending one
of the existing Generator classes nHibernate provides.&amp;nbsp; This was done
to support working with a legacy database which uses a custom id
generation strategy.&amp;nbsp; One thing I like to do with all my apps is write
some basic CRUD tests to make sure my mappings and queries are all
working properly.&lt;/p&gt;
&lt;p&gt;To do this I use Oren&amp;#39;s approach of &lt;a href="http://ayende.com/Blog/archive/2009/04/28/nhibernate-unit-testing.aspx"&gt;creating a temporary in-memory database &lt;/a&gt;using SQLite to work with.&amp;nbsp; This involves using the&amp;nbsp; &lt;code&gt;SchemaExport &lt;/code&gt;tool
in nHibernate to generate the DDL to create the database.&amp;nbsp; If you are
using a table generator strategy it also needs to create the table and
columns for the custom id generator.&amp;nbsp; In some cases you may need to
customize the DDL for your Id table.&lt;/p&gt;
&lt;p&gt;&amp;lt;!--{12519956184740}--&amp;gt;&lt;/p&gt;
&lt;p&gt;As I discovered the code to generate the table for a custom id is
only run once per class (and then only on the base class if you are
inheriting).&amp;nbsp; For my id generator there is a separate column used for
each table in the database.&amp;nbsp; So, for example the &lt;i&gt;Price &lt;/i&gt;table has a column in the &lt;i&gt;Counter &lt;/i&gt;table called &lt;i&gt;Price &lt;/i&gt;to store it&amp;#39;s counter and the &lt;i&gt;Holding &lt;/i&gt;table has a column called &lt;i&gt;Holding&lt;/i&gt;.&lt;/p&gt;
&lt;p&gt;The TableGenerator class supports two parameters which are set in the HBM files (or using fluent) &lt;i&gt;tablename &lt;/i&gt;and &lt;i&gt;columnname &lt;/i&gt;which
set the table and column names used for the id counter.&amp;nbsp; If you have a
copy of the nHibernate source code you will see that the TableGenerator
class calls&lt;code&gt; SqlCreateStrings()&lt;/code&gt;and &lt;code&gt;SqlDropString()&lt;/code&gt;
to create and drop the table for the id generator.&amp;nbsp; Unfortunately, as I
mentioned above this is only called once, and so it only creates one
table with one column for whichever mapping is handled first.&lt;/p&gt;
&lt;p&gt;In order to fix this I had to copy the &lt;code&gt;TableGenerator &lt;/code&gt;class (yes, yes, I know copy-paste is bad form) and modify those functions.&amp;nbsp; I also added a third parameter &lt;i&gt;allcolumnnames&lt;/i&gt; in which I can provide a comma separated list of all the columns the table needs to have (one per table in my database).&amp;nbsp; The &lt;code&gt;SqlCreateStrings()&lt;/code&gt;is simple changed to:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] SqlCreateStrings(&lt;span style="color:#2b91af;"&gt;Dialect&lt;/span&gt; dialect) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; create = &lt;span style="color:#a31515;"&gt;&amp;quot;create table &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;string&lt;/span&gt; insert = &lt;span style="color:#a31515;"&gt;&amp;quot;insert into &amp;quot;&lt;/span&gt; + tableName + &lt;span style="color:#a31515;"&gt;&amp;quot; values (&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;string&lt;/span&gt; s &lt;span style="color:blue;"&gt;in&lt;/span&gt; allcolumns.Split(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;)) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; create += &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + s + &lt;span style="color:#a31515;"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + dialect.GetTypeName(columnSqlType) + &lt;span style="color:#a31515;"&gt;&amp;quot;,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; insert += &lt;span style="color:#a31515;"&gt;&amp;quot; 1,&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; create = create.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; insert = insert.Trim(&lt;span style="color:#a31515;"&gt;&amp;#39;,&amp;#39;&lt;/span&gt;) + &lt;span style="color:#a31515;"&gt;&amp;quot; )&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &lt;span style="color:blue;"&gt;return&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt;[] {create, insert};&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;(Yes, yes, I know I should have learned from my &lt;a href="http://www.lucisferre.org/coding/index.php/2009/08/04/strings-are-immutable-stupid"&gt;last post &lt;/a&gt;and used a &lt;code&gt;StringBuilder &lt;/code&gt;here as I pointed out in my last post, but seriously, this method will only ever be called once)&lt;/p&gt;
&lt;p&gt;Now when it is called it will create the correct table with all the
columns I need. A couple of example mappings for the database in
FluentNH are shown below:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; InvestSpecMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpecID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt; PriceMap() {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; WithTable(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(&lt;span style="color:#a31515;"&gt;&amp;quot;InvestPriceID&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestSpec, InvestPrice&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;InvestPrice&amp;quot;&lt;/span&gt;); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To be completely &amp;quot;clean code&amp;quot; and avoid repeating myself I should probably factor out the &lt;code&gt;Id() &lt;/code&gt;code into a method in an inhereted base class so I can call it from each mapping class like this:&lt;/p&gt;
&lt;div style="margin:16px;padding:8px;background:white none repeat scroll 0% 0%;font-family:Courier New;font-size:8pt;color:black;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&lt;span style="color:blue;"&gt;protected&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; SetIdGenerator(&lt;span style="color:blue;"&gt;string&lt;/span&gt; tablename) {&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; Id(x =&amp;gt; x.Id).ColumnName(TableName + &lt;span style="color:#a31515;"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .SetGeneratorClass(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;FdpSequenceGen&lt;/span&gt;).AssemblyQualifiedName)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;allcolumns&amp;quot;&lt;/span&gt;, AllColumns)&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;table&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;&amp;quot;zSysCounters&amp;quot;&lt;/span&gt;) &lt;span style="color:green;"&gt;//ID counter table name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;&amp;nbsp; &amp;nbsp; .AddGeneratorParam(&lt;span style="color:#a31515;"&gt;&amp;quot;column&amp;quot;&lt;/span&gt;, TableName); &lt;span style="color:green;"&gt;//ID counter column name&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin:0px;padding:0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Now I can unit test using a temporary in-memory SQLite database and all my tests are passing.&lt;/p&gt;</description></item><item><title>Checking if an Unloaded Collection Contains Elements</title><link>http://nhforge.org/wikis/howtonh/checking-if-an-unloaded-collection-contains-elements/revision/0.aspx</link><pubDate>Fri, 18 Nov 2011 15:35:52 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:95</guid><dc:creator>Ricardo Peres</dc:creator><description>Current revision posted to How to by Ricardo Peres on 18/11/2011 12:35:52 p.m.&lt;br /&gt;
&lt;p&gt;If you want to know if an unloaded collection in an entity contains elements, or count them, without actually loading them, you need to use a custom query; that is because the&amp;nbsp;&lt;strong&gt;Count&amp;nbsp;&lt;/strong&gt;property (if the collection is not mapped with&amp;nbsp;&lt;strong&gt;lazy=&amp;rdquo;extra&amp;rdquo;&lt;/strong&gt;) and the LINQ&amp;nbsp;&lt;strong&gt;Count()&lt;/strong&gt;&amp;nbsp;and&lt;strong&gt;Any()&lt;/strong&gt;&amp;nbsp;methods force the whole collection to be loaded.&lt;/p&gt;
&lt;p&gt;You can use something like these two methods, one for checking if there are any values, the other for actually counting them:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; Boolean Exists(&lt;span&gt;this&lt;/span&gt; ISession session, IEnumerable collection)&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre&gt;     &lt;span&gt;if&lt;/span&gt; (collection &lt;span&gt;is&lt;/span&gt; IPersistentCollection)&lt;/pre&gt;
&lt;pre&gt;     {&lt;/pre&gt;
&lt;pre&gt;         IPersistentCollection col = collection &lt;span&gt;as&lt;/span&gt; IPersistentCollection;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre&gt;         &lt;span&gt;if&lt;/span&gt; (col.WasInitialized == &lt;span&gt;false&lt;/span&gt;)&lt;/pre&gt;
&lt;pre&gt;         {&lt;/pre&gt;
&lt;pre&gt;                 String[] roleParts = col.Role.Split(&lt;span&gt;&amp;#39;.&amp;#39;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;                 String ownerTypeName = String.Join(&lt;span&gt;&amp;quot;.&amp;quot;&lt;/span&gt;, roleParts, 0, roleParts.Length - 1);&lt;/pre&gt;
&lt;pre&gt;                 String ownerCollectionName = roleParts.Last();&lt;/pre&gt;
&lt;pre&gt;                 String hql = &lt;span&gt;&amp;quot;select 1 from &amp;quot;&lt;/span&gt; + ownerTypeName + &lt;span&gt;&amp;quot; it where it.id = :id and exists elements(it.&amp;quot;&lt;/span&gt; + ownerCollectionName + &lt;span&gt;&amp;quot;)&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;                 Boolean exists = session.CreateQuery(hql).SetParameter(&lt;span&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, col.Key).List().Count == 1;&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre&gt;                 &lt;span&gt;return&lt;/span&gt; (exists);&lt;/pre&gt;
&lt;pre&gt;         }&lt;/pre&gt;
&lt;pre&gt;     }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre&gt;     &lt;span&gt;return&lt;/span&gt; ((collection &lt;span&gt;as&lt;/span&gt; IEnumerable).OfType&amp;lt;Object&amp;gt;().Any());&lt;/pre&gt;
&lt;pre&gt; }&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp; &lt;/pre&gt;
&lt;pre&gt;&lt;span&gt;public&lt;/span&gt; &lt;span&gt;static&lt;/span&gt; Int64 Count(&lt;span&gt;this&lt;/span&gt; ISession session, IEnumerable collection)&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre&gt;    &lt;span&gt;if&lt;/span&gt; (collection &lt;span&gt;is&lt;/span&gt; IPersistentCollection)&lt;/pre&gt;
&lt;pre&gt;    {&lt;/pre&gt;
&lt;pre&gt;        IPersistentCollection col = collection &lt;span&gt;as&lt;/span&gt; IPersistentCollection;&lt;/pre&gt;
&lt;pre&gt; &lt;/pre&gt;
&lt;pre&gt;        &lt;span&gt;if&lt;/span&gt; (col.WasInitialized == &lt;span&gt;false&lt;/span&gt;)&lt;/pre&gt;
&lt;pre&gt;        {&lt;/pre&gt;
&lt;pre&gt;            String[] roleParts = col.Role.Split(&lt;span&gt;&amp;#39;.&amp;#39;&lt;/span&gt;);&lt;/pre&gt;
&lt;pre&gt;            String ownerTypeName = String.Join(&lt;span&gt;&amp;quot;.&amp;quot;&lt;/span&gt;, roleParts, 0, roleParts.Length - 1);&lt;/pre&gt;
&lt;pre&gt;            String ownerCollectionName = roleParts.Last();&lt;/pre&gt;
&lt;pre&gt;            String hql = &lt;span&gt;&amp;quot;select count(elements(it.&amp;quot;&lt;/span&gt; + ownerCollectionName + &lt;span&gt;&amp;quot;)) from &amp;quot;&lt;/span&gt; + ownerTypeName + &lt;span&gt;&amp;quot; it where it.id = :id&amp;quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;            Int64 count = session.CreateQuery(hql).SetParameter(&lt;span&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, col.Key).UniqueResult&amp;lt;Int64&amp;gt;();&lt;/pre&gt;
&lt;pre&gt; &lt;/pre&gt;
&lt;pre&gt;            &lt;span&gt;return&lt;/span&gt; (count);&lt;/pre&gt;
&lt;pre&gt;        }&lt;/pre&gt;
&lt;pre&gt;    }&lt;/pre&gt;
&lt;pre&gt; &lt;/pre&gt;
&lt;pre&gt;    &lt;span&gt;return&lt;/span&gt; ((collection &lt;span&gt;as&lt;/span&gt; IEnumerable).OfType&amp;lt;Object&amp;gt;().Count());&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;Here&amp;#39;s how:&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;MyEntity entity = session.Load(100);&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;if (session.Exists(entity.SomeCollection))&lt;/pre&gt;
&lt;pre&gt;{&lt;/pre&gt;
&lt;pre&gt;    Int32 count = session.Count(entity.SomeCollection);&lt;/pre&gt;
&lt;pre&gt;    //...&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/0.aspx</link><pubDate>Sat, 29 Oct 2011 01:07:45 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:73</guid><dc:creator>brendan2452</dc:creator><description>Current revision posted to How to by brendan2452 on 28/10/2011 10:07:45 p.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;IUserType&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Filters&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;l18n&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;localization&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on a post in spanish on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a Fabio &lt;span style="text-decoration: line-through; color: red;"&gt;Maulo&lt;/span&gt;&lt;/a&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;`&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Maulo`s&lt;/span&gt; post called Localized Property with NHibernate, in which he discusses the use of the LocalizablePropertyType found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This&amp;nbsp;second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: Localizing NHibernate: Contextual Parameters by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: Create a multi languaged domain model with NHibernate and C# by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: Mapping translations in NHibernate by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: Localizable entities with Nhibernate part 1 (part 2 and part 3) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough&amp;nbsp; example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;[howtonh:stuffynoseremedy.net/stuffy-nose-remedy/|stuffy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;nose&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;remedy]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;]&lt;/span&gt; mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using NHibernate.Search&amp;nbsp;and Lucene.NET using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in NHUsers discussion on the topic.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/13.aspx</link><pubDate>Fri, 28 Oct 2011 14:13:21 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:523</guid><dc:creator>John Davidson</dc:creator><description>Revision 13 posted to How to by John Davidson on 28/10/2011 11:13:21 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Method 1: Using a custom IUserType&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;a href="http://www.casinomagic.co.uk/%20%20%20"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Best&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Online&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Casino&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Method 2: Using several tables&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This&amp;nbsp;second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.1&lt;/strong&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.2&lt;/strong&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.3&lt;/strong&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.4&lt;/strong&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough&amp;nbsp; example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Conclusions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp;and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/12.aspx</link><pubDate>Fri, 28 Oct 2011 13:56:54 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:522</guid><dc:creator>liliasmit</dc:creator><description>Revision 12 posted to How to by liliasmit on 28/10/2011 10:56:54 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database. &lt;a href="http://www.casinomagic.co.uk/%20%20%20"&gt;&lt;span style="background: SpringGreen;"&gt;Best&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Online&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Casino&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This&amp;nbsp;second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough&amp;nbsp; example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp;and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/11.aspx</link><pubDate>Wed, 06 Jul 2011 13:22:53 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:521</guid><dc:creator>John Davidson</dc:creator><description>Revision 11 posted to How to by John Davidson on 06/07/2011 10:22:53 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Method 1: Using a custom IUserType&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Method 2: Using several tables&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This&lt;a href="http://www.regentmbaprojects.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;MBA&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Project&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;reports&lt;/span&gt;&amp;nbsp;second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.1&lt;/strong&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.2&lt;/strong&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping&lt;a href="http://hfiainsurance.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;auto&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;insurance&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;quotes&lt;/span&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.3&lt;/strong&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.4&lt;/strong&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough&amp;nbsp; example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Conclusions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp;and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/10.aspx</link><pubDate>Wed, 06 Jul 2011 04:59:56 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:486</guid><dc:creator>MBA Project reports</dc:creator><description>Revision 10 posted to How to by MBA Project reports on 06/07/2011 01:59:56 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This &lt;a href="http://www.regentmbaprojects.com/"&gt;&lt;span style="background: SpringGreen;"&gt;MBA&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Project&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reports&lt;/span&gt;&lt;/a&gt; second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough&amp;nbsp; example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp;and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/9.aspx</link><pubDate>Fri, 01 Jul 2011 10:41:47 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:485</guid><dc:creator>John Davidson</dc:creator><description>Revision 9 posted to How to by John Davidson on 01/07/2011 07:41:47 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Method 1: Using a custom IUserType&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Method 2: Using several tables&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.1&lt;/strong&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.2&lt;/strong&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.3&lt;/strong&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.4&lt;/strong&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Conclusions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp; &lt;a href="http://www.cjmdefense.com/dui-lawyer-huntington-beach"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;DUI&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Lawyer&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;huntington&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Beach&lt;/span&gt;&lt;/a&gt;&amp;nbsp;and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/8.aspx</link><pubDate>Fri, 01 Jul 2011 10:41:12 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:470</guid><dc:creator>John Davidson</dc:creator><description>Revision 8 posted to How to by John Davidson on 01/07/2011 07:41:12 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Method 1: Using a custom IUserType&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Mustuniversity&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;|&lt;/span&gt;
&lt;a href="http://www.aboutmustuniversity.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Must&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Must&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Accredited&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;|&lt;/span&gt; &lt;a href="http://www.aboutmustuniversity.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Mustuniversity&lt;/span&gt;&lt;/a&gt;
 &lt;span style="text-decoration: line-through; color: red;"&gt;|&lt;/span&gt; &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Must&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;University&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hhgregg.com/ProductDetail.asp?productId=19569"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;LCD&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;HDTV&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Method 2: Using several tables&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.1&lt;/strong&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.2&lt;/strong&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.3&lt;/strong&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Proposal 2.4&lt;/strong&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Conclusions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp; &lt;a href="http://www.cjmdefense.com/dui-lawyer-huntington-beach"&gt;DUI Lawyer in huntington Beach&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/7.aspx</link><pubDate>Fri, 01 Jul 2011 07:43:11 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:469</guid><dc:creator>Criminal Defense Lawyer in Long Beach</dc:creator><description>Revision 7 posted to How to by Criminal Defense Lawyer in Long Beach on 01/07/2011 04:43:11 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns. &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;Mustuniversity&lt;/a&gt; |
&lt;a href="http://www.aboutmustuniversity.com/"&gt;Must
University&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database. &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;Must University
Accredited&lt;/a&gt; | &lt;a href="http://www.aboutmustuniversity.com/"&gt;Mustuniversity&lt;/a&gt;
 | &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;Must
University&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hhgregg.com/ProductDetail.asp?productId=19569"&gt;LCD HDTV&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt;&amp;nbsp; &lt;a href="http://www.cjmdefense.com/dui-lawyer-huntington-beach"&gt;&lt;span style="background: SpringGreen;"&gt;DUI&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Lawyer&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;huntington&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Beach&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/6.aspx</link><pubDate>Wed, 30 Mar 2011 11:47:46 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:468</guid><dc:creator>markweee</dc:creator><description>Revision 6 posted to How to by markweee on 30/03/2011 08:47:46 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns. &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Mustuniversity&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;|&lt;/span&gt;
&lt;a href="http://www.aboutmustuniversity.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Must&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database. &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Accredited&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;|&lt;/span&gt; &lt;a href="http://www.aboutmustuniversity.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Mustuniversity&lt;/span&gt;&lt;/a&gt;
 &lt;span style="background: SpringGreen;"&gt;|&lt;/span&gt; &lt;a href="http://www.mustuniversityaccreditation.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Must&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;University&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hhgregg.com/ProductDetail.asp?productId=19569"&gt;LCD HDTV&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/5.aspx</link><pubDate>Fri, 24 Sep 2010 17:18:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:384</guid><dc:creator>mikeon098</dc:creator><description>Revision 5 posted to How to by mikeon098 on 24/09/2010 02:18:41 p.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hhgregg.com/ProductDetail.asp?productId=19569"&gt;LCD HDTV&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://hfiainsurance.com/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/4.aspx</link><pubDate>Thu, 26 Aug 2010 12:35:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:348</guid><dc:creator>tcook23</dc:creator><description>Revision 4 posted to How to by tcook23 on 26/08/2010 09:35:37 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.hhgregg.com/ProductDetail.asp?productId=19569"&gt;&lt;span style="background: SpringGreen;"&gt;LCD&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;HDTV&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://zolpo.com/auto-insurance/"&gt;auto insurance quotes&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/3.aspx</link><pubDate>Fri, 28 May 2010 12:41:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:343</guid><dc:creator>mike3050</dc:creator><description>Revision 3 posted to How to by mike3050 on 28/05/2010 09:41:41 a.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary (internally) but we choose to expose it as a String developing the getter (in Fabio&amp;#39;s post) to make it easier for the user.&lt;/p&gt;
&lt;p&gt;Alternatively, you can create your our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method. This, as well, can be exposed as a string through the proper getter.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping &lt;a href="http://zolpo.com/auto-insurance/"&gt;&lt;span style="background: SpringGreen;"&gt;auto&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;insurance&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;quotes&lt;/span&gt;&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/2.aspx</link><pubDate>Tue, 16 Mar 2010 15:27:42 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:317</guid><dc:creator>Marc Climent</dc:creator><description>Revision 2 posted to How to by Marc Climent on 16/03/2010 12:27:42 p.m.&lt;br /&gt;
&lt;h2&gt;Localization techniques&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: IUserType, Filters, l18n, localization, property&lt;/div&gt;

&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;internally)&lt;/span&gt; but we &lt;span style="background: SpringGreen;"&gt;choose&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;expose&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;String&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;developing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;getter&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Fabio&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;post&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;make&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;it&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;easier&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;user&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;Alternatively&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; can create &lt;span style="background: SpringGreen;"&gt;your&lt;/span&gt; our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;well&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;exposed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;as&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;string&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;through&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;proper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;getter&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;li&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;The&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;entity&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;property&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;t&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;be&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;string&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Localization techniques</title><link>http://nhforge.org/wikis/howtonh/localization-techniques/revision/1.aspx</link><pubDate>Tue, 16 Mar 2010 12:18:25 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:312</guid><dc:creator>Marc Climent</dc:creator><description>Revision 1 posted to How to by Marc Climent on 16/03/2010 09:18:25 a.m.&lt;br /&gt;
&lt;p&gt;This page is a summary of the two main different approaches found all around the web on Property Localization using NHibernate, based on &lt;a href="http://codelog.climens.net/2010/03/16/nhibernate-y-localizacion/"&gt;a post in spanish&lt;/a&gt; on my own blog.&lt;/p&gt;
&lt;p&gt;There are roughly two main paths&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Method 1: Using a custom IUserType&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This method is exposed thoroughly in a &lt;a href="/members/fabiomaulo/default.aspx"&gt;Fabio Maulo&lt;/a&gt;`s post called Localized Property with NHibernate, in which he discusses the use of the &lt;a href="http://code.google.com/p/unhaddins/source/browse/trunk/uNhAddIns/uNhAddIns/UserTypes/LocalizablePropertyType.cs"&gt;LocalizablePropertyType&lt;/a&gt; found in uNhAddIns.&lt;/p&gt;
&lt;p&gt;It is based on implementing IUserType, providing methods to serialize and deserialize the property. This way, we make use of NHibernate extensibility to provide a seamless mechanism to store and retrieve the property in a field of the database.&lt;/p&gt;
&lt;p&gt;In the example, the property is a Dictionary but we can create our own class to contain all the translations and ease the addition and removal of them as well as properties to have quick access to the current localization string based on &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt;. In some of the proposals in the second method discussed there are examples of such classes that can be easily adapted to this method.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Simple and easy&lt;/li&gt;
&lt;li&gt;Data stored in the same table as the entity&lt;/li&gt;
&lt;li&gt;Fairly transparent, specially if a custom object is used to simplify the access to the translations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value of the field is not understandable by the DBMS (no indexing, sorting or whatever)&lt;/li&gt;
&lt;li&gt;The entity property can&amp;#39;t be a string&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Method 2: Using several tables&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;This second method has many variants, but all of them use one or more tables to store the translations, making them visible to the DBMS.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.1&lt;/b&gt;: &lt;a href="http://ayende.com/Blog/archive/2006/12/26/localizingnhibernatecontextualparameters.aspx"&gt;Localizing NHibernate: Contextual Parameters&lt;/a&gt; by Ayende&lt;/p&gt;
&lt;p&gt;This is an interesting approach, quite simple in explanation but uses a couple of advanced NHibernate features such as formulas and filters. The advantage is that the property of the entity is a simple string and the selection of the language to load is made through the formula based on a session parameter.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.2&lt;/b&gt;: &lt;a href="http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/"&gt;Create a multi languaged domain model with NHibernate and C#&lt;/a&gt; by Michal @ WebDevBros&lt;/p&gt;
&lt;p&gt;This one uses a nice crafted class to store and retrieve localization information and a couple of tables, one containing the dictionaries that create the relationship between the entity and the translations, that are accessed via a &amp;lt;map&amp;gt; in the mapping. (NOTE: I have not tested it, and he says that you can map several properties in the same entity with this method but not with the provided mapping)&lt;/p&gt;
&lt;p&gt;The class called PhraseDictionary can be easily used through a IUserType with the first method.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.3&lt;/b&gt;: &lt;a href="http://www.siimviikman.com/2010/02/24/mapping-translations-in-nhibernate/"&gt;Mapping translations in NHibernate&lt;/a&gt; by Siim Viikman&lt;/p&gt;
&lt;p&gt;The method proposed here is very similar to the former one, in this case using three tables, but that&amp;#39;s up to you. It explains how to do the mapping using an Id different from the PK (which can&amp;#39;t be done at this moment using Fluent NHibernate, just in case you want to).&lt;/p&gt;
&lt;p&gt;The PhraseDictionary has been replaced by the Translation class, but essentially are the same, holders of the translation dictionary. Can be adapted as well to use it with method 1.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Proposal 2.4&lt;/b&gt;: &lt;a href="http://www.codewrecks.com/blog/index.php?p=41"&gt;Localizable entities with Nhibernate part 1&lt;/a&gt; (&lt;a href="http://www.codewrecks.com/blog/index.php?p=42"&gt;part 2&lt;/a&gt; and &lt;a href="http://www.codewrecks.com/blog/index.php/2007/05/23/localize-entity-with-nhibernate-part-3/"&gt;part 3&lt;/a&gt;) by Alkampfer&lt;/p&gt;
&lt;p&gt;A very thorough example. It maps only one property per entity, simplifying the mappings and the table structure and can be more than enough for many solutions. It discusses a lot about a Registry object to hold the current locale, but you can change that to rely in &lt;i&gt;Thread.CurrentThread.CurrentCulture&lt;/i&gt; and simplify a little bit.&lt;/p&gt;
&lt;p&gt;Pros:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DBMS-aware solution&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;More complex mappings&lt;/li&gt;
&lt;li&gt;More complex data structure&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;Localization is painful in most scenarios and some guidelines can be followed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you don&amp;#39;t really need that a property is localizable, don&amp;#39;t make it localizable. Ok, in most cases is not your decision. If you have choice, think it twice or thrice before making a property localizable.&lt;/li&gt;
&lt;li&gt;If you don&amp;#39;t need to use DBMS capabilities on the field (such as sorting), go for the first method, it&amp;#39;s easy to to the IUserType and the mappings and as well as the serialization is well tested won&amp;#39;t give much trouble&lt;/li&gt;
&lt;li&gt;If you need DBMS capabilities, in most cases only some properties will need this, so instead of embracing blindly the second method, think if a mixed solution is not simpler and better for you. (Was in my case)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a side note, if you need to index the property, think of using &lt;a href="http://darioquintana.com.ar/blogging/2007/12/09/nhibernate-search/"&gt;NHibernate.Search&lt;/a&gt; and &lt;a href="http://lucene.apache.org/lucene.net/"&gt;Lucene.NET&lt;/a&gt; using method 1 and a custom Bridge implementation to store the localizations in the index.&lt;/p&gt;
&lt;p&gt;Finally, thanks to all that participated in &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/b099f9f4a10a08f6"&gt;NHUsers discussion&lt;/a&gt; on the topic.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/0.aspx</link><pubDate>Mon, 19 Sep 2011 21:52:01 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:17</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 19/09/2011 06:52:01 p.m.&lt;br /&gt;
&lt;h2&gt;Configure Log4Net for use with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;logging&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;log4net&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;The&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;Log4Net&lt;/a&gt;&amp;nbsp;assembly is distributed with NHibernate binaries.&lt;/p&gt;
&lt;p&gt;If you are not sure about details of the configuration shown below please refer to &lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt; documentation. 
&lt;/p&gt;
&lt;h4&gt;Running without Log4Net&lt;/h4&gt;
&lt;p&gt;If you only want NHibernate to log the queries it sends to the data source when running unit tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the &lt;strong&gt;show_sql&lt;/strong&gt; key to the NHibernate configuration. If you are using a separate xml file to configure NHibernate (e.g. the hibernate.cfg.xml) then its content might look similar to the one below&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;am&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;too&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;write&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;an&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;academic&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;paper&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;just&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;do&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;not&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;have&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;time&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;for&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;writing&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Thus&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;need&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;go&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;a href="http://www.exclusivepapers.com/essay-writing-service.php"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;writing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;service&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;custom&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;papers&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;which&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;are&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;composed&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;best&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;writing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;experts&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;I&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;understand&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;that&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;lot&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;people&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;cannot&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;become&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;successful&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;without&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;writing&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;assistance&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4d238d7a-da04-4b6f-858d-fc1bfa497df4" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now when running any unit test that involves NHibernate the queries generated by NHibernate will be logged in the output window (be it the unit test runner of &lt;strong&gt;Resharper&lt;/strong&gt; or be it the Output Window of Visual Studio when you use&lt;strong&gt;TestDriven&lt;/strong&gt;). 
&lt;/p&gt;
&lt;h4&gt;Logging with Log4Net&lt;/h4&gt;
&lt;p&gt;If you want to use Log4Net to collect logging information generated by NHibernate you have to add the necessary configuration to the config file of your application. If you want to log to two different targets (e.g. to the console and to a file) the relevant sections in your config file might look like follows&lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Thesis&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt;&lt;/a&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Dissertation&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4652318c-495c-4713-8a6f-73a04cf8934a" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Others sections &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;      type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Some others configurations &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; This section contains the log4net configuration settings &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;debug&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Define some output appenders &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;trace&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.TraceAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.ConsoleAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;rollingFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;File&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log.txt&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;AppendToFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;RollingStyle&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DatePattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;yyyy.MM.dd&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;StaticLogFileName&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d [%t] %-5p %c - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Setup the root category, add the appenders and set the default priority &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;priority &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender-ref &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;ref&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here I define that all output with a priority of at least DEBUG goes to the console. 
&lt;/p&gt;
&lt;p&gt;Now I have to tell my application that I want to use Log4Net. I can do that with an assembly level attribute. That is, you have to put the following code snippet somewhere in your application (e.g. the &amp;quot;&lt;strong&gt;Global. asax&amp;quot;&lt;/strong&gt; if you are building a web application) 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#008000;"&gt;// Configure log4net using the .config file&lt;/span&gt;&lt;br /&gt;[assembly: log4net.Config.XmlConfigurator(Watch = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;having done do we can now run a first unit test that accesses the database via NHibernate. The output generated by Log4Net in the unit test runner is similar to this 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:40:07,249 INFO NHibernate 2.0.0.4000 (2.0.0.4000)&lt;br /&gt;2008-07-02 08:40:07,251 INFO hibernate-configuration section not found in application configuration file&lt;br /&gt;2008-07-02 08:40:07,254 INFO Bytecode provider name : lcg&lt;br /&gt;2008-07-02 08:40:07,257 INFO Using reflection optimizer&lt;br /&gt;2008-07-02 08:40:08,515 DEBUG connection.provider=NHibernate.Connection.DriverConnectionProvider&lt;br /&gt;2008-07-02 08:40:08,516 DEBUG dialect=NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.driver_class=NHibernate.Driver.SqlClientDriver&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;2008-07-02 08:40:08,518 DEBUG show_sql=false&lt;br /&gt;2008-07-02 08:40:08,522 DEBUG properties: System.Collections.Generic.Dictionary`2[System.String,System.String]&lt;br /&gt;2008-07-02 08:40:08,526 INFO Mapping resource: LoggingSample.Person.hbm.xml&lt;br /&gt;2008-07-02 08:40:08,872 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person&lt;br /&gt;2008-07-02 08:40:11,537 DEBUG Mapped property: Id -&amp;gt; Id, type: Int32&lt;br /&gt;2008-07-02 08:40:11,612 DEBUG Mapped property: LastName -&amp;gt; LastName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: FirstName -&amp;gt; FirstName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: Birthdate -&amp;gt; Birthdate, type: DateTime&lt;br /&gt;2008-07-02 08:40:11,632 INFO checking mappings queue&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-many association mappings&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-one association property references&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing foreign key constraints&lt;br /&gt;2008-07-02 08:40:11,712 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,716 INFO Using dialect defined converter&lt;br /&gt;...&lt;br /&gt;2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person&lt;br /&gt;2008-07-02 08:40:12,411 DEBUG  Version select: SELECT Id FROM Person WHERE Id = ?&lt;br /&gt;2008-07-02 08:40:12,412 DEBUG  Snapshot select: SELECT person_.Id, person_.LastName as LastName0_, person_.FirstName as FirstName0_, person_.Birthdate as Birthdate0_ FROM Person person_ WHERE person_.Id=?&lt;br /&gt;...&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;as you can see LOADS of information. 
&lt;/p&gt;
&lt;p&gt;To tune the generated logging information a little bit we can filter the output generated by NHibernate by putting the following configuration section into our config file (put them just after the &lt;strong&gt;root&lt;/strong&gt; section inside the &lt;strong&gt;log4net&lt;/strong&gt; node)&lt;/p&gt;
&lt;div style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:54ffa529-54b3-414a-8144-22105142bbba" class="wlWriterEditableSmartContent"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;WARN&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.SQL&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously NHibernate defines two different loggers &lt;strong&gt;NHibernate&lt;/strong&gt; and &lt;strong&gt;NHibernate.SQL&lt;/strong&gt;. The first one receives all logging output that NHibernate generates where as the second one only receives the sql statements generated by NHibernate. 
&lt;/p&gt;
&lt;p&gt;With the above settings in place the output generated is reduced to 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, &lt;br /&gt;                        person0_.LastName as LastName0_, &lt;br /&gt;                        person0_.FirstName as FirstName0_, &lt;br /&gt;                        person0_.Birthdate as Birthdate0_ &lt;br /&gt;                        from Person person0_&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s what we want. Only if we have some weird problems we need to change the priority level of the &lt;strong&gt;NHibernate&lt;/strong&gt; filter to say INFO or DEBUG.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/5.aspx</link><pubDate>Mon, 19 Sep 2011 11:10:23 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:519</guid><dc:creator>barbarasmith22</dc:creator><description>Revision 5 posted to How to by barbarasmith22 on 19/09/2011 08:10:23 a.m.&lt;br /&gt;
&lt;h2&gt;Configure Log4Net for use with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: logging, log4net&lt;/div&gt;

&lt;p&gt;The&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;Log4Net&lt;/a&gt;&amp;nbsp;assembly is distributed with NHibernate binaries.&lt;/p&gt;
&lt;p&gt;If you are not sure about details of the configuration shown below please refer to &lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt; documentation. 
&lt;/p&gt;
&lt;h4&gt;Running without Log4Net&lt;/h4&gt;
&lt;p&gt;If you only want NHibernate to log the queries it sends to the data source when running unit tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the &lt;b&gt;show_sql&lt;/b&gt; key to the NHibernate configuration. If you are using a separate xml file to configure NHibernate (e.g. the hibernate.cfg.xml) then its content might look similar to the one below &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Term&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Research&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Writing&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;am&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;too&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;write&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;an&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;academic&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;paper&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;just&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;do&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;not&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;have&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;time&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;writing&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Thus&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;need&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;go&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;a href="http://www.exclusivepapers.com/essay-writing-service.php"&gt;&lt;span style="background: SpringGreen;"&gt;essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;writing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;service&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;get&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;custom&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;papers&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;which&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;composed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;by&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;best&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;writing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;experts&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;I&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;understand&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;that&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lot&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;people&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cannot&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;become&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;successful&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;without&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;writing&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assistance&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4d238d7a-da04-4b6f-858d-fc1bfa497df4" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now when running any unit test that involves NHibernate the queries generated by NHibernate will be logged in the output window (be it the unit test runner of &lt;b&gt;Resharper&lt;/b&gt; or be it the Output Window of Visual Studio when you use&lt;b&gt;TestDriven&lt;/b&gt;). 
&lt;/p&gt;
&lt;h4&gt;Logging with Log4Net&lt;/h4&gt;
&lt;p&gt;If you want to use Log4Net to collect logging information generated by NHibernate you have to add the necessary configuration to the config file of your application. If you want to log to two different targets (e.g. to the console and to a file) the relevant sections in your config file might look like follows &lt;a href="http://www.thesisdom.com/"&gt;Thesis Writing&lt;/a&gt;
AND &lt;a href="http://www.dissertationdom.com/"&gt;Dissertation
Writing&lt;/a&gt; AND &lt;a href="http://www.essaydom.com/"&gt;Essay
Writing&lt;/a&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4652318c-495c-4713-8a6f-73a04cf8934a" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Others sections &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;      type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Some others configurations &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; This section contains the log4net configuration settings &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;debug&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Define some output appenders &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;trace&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.TraceAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.ConsoleAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;rollingFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;File&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log.txt&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;AppendToFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;RollingStyle&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DatePattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;yyyy.MM.dd&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;StaticLogFileName&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d [%t] %-5p %c - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Setup the root category, add the appenders and set the default priority &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;priority &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender-ref &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;ref&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here I define that all output with a priority of at least DEBUG goes to the console. 
&lt;/p&gt;
&lt;p&gt;Now I have to tell my application that I want to use Log4Net. I can do that with an assembly level attribute. That is, you have to put the following code snippet somewhere in your application (e.g. the &amp;quot;&lt;b&gt;Global. asax&amp;quot;&lt;/b&gt; if you are building a web application) 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#008000;"&gt;// Configure log4net using the .config file&lt;/span&gt;&lt;br /&gt;[assembly: log4net.Config.XmlConfigurator(Watch = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;having done do we can now run a first unit test that accesses the database via NHibernate. The output generated by Log4Net in the unit test runner is similar to this 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:40:07,249 INFO NHibernate 2.0.0.4000 (2.0.0.4000)&lt;br /&gt;2008-07-02 08:40:07,251 INFO hibernate-configuration section not found in application configuration file&lt;br /&gt;2008-07-02 08:40:07,254 INFO Bytecode provider name : lcg&lt;br /&gt;2008-07-02 08:40:07,257 INFO Using reflection optimizer&lt;br /&gt;2008-07-02 08:40:08,515 DEBUG connection.provider=NHibernate.Connection.DriverConnectionProvider&lt;br /&gt;2008-07-02 08:40:08,516 DEBUG dialect=NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.driver_class=NHibernate.Driver.SqlClientDriver&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;2008-07-02 08:40:08,518 DEBUG show_sql=false&lt;br /&gt;2008-07-02 08:40:08,522 DEBUG properties: System.Collections.Generic.Dictionary`2[System.String,System.String]&lt;br /&gt;2008-07-02 08:40:08,526 INFO Mapping resource: LoggingSample.Person.hbm.xml&lt;br /&gt;2008-07-02 08:40:08,872 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person&lt;br /&gt;2008-07-02 08:40:11,537 DEBUG Mapped property: Id -&amp;gt; Id, type: Int32&lt;br /&gt;2008-07-02 08:40:11,612 DEBUG Mapped property: LastName -&amp;gt; LastName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: FirstName -&amp;gt; FirstName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: Birthdate -&amp;gt; Birthdate, type: DateTime&lt;br /&gt;2008-07-02 08:40:11,632 INFO checking mappings queue&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-many association mappings&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-one association property references&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing foreign key constraints&lt;br /&gt;2008-07-02 08:40:11,712 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,716 INFO Using dialect defined converter&lt;br /&gt;...&lt;br /&gt;2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person&lt;br /&gt;2008-07-02 08:40:12,411 DEBUG  Version select: SELECT Id FROM Person WHERE Id = ?&lt;br /&gt;2008-07-02 08:40:12,412 DEBUG  Snapshot select: SELECT person_.Id, person_.LastName as LastName0_, person_.FirstName as FirstName0_, person_.Birthdate as Birthdate0_ FROM Person person_ WHERE person_.Id=?&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;as you can see LOADS of information. 
&lt;/p&gt;
&lt;p&gt;To tune the generated logging information a little bit we can filter the output generated by NHibernate by putting the following configuration section into our config file (put them just after the &lt;b&gt;root&lt;/b&gt; section inside the &lt;b&gt;log4net&lt;/b&gt; node)&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:54ffa529-54b3-414a-8144-22105142bbba" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;WARN&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.SQL&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously NHibernate defines two different loggers &lt;b&gt;NHibernate&lt;/b&gt; and &lt;b&gt;NHibernate.SQL&lt;/b&gt;. The first one receives all logging output that NHibernate generates where as the second one only receives the sql statements generated by NHibernate. 
&lt;/p&gt;
&lt;p&gt;With the above settings in place the output generated is reduced to 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, &lt;br /&gt;                        person0_.LastName as LastName0_, &lt;br /&gt;                        person0_.FirstName as FirstName0_, &lt;br /&gt;                        person0_.Birthdate as Birthdate0_ &lt;br /&gt;                        from Person person0_&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s what we want. Only if we have some weird problems we need to change the priority level of the &lt;b&gt;NHibernate&lt;/b&gt; filter to say INFO or DEBUG.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/4.aspx</link><pubDate>Wed, 30 Mar 2011 11:21:17 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:518</guid><dc:creator>markweee</dc:creator><description>Revision 4 posted to How to by markweee on 30/03/2011 08:21:17 a.m.&lt;br /&gt;
&lt;h2&gt;Configure Log4Net for use with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: logging, log4net&lt;/div&gt;

&lt;p&gt;The&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;Log4Net&lt;/a&gt;&amp;nbsp;assembly is distributed with NHibernate binaries.&lt;/p&gt;
&lt;p&gt;If you are not sure about details of the configuration shown below please refer to &lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt; documentation. 
&lt;/p&gt;
&lt;h4&gt;Running without Log4Net&lt;/h4&gt;
&lt;p&gt;If you only want NHibernate to log the queries it sends to the data source when running unit tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the &lt;b&gt;show_sql&lt;/b&gt; key to the NHibernate configuration. If you are using a separate xml file to configure NHibernate (e.g. the hibernate.cfg.xml) then its content might look similar to the one below &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Term&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Research&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4d238d7a-da04-4b6f-858d-fc1bfa497df4" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now when running any unit test that involves NHibernate the queries generated by NHibernate will be logged in the output window (be it the unit test runner of &lt;b&gt;Resharper&lt;/b&gt; or be it the Output Window of Visual Studio when you use&lt;b&gt;TestDriven&lt;/b&gt;). 
&lt;/p&gt;
&lt;h4&gt;Logging with Log4Net&lt;/h4&gt;
&lt;p&gt;If you want to use Log4Net to collect logging information generated by NHibernate you have to add the necessary configuration to the config file of your application. If you want to log to two different targets (e.g. to the console and to a file) the relevant sections in your config file might look like follows &lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Thesis&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt;&lt;/a&gt;
&lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Dissertation&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Writing&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4652318c-495c-4713-8a6f-73a04cf8934a" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Others sections &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;      type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Some others configurations &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; This section contains the log4net configuration settings &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;debug&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Define some output appenders &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;trace&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.TraceAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.ConsoleAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;rollingFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;File&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log.txt&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;AppendToFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;RollingStyle&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DatePattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;yyyy.MM.dd&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;StaticLogFileName&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d [%t] %-5p %c - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Setup the root category, add the appenders and set the default priority &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;priority &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender-ref &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;ref&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here I define that all output with a priority of at least DEBUG goes to the console. 
&lt;/p&gt;
&lt;p&gt;Now I have to tell my application that I want to use Log4Net. I can do that with an assembly level attribute. That is, you have to put the following code snippet somewhere in your application (e.g. the &amp;quot;&lt;b&gt;Global. asax&amp;quot;&lt;/b&gt; if you are building a web application) 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#008000;"&gt;// Configure log4net using the .config file&lt;/span&gt;&lt;br /&gt;[assembly: log4net.Config.XmlConfigurator(Watch = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;having done do we can now run a first unit test that accesses the database via NHibernate. The output generated by Log4Net in the unit test runner is similar to this 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:40:07,249 INFO NHibernate 2.0.0.4000 (2.0.0.4000)&lt;br /&gt;2008-07-02 08:40:07,251 INFO hibernate-configuration section not found in application configuration file&lt;br /&gt;2008-07-02 08:40:07,254 INFO Bytecode provider name : lcg&lt;br /&gt;2008-07-02 08:40:07,257 INFO Using reflection optimizer&lt;br /&gt;2008-07-02 08:40:08,515 DEBUG connection.provider=NHibernate.Connection.DriverConnectionProvider&lt;br /&gt;2008-07-02 08:40:08,516 DEBUG dialect=NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.driver_class=NHibernate.Driver.SqlClientDriver&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;2008-07-02 08:40:08,518 DEBUG show_sql=false&lt;br /&gt;2008-07-02 08:40:08,522 DEBUG properties: System.Collections.Generic.Dictionary`2[System.String,System.String]&lt;br /&gt;2008-07-02 08:40:08,526 INFO Mapping resource: LoggingSample.Person.hbm.xml&lt;br /&gt;2008-07-02 08:40:08,872 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person&lt;br /&gt;2008-07-02 08:40:11,537 DEBUG Mapped property: Id -&amp;gt; Id, type: Int32&lt;br /&gt;2008-07-02 08:40:11,612 DEBUG Mapped property: LastName -&amp;gt; LastName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: FirstName -&amp;gt; FirstName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: Birthdate -&amp;gt; Birthdate, type: DateTime&lt;br /&gt;2008-07-02 08:40:11,632 INFO checking mappings queue&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-many association mappings&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-one association property references&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing foreign key constraints&lt;br /&gt;2008-07-02 08:40:11,712 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,716 INFO Using dialect defined converter&lt;br /&gt;...&lt;br /&gt;2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person&lt;br /&gt;2008-07-02 08:40:12,411 DEBUG  Version select: SELECT Id FROM Person WHERE Id = ?&lt;br /&gt;2008-07-02 08:40:12,412 DEBUG  Snapshot select: SELECT person_.Id, person_.LastName as LastName0_, person_.FirstName as FirstName0_, person_.Birthdate as Birthdate0_ FROM Person person_ WHERE person_.Id=?&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;as you can see LOADS of information. 
&lt;/p&gt;
&lt;p&gt;To tune the generated logging information a little bit we can filter the output generated by NHibernate by putting the following configuration section into our config file (put them just after the &lt;b&gt;root&lt;/b&gt; section inside the &lt;b&gt;log4net&lt;/b&gt; node)&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:54ffa529-54b3-414a-8144-22105142bbba" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;WARN&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.SQL&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously NHibernate defines two different loggers &lt;b&gt;NHibernate&lt;/b&gt; and &lt;b&gt;NHibernate.SQL&lt;/b&gt;. The first one receives all logging output that NHibernate generates where as the second one only receives the sql statements generated by NHibernate. 
&lt;/p&gt;
&lt;p&gt;With the above settings in place the output generated is reduced to 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, &lt;br /&gt;                        person0_.LastName as LastName0_, &lt;br /&gt;                        person0_.FirstName as FirstName0_, &lt;br /&gt;                        person0_.Birthdate as Birthdate0_ &lt;br /&gt;                        from Person person0_&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s what we want. Only if we have some weird problems we need to change the priority level of the &lt;b&gt;NHibernate&lt;/b&gt; filter to say INFO or DEBUG.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/3.aspx</link><pubDate>Fri, 17 Oct 2008 00:23:49 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:373</guid><dc:creator>Dario Quintana</dc:creator><description>Revision 3 posted to How to by Dario Quintana on 16/10/2008 09:23:49 p.m.&lt;br /&gt;
&lt;h2&gt;Configure Log4Net for use with NHibernate&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;logging&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;log4net&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;The&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;Log4Net&lt;/a&gt;&amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;assebly&lt;/span&gt; &lt;/a&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;assembly&lt;/span&gt; is distributed with NHibernate binaries.&lt;/p&gt;
&lt;p&gt;If you are not sure about details of the configuration shown below please refer to &lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt; documentation. 
&lt;/p&gt;
&lt;h4&gt;Running without Log4Net&lt;/h4&gt;
&lt;p&gt;If you only want NHibernate to log the queries it sends to the data source when running unit tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the &lt;b&gt;show_sql&lt;/b&gt; key to the NHibernate configuration. If you are using a separate xml file to configure NHibernate (e.g. the hibernate.cfg.xml) then its content might look similar to the one below&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4d238d7a-da04-4b6f-858d-fc1bfa497df4" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now when running any unit test that involves NHibernate the queries generated by NHibernate will be logged in the output window (be it the unit test runner of &lt;b&gt;Resharper&lt;/b&gt; or be it the Output Window of Visual Studio when you use&lt;b&gt;TestDriven&lt;/b&gt;). 
&lt;/p&gt;
&lt;h4&gt;Logging with Log4Net&lt;/h4&gt;
&lt;p&gt;If you want to use Log4Net to collect logging information generated by NHibernate you have to add the necessary configuration to the config file of your application. If you want to log to two different targets (e.g. to the console and to a file) the relevant sections in your config file might look like follows&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4652318c-495c-4713-8a6f-73a04cf8934a" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt;xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Others sections &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;section &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;      type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configSections&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Some others configurations &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; This section contains the log4net configuration settings &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;debug&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Define some output appenders &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;trace&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.TraceAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.ConsoleAppender, log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;             value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d{ABSOLUTE} %-5p %c{1}:%L - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;rollingFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;File&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log.txt&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;AppendToFile&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;RollingStyle&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DatePattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;yyyy.MM.dd&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;StaticLogFileName&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;param &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;&lt;br /&gt;          value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;%d [%t] %-5p %c - %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;layout&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000;"&gt; Setup the root category, add the appenders and set the default priority &lt;/span&gt;&lt;span style="color:#008000;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;priority &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;appender-ref &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;ref&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;console&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here I define that all output with a priority of at least DEBUG goes to the console. 
&lt;/p&gt;
&lt;p&gt;Now I have to tell my application that I want to use Log4Net. I can do that with an assembly level attribute. That is, you have to put the following code snippet somewhere in your application (e.g. the &amp;quot;&lt;b&gt;Global. asax&amp;quot;&lt;/b&gt; if you are building a web application) 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#008000;"&gt;// Configure log4net using the .config file&lt;/span&gt;&lt;br /&gt;[assembly: log4net.Config.XmlConfigurator(Watch = &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;having done do we can now run a first unit test that accesses the database via NHibernate. The output generated by Log4Net in the unit test runner is similar to this 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:40:07,249 INFO NHibernate 2.0.0.4000 (2.0.0.4000)&lt;br /&gt;2008-07-02 08:40:07,251 INFO hibernate-configuration section not found in application configuration file&lt;br /&gt;2008-07-02 08:40:07,254 INFO Bytecode provider name : lcg&lt;br /&gt;2008-07-02 08:40:07,257 INFO Using reflection optimizer&lt;br /&gt;2008-07-02 08:40:08,515 DEBUG connection.provider=NHibernate.Connection.DriverConnectionProvider&lt;br /&gt;2008-07-02 08:40:08,516 DEBUG dialect=NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.driver_class=NHibernate.Driver.SqlClientDriver&lt;br /&gt;2008-07-02 08:40:08,517 DEBUG connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;br /&gt;2008-07-02 08:40:08,518 DEBUG show_sql=false&lt;br /&gt;2008-07-02 08:40:08,522 DEBUG properties: System.Collections.Generic.Dictionary`2[System.String,System.String]&lt;br /&gt;2008-07-02 08:40:08,526 INFO Mapping resource: LoggingSample.Person.hbm.xml&lt;br /&gt;2008-07-02 08:40:08,872 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person&lt;br /&gt;2008-07-02 08:40:11,537 DEBUG Mapped property: Id -&amp;gt; Id, type: Int32&lt;br /&gt;2008-07-02 08:40:11,612 DEBUG Mapped property: LastName -&amp;gt; LastName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: FirstName -&amp;gt; FirstName, type: String&lt;br /&gt;2008-07-02 08:40:11,614 DEBUG Mapped property: Birthdate -&amp;gt; Birthdate, type: DateTime&lt;br /&gt;2008-07-02 08:40:11,632 INFO checking mappings queue&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-many association mappings&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing one-to-one association property references&lt;br /&gt;2008-07-02 08:40:11,634 INFO processing foreign key constraints&lt;br /&gt;2008-07-02 08:40:11,712 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect&lt;br /&gt;2008-07-02 08:40:11,716 INFO Using dialect defined converter&lt;br /&gt;...&lt;br /&gt;2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person&lt;br /&gt;2008-07-02 08:40:12,411 DEBUG  Version select: SELECT Id FROM Person WHERE Id = ?&lt;br /&gt;2008-07-02 08:40:12,412 DEBUG  Snapshot select: SELECT person_.Id, person_.LastName as LastName0_, person_.FirstName as FirstName0_, person_.Birthdate as Birthdate0_ FROM Person person_ WHERE person_.Id=?&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;as you can see LOADS of information. 
&lt;/p&gt;
&lt;p&gt;To tune the generated logging information a little bit we can filter the output generated by NHibernate by putting the following configuration section into our config file (put them just after the &lt;b&gt;root&lt;/b&gt; section inside the &lt;b&gt;log4net&lt;/b&gt; node)&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:54ffa529-54b3-414a-8144-22105142bbba" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;WARN&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;NHibernate.SQL&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;      &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;level &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;value&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;logger&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously NHibernate defines two different loggers &lt;b&gt;NHibernate&lt;/b&gt; and &lt;b&gt;NHibernate.SQL&lt;/b&gt;. The first one receives all logging output that NHibernate generates where as the second one only receives the sql statements generated by NHibernate. 
&lt;/p&gt;
&lt;p&gt;With the above settings in place the output generated is reduced to 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, &lt;br /&gt;                        person0_.LastName as LastName0_, &lt;br /&gt;                        person0_.FirstName as FirstName0_, &lt;br /&gt;                        person0_.Birthdate as Birthdate0_ &lt;br /&gt;                        from Person person0_&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s what we want. Only if we have some weird problems we need to change the priority level of the &lt;b&gt;NHibernate&lt;/b&gt; filter to say INFO or DEBUG.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/2.aspx</link><pubDate>Sun, 07 Sep 2008 17:20:36 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:184</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 2 posted to How to by Fabio Maulo on 07/09/2008 02:20:36 p.m.&lt;br /&gt;
&lt;h2&gt;Configure Log4Net for use with NHibernate&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Recently&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;there&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;was&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;question&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;in&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt;&amp;nbsp;&lt;a href="http://groups.google.com/group/nhusers?hl=en"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHUser&lt;/span&gt;&lt;/a&gt;&amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;group&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;about&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;how&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;configure&lt;/span&gt;&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt;&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;Log4Net&lt;/strong&gt;&amp;nbsp;&lt;span style="text-decoration: line-through; color: red;"&gt;to&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;log&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;queries&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;generated&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;by&lt;/span&gt; &lt;/a&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;assebly&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;is&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;distributed&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; NHibernate &lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;First&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;of&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;all&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;:&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;get&lt;/span&gt;&amp;nbsp;&lt;strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Log4Net&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;here&lt;/span&gt;&lt;/a&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;There&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;also&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;find&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;the&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;documentation&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;binaries&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;If you are not sure about details of the configuration shown below please refer to &lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt; documentation. 
&lt;/p&gt;
&lt;h4&gt;Running without Log4Net&lt;/h4&gt;
&lt;p&gt;If you only want NHibernate to log the queries it sends to the data source when running unit tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the &lt;strong&gt;show_sql&lt;/strong&gt; key to the NHibernate configuration. If you are using a separate xml file to configure NHibernate (e.g. the hibernate.cfg.xml) then its content might look similar to the one below&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4d238d7a-da04-4b6f-858d-fc1bfa497df4" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;xml &lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;version&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;version=&lt;/span&gt;&amp;quot;1.0&amp;quot; &lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;encoding&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;encoding=&lt;/span&gt;&amp;quot;utf-8&amp;quot; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;?&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;connection.provider&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;dialect&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;connection.driver_class&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;connection.connection_string&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;=&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;show_sql&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;show_sql&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;true&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now when running any unit test that involves NHibernate the queries generated by NHibernate will be logged in the output window (be it the unit test runner of &lt;strong&gt;Resharper&lt;/strong&gt; or be it the Output Window of Visual Studio when you &lt;span style="text-decoration: line-through; color: red;"&gt;useTestDriven)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;use&lt;/span&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;TestDriven&lt;/span&gt;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;. 
&lt;/p&gt;
&lt;h4&gt;Logging with Log4Net&lt;/h4&gt;
&lt;p&gt;If you want to use Log4Net to collect logging information generated by NHibernate you have to add the necessary configuration to the config file of your application. If you want to log to two different targets (e.g. to the console and to a file) the relevant sections in your config file might look like follows&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;..&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;configSections&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;section&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:4652318c-495c-4713-8a6f-73a04cf8934a" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;?&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF00FF;"&gt;&lt;span style="background: SpringGreen;"&gt;xml&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;version=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;encoding=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;?&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;span style="background: SpringGreen;"&gt;Others&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;sections&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;section&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;log4net&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
      &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/configSections&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;log4net&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;log4net.Appender.ConsoleAppender,&lt;/span&gt;
&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configSections&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;span style="background: SpringGreen;"&gt;Some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;others&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;configurations&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

  &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;span style="background: SpringGreen;"&gt;This&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;section&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;contains&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; log4net &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;layout&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;log4net.Layout.PatternLayout,&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;settings&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;log4net &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;param&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;debug&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;span style="background: SpringGreen;"&gt;Define&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;some&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;output&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;appenders&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;trace&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
          &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Appender.TraceAppender,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;log4net&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;ConversionPattern&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;%d&lt;/span&gt;
&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
             &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;%d{ABSOLUTE}&lt;/span&gt; %&lt;span style="text-decoration: line-through; color: red;"&gt;p&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-5p&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;%&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;c{1}:%L&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/layout&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/appender&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;RollingFile&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;console&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
          &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Appender.ConsoleAppender,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;log4net&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
             &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;%d{ABSOLUTE}&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;%&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-5p&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;%&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;c{1}:%L&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;%&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;m%n&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;rollingFile&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
          &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;param&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;File&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;log.txt&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;param&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;AppendToFile&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;param&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;RollingStyle&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Date&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;DatePattern&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;yyyy.MM.dd&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;layout&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;type=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;conversionPattern&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;StaticLogFileName&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
        &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;param&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;ConversionPattern&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt;
          &lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;%d &lt;span style="background: SpringGreen;"&gt;[&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;%t]&lt;/span&gt; %&lt;span style="text-decoration: line-through; color: red;"&gt;p&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-5p&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;%&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;c&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt; %m%n&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/layout&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/appender&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;root&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;priority&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;layout&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;!--&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#008000;"&gt; &lt;span style="background: SpringGreen;"&gt;Setup&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;root&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;category&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;add&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;appenders&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;and&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;set&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;default&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;priority&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#008000;"&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;-&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;root&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;priority&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ref=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;appender-ref&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;ref&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;console&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/root&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;..&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;root&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

  &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;log4net&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Here I define that all output with a priority of at least DEBUG goes to the console. 
&lt;/p&gt;
&lt;p&gt;Now I have to tell my application that I want to use Log4Net. I can do that with an assembly level attribute. That is, you have to put the following code snippet somewhere in your application (e.g. the &amp;quot;&lt;strong&gt;Global. asax&amp;quot;&lt;/strong&gt; if you are building a web application) 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;&lt;span style="color:#008000;"&gt;// Configure log4net using the .config file&lt;/span&gt;
[assembly: log4net.Config.XmlConfigurator(Watch = &lt;span style="text-decoration: line-through; color: red;"&gt;true)&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;true&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;]&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;having done do we can now run a first unit test that accesses the database via NHibernate. The output generated by Log4Net in the unit test runner is similar to this 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;2008-07-02 08:40:07,249 INFO NHibernate &lt;span style="text-decoration: line-through; color: red;"&gt;2.0.0.1001&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;2.0.0.4000&lt;/span&gt; (&lt;span style="text-decoration: line-through; color: red;"&gt;2.0.0.1001)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;2.0.0.4000)&lt;/span&gt;
2008-07-02 08:40:07,251 INFO hibernate-configuration section not found in application configuration file
2008-07-02 08:40:07,254 INFO Bytecode provider name : lcg
2008-07-02 08:40:07,257 INFO Using reflection optimizer
2008-07-02 08:40:08,515 DEBUG connection.provider=NHibernate.Connection.DriverConnectionProvider
2008-07-02 08:40:08,516 DEBUG dialect=NHibernate.Dialect.MsSql2005Dialect
2008-07-02 08:40:08,517 DEBUG connection.driver_class=NHibernate.Driver.SqlClientDriver
2008-07-02 08:40:08,517 DEBUG connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;
2008-07-02 08:40:08,518 DEBUG show_sql=false
2008-07-02 08:40:08,522 DEBUG properties: System.Collections.Generic.Dictionary`2[System.String,System.String]
2008-07-02 08:40:08,526 INFO Mapping resource: LoggingSample.Person.hbm.xml
2008-07-02 08:40:08,872 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect
2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person
2008-07-02 08:40:11,537 DEBUG Mapped property: Id -&amp;gt; Id, type: Int32
2008-07-02 08:40:11,612 DEBUG Mapped property: LastName -&amp;gt; LastName, type: String
2008-07-02 08:40:11,614 DEBUG Mapped property: FirstName -&amp;gt; FirstName, type: String
2008-07-02 08:40:11,614 DEBUG Mapped property: Birthdate -&amp;gt; Birthdate, type: DateTime
2008-07-02 08:40:11,632 INFO checking mappings queue
2008-07-02 08:40:11,634 INFO processing one-to-many association mappings
2008-07-02 08:40:11,634 INFO processing one-to-one association property references
2008-07-02 08:40:11,634 INFO processing foreign key constraints
2008-07-02 08:40:11,712 INFO Using dialect: NHibernate.Dialect.MsSql2005Dialect
2008-07-02 08:40:11,716 INFO Using dialect defined converter
...
2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person
2008-07-02 08:40:12,411 DEBUG  Version select: SELECT Id FROM Person WHERE Id = ?
2008-07-02 08:40:12,412 DEBUG  Snapshot select: SELECT person_.Id, person_.LastName as LastName0_, person_.FirstName as FirstName0_, person_.Birthdate as Birthdate0_ FROM Person person_ WHERE person_.Id=?
...
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;as you can see LOADS of information. 
&lt;/p&gt;
&lt;p&gt;To tune the generated logging information a little bit we can filter the output generated by NHibernate by putting the following configuration section into our config file (put them just after the &lt;strong&gt;root&lt;/strong&gt; section inside the &lt;strong&gt;log4net&lt;/strong&gt; node)&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;logger&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/p&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:F2210F5F-69EB-4d4c-AFF7-B8A050E9CC72:54ffa529-54b3-414a-8144-22105142bbba" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;
&lt;pre style="width:100%;"&gt;&lt;div&gt;&lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;logger&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;NHibernate&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;additivity=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;false&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;level&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;WARN&amp;quot;/&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ref=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;RollingFile&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ref=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;level&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;WARN&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/logger&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;logger&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;name=&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;logger&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;

    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;logger&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&amp;quot;NHibernate.SQL&amp;quot;&lt;span style="text-decoration: line-through; color: red;"&gt;additivity=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;false&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;level&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;value=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ALL&amp;quot;/&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ref=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;RollingFile&amp;quot;&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;appender-ref&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;ref=&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;
      &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;level&lt;/span&gt; &lt;/span&gt;&lt;span style="color:#FF0000;"&gt;&lt;span style="background: SpringGreen;"&gt;value&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;=&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;DEBUG&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#FF0000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;/&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;lt;/logger&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;&lt;span style="background: SpringGreen;"&gt;logger&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Obviously NHibernate defines two different loggers &lt;strong&gt;NHibernate&lt;/strong&gt; and &lt;strong&gt;NHibernate.SQL&lt;/strong&gt;. The first one receives all logging output that NHibernate generates where as the second one only receives the sql statements generated by NHibernate. 
&lt;/p&gt;
&lt;p&gt;With the above settings in place the output generated is reduced to 
&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, 
                        person0_.LastName as LastName0_, 
                        person0_.FirstName as FirstName0_, 
                        person0_.Birthdate as Birthdate0_ 
                        from Person person0_
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s what we want. Only if we have some weird problems we need to change the priority level of the &lt;strong&gt;NHibernate&lt;/strong&gt; filter to say INFO or DEBUG&lt;span style="text-decoration: line-through; color: red;"&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;As&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;usual&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;you&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;can&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;find&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;a&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;demo&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;solution&lt;/span&gt;&amp;nbsp;&lt;a href="http://hibernatingrhinos.googlecode.com/svn/trunk/Logging"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;here&lt;/span&gt;.&lt;/p&gt;</description></item><item><title>Configure Log4Net for use with NHibernate</title><link>http://nhforge.org/wikis/howtonh/configure-log4net-for-use-with-nhibernate/revision/1.aspx</link><pubDate>Sun, 07 Sep 2008 05:09:37 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:51</guid><dc:creator>gabriel.schenker</dc:creator><description>Revision 1 posted to How to by gabriel.schenker on 07/09/2008 02:09:37 a.m.&lt;br /&gt;
&lt;p&gt;Recently there was a
question in the&amp;nbsp;&lt;a href="http://groups.google.com/group/nhusers?hl=en"&gt;NHUser&lt;/a&gt;&amp;nbsp;group about how to
configure&amp;nbsp;&lt;strong&gt;Log4Net&lt;/strong&gt;&amp;nbsp;to log the queries
generated by NHibernate. First of all: you can get&amp;nbsp;&lt;strong&gt;Log4Net&lt;/strong&gt;&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;here&lt;/a&gt;. There you also find
the documentation. If you are not sure about details of the configuration shown
below please refer to&amp;nbsp;&lt;a href="http://logging.apache.org/log4net/index.html"&gt;this&lt;/a&gt;&amp;nbsp;documentation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Running without
Log4Net&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you only want
NHibernate to log the queries it sends to the data source when running unit
tests you don&amp;#39;t have to configure Log4Net at all. It suffices to add the&amp;nbsp;&lt;strong&gt;show_sql&lt;/strong&gt;&amp;nbsp;key to the NHibernate
configuration. If you are using a separate xml file to configure NHibernate
(e.g. the hibernate.cfg.xml) then its content might look similar to the one
below&lt;/p&gt;
&lt;p class="MsoNormal"&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&amp;lt;?&lt;/span&gt;&lt;span&gt;xml&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;version&lt;/span&gt;&lt;span&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;encoding&lt;/span&gt;&lt;span&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt;&lt;span&gt; ?&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;hibernate-configuration&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;xmlns&lt;/span&gt;&lt;span&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;session-factory&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Dialect.MsSql2005Dialect&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;Server=(local);Database=NHibernateFAQ;Integrated Security=SSPI;&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span&gt;=&amp;quot;show_sql&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;true&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;property&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&lt;span&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;session-factory&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span&gt;&amp;lt;/&lt;/span&gt;&lt;span&gt;hibernate-configuration&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;&amp;lt;o:p&amp;gt;&amp;lt;/o:p&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;Now when running any
unit test that involves NHibernate the queries generated by NHibernate will be
logged in the output window (be it the unit test runner of&amp;nbsp;Resharper&amp;nbsp;or be it the Output
Window of Visual Studio when you useTestDriven).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Logging with Log4Net&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you want to use
Log4Net to collect logging information generated by NHibernate you have to add
the necessary configuration to the config file of your application. If you want
to log to two different targets (e.g. to the console and to a file) the relevant
sections in your config file might look like follows&lt;/p&gt;
&lt;p&gt;&amp;lt;configuration&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;
...&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;configSections&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;section name=&amp;quot;log4net&amp;quot; type=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;/configSections&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;log4net&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;appender name=&amp;quot;ConsoleAppender&amp;quot; type=&amp;quot;log4net.Appender.ConsoleAppender,
log4net&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;layout type=&amp;quot;log4net.Layout.PatternLayout,
log4net&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;param name=&amp;quot;ConversionPattern&amp;quot; value=&amp;quot;%d
%p %m%n&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;/layout&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/appender&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;appender name=&amp;quot;RollingFile&amp;quot; type=&amp;quot;log4net.Appender.RollingFileAppender,log4net&amp;quot; &amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;param name=&amp;quot;File&amp;quot; value=&amp;quot;log.txt&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;param name=&amp;quot;AppendToFile&amp;quot; value=&amp;quot;true&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;param name=&amp;quot;DatePattern&amp;quot; value=&amp;quot;yyyy.MM.dd&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;layout type=&amp;quot;log4net.Layout.PatternLayout,log4net&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;conversionPattern value=&amp;quot;%d
%p %m%n&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;/layout&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/appender&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;root&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;priority value=&amp;quot;DEBUG&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;lt;appender-ref ref=&amp;quot;ConsoleAppender&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/root&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
...&lt;/p&gt;
&lt;p&gt;&amp;lt;/configuration&amp;gt;&lt;/p&gt;
&lt;p&gt;Here I define that all
output with a priority of at least DEBUG goes to the console.&lt;/p&gt;
&lt;p&gt;Now I have to tell my
application that I want to use Log4Net. I can do that with an assembly level
attribute. That is, you have to put the following code snippet somewhere in
your application (e.g. the &amp;quot;&lt;strong&gt;Global. asax&amp;quot;&lt;/strong&gt;&amp;nbsp;if you are building a
web application)&lt;/p&gt;
&lt;p&gt;// Configure log4net using the .config
file&lt;/p&gt;
&lt;p&gt;[assembly:
log4net.Config.XmlConfigurator(Watch = true)]&lt;/p&gt;
&lt;p&gt;having done do we can
now run a first unit test that accesses the database via NHibernate. The output
generated by Log4Net in the unit test runner is similar to this&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:07,249 INFO NHibernate
2.0.0.1001 (2.0.0.1001)&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:07,251 INFO
hibernate-configuration section not found in application configuration file&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:07,254 INFO Bytecode
provider name : lcg&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:07,257 INFO Using
reflection optimizer&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,515 DEBUG
connection.provider=NHibernate.Connection.DriverConnectionProvider&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,516 DEBUG
dialect=NHibernate.Dialect.MsSql2005Dialect&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,517 DEBUG
connection.driver_class=NHibernate.Driver.SqlClientDriver&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,517 DEBUG
connection.connection_string=Server=(local);Database=NHibernateFAQ;Integrated
Security=SSPI;&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,518 DEBUG show_sql=false&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,522 DEBUG properties:
System.Collections.Generic.Dictionary`2[System.String,System.String]&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,526 INFO Mapping
resource: LoggingSample.Person.hbm.xml&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:08,872 INFO Using
dialect: NHibernate.Dialect.MsSql2005Dialect&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,425 INFO Mapping class: LoggingSample.Person -&amp;gt; Person&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,537 DEBUG Mapped
property: Id -&amp;gt; Id, type: Int32&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,612 DEBUG Mapped
property: LastName -&amp;gt; LastName, type: String&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,614 DEBUG Mapped
property: FirstName -&amp;gt; FirstName, type: String&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,614 DEBUG Mapped
property: Birthdate -&amp;gt; Birthdate, type: DateTime&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,632 INFO checking
mappings queue&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,634 INFO processing
one-to-many association mappings&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,634 INFO processing
one-to-one association property references&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,634 INFO processing
foreign key constraints&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,712 INFO Using
dialect: NHibernate.Dialect.MsSql2005Dialect&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:11,716 INFO Using dialect
defined converter&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:12,410 DEBUG Static SQL for entity: LoggingSample.Person&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:12,411 DEBUG&amp;nbsp; Version select: SELECT Id FROM Person WHERE
Id = ?&lt;/p&gt;
&lt;p&gt;2008-07-02 08:40:12,412 DEBUG&amp;nbsp; Snapshot select: SELECT person_.Id,
person_.LastName as
LastName0_, person_.FirstName as
FirstName0_, person_.Birthdate as
Birthdate0_ FROM Person person_ WHERE person_.Id=?&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;as you can see LOADS
of information.&lt;/p&gt;
&lt;p&gt;To tune the generated
logging information a little bit we can filter the output generated by
NHibernate by putting the following configuration section into our config file
(put them just after the&amp;nbsp;&lt;strong&gt;root&lt;/strong&gt;&amp;nbsp;section inside the&amp;nbsp;&lt;strong&gt;log4net&lt;/strong&gt;&amp;nbsp;node)&lt;/p&gt;
&lt;p&gt;&amp;lt;logger name=&amp;quot;NHibernate&amp;quot; additivity=&amp;quot;false&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;level value=&amp;quot;WARN&amp;quot;/&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;appender-ref ref=&amp;quot;RollingFile&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;appender-ref ref=&amp;quot;ConsoleAppender&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;/logger&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;lt;logger name=&amp;quot;NHibernate.SQL&amp;quot; additivity=&amp;quot;false&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;level value=&amp;quot;ALL&amp;quot;/&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;appender-ref ref=&amp;quot;RollingFile&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;appender-ref ref=&amp;quot;ConsoleAppender&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;/logger&amp;gt;&lt;/p&gt;
&lt;p&gt;Obviously NHibernate
defines two different loggers&amp;nbsp;&lt;strong&gt;NHibernate&lt;/strong&gt;&amp;nbsp;and&amp;nbsp;&lt;strong&gt;NHibernate.SQL&lt;/strong&gt;. The first one
receives all logging output that NHibernate generates where as the second one
only receives the sql statements generated by NHibernate.&lt;/p&gt;
&lt;p&gt;With the above
settings in place the output generated is reduced to&lt;/p&gt;
&lt;p&gt;2008-07-02 08:48:58,636 DEBUG select person0_.Id as Id0_, &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; person0_.LastName as LastName0_, &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; person0_.FirstName as FirstName0_, &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; person0_.Birthdate as Birthdate0_ &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from Person person0_&lt;/p&gt;
&lt;p&gt;That&amp;#39;s what we want.
Only if we have some weird problems we need to change the priority level of the&amp;nbsp;&lt;strong&gt;NHibernate&lt;/strong&gt;&amp;nbsp;filter to say INFO or
DEBUG.&lt;/p&gt;
&lt;p&gt;As usual you can find
a demo solution&amp;nbsp;&lt;a href="http://hibernatingrhinos.googlecode.com/svn/trunk/Logging"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/0.aspx</link><pubDate>Thu, 08 Sep 2011 00:31:52 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:26</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 07/09/2011 09:31:52 p.m.&lt;br /&gt;
&lt;h2&gt;Get unique results from joined queries&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;query&lt;/span&gt;&lt;/div&gt;

&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;where Comment.Name refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;  p
   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    Post p
   &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;    p.Comments c
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;   c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&amp;quot;;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So, you go ahead and&lt;/div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Assuming the next data is in the DB:&lt;/p&gt;
&lt;pre&gt;  Post#1&lt;br /&gt;
    Comment by &amp;#39;Ken Egozi&amp;#39;
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;

  Post#2
    Comment by &amp;#39;Fabio Maulo&amp;#39;

  Post#3
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;&lt;/pre&gt;
&lt;p&gt;you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;br /&gt;However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3&lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Term&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Research&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory.&lt;a href="http://www.thesisdom.com/buy-thesis/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Thesis&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Dissertation&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Buy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  {p.*}
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Posts p
     &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt;  Comments c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; c.PostId = p.Id
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;   c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object. Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines. there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless. &lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Posts p
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;EXISTS&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 1
      &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Comments c
      &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  c.PostId = p.Id
        &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;  c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API. &lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;The syntax in HQL:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;   Post p
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; Comment c
      &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; c.Post = p
      &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
and in Criteria API:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;Ken Egozi&amp;quot;&lt;/span&gt;))
     .Add(Restrictions.EqProperty(&lt;span style="color:#006080;"&gt;&amp;quot;Post.Id&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;p.Id&amp;quot;&lt;/span&gt;))
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = session.CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Post), &lt;span style="color:#006080;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;)
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers. all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .SetResultTransformer(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/5.aspx</link><pubDate>Wed, 30 Mar 2011 11:28:01 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:517</guid><dc:creator>markweee</dc:creator><description>Revision 5 posted to How to by markweee on 30/03/2011 08:28:01 a.m.&lt;br /&gt;
&lt;h2&gt;Get unique results from joined queries&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: query&lt;/div&gt;

&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;where Comment.Name refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;  p
   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    Post p
   &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;    p.Comments c
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;   c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&amp;quot;;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So, you go ahead and&lt;/div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Assuming the next data is in the DB:&lt;/p&gt;
&lt;pre&gt;  Post#1&lt;br /&gt;
    Comment by &amp;#39;Ken Egozi&amp;#39;
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;

  Post#2
    Comment by &amp;#39;Fabio Maulo&amp;#39;

  Post#3
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;&lt;/pre&gt;
&lt;p&gt;you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;br /&gt;However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3 &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Term&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Research&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory. &lt;a href="http://www.thesisdom.com/buy-thesis/"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Thesis&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Dissertation&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Buy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  {p.*}
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Posts p
     &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt;  Comments c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; c.PostId = p.Id
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;   c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object. Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines. there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless. &lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Posts p
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;EXISTS&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 1
      &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Comments c
      &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  c.PostId = p.Id
        &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;  c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API. &lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;The syntax in HQL:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;   Post p
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; Comment c
      &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; c.Post = p
      &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
and in Criteria API:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;Ken Egozi&amp;quot;&lt;/span&gt;))
     .Add(Restrictions.EqProperty(&lt;span style="color:#006080;"&gt;&amp;quot;Post.Id&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;p.Id&amp;quot;&lt;/span&gt;))
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = session.CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Post), &lt;span style="color:#006080;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;)
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers. all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .SetResultTransformer(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/4.aspx</link><pubDate>Thu, 18 Sep 2008 16:10:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:376</guid><dc:creator>Woil</dc:creator><description>Revision 4 posted to How to by Woil on 18/09/2008 01:10:22 p.m.&lt;br /&gt;
&lt;h2&gt;Get unique results from joined queries&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;query&lt;/span&gt;&lt;/div&gt;

&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;where &lt;span style="text-decoration: line-through; color: red;"&gt;Commment.Name&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Comment.Name&lt;/span&gt; refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;  p
   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    Post p
   &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;    p.Comments c
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;   c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&amp;quot;;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So, you go ahead and&lt;/div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Assuming the next data is in the DB:&lt;/p&gt;
&lt;pre&gt;  Post#1&lt;br /&gt;
    Comment by &amp;#39;Ken Egozi&amp;#39;
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;

  Post#2
    Comment by &amp;#39;Fabio Maulo&amp;#39;

  Post#3
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;&lt;/pre&gt;
&lt;p&gt;you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;br /&gt;However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory.&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  {p.*}
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Posts p
     &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt;  Comments c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; c.PostId = p.Id
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;   c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object. Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines. there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless. &lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Posts p
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;EXISTS&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 1
      &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Comments c
      &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  c.PostId = p.Id
        &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;  c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API. &lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;The syntax in HQL:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;   Post p
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; Comment c
      &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; c.Post = p
      &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
and in Criteria API:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;Ken Egozi&amp;quot;&lt;/span&gt;))
     .Add(Restrictions.EqProperty(&lt;span style="color:#006080;"&gt;&amp;quot;Post.Id&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;p.Id&amp;quot;&lt;/span&gt;))
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = session.CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Post), &lt;span style="color:#006080;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;)
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers. all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .SetResultTransformer(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/3.aspx</link><pubDate>Fri, 12 Sep 2008 14:43:10 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:127</guid><dc:creator>Ken Egozi</dc:creator><description>Revision 3 posted to How to by Ken Egozi on 12/09/2008 11:43:10 a.m.&lt;br /&gt;
&lt;h2&gt;Get unique results from joined queries&lt;/h2&gt;
&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;where Commment.Name refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;   &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;  p
   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    Post p
   &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;    p.Comments c
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;   c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&amp;quot;;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So, you go ahead and&lt;/div&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;   var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Assuming the next data is in the DB:&lt;/p&gt;
&lt;pre&gt;  Post#1&lt;br /&gt;
    Comment by &amp;#39;Ken Egozi&amp;#39;
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;

  Post#2
    Comment by &amp;#39;Fabio Maulo&amp;#39;

  Post#3
    Comment by &amp;#39;Fabio Maulo&amp;#39;
    Comment by &amp;#39;Ken Egozi&amp;#39;&lt;/pre&gt;
&lt;p&gt;you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;br /&gt;
However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory.&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  {p.*}
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Posts p
     &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt;  Comments c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; c.PostId = p.Id
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;   c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object. Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines. there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless. 
&lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Posts p
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;EXISTS&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 1
      &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Comments c
      &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  c.PostId = p.Id
        &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;  c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API. 
&lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;The syntax in HQL:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;   Post p
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; Comment c
      &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; c.Post = p
      &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
and in Criteria API:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&lt;span style="color:#006080;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;Ken Egozi&amp;quot;&lt;/span&gt;))
     .Add(Restrictions.EqProperty(&lt;span style="color:#006080;"&gt;&amp;quot;Post.Id&amp;quot;&lt;/span&gt;, &lt;span style="color:#006080;"&gt;&amp;quot;p.Id&amp;quot;&lt;/span&gt;))
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = session.CreateCriteria(&lt;span style="color:#0000ff;"&gt;typeof&lt;/span&gt; (Post), &lt;span style="color:#006080;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;)
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers. all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;div&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt; var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;&lt;/span&gt;;
   var posts = session.CreateQuery(hql)
      .SetResultTransformer(&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/2.aspx</link><pubDate>Fri, 12 Sep 2008 14:34:49 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:101</guid><dc:creator>Fabio Maulo</dc:creator><description>Revision 2 posted to How to by Fabio Maulo on 12/09/2008 11:34:49 a.m.&lt;br /&gt;
&lt;h2&gt;Get unique results from joined queries&lt;/h2&gt;
&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;where Commment.Name refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;select&lt;/span&gt;  p
   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;    Post p
   &lt;span style="color:#0000ff;"&gt;join&lt;/span&gt;    p.Comments c
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt;   c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&amp;quot;;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;So, you go ahead and&lt;/div&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;;&lt;/span&gt;
   &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;pre&gt;Assuming the next data is in the DB:&lt;br /&gt;&amp;nbsp; Post#1&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;&lt;br /&gt;&amp;nbsp; Post#2&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;&lt;br /&gt;&amp;nbsp; Post#3&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;&lt;br /&gt;you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;/pre&gt;
&lt;p&gt;However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory.&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt;  {p.*}
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;    Posts p
     &lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt;  Comments c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; c.PostId = p.Id
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;   c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object. Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world: &lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines. there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless. 
&lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *
   &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Posts p
   &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;EXISTS&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; 1
      &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt;   Comments c
      &lt;span style="color:#0000ff;"&gt;WHERE&lt;/span&gt;  c.PostId = p.Id
        &lt;span style="color:#0000ff;"&gt;AND&lt;/span&gt;  c.[Name] = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API. 
&lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;The syntax in HQL:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt;   &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt;   Post p
   &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;exists&lt;/span&gt;
   (
      &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; Comment c
      &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; c.Post = p
      &lt;span style="color:#0000ff;"&gt;and&lt;/span&gt; c.Name = &lt;span style="color:#006080;"&gt;&amp;#39;Ken Egozi&amp;#39;&lt;/span&gt;
   )&lt;/pre&gt;
&lt;/div&gt;
and in Criteria API:&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Name&amp;quot;,&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:#006080;"&gt;&amp;quot;Ken Egozi&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;))&lt;/span&gt;
     &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;
     .Add(Restrictions.EqProperty(&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;Post.Id&amp;quot;,&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;p.Id&amp;quot;))&lt;/span&gt;
     &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;Post.Id&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;,&lt;/span&gt; &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;p.Id&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = &lt;span style="text-decoration: line-through; color: red;"&gt;session.CreateCriteria(typeof&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;session.CreateCriteria&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;typeof&lt;/span&gt;&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;(&lt;/span&gt;Post), &lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;p&amp;quot;)&lt;/span&gt;
     &lt;span style="color:#006080;"&gt;&lt;span style="background: SpringGreen;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;)&lt;/span&gt;
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers. all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;div&gt;
&lt;pre style="font-size:8pt;margin:0em;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, &amp;#39;Courier New&amp;#39;, courier, monospace;background-color:#f4f4f4;border-style:none;padding:0px;"&gt; var hql = &lt;span style="color:#006080;"&gt;@&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&lt;span style="text-decoration: line-through; color: red;"&gt;&amp;quot;;&lt;/span&gt;
   &lt;span style="background: SpringGreen;"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;;&lt;/span&gt;
   var posts = session.CreateQuery(hql)
      .&lt;span style="text-decoration: line-through; color: red;"&gt;SetResultTransformer(new&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;SetResultTransformer(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&lt;span style="background: SpringGreen;"&gt;new&lt;/span&gt;&lt;/span&gt; DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Get unique results from joined queries</title><link>http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries/revision/1.aspx</link><pubDate>Fri, 12 Sep 2008 07:32:51 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:100</guid><dc:creator>Ken Egozi</dc:creator><description>Revision 1 posted to How to by Ken Egozi on 12/09/2008 04:32:51 a.m.&lt;br /&gt;
&lt;h3&gt;Problem:&lt;/h3&gt;
&lt;p&gt;A recurring issue that new NHibernate users keep asking about, is the fact that they sometimes get duplicate results from their queries&lt;/p&gt;
&lt;p&gt;For example, take a look at the next model:&lt;br /&gt;
&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/post-and-comment.png" alt="" /&gt;&lt;br /&gt;
where Commment.Name refer to the name of the person who wrote the comment.&lt;/p&gt;
&lt;p&gt;If you&amp;#39;d want to issue a query on Post that joins to Comment, say &amp;quot;all of the posts that were commented by &amp;#39;Ken Egozi&amp;#39;&amp;quot;, the next hql query should do the trick:&lt;/p&gt;
&lt;pre&gt;   select  p
   from    Post p
   join    p.Comments c
   where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;;
&lt;/pre&gt;
&lt;p&gt;So, you go ahead and&lt;/p&gt;
&lt;pre&gt;   var hql = @&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;;
   var posts = session.CreateQuery(hql)
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;p&gt;Assuming the next data is in the DB:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;Post#1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;Post#2&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;Post#3&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Fabio Maulo&amp;#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Comment by &amp;#39;Ken Egozi&amp;#39;&lt;br /&gt;
&lt;br /&gt;
you&amp;#39;d expect the list &amp;#39;posts&amp;#39; to include two posts: Post#1 and Post#3.&lt;/p&gt;
&lt;p&gt;However, if you&amp;#39;d look into posts, you&amp;#39;ll see that it has three entries, two entries pointing to Post#1 and one entry pointing to Post#3&lt;/p&gt;
&lt;p&gt;Note that NHibernate is smart enough not to create two separate instances for Post#1, but rather a single instance will be created, and the two entries in the list will point to the same instance in memory.&lt;/p&gt;
&lt;h3&gt;Explanation:&lt;/h3&gt;
&lt;p&gt;The said hql query (or an equivalent Criteria) is translated to the joined SELECT clause&lt;/p&gt;
&lt;pre&gt;   SELECT  {p.*}
   FROM    Posts p
     JOIN  Comments c ON c.PostId = p.Id
   WHERE   c.[Name] = &amp;#39;Ken Egozi&amp;#39;&lt;/pre&gt;
&lt;p&gt;where {p.*} is substituted by the list of fields needed to satisfy a Post object.
Running this SQL query directly on the data detailed above, will result with three lines in the resultset.&lt;/p&gt;
&lt;h3&gt;Solutions&lt;/h3&gt;
&lt;p&gt;There are three solutions to that in SQL world:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The DISTINCT keyword, when added on the fields of Posts, will ensure that there will be no duplicate lines.
there are a few problems with that. Some RDBMS systems will not allow DISTINCT over certain data types, like the blob types in SQL Server. And if you also need to retrieve the joined data, then the DISTINCT keyword will be useless.
&lt;/li&gt;
&lt;li&gt;Using a subselect
&lt;pre&gt;   SELECT *
   FROM   Posts p
   WHERE EXISTS
   (
      SELECT 1
      FROM   Comments c
      WHERE  c.PostId = p.Id
        AND  c.[Name] = &amp;#39;Ken Egozi&amp;#39;
   )&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in the client code.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The solutions in NHibernate&amp;#39;s world, in accordance to the SQL solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use the &amp;#39;distinct&amp;#39; keyword. &lt;br /&gt;
The problems that exists in the SQL world still apply, and it can only be used in HQL, not it Criteria API.&lt;/li&gt;
&lt;li&gt;Use subselects.&lt;br /&gt;
The syntax in HQL:&lt;br /&gt;
&lt;pre&gt;   from   Post p
   where exists
   (
      from Comment c
      where c.Post = p
      and c.Name = &amp;#39;Ken Egozi&amp;#39;
   )&lt;/pre&gt;
and in Criteria API:&lt;br /&gt;
&lt;pre&gt;  var commentsByKenEgozi = DetachedCriteria.For&amp;lt;comment&amp;gt;()
     .Add(Restrictions.Eq(&amp;quot;Name&amp;quot;, &amp;quot;Ken Egozi&amp;quot;))
     .Add(Restrictions.EqProperty(&amp;quot;Post.Id&amp;quot;, &amp;quot;p.Id&amp;quot;))
     .SetProjection(NHibernate.Criterion.Projections.Id());
 
  var posts = session.CreateCriteria(typeof (Post), &amp;quot;p&amp;quot;)
     .Add(Subqueries.Exists(commentsByKenEgozi))
     .List&amp;lt;post&amp;gt;();&amp;lt;/comment&amp;gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Remove duplicates in client code.&lt;br /&gt;
That&amp;#39;s actually pretty easy with NHibernate, thanks to the result transformers.
all you need to do is add .SetResultTransformer(new DistinctRootEntityResultTransformer()) to either your query, or criteria, and NHibernate will remove duplicate entries from the resulted list.&lt;br /&gt;
so, using the same hql as in the first example, you can write&lt;br /&gt;
&lt;pre&gt;   var hql = @&amp;quot;
      select  p
      from    Post p
      join    p.Comments c
      where   c.Name = &amp;#39;Ken Egozi&amp;#39;&amp;quot;;
   var posts = session.CreateQuery(hql)
      .SetResultTransformer(new DistinctRootEntityResultTransformer())
      .List&amp;lt;post&amp;gt;();&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/0.aspx</link><pubDate>Thu, 08 Sep 2011 00:18:34 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:32</guid><dc:creator>John Davidson</dc:creator><description>Current revision posted to How to by John Davidson on 07/09/2011 09:18:34 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="text-decoration: line-through; color: red;"&gt;lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;loading&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;strong&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt;&lt;span style="color:#0000ff;"&gt;&lt;strong&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a class="ExistingPageLink" title="Click to view the page titled: Run in Medium Trust" href="/wikis/howtonh/run-in-medium-trust.aspx"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Sample Commands&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Generate&amp;nbsp;proxies for ActiveRecord&lt;br /&gt;&lt;/strong&gt;&lt;span&gt;NHPG.exe &lt;i&gt;/g&lt;span style="text-decoration:underline;"&gt;:activerecord&lt;/span&gt;&lt;/i&gt;&amp;nbsp;/o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;strong&gt;Generate&amp;nbsp;proxies with multiple class mapping assemblies &lt;/b&gt;&lt;/span&gt;&lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Research&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Papers&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt;
&lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Term&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Papers&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;NHPG.exe&amp;nbsp;/o:Example.Domain.Proxies.dll &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;Example.Domain1.dll&lt;/span&gt; &lt;span style="text-decoration:underline;"&gt;Example.Domain2.dll&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;strong&gt;Generate proxies with a custom Dialect &lt;/b&gt;&lt;/span&gt;&lt;a href="http://www.essaydom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Essays&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Dissertations&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Theses&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;NHPG.exe &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;/d:&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;NHibernate.Dialect.Oracle9Dialect&lt;/span&gt;&lt;/i&gt; /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/12.aspx</link><pubDate>Wed, 30 Mar 2011 11:30:52 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:516</guid><dc:creator>markweee</dc:creator><description>Revision 12 posted to How to by markweee on 30/03/2011 08:30:52 a.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: lazy loading&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;b&gt;Sample Commands&lt;/b&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Generate&amp;nbsp;proxies for ActiveRecord&lt;br /&gt;&lt;/b&gt;&lt;span&gt;NHPG.exe &lt;i&gt;/g&lt;span style="text-decoration:underline;"&gt;:activerecord&lt;/span&gt;&lt;/i&gt;&amp;nbsp;/o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;b&gt;Generate&amp;nbsp;proxies with multiple class mapping assemblies &lt;/b&gt;&lt;/span&gt;&lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Research&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Papers&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt;
&lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Term&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Papers&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span&gt;NHPG.exe&amp;nbsp;/o:Example.Domain.Proxies.dll &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;Example.Domain1.dll&lt;/span&gt; &lt;span style="text-decoration:underline;"&gt;Example.Domain2.dll&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;b&gt;Generate proxies with a custom Dialect &lt;/b&gt;&lt;/span&gt;&lt;a href="http://www.essaydom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Essays&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Dissertations&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Theses&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;span&gt;NHPG.exe &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;/d:&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;NHibernate.Dialect.Oracle9Dialect&lt;/span&gt;&lt;/i&gt; /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/11.aspx</link><pubDate>Wed, 08 Oct 2008 18:03:24 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:377</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 11 posted to How to by Bill Pierce on 08/10/2008 03:03:24 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;div style="font-size: 90%;"&gt;Filed under: &lt;span style="background: SpringGreen;"&gt;lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;loading&lt;/span&gt;&lt;/div&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;b&gt;Sample Commands&lt;/b&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Generate&amp;nbsp;proxies for ActiveRecord&lt;br /&gt;&lt;/b&gt;&lt;span&gt;NHPG.exe &lt;i&gt;/g&lt;span style="text-decoration:underline;"&gt;:activerecord&lt;/span&gt;&lt;/i&gt;&amp;nbsp;/o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;b&gt;Generate&amp;nbsp;proxies with multiple class mapping assemblies&lt;br /&gt;&lt;/b&gt;NHPG.exe&amp;nbsp;/o:Example.Domain.Proxies.dll &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;Example.Domain1.dll&lt;/span&gt; &lt;span style="text-decoration:underline;"&gt;Example.Domain2.dll&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;b&gt;&lt;span style="background: SpringGreen;"&gt;Generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;proxies&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;a&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;custom&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Dialect&lt;/span&gt;&lt;br /&gt;&lt;/b&gt;&lt;span style="background: SpringGreen;"&gt;NHPG.exe&lt;/span&gt; &lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;d:&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/span&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;NHibernate.Dialect.Oracle9Dialect&lt;/span&gt;&lt;/span&gt;&lt;/i&gt; &lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;o:Example.Domain.Proxies.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Example.Domain.dll&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/10.aspx</link><pubDate>Tue, 23 Sep 2008 18:22:57 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:172</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 10 posted to How to by Bill Pierce on 23/09/2008 03:22:57 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Sample Commands&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Generate&amp;nbsp;proxies for ActiveRecord&lt;br /&gt;&lt;/strong&gt;&lt;span&gt;NHPG.exe &lt;em&gt;/g&lt;span style="text-decoration:underline;"&gt;:activerecord&lt;/span&gt;&lt;/em&gt;&amp;nbsp;/o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;&lt;strong&gt;Generate&amp;nbsp;proxies with multiple class mapping assemblies&lt;br /&gt;&lt;/strong&gt;NHPG.exe&amp;nbsp;/o:Example.Domain.Proxies.dll &lt;em&gt;&lt;span style="text-decoration:underline;"&gt;Example.Domain1.dll&lt;/span&gt; &lt;span style="text-decoration:underline;"&gt;Example.Domain2.dll&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/9.aspx</link><pubDate>Tue, 23 Sep 2008 18:11:02 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:149</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 9 posted to How to by Bill Pierce on 23/09/2008 03:11:02 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Sample Commands&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Pre-Generate&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Lazy&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Loading&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Proxies&lt;/span&gt; &lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Generate&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;proxies&lt;/span&gt; for ActiveRecord&lt;br /&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;"&gt;NHPG.exe &lt;em&gt;/g&lt;span style="text-decoration:underline;"&gt;:activerecord&lt;/span&gt;&lt;/em&gt;&amp;nbsp;/o:Example.ActiceRecordDomain.Proxies.dll Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:x-small;"&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Generate&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;proxies&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;multiple&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;class&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;mapping&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;assemblies&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;&lt;span style="background: SpringGreen;"&gt;NHPG.exe&lt;/span&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;o:Example.Domain.Proxies.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Example.Domain1.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Example.Domain2.dll&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/8.aspx</link><pubDate>Tue, 23 Sep 2008 18:09:19 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:148</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 8 posted to How to by Bill Pierce on 23/09/2008 03:09:19 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\bin\net-2.0\NHPG.exe /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;br /&gt;NHPG.exe:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Sample&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Commands&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;span style="background: SpringGreen;"&gt;Pre-Generate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Loading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Proxies&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;for&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ActiveRecord&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="background: SpringGreen;"&gt;NHPG.exe&lt;/span&gt; &lt;em&gt;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;g&lt;/span&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;activerecord&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&amp;nbsp;&lt;span style="background: SpringGreen;"&gt;/&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;o:Example.ActiceRecordDomain.Proxies.dll&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Example.ActiveRecordDomain.dll&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/7.aspx</link><pubDate>Tue, 23 Sep 2008 03:58:50 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:147</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 7 posted to How to by Bill Pierce on 23/09/2008 12:58:50 a.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;**This&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;is&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;preliminary&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;documentation**&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;*&lt;/span&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=216446&amp;amp;package_id=292389"&gt;NHibernate ProxyGenerators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;.&lt;span style="text-decoration: line-through; color: red;"&gt;.\..\..\lib\NHPG&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;.\..\..\lib\bin\net-2.0\NHPG.exe&lt;/span&gt; /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;NHPG&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="background: SpringGreen;"&gt;NHPG.exe&lt;/span&gt;:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;b&gt;&lt;i&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#000000;"&gt;&amp;gt;&lt;br /&gt;&lt;i&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:x-small;"&gt;You&amp;#39;re done!&amp;nbsp; Pre-generated lazy loading proxies can be used with NHibernate to &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;Run in Medium Trust&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/6.aspx</link><pubDate>Mon, 22 Sep 2008 17:19:24 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:145</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 6 posted to How to by Bill Pierce on 22/09/2008 02:19:24 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;***This is preliminary documentation***&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for NHibernate ProxyGenerators&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\NHPG /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;br /&gt;NHPG:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#000000;"&gt;&amp;gt;&lt;br /&gt;&lt;em&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size:x-small;"&gt;&lt;span style="background: SpringGreen;"&gt;You&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;re&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;done&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;!&lt;/span&gt;&amp;nbsp; &lt;span style="background: SpringGreen;"&gt;Pre-generated&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;lazy&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;loading&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;proxies&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;can&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;used&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;with&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;a href="/wikis/howtonh/run-in-medium-trust.aspx" title="Click to view the page titled: Run in Medium Trust" class="ExistingPageLink"&gt;&lt;span style="background: SpringGreen;"&gt;Run&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Medium&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Trust&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/5.aspx</link><pubDate>Mon, 22 Sep 2008 17:15:02 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:143</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 5 posted to How to by Bill Pierce on 22/09/2008 02:15:02 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;***This is preliminary documentation***&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for &lt;span style="text-decoration: line-through; color: red;"&gt;[&lt;/span&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;howtonh:/media/g/proxygenerators/default.aspx|NHibernate&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;NHibernate&lt;/span&gt; ProxyGenerators&lt;span style="text-decoration: line-through; color: red;"&gt;]&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\NHPG /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;br /&gt;NHPG:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#000000;"&gt;&amp;gt;&lt;br /&gt;&lt;em&gt;Replace Example.Domain.Proxies with the name of the assembly used for argument &amp;#39;/o&amp;#39; in Step 4.&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Pre-Generate Lazy Loading Proxies</title><link>http://nhforge.org/wikis/howtonh/pre-generate-lazy-loading-proxies/revision/4.aspx</link><pubDate>Mon, 22 Sep 2008 17:14:41 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:142</guid><dc:creator>Bill Pierce</dc:creator><description>Revision 4 posted to How to by Bill Pierce on 22/09/2008 02:14:41 p.m.&lt;br /&gt;
&lt;h2&gt;Pre-Generate Lazy Loading Proxies&lt;/h2&gt;
&lt;p&gt;***This is preliminary documentation***&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Download the latest binaries for [howtonh:/media/g/proxygenerators/default.aspx|NHibernate &lt;span style="text-decoration: line-through; color: red;"&gt;ProxyGenerators]&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;ProxyGenerators&lt;/span&gt;]&lt;/li&gt;
&lt;li&gt;Extract the binaries to your &amp;quot;lib&amp;quot; directory relative to your source files&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Extract.jpg" alt="" /&gt;&lt;/li&gt;
&lt;li&gt;Open a command prompt and navigate the to &amp;quot;bin&amp;quot; directory in your web project.&lt;/li&gt;
&lt;li&gt;Enter the following command:&lt;br /&gt;..\..\..\lib\NHPG /o:Example.Domain.Proxies.dll Example.Domain.dll&lt;br /&gt;&lt;img src="/cfs-file.ashx/__key/CommunityServer.Wikis.Components.Files/howtonh/Prompt.jpg" alt="" /&gt;&lt;br /&gt;NHPG:&amp;nbsp; The console application that generates the proxies.&amp;nbsp; Included in the binary distribution from step 1&lt;br /&gt;/o: The path to the assembly that will be generated by NHPG&lt;br /&gt;Example.Domain.dll: The assembly that contains your NHibernate Mappings (hbm.xml files)&lt;/li&gt;
&lt;li&gt;Add &amp;#39;proxyfactory.factory_class&amp;#39; to your NHibernate configuration file:&lt;br /&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;hibernate-configuration&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;xmlns&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;urn:nhibernate-configuration-2.2&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;dialect&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.provider&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.driver_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;connection.connection_string&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;Server=(local);Database=Development;Trusted_Connection=True;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;mapping&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;assembly&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;Example.Domain&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt;&lt;span style="font-size:x-small;color:#ff00ff;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;&lt;span style="font-size:x-small;color:#ff0000;"&gt;name&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;"&gt;CastleStaticProxyFactoryFactory, Example.Domain.Proxies&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;property&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;strong&gt;&lt;em&gt;&lt;span style="text-decoration:underline;"&gt;&amp;gt;&lt;/span&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&lt;span style="font-size:x-small;color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;&lt;span style="font-size:x-small;color:#800000;"&gt;session-factory&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:x-small;color:#000