<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicious.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tuna Toksoz : nhibernate</title><link>http://devlicious.com/blogs/tuna_toksoz/archive/tags/nhibernate/default.aspx</link><description>Tags: nhibernate</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>My Thoughts on ORM comparisons</title><link>http://devlicious.com/blogs/tuna_toksoz/archive/2009/08/21/my-thoughts-on-orm-comparisons.aspx</link><pubDate>Fri, 21 Aug 2009 16:27:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:50165</guid><dc:creator>Tuna Toksoz</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/rsscomments.aspx?PostID=50165</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/commentapi.aspx?PostID=50165</wfw:comment><comments>http://devlicious.com/blogs/tuna_toksoz/archive/2009/08/21/my-thoughts-on-orm-comparisons.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;UPDATE 1: I made clear that NH actually performs better in those posts, when a simple optimization such as Enabled Batching.&lt;br /&gt;UPDATE 2: Please check &lt;a href="http://ayende.com/Blog/archive/2009/08/22/nhibernate-perf-tricks.aspx"&gt;Ayende&amp;#39;s latest post&lt;/a&gt;&amp;nbsp;for how those benchmarks can be made fair.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;I am a bit late to the party but there we go.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Last several days, there have been a hot debate on the performance and capability of NH vs some other commercial/noncommercial ORMs. This issue has been discussed both in NHibernate Developer List and in &lt;a href="http://ayende.com"&gt;Ayende&amp;rsquo;s&lt;/a&gt; blog. I have recently noticed another benchmark between NH and EF, which I hope to address, too. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Fallacies of benchmark&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;What I have seen in those benchmarks is the wrong use of ORMs. This statement has two aspects: Wrong use of ORM and wrong use of NH itself.&lt;/p&gt;
&lt;p&gt;In ORM, you don&amp;rsquo;t use it to do bulk operations. And 0.001 second difference will not differ anything in your application. If it does, you don&amp;rsquo;t also use RDBMS, you use something else.&lt;/p&gt;
&lt;p&gt;There is no point on making a loop of thousands of iterations which saves an entity. &lt;a href="http://davybrion.com"&gt;Davy&lt;/a&gt; has well pointed out that this has a lot of implications in NHibernate, unlike some other ORMs. First of all, NHibernate first-level cache will be in action and it will fill the cache with each item saved. Secondly, as NH makes use of POCO it has no mechanism of things like INotifyPropertyChanged, it has to compare the old to the new value. As you may guess, the more entity you have, the longer it will take. No exception. If you want to leave first-level cache out of the game, you use &lt;a href="http://nhforge.org/blogs/nhibernate/archive/2008/10/30/bulk-data-operations-with-nhibernate-s-stateless-sessions.aspx"&gt;StatelessSession&lt;/a&gt; feature of NHibernate, no exception. Actually when you check &lt;a href="http://fabiomaulo.blogspot.com/2009/08/nhibernate-perfomance-cramer-vs-cramer.html"&gt;Fabio Maulo&amp;rsquo;s post&lt;/a&gt; you&amp;rsquo;ll see it can even become faster without Stateless Session.&lt;/p&gt;
&lt;p&gt;Another point I have noticed in both benchmarks is that they are/were not fair. If I compare it to EF, lets say, ctx.SaveChanges() will batch the chages and send the query to the database in one go. NH has this feature, as Ayende already pointed out, &lt;a href="http://ayende.com/Blog/2006/09/16/BatchingSupportInNHibernate.aspx"&gt;since 2006.&lt;/a&gt; However, NH by default doesn&amp;rsquo;t make use of it, and this is not a bug, it is a feature. You have to use setting to enable it. &lt;/p&gt;
&lt;p&gt;Gergely Orosz has made his own benchmark and didn&amp;#39;t use batching support and NH fell well behind of EF. I contacted him and he was kind enough to discuss the issues. I showed him how to enable batching and why not to flush everytime, as it wouldn&amp;#39;t be fair. Now NH performs much better and he said he will update his post.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Benchmark the community, too&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;NHibernate has a big community, and great minds as committers. It really does matter. Let me give you some statistics about NHibernate development and its community&lt;/p&gt;
&lt;p&gt;Here is the commit statistics.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/tuna_5F00_toksoz/image_5F00_1A86DDE0.png"&gt;&lt;img border="0" width="456" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/tuna_5F00_toksoz/image_5F00_thumb_5F00_4D766E47.png" alt="image" height="138" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" /&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It is one of the most active projects in .net environment. The image above doesn&amp;rsquo;t include the contrib projects. A great kiss goes&amp;nbsp; to &lt;a href="http://fabiomaulo.blogspot.com/"&gt;Fabio Maulo&lt;/a&gt; for his massive efforts.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s look at discussion group, shall we?&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/tuna_5F00_toksoz/image_5F00_0F99D6CB.png"&gt;&lt;img border="0" width="459" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/tuna_5F00_toksoz/image_5F00_thumb_5F00_3C4290A4.png" alt="image" height="108" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Do you know any other group with that activity? I don&amp;rsquo;t know.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In addition, NHibernate gives you more than _ANY_ ORM can give. It gives you a lot of extensibility points which can be used in a lot of scenario. Validator, Search, Spatial, Shards to name a few.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;NHibernate is the obvious winner.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicious.com/aggbug.aspx?PostID=50165" width="1" height="1"&gt;</description><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/nhibernate/default.aspx">nhibernate</category><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/benchmarking/default.aspx">benchmarking</category><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/orm/default.aspx">orm</category></item><item><title>NHibernate Linq Released</title><link>http://devlicious.com/blogs/tuna_toksoz/archive/2009/07/26/nhibernate-linq-released.aspx</link><pubDate>Sun, 26 Jul 2009 20:27:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:49565</guid><dc:creator>Tuna Toksoz</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/rsscomments.aspx?PostID=49565</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/commentapi.aspx?PostID=49565</wfw:comment><comments>http://devlicious.com/blogs/tuna_toksoz/archive/2009/07/26/nhibernate-linq-released.aspx#comments</comments><description>&lt;p&gt;Just as a reminder, even though you might have seen it before on twitter or &lt;a href="http://www.nhforge.org"&gt;nhforge&lt;/a&gt; or &lt;a href="http://nhforge.org"&gt;Ayende&amp;rsquo;s blog&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As of today, we released the first version of NHibernate.Linq. It is currently based on Criteria API, and can handle many situations that it can handle. &lt;/p&gt;
&lt;p&gt;NHibernate.Linq was the first project that I contribute as a committer (it was in Rhino Tools), it is a strange feeling to see its release.&lt;/p&gt;
&lt;p&gt;I would like to thank &lt;a href="http://ayende.com/blog"&gt;Oren Eini(Ayende),&lt;/a&gt; &lt;a href="http://chadly.net"&gt;Chad Lee&lt;/a&gt; and &lt;a href="http://www.primedigit.com/"&gt;Will Shaver&lt;/a&gt; for making this project come true.&lt;/p&gt;
&lt;p&gt;You can read the official announcement &lt;a href="http://nhforge.org/blogs/nhibernate/archive/2009/07/26/nhibernate-linq-1-0-ga-released.aspx"&gt;here on NHForge&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicious.com/aggbug.aspx?PostID=49565" width="1" height="1"&gt;</description><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/nhibernate/default.aspx">nhibernate</category><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/linq/default.aspx">linq</category></item><item><title>Querying on Child Count With NHibernate</title><link>http://devlicious.com/blogs/tuna_toksoz/archive/2009/07/10/querying-on-child-count-with-nhibernate.aspx</link><pubDate>Thu, 09 Jul 2009 23:23:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:49317</guid><dc:creator>Tuna Toksoz</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/rsscomments.aspx?PostID=49317</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/commentapi.aspx?PostID=49317</wfw:comment><comments>http://devlicious.com/blogs/tuna_toksoz/archive/2009/07/10/querying-on-child-count-with-nhibernate.aspx#comments</comments><description>&lt;p&gt;This is a recent question raised in &lt;a href="http://groups.google.com/group/nhusers" target="_blank"&gt;NHibernate Users Group&lt;/a&gt;. The user wanted to realize the following query with Criteria api.&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;var result = db.Person.Where(x =&amp;gt; x.Pets.Count &amp;gt; 0 &amp;amp;&amp;amp; x.Alive).OrderBy(x =&amp;gt; x.Name);&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is not a simple query, but it has a solution&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;  
DetachedCriteria crit = DetachedCriteria.For(typeof (Person), &amp;quot;p2&amp;quot;)
    .CreateCriteria(&amp;quot;p2.Pets&amp;quot;,&amp;quot;Pets&amp;quot;)
    .Add(Restrictions.EqProperty(&amp;quot;p.Id&amp;quot;, &amp;quot;p2.Id&amp;quot;))
    .SetProjection(Projections.Count(&amp;quot;Pets.Id&amp;quot;));

