Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
A Few NHibernate Tips

I've been updating the documentation for S#arp Architecture and thought I'd share the tips I'm including for NHibernate.  (When referenced below, "NRD" stands for NHibernate Reference Documentation.)

General Mapping File Tips

  • For organization purposes, only declare one class per mapping file.
  • For consistency and better organization, regardless of using HBMs or Fluent NHibernate, keep the mapping artifacts in your data layer project, e.g., MyProject.Data.
  • Don’t bother including database meta data (e.g., column length) in mapping files unless intending to auto generate the SQL using the hbm2dll tool.
  • If using HBMs, set the assembly and namespace for the <hibernate-mapping> tag to decrease typing and ease future maintenance.

Associations

  • Set .Inverse(), or inverse=true if you’re using HBMs, on the parent in a bi-directional parent/child relationship to avoid foreign key constraint violations while making the relationship easy to maintain (NRD 6.8 Bidirectional Associations).  But note that large bags mapped with this setting are inefficient and should be avoided (NRD 6.2 Mapping a Collection).
  • When mapping many-to-many relationships, use an <idbag> for much increased performance (NRD 6.7 Using an <idbag>).
  • Remember that there is a difference between <bag> and <list> (list having an index property) and should be used appropriately; either may be mapped to a generic IList<> (NRD 6.2 Mapping a Collection).

Mapping Strategies

  • While there are a few inheritance-mapping strategies to choose from, favor the table-per-class-hierarchy mapping strategy but consider your needs carefully, as well (NRD 5.1.13 subclass).
  • If you’re dealing with money, create a Money class and an associated ICompositeUserType for the monetary amount to bind to it, e.g., here.  Storing money as a decimal in your domain layer is quite limiting.

This list isn't comprehensive by any means, but hopefully you'll find a couple of useful tidbits.

Billy McCafferty


Posted 03-07-2009 4:16 PM by Billy McCafferty
Filed under:

[Advertisement]

Comments

DotNetShoutout wrote A Few NHibernate Tips - Billy McCafferty
on 03-07-2009 6:37 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Tuna Toksoz wrote re: A Few NHibernate Tips
on 03-08-2009 5:31 AM

I'll add one few more.

Favor ORM style generators (hilo, guid.comb).

generator="native" or generator="identity" nullifies batching and breaks UoW. In this case, rollback may not work and explicit delete is required. It also doesn't batch several statements.

DotNetKicks.com wrote A Few NHibernate Tips
on 03-08-2009 5:33 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Tuna Toksoz wrote re: A Few NHibernate Tips
on 03-08-2009 5:38 AM

One another is, it improves startup performance to merge mapping xmls. The reason can be read here

ayende.com/.../Real-World-NHibernate-Reducing-startup-times-for-large-amount-of.aspx .

A new feature in NHibernate for this bottleneck is on the way. Basically it will make it possible to serialize Mappig metadata(things produced when an hbm is parsed), so that no schema validation will be necessary.

Marek Blotny wrote re: A Few NHibernate Tips
on 03-08-2009 9:31 AM

Hi Billy,

I have one question, you wrote "Don’t bother including database meta data (e.g., column length) in mapping files unless intending to auto generate the SQL using the hbm2dll tool."

Let's say that I don't use hbm2dll, why do you think that this effort won't pay off?

PS. Interesting post!

Arjan`s World » LINKBLOG for March 8, 2009 wrote Arjan`s World &raquo; LINKBLOG for March 8, 2009
on 03-08-2009 2:34 PM

Pingback from  Arjan`s World    &raquo; LINKBLOG for March 8, 2009

Billy McCafferty wrote re: A Few NHibernate Tips
on 03-10-2009 6:02 PM

Marek,

At the risk of being laconic, why do you think the effort *will* pay off if you're not using any kind of SQL generator based on the mappings?

Marek Blotny wrote re: A Few NHibernate Tips
on 03-11-2009 9:29 AM

Billy,

I think my question wasn't clear, I was assuming that NHibernate will use column length not only to generate SQL but also to somehow validate data. But apparently it was a wrong assumption, I have done some research and I know that now, you are right that including database meta data doesn't make much sense.

Fabio Maulo wrote re: A Few NHibernate Tips
on 03-24-2009 12:20 AM

Only to say that references for 2.0.0 are here

nhforge.org/.../index.html

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)