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).
-
-
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