ICriteria c = s.CreateCriteria(typeof (Person), &amp;quot;p&amp;quot;)
    .Add(Restrictions.Gt(Projections.SubQuery(crit), 0))
    .Add(Restrictions.Eq(&amp;quot;p.Alive&amp;quot;,true))
    .AddOrder(Order.Asc(&amp;quot;p.Name&amp;quot;));&lt;/pre&gt;

&lt;p&gt;What we had to do is to create a DetachedCriteria and on that execute CreateCriteria so that we can do querying on our collection.&lt;/p&gt;

&lt;p&gt;The other way is simpler, but requires you to use HQL (below query is provided by &lt;a href="http://fabiomaulo.blogspot.com/" target="_blank"&gt;Fabio Maulo&lt;/a&gt;)&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;session.CreateQuery(&amp;quot;from Person p where  size(p.Pets) &amp;gt; 0 and p.Visible = true order by p.Name&amp;quot;)&lt;/pre&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;session.CreateQuery(&amp;quot;from Person p where  p.Pets.size &amp;gt; 0 and p.Visible = true order by p.Name&amp;quot;)&lt;/pre&gt;

&lt;p&gt;I hope this helps.&lt;/p&gt;

&lt;p&gt;&lt;a style="text-decoration:none;" href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fdevlicio.us%2fblogs%2ftuna_toksoz%2farchive%2f2009%2f07%2f10%2fquerying-on-child-count-with-nhibernate.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fdevlicio.us%2fblogs%2ftuna_toksoz%2farchive%2f2009%2f07%2f10%2fquerying-on-child-count-with-nhibernate.aspx&amp;amp;bgcolor=3333FF" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://dotnetshoutout.com/Querying-on-Child-Count-With-NHibernate-Tuna-Toksoz-Devlicious" rev="vote-for"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fdevlicio.us%2Fblogs%2Ftuna_toksoz%2Farchive%2F2009%2F07%2F10%2Fquerying-on-child-count-with-nhibernate.aspx" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicious.com/aggbug.aspx?PostID=49317" width="1" height="1"&gt;</description><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/nhibernate/default.aspx">nhibernate</category></item><item><title>Fluent Interface for NH Facility – Take 1</title><link>http://devlicious.com/blogs/tuna_toksoz/archive/2009/06/27/fluent-interface-for-nh-facility-take-1.aspx</link><pubDate>Sat, 27 Jun 2009 11:39:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:48777</guid><dc:creator>Tuna Toksoz</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/rsscomments.aspx?PostID=48777</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/commentapi.aspx?PostID=48777</wfw:comment><comments>http://devlicious.com/blogs/tuna_toksoz/archive/2009/06/27/fluent-interface-for-nh-facility-take-1.aspx#comments</comments><description>&lt;p&gt;We’ve been getting many requests on having fluent configuration for NHibernate Integration Facility, and as I like programmatic configuration more than XML configuration (did I mention that I hate XML?), I decided to work on it. After 2-3 hours, I got the below more or less working&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;container.Register(
Fluently.ConfigureFacility()
    .Id(&amp;quot;nhibernateFacility&amp;quot;)
    .DefaultConfigurationBuilder&amp;lt;DefaultConfigurationBuilder&amp;gt;()
    .DefaultConfigurationPersister&amp;lt;DefaultConfigurationPersister&amp;gt;()
    .AddFactory(
                    Fluently.ConfigureFactory()
                        .Alias(&amp;quot;myAlias&amp;quot;)
                        .Id(&amp;quot;myId&amp;quot;)
                        .UsingConfiguration(
                        FactoryConfigurator.DefaultBuilder()
                            .ConnectionProvider(&amp;quot;…………………………&amp;quot;)
                            .ConnectionDriver(&amp;quot;…………………………&amp;quot;)
                            .ConnectionString(&amp;quot;…………………………&amp;quot;)
                            .Dialect(&amp;quot;…………………………&amp;quot;)
                            .ProxyFactory(&amp;quot;…………………………&amp;quot;)
                            .Assemblies(&amp;quot;…………………………&amp;quot;)))
    .AddFactory(
                    Fluently.ConfigureFactory()
                        .Alias(&amp;quot;myAlias&amp;quot;)
                        .Id(&amp;quot;myId&amp;quot;)
                        .UsingConfiguration(
                        FactoryConfigurator.XmlBuilder().File(&amp;quot;myFile.xml&amp;quot;))));&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The pieces in Italic are not necessary to write as they have their defaults, also we have some generic overloads for things like Dialect, ProxyFactory and ConnectionProvider.&lt;/p&gt;

