Fluent NHibernate
Standard Mapping Components

If you haven't setup a Fluent NHibernate project before, you should checkout the GettingStarted: Introduction guide.

As mentioned in ClassMap, all mappings are done from inside the constructor of a ClassMap<T> derived class; so baring that in mind, all examples are going to exclude the surrounding class.

Simple component

// schema
AddressNumber int
AddressStreet varchar(100)

// model
public Address Address { get; set; }

public class Address
{
  public int Number { get; set; }
  public string Street { get; set; }
}

// mapping
Component(x => x.Address, c =>
{
  c.Map(x => x.Number);
  c.Map(x => x.Street);
});

In this example, the first lambda (x) is used for specifying which property on your model is a component, and the second lambda (c) is used for actually defining what the component is. When you're working inside the second lambda, a lot of the methods that are available with the ClassMap are able to be used with components.

Adding a reference to the component's parent

If you need to have a property in your component that references it's parent, you can use the WithParentReference method.

// model
public Address Address { get; set; }

public class Address
{
  public int Number { get; set; }
  public string Street { get; set; }
  public Person Resident { get; set; }
}

// mapping
Component(x => x.Address, c =>
{
  c.Map(x => x.Number);
  c.Map(x => x.Street);
  c.WithParentReference(x => x.Resident);
});