Stefan is correct, use components. In FluentNHibernate, it will look like:
class Branch
{
string Name { get; set; }
DateTime DateOpened { get; set; }
}
class Employee
{
int Id { get; set; }
string Name { get; set; }
Branch Branch { get; set; }
}
class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.Id);
Map(x => x.Name);
Component<Branch>(x => x.Branch, c => {
c.Map(x => x.Name);
c.Map(x => x.DateOpened);
});
}
}
The Component<Branch>
can be shortened to just Component
since the compiler will figure out the type automatically. Also, what I like to do is provide an AsComponent
function on a class for my component so I don't have to repeat myself everywhere I map the component:
public static class BranchMap
{
public Action<ComponentPart<Branch>> AsComponent()
{
return c =>
{
c.Map(x => x.Name);
c.Map(x => x.DateOpened);
};
}
}
This also makes it easy to add in prefix/suffix functions for the cases where an entity may have more than one instance of a component with a different naming scheme.