&lt;p&gt;What the above interface does is that it actually converts all the above configuration to IConfiguration and add them to the container as Facility Configuration, this is actually what is done behind the scenes when you use XML configuration. &lt;/p&gt;

&lt;p&gt;I asked for a review over the syntax from several tweeps (&lt;a href="http://twitter.com/kkozmic"&gt;@kkozmic&lt;/a&gt;, &lt;a href="http://twitter.com/dagda1"&gt;@dagda1&lt;/a&gt;, &lt;a href="http://twitter.com/mikehadlow"&gt;@mikehadlow&lt;/a&gt;, &lt;a href="http://twitter.com/chriscanal"&gt;@chriscanal&lt;/a&gt;) and from one other NH Facility user, German Schuager, and got great feedback.&lt;/p&gt;

&lt;p&gt;One of the issues that they pointed out with the above interface is that it is less discoverable. You have to find Fluently class, and then FactoryConfigurator class. Another issue is that it feels less natural to configure the facility like this. Instead, they prefer the configuration take place right on the facility.&lt;/p&gt;

&lt;p&gt;This one is what &lt;a href="http://blog.schuager.com"&gt;German Schuager&lt;/a&gt; has suggested&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;container.AddFacility&amp;lt;NHibernateFacility&amp;gt;(&amp;quot;nhibernateFacility&amp;quot;, cfg =&amp;gt; cfg
        .DefaultConfigurationBuilder&amp;lt;DefaultConfigurationBuilder&amp;gt;()
        .DefaultConfigurationPersister&amp;lt;DefaultConfigurationPersister&amp;gt;()
        .AddFactory(&amp;quot;id1&amp;quot;, f =&amp;gt; f
                .Alias(&amp;quot;myAlias&amp;quot;)
                .UsingConfiguration&amp;lt;DefaultBuilder&amp;gt;(c =&amp;gt; c
                        .ConnectionProvider(&amp;quot;………&amp;quot;)
                        .ConnectionDriver(&amp;quot;………&amp;quot;)
                        .ConnectionString(&amp;quot;………&amp;quot;)
                        .Dialect(&amp;quot;………&amp;quot;)
                        .ProxyFactory(&amp;quot;………&amp;quot;)
                        .Assemblies(&amp;quot;………&amp;quot;)
                ))
        .AddFactory(&amp;quot;id2&amp;quot;, f =&amp;gt; f
                .Alias(&amp;quot;alias&amp;quot;)
                .UsingConfiguration&amp;lt;XmlBuilder&amp;gt;(c =&amp;gt; c
                        .ReadFrom(&amp;quot;myfile.xml&amp;quot;)
                ))
 });&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and similar one from &lt;a href="http://kozmic.pl"&gt;Krzysztof Kozmic&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Looks like i was the only one that is fan of Castle Microkernel style Fluent Interface. &lt;/p&gt;

