<?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>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate.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>Creating a custom id generator for nHibernate</title><link>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate.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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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="background: SpringGreen;"&gt;FluentNH&lt;/span&gt;, IIdentifierGenerator, IPersistentIdentifierGenerator, &lt;span style="text-decoration: line-through; color: red;"&gt;TableGenerator&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;Mapping&lt;/span&gt;, &lt;span style="text-decoration: line-through; color: red;"&gt;FluentNH&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;TableGenerator&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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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><comments>http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate/comments.aspx</comments><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></channel></rss>
