Fluent NHibernate Wiki
Im>JamesGregory
No edit summary
Im>JamesGregory
mNo edit summary
Line 1: Line 1:
  +
_TOC_
  +
 
Fluent mapping is the de-facto mapping style that Fluent NHibernate uses. It's a [http://martinfowler.com/bliki/FluentInterface.html fluent interface] that allows you to map your entities completely in code, with all the compile-time safety and refactorability that brings.
 
Fluent mapping is the de-facto mapping style that Fluent NHibernate uses. It's a [http://martinfowler.com/bliki/FluentInterface.html fluent interface] that allows you to map your entities completely in code, with all the compile-time safety and refactorability that brings.
   

Revision as of 09:11, 31 July 2009

_TOC_

Fluent mapping is the de-facto mapping style that Fluent NHibernate uses. It's a fluent interface that allows you to map your entities completely in code, with all the compile-time safety and refactorability that brings.

The getting started guide has a good introduction to mapping with the fluent interface. Once you've read that then you should be able to find a bit more detail here.

ClassMap

ClassMap<T> is the basis of all your mappings, you derive from this to map anything.

public class PersonMap : ClassMap<Person>
{
  public PersonMap()
  {
  }
}

Inside the constructor is where you map the various properties of your entity.

Mappings

Every mapping inside a ClassMap<T> is built using lambda expressions, which allow us to reference the properties on your entities without sacrificing compile-time safety. The lambdas typically take the form of x => x.Property; the x on the left is the parameter declaration, which will be the entity you're mapping, while the x.Property is accessing a property on your entity (coincidently called Property in this case). You'll quickly get used to these lambdas, as they're used everywhere in Fluent NHibernate.

Once you've declared your ClassMap<T> you're going to need to map the properties on your entity. There are several methods available that map your properties in different ways, and each one of those is a method chain that you can use to customise the individual mapping.

Id

Every mapping requires an Id of some kind, these are mapped using the Id method; this method takes a lambda expression of the property you want to use as your Id. Depending on the return type of the property you supply, Fluent NHibernate will make some assumptions about the kind of identifier you're using. For example, if your Id property is an int, then we assume you're using an identity column; similarly, if you use a Guid then we assume it's a Guid Comb.

Id(x => x.Id);

That's the most common scenario for mapping your Id. Customisations can be done by chaining methods off the Id call. For example, if we were to need to change what column the property maps to we could use the Column method, or for explicitly specifying the identity generator you could use the GeneratedBy property.

Id(x => x.Id)
  .Column("PersonId")
  .GeneratedBy.Assigned();

In this example we're specifying that the Id property is mapped to a PersonId column in the database, and it's using an assigned generator.

Fluent NHibernate's interface is designed for discoverability. Everything you need should be easy to find "under" the declaring method using method chains.

You can read more about mapping identities in the [[Fluent mapping identities|identities page].

Properties

Property mappings make up a large amount of any mapped domain, so it's best that you know how to map them. They're just as simple as identities, except we use the Map method.

Map(x => x.FirstName);