&lt;p&gt;Let’s see what I’ll come up for the second take, If I ever do it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicious.com/aggbug.aspx?PostID=48777" width="1" height="1"&gt;</description><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/nhibernate/default.aspx">nhibernate</category><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/castle/default.aspx">castle</category></item><item><title>Id Generation for db4o</title><link>http://devlicious.com/blogs/tuna_toksoz/archive/2009/05/18/id-generation-for-db4o.aspx</link><pubDate>Mon, 18 May 2009 16:55:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:46884</guid><dc:creator>Tuna Toksoz</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/rsscomments.aspx?PostID=46884</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/commentapi.aspx?PostID=46884</wfw:comment><comments>http://devlicious.com/blogs/tuna_toksoz/archive/2009/05/18/id-generation-for-db4o.aspx#comments</comments><description>&lt;p&gt;&lt;i&gt;&lt;b&gt;Disclaimer: This will be &lt;/b&gt;&lt;/i&gt;&lt;a href="http://www.tunatoksoz.com/post/Decoupled-design-with-events.aspx"&gt;&lt;i&gt;&lt;b&gt;another&lt;/b&gt;&lt;/i&gt;&lt;/a&gt;&lt;i&gt;&lt;b&gt;&amp;#160;&lt;/b&gt;&lt;/i&gt;&lt;a href="http://www.tunatoksoz.com/post/Some-tips-on-events.aspx"&gt;&lt;i&gt;&lt;b&gt;post&lt;/b&gt;&lt;/i&gt;&lt;/a&gt;&lt;i&gt;&lt;b&gt; on events and extensibility points of a framework, but I can’t resist!&lt;/b&gt;&lt;/i&gt; &lt;/p&gt;  &lt;p&gt;Recently, on one of my pet projects, I wanted to use db4o, and the problem was I should have explicit POIDs on my objects. Db4o has two mechanisms for Id stuff, one is GetId function, which is the physical address of the object on the disk and the second was the UUID that is used for replication purposes. The first one is fast but you can’t rely on it because the physical address may change after defragmentation, and the other is not very readable. This is the time I started to think of Id Generators for db4o. My friend &lt;a href="http://blog.darioquintana.com.ar/"&gt;Dario Quintana&lt;/a&gt; from &lt;a href="http://www.nhforge.org"&gt;NHibernate&lt;/a&gt; definitely had same needs(who doesn’t?) and as one having the right spirit, he implemented similar feature for db4o but this time with touching the codebase. My feature will use db4o Extensibility Point and will allow you to have several different Id Generators. I will use ideas from &lt;a href="http://devlicio.us/blogs/tuna_toksoz/archive/2009/03/20/nhibernate-poid-generators-revealed.aspx"&gt;NHibernate POID generators&lt;/a&gt; specifically HiLo algorithm.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;What is HiLo algorithm?&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;It is a nice algorithm that allows you to have ids without putting locks, thus slowing down the things. The algorithm works as follows.&lt;/p&gt;  &lt;p&gt;Each Generator requests a Hi value from database and with every request, this value is incremented. The generator then uses a magic formula (Hi-1)*Capacity+(++Lo) value. Lo values are generated by the generator and incremented with every new object saved into database. Hi value is stored in database while Lo value is stored in a variable in the client. When Lo becomes larger than Capacity (that is Lo values are exhausted), the generator requests a new Hi value from the database. As you see, we don’t issue many requests for Hi values thus there is practically no overhead on the database side. The larger the capacity, the less there is need to request for new id thus the less overhead. However, Hi Capacity means there will be ranges that will never ever be used, and people dislike it (why does it bother them is a question for me, too). Practically Int32 can handle values up to &lt;b&gt;2,147,483,647&lt;/b&gt; while Int64 can handle up to &lt;b&gt;9,223,372,036,854,775,807&lt;/b&gt; (no I could not say this number in spoken English but learnt that is is &lt;b&gt;nine quantillion&lt;/b&gt;). Having Capacity=32767 will allow you to have 281483566907400 Hi values and assuming you have 100 servers… wait it is obvious that you cannot consume all those in several million years?&lt;/p&gt;  &lt;p&gt;&lt;b&gt;How can we integrate it into db4o?&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Well, this is easy. Db4o provides us an event mechanism that can be used for such stuff. It has several events namely Creating, Created, Updating, Updated etc. All we have to do is to catch this event on the server side, and increment Hi values there. There is one thing to be careful about: Db4o events may fool you. In case of Client/Server mode you should call the below code on ServerContainer, otherwise you may have to use another ObjectClient as each object container runs in its own transaction. If you are using it as embedded database, then you won’t have any problem with it. &lt;/p&gt;  &lt;p&gt;Now, I define a contract called IIdGenerator&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;public interface IIdGenerator
{
    object Generate();
}
public interface IIdGenerator&amp;lt;T&amp;gt;:IIdGenerator
{
    new T Generate();
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Pretty self explanatory, isnt’ it? Now it comes to persistent id generator, which is for now the increment generator&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;public class IncrementGenerator:BaseIdGenerator&amp;lt;long&amp;gt;
{
    private readonly Type type;
    private readonly IObjectContainer container;
    private readonly string semaphoreName;
    public IncrementGenerator(Type type,IObjectContainer container)
    {
        this.type = type;
        this.container = container;
        this.semaphoreName = string.Format(&amp;quot;id_gen_{0}&amp;quot;, this.type.Name);
    }

    public override long Generate()
    {
        long valueToBeReturned;
        while (!container.Ext().SetSemaphore(semaphoreName, 1000)) ;//Do some busy wait
        IObjectSet set=this.container.QueryByExample(new IncrementTypeValuePair{Type=type});
        IncrementTypeValuePair pair;

        if (set.Count == 0)
            pair = new IncrementTypeValuePair {Type = type};
        else
            pair = set[0] as IncrementTypeValuePair;
        valueToBeReturned = ++pair.Value;
        container.Store(pair);
        container.Ext().ReleaseSemaphore(semaphoreName);
        return valueToBeReturned;
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;There we used Semaphore in order not to have concurrency issue, otherwise we could give very same id to different objects. This id generator will go to db everytime an id is requested, and it will create some bottleneck in case several hundreds of entities are inserted in a second. As an enhancement to this generator, we’ll inherit from this generator and create HiLoGenerator.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;[MethodImpl(MethodImplOptions.Synchronized)]
public override long Generate()
{
    if(currentLo&amp;gt;=capacity)
    {
        currentHi=base.Generate();
        currentLo = 0;
    }

    return (currentHi - 1)*capacity + (++currentLo);
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Exactly what I told in oral.&lt;/p&gt;

&lt;p&gt;As the last step, I designed an ugly fluent interface for this, which looks like the following&lt;/p&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;var serverContainer = this.server.Ext().ObjectContainer();
serverContainer.IdMap(Map&amp;lt;Person&amp;gt;.On(x =&amp;gt; x.Id)
         .SetGenerator(new HiLoGenerator(3, typeof(Person),serverContainer)));
var person = new Person();
container.Store(person);&lt;/pre&gt;

&lt;p&gt;The event wiring stuff is done a bit ugly too.&lt;/p&gt;

&lt;pre class="c-sharp" name="code"&gt;void registry_Creating(object sender, CancellableObjectEventArgs args)
{
    object item = args.Object;
    foreach (var map in idMaps)
    {
        if(map.EntityType.IsAssignableFrom(item.GetType()))
        {
            MemberExpression member = ((LambdaExpression)map.Expression).Body as MemberExpression;
            ((PropertyInfo) member.Member).SetValue(item, map.Generator.Generate(),null);
            break;
        }
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whole code can be found &lt;a href="http://tunatoksoz.com/files/Db4o.IdGenerators.rar"&gt;if you follow this link.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you liked this post, please kick it.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fdevlicio.us%2fblogs%2ftuna_toksoz%2farchive%2f2009%2f05%2f18%2fid-generation-for-db4o.aspx%23comments"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fdevlicio.us%2fblogs%2ftuna_toksoz%2farchive%2f2009%2f05%2f18%2fid-generation-for-db4o.aspx%23comments&amp;amp;bgcolor=3333FF" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://dotnetshoutout.com/Id-Generation-for-db4o-Tuna-Toksoz-Devlicious" rev="vote-for"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fdevlicio.us%2Fblogs%2Ftuna_toksoz%2Farchive%2F2009%2F05%2F18%2Fid-generation-for-db4o.aspx" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicious.com/aggbug.aspx?PostID=46884" width="1" height="1"&gt;</description><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/nhibernate/default.aspx">nhibernate</category><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/db4o/default.aspx">db4o</category></item><item><title>Castle NHibernate Facility – Configuration</title><link>http://devlicious.com/blogs/tuna_toksoz/archive/2009/04/06/castle-nhibernate-facility-configuration.aspx</link><pubDate>Mon, 06 Apr 2009 12:56:47 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:45392</guid><dc:creator>Tuna Toksoz</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/rsscomments.aspx?PostID=45392</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/commentapi.aspx?PostID=45392</wfw:comment><comments>http://devlicious.com/blogs/tuna_toksoz/archive/2009/04/06/castle-nhibernate-facility-configuration.aspx#comments</comments><description>&lt;p&gt;Castle’s extensibility points provide you many ways to make your life easier. One and most widely used extensibility point is the facilities. By using facilities, you can integrate various other frameworks and technologies easily. &lt;/p&gt; &lt;p&gt;I am going to talk about NHibernate Integration Facility, which is currently lead by me. It was originally written by Hamilton Verissimo, and its current shape is more or less the same as what he wrote. &lt;/p&gt; &lt;p&gt;The purpose of this post is to get feedback and shape the documentation accordingly.&lt;/p&gt; &lt;p&gt;Now, after an historical introduction, lets talk about the facility’s purpose, and in as the first part of the documentation, the configuration.&lt;/p&gt; &lt;p&gt;&lt;font size="3"&gt;&lt;strong&gt;The purpose &lt;br /&gt;&lt;/strong&gt;&lt;/font&gt;The purpose of this facility to provide an easy way to integrate NHibernate into MicroKernel backed applications. The facility provides a nice way to manage multiple session factories, a good way to manage sessions and it also provides some other structures that can be used to further integrate other frameworks such as Fluent NHibernate. It also plays nicely with other Castle services such as Transaction Management. &lt;br /&gt;&lt;/p&gt; &lt;p&gt;&lt;font size="3"&gt;&lt;br /&gt;&lt;strong&gt;The Configuration&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt;Currently, the facility can only be configured via XML, but it provides some extension points that can be used to configure it programmatically. The traditional way of configuring it is as given below.&amp;nbsp; &lt;/p&gt;&lt;pre class="xml" name="code"&gt;&amp;lt;configuration&amp;gt;
    &amp;lt;facilities&amp;gt;
        &amp;lt;facility
          id=&amp;quot;nhibernatefacility&amp;quot;
          type=&amp;quot;Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration&amp;quot;
          [optional: configurationBuilder=&amp;quot;Your custom configuration builder&amp;quot;] [optional: isWeb=&amp;quot;Your custom configuration builder&amp;quot;]&amp;gt;
            &amp;lt;factory id=&amp;quot;sessionFactory1&amp;quot;&amp;gt;
                &amp;lt;settings&amp;gt;
                    &amp;lt;item key=&amp;quot;connection.provider&amp;quot;&amp;gt;NHibernate.Connection.DriverConnectionProvider&amp;lt;/item&amp;gt;
                    &amp;lt;item key=&amp;quot;connection.driver_class&amp;quot;&amp;gt;NHibernate.Driver.SqlClientDriver&amp;lt;/item&amp;gt;
                    &amp;lt;item key=&amp;quot;connection.connection_string&amp;quot;&amp;gt;Data Source=.;Initial Catalog=test;Integrated Security=SSPI&amp;lt;/item&amp;gt;
                    &amp;lt;item key=&amp;quot;dialect&amp;quot;&amp;gt;NHibernate.Dialect.MsSql2000Dialect&amp;lt;/item&amp;gt;
                    &amp;lt;item key=&amp;quot;proxyfactory.factory_class&amp;quot;&amp;gt;NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle&amp;lt;/item&amp;gt;
                &amp;lt;/settings&amp;gt;
                &amp;lt;assemblies&amp;gt;
                    &amp;lt;assembly&amp;gt;YourAssembly.Name.Here&amp;lt;/assembly&amp;gt;
                &amp;lt;/assemblies&amp;gt;
            &amp;lt;/factory&amp;gt;
        &amp;lt;/facility&amp;gt;
    &amp;lt;/facilities&amp;gt;
&amp;lt;/configuration&amp;gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;isWeb:&lt;/strong&gt; In case of web application, you should set this to true, implement IContainerAccessor in your HttpApplication (the global.asax) also add following lines to your &amp;lt;httpModules&amp;gt; section. &lt;/p&gt;&lt;pre name="code" class="xml"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="xml" name="code"&gt;&amp;lt;httpModules&amp;gt;
    &amp;lt;add name=&amp;quot;NHibernateSessionWebModule&amp;quot;
             type=&amp;quot;Castle.Facilities.NHibernateIntegration.Components.SessionWebModule, Castle.Facilities.NHibernateIntegration&amp;quot;/&amp;gt;
&amp;lt;/httpModules&amp;gt;&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;As it can bee seen here, it is not very much different from the way we configure NHibernate. What happens behind the scenes are that: Facility creates an NHibernate Configuration, registers the SessionManager, and make those ready to be resolved. &lt;br /&gt;&lt;br /&gt;The facility provides you some way to modify the Configuration that is created behind the scenes. The point is called &lt;strong&gt;IConfigurationContributor&lt;/strong&gt;. By implementing classes that derive from that interface and registering them to container, you’ll be able to modify the NHibernate Configuration, just before the SessionFactory is resolved. The interface is simple &lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;public interface IConfigurationContributor
{
    void Process(string name,Configuration config);
}&lt;/pre&gt;
&lt;p&gt;The name corresponds to the id in the configuration. &lt;br /&gt;&lt;br /&gt;You can also Configure NHibernate Configuration via the IConfigurationBuilder interface. Currently, there are 2 built in ConfigurationBuilders, namely DefaultConfigurationBuilder and XmlConfigurationBuilder. &lt;br /&gt;The default one is the one that parses the thing you see above while XmlConfigurationBuilder uses NH’s native xmls. &lt;br /&gt;&lt;br /&gt;In the next post, I will hopefully show to integrate FluentNHibernate using custom ConfigurationBuilder &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicious.com/aggbug.aspx?PostID=45392" width="1" height="1"&gt;</description><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/nhibernate/default.aspx">nhibernate</category><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/castle/default.aspx">castle</category></item><item><title>An improvement on SessionFactory Initialization</title><link>http://devlicious.com/blogs/tuna_toksoz/archive/2009/03/14/an-improvement-on-sessionfactory-initialization.aspx</link><pubDate>Fri, 13 Mar 2009 22:40:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:44962</guid><dc:creator>Tuna Toksoz</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/rsscomments.aspx?PostID=44962</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/tuna_toksoz/commentapi.aspx?PostID=44962</wfw:comment><comments>http://devlicious.com/blogs/tuna_toksoz/archive/2009/03/14/an-improvement-on-sessionfactory-initialization.aspx#comments</comments><description>&lt;p&gt;&lt;b&gt;UPDATE: I have just committed the PersistentConfigurationBuilder for Castle NHibernate Facility. Thank you Jonathon Rossi for informing me!&lt;/b&gt;     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;We have received several complaints about slowness of SessionFactory initialization when there’s hundreds of entities, and &lt;a href="http://ayende.com/Blog/" target="_blank"&gt;Ayende&lt;/a&gt; has replied &lt;a href="http://ayende.com/Blog/archive/2007/10/26/Real-World-NHibernate-Reducing-startup-times-for-large-amount-of.aspx" target="_blank"&gt;one of them here&lt;/a&gt;. It even gets worse if you’re using it in a web environment. You may think that it is not a problem since SessionFactory is initialized once in a web environment, but the major impact is not on production but development. Think how many times you start your application a day. &lt;/p&gt;  &lt;p&gt;The problem is not really with NHibernate but with &lt;b&gt;xml validation&lt;/b&gt; against the schema. Here are some profiler results for SessionFactory initialization with one thousand entities:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;img alt="image" src="http://www.tunatoksoz.com/image.axd?picture=nhibernate_nonserialized.png" width="702" height="214" /&gt;&lt;/p&gt;  &lt;p&gt;As you see, the adding XML resources takes the most time and the reason behind this is the schema validation. There is also an I/O cost involved (1040 resources should be read by NHibernate). There are several ways to get rid of it, one being the &lt;b&gt;serialization of configuration&lt;/b&gt;. I spend 3 days (statics prevented me from spotting some bugs in the code) on this and I believe it pretty much works for every configuration. Another way of doing this is the merging of HBM files, which I believe faster than Serialization as Deserialization also takes some amount.&lt;/p&gt;  &lt;p&gt;Now the results for the one using the Deserialized Configuration.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;img alt="image" src="http://www.tunatoksoz.com/image.axd?picture=nhibernate_serialized.png" /&gt;&lt;/p&gt;  &lt;p&gt;A nice feature of dotTrace allows us to compare the performance improvements over the old way.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;img alt="image" src="http://www.tunatoksoz.com/image.axd?picture=nhibernate_diff.png" /&gt;&lt;/p&gt;  &lt;p&gt;We got &lt;b&gt;10 seconds rescued!&lt;/b&gt; Yay!&lt;/p&gt;  &lt;p&gt;Now I am going to show how I used this feature in Castle NHibernate Facility. We have IConfigurationBuilder that is used to integrate various Configuration sources (such as FluentNHibernate). &lt;/p&gt;  &lt;p&gt;First of all I must ensure that if any of the files that are used to create the Configuration change, we shouldn’t use the serialized configuration, instead the Configuration should be re-created. &lt;/p&gt; &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;  &lt;pre class="c-sharp" name="code"&gt;public override Configuration GetConfiguration(IConfiguration config)
{
    log.Debug(&amp;quot;Building the Configuration&amp;quot;);

    string fileName = config.Attributes[&amp;quot;fileName&amp;quot;];

    IConfiguration dependsOn = config.Children[&amp;quot;dependsOn&amp;quot;];
    IList&amp;lt;string&amp;gt; list = new List&amp;lt;string&amp;gt;();

    foreach (var on in dependsOn.Children)
        list.Add(on.Value);

    Configuration cfg;
    if (IsNewConfigurationRequired(fileName, list))
    {
        log.Debug(&amp;quot;Configuration is either old or some of the dependencies have changed&amp;quot;);
        using(var fileStream = new FileStream(fileName, FileMode.OpenOrCreate))
        {
            cfg = base.GetConfiguration(config);
            this.WriteConfigurationToStream(fileStream, cfg);
        }
    }
    else
    {
        using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate))
        {
            cfg = this.GetConfigurationFromStream(fileStream);
        }
    }
    return cfg;
}



protected virtual bool IsNewConfigurationRequired(string fileName,IList&amp;lt;string&amp;gt; dependencies)
{
    if (!File.Exists(fileName))
        return true;
    FileInfo fi = new FileInfo(fileName);
    DateTime lastModified = fi.LastWriteTime;
    bool requiresNew=false;
    for (int i = 0; i &amp;lt; dependencies.Count &amp;amp;&amp;amp; !requiresNew; i++)
    {
        FileInfo dependency = new FileInfo(dependencies);
        DateTime dependencyLastModified = dependency.LastWriteTime;
        requiresNew |= dependencyLastModified &amp;gt; lastModified;
    }
    return requiresNew;
}&lt;/pre&gt;

&lt;p&gt;Code doesn’t look really good, I guess, so I am open to any suggestions on improvement. The code is not yet in Castle Codebase, as our NH dependency on trunk is not the latest (and i am too lazy to update it). When I find time, I may update the dependency if others agree. 
  &lt;br /&gt;

  &lt;br /&gt;&lt;b&gt;There is one thing that you have to be careful about. You must be aware that if you’re using IUserType, IInterceptor, ISqlFunction etc, all of those should be Serializable too!&lt;/b&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicious.com/aggbug.aspx?PostID=44962" width="1" height="1"&gt;</description><category domain="http://devlicious.com/blogs/tuna_toksoz/archive/tags/nhibernate/default.aspx">nhibernate</category></item></channel></rss>