I am creating data model for two objects Employee and Department. Employee has a list of departments and Department has a list of employees.
class Employee{
private IList<Department> _departments;
public Employee()
{
_departments = new List<Department>();
}
public virtual ReadOnlyCollection<Department> Departments{
get {return new ReadOnlyCollection<Department>(_departments);}
}
}
class Department{
private IList<Employee> _employees;
public Department()
{
_departments = new List<Employee>();
}
public virtual ReadOnlyCollection<Employee> Employees{
get {return new ReadOnlyCollection<Employee>(_employees);}
}
}
How do I write AddEmployee method in Department class and AddDepartment method in Employee class to make it in sync with nHibernate? I wrote this in Employee class
public virtual void AddDepartment(Department department)
{
if (!department.Employees.Contains(this))
{
department.Employees.Add(this);
}
_departments.Add(department);
}
But it doesnt work as I expected it to work.Can someone help.
See Question&Answers more detail:os