Fluent NHibernate Has a Wiki

by Jon Arild Tørresdal on Feb 09, 2009 |

Fluent NHibernate (previously covered by InfoQ) is an alternative to using XML mappings in NHibernate. Fluent NHibernate is using a fluent interface allowing one to define mappings in code instead of XML. Some people in the community have complained about the lack of documentation for Fluent NHibernate and as a response James Gregory recently announced the official Wiki for Fluent NHibernate.

 Examples of documentation found in the Wiki are:

The Wiki shows a typical mapping scenario with both XML and Fluent NHibernate. Using XML it would look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="QuickStart" assembly="QuickStart">
<class name="Cat" table="Cat">
<id name="Id">
<generator class="identity" />
<property name="Name">
<column name="Name" length="16" not-null="true" />
<property name="Sex" />
<many-to-one name="Mate" />
<bag name="Kittens">
<key column="mother_id"/>
<one-to-many class="Cat"/>

 The same mapping using Fluent NHibernate would look like this:

public class CatMap : ClassMap  
public CatMap()
Id(x => x.Id);
Map(x => x.Name)
Map(x => x.Sex);
References(x => x.Mate);
HasMany(x => x.Kittens);

They continue by saying:

While the separation of code and XML is nice, it can lead to several undesirable situations.

They then list some examples:

  • Due to XML not being evaluated by the compiler, you can rename properties in your classes that aren't updated in your mappings; in this situation, you wouldn't find out about the breakage until the mappings are parsed at runtime.
  • XML is verbose; NHibernate has gradually reduced the mandatory XML elements, but you still can't escape the verbosity of XML.
  • Repetitive mappings - NHibernate HBM mappings can become quite verbose if you find yourself specifying the same rules over again. For example if you need to ensure all string properties must be not-null, and have a length of 1000, and all ints must have a default value of -1.

In August last year, Oren Eini (a.k.a. Ayende Rahien), pointed out that Fluent NHibernate didn’t add any value because there had to be a mapping class per entity. Since then however, the project has evolved and they now have the concept of Auto Mapping, which is what Ayende was asking for. The Auto Mapping feature uses a set of conventions to automatically map all entities, not requesting a mapping class per entity.

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Programmatic Configuration (Fluent APIs) is the way to go! by Sergio Oliveira

Since 2005 I am a supporter of the many advantages that a Fluent API has when compared to annotation or XML.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss
General Feedback
Marketing and all content copyright © 2006-2016 C4Media Inc. hosted at Contegix, the best ISP we've ever worked with.
Privacy policy

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.