In my domain, an Employee and a Department have a one-to-many bidirectional relationship; to let the child Employee synchronize this, I have an 'internal' access field for the Set (Iesi for NHibernate) of Employees that are in the Department, which would otherwise be readonly public. Like so:
Department class:
protected internal ISet<Employee> _staff;
public virtual ReadOnlyCollection<Employee> Staff {
get { return new List<Employee>(_staff).AsReadOnly(); }
}
public virtual void AddStaff(Employee emp) {
emp.Department = this; }
}
Employee class:
private Department _department;
public virtual Department Department {
set {
// check valid value, etc.
value._staff.Add(this);
}
}
I made access in my (FNH) mapping AsField(Prefix.Underscore), but since I can't make the Department._staff field virtual NH is not happy. I guess I could make the field a virtual property and force feed it, but that feels like I'm making the domain class overly aware of persistence.
I'm learning both NH and FNH and I know I need a good primer in relationship mapping, but my primary question for this post is the logic in my domain classes:
1) Is this a good c# programming pattern for a readonly set in a biderectional relationship?
2) What is the best way to make this more usable to NHibernate?
Thanks for sharing!
Berryl