I'm pretty sure you cannot do what you are asking, with the auto-"merge voodoo" of the Foo.Bars (thus my question in the comments of the question).
I think this is what you can do. Note, I've come up with something a little less generic than Foo and Bar.
I have a "Project". And an Employee. Each Employee can "own" or "work-on" a project (or both). Obviously, in the real world this would be dumb to force an employee to only own or work on a single project. But it works here.
The Project has (many) Owners and Workers. And I have a get(readonly get) property for Anyone-associated-with-the-project. That is where you merge the two distinct relationships.
I have no idea on the Lazy-Initialization with this. I'm just pointing that the auto-merge thing you're looking for.. probably isn't in the cards.
public class Project
{
public virtual int ProjectKey { get; set; }
public virtual string ProjectName { get; set; }
public virtual IList<Employee> ProjectOwners { get; set; }
public virtual IList<Employee> ProjectWorkers { get; set; }
public virtual IList<Employee> AnybodyAssociatedWithThisProject
{
get
{
List<Employee> returnItems = new List<Employee>();
if (null != this.ProjectOwners)
{
returnItems.AddRange(this.ProjectOwners);
}
if (null != this.ProjectOwners)
{
returnItems.AddRange(this.ProjectWorkers);
}
return returnItems;
}
}
}
public class Employee
{
public virtual int EmployeeKey { get; set; }
public virtual string SSN { get; set; }
public virtual Project OwnedProject { get; set; }
public virtual Project WorkedOnProject { get; set; }
}
public class ProjectMap : ClassMap<Project>
{
public ProjectMap()
{
Id(x => x.ProjectKey);
Map(x => x.ProjectName);
HasMany(x => x.ProjectOwners)
.KeyColumn("OwnedProjectKey");
HasMany(x => x.ProjectWorkers)
.KeyColumn("WorkedOnProjectKey");
Table("Project");
}
}
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Id(x => x.EmployeeKey);
Map(x => x.SSN);
References(x => x.OwnedProject).Column("OwnedProjectKey");
References(x => x.WorkedOnProject).Column("WorkedOnProjectKey");
Table("Employee");
}
}
I tested some data. Everything seems to work. The one caveat is that if an Employee "owns" and "works-on" a Project, my simple ".AddRange" merge code will duplicate that Employee. Other than that, it looks solid.