0

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.

Cole W
  • 15,123
  • 6
  • 51
  • 85
8GB
  • 79
  • 3
  • 13
  • I have posted an example below on how I handle these scenarios but why does your code not work as expected? What are you expecting? – Cole W Apr 11 '12 at 01:51
  • It is resulting in circular dependancies when am trying to save this like db.session.Save(Employee); It resuts in Employee having departments and that departments hasve employees within and so on.. It is circular. I am not sure if that is correct.Can someone correct me if am wrong? – 8GB Apr 11 '12 at 13:58

1 Answers1

1

This is an example of how I handle these relationships:

public class User
{
    private IList<Group> groups;
    public virtual IEnumerable<Group> Groups { get { return groups.Select(x => x); } }

    public virtual void AddGroup(Group group)
    {
        if (this.groups.Contains(group))
            return;

        this.groups.Add(group);
        group.AddUser(this);
    }

    public virtual void RemoveGroup(Group group)
    {
        if (!this.groups.Contains(group))
            return;

        this.groups.Remove(group);
        group.RemoveUser(this);
    }
}

My User mapping looks like this:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        //Id, Table etc have been omitted

        HasManyToMany(x => x.Groups)
            .Table("USER_GROUP_COMPOSITE")
            .ParentKeyColumn("USER_ID")
            .ChildKeyColumn("GROUP_ID")
            .Access.CamelCaseField()
            .Cascade.SaveUpdate()
            .Inverse()
            .FetchType.Join();
    }
 }

My Group mapping looks like this:

public class GroupMap : ClassMap<Group>
{
    public GroupMap()
    {
        //Id, Table etc have been omitted

        HasManyToMany(x => x.Users)
            .Table("USER_GROUP_COMPOSITE")
            .ParentKeyColumn("GROUP_ID")
            .ChildKeyColumn("USER_ID")
            .Access.CamelCaseField();
    }
}
Cole W
  • 15,123
  • 6
  • 51
  • 85
  • I do it exactly the same way, at least that separates the boilerplate code from the application code and gives you a single point of failure. You might even Add convenience methods (Add, Remove) to the Group-Class for removing users. – Sebastian Edelmeier Apr 16 '12 at 09:04
  • @SebastianEdelmeier, There are methods in that class to do that. Both the add and remove call those methods in `Group` in the above code. – Cole W Apr 16 '12 at 11:32