NHibernate, the original and better than LINQ to SQL.. for now?
My first pure LINQ to SQL (L2S) greenfields project proved to be quite the headache (reasons for my headache)! I’ve decided for my next personal project to use NHibernate instead.
The reason I found L2S so attractive was that the designer did all the heavy lifting for me and generated all the code. I realise now that L2S is just Microsoft’s version of NHibernate, and that most of the things I didn’t like with L2S aren’t an issue with NHibernate as long as you are happy to customise a code generator.
Code Generation
MyGeneration - open source code generation template-based software. Is as good as the template, but the results are pretty good.
http://www.mygenerationsoftware.com/
I’m currently using “NHibernate Sharp 1.4” template. I like it because it generates the hbm.xml files, the strongly typed data row class, AND an interfaces for the typed classes!
The one issue is, if you are using the current version of NHibernate (1.2.1.GA), you will need to set the generate to enable option “Use NH 2.2 Mapping” in the generator, or change the hbm.xml files from
xmlns="urn:nhibernate-mapping-2.0"
to
xmlns="urn:nhibernate-mapping-2.2"
NHibernate
I installed the latest stable release (1.2.1.GA), but I’m wondering what the major differences are for 2.0.Alpha?
I followed the essence of the quick start guide and had very few problems apart from:
* If you’re not using SQL Server 2000, you will need to change the “nhibernate” section of your .config file to match your database. Database guide here.
* When you are adding assemblies to the config to point at the assembly that includes the mapping files, eg.
cfg.AddAssembly("myAssembly");
the string you use in that function is NOT the namespace but the following part, in bold, found in the hbm.xml files.
<class name="NHibernate.Examples.QuickStart.User,
NHibernate.Examples" table="users">
I’ll have more to say on this when I get a chance to use it more thoroughly, but so far, so good. Whe LINQ to SQL version 2 comes out, I’m sure it’s going to up the ante considerably and be a real contender, but for now, I’m throwing in the towel.
Added:
I also prefer to use Int32 for versioning instead of timestamp as it is recommended by NHibernate themselves and it is more human-friendly. So I perform this Find-And-Replace to change the generated code to what I want from:
<timestamp generated="never" unsaved-value="undefined"
name="Version" column="[Version]" />
to:
<version generated="never" unsaved-value="0"
name="Version" column="[Version]" />
Also, if you manually add your files to your solution you need to make sure that all of the type .hbm.xml have Build Action=”Embedded Resource” in their properties or else when you call cfg.AddAssembly(), it won’t be able to find the files.