Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Fluent NHibernate Has a Wiki

Fluent NHibernate Has a Wiki

This item in japanese


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.

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

Community comments

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

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