Hussein Abbas
Hussein Abbas

Reputation: 712

How to map IDictionary that has a IList value?

Using NHibernate, how can I map the following scenario:

public class User
{
    public virtual IDictionary<Project, IList<Role>> ProjectAssignments { get; set; }
}

In the database I have separate tables for User, Project and Role and a fourth table for the relationship.

Any help would be appreciated.

Upvotes: 3

Views: 355

Answers (1)

Dave W
Dave W

Reputation: 427

You can't - from Ayende's blog discussing <map>

I would like to point out that I have still not covered all the options for <map/>, there are even more options (Although, IDictionary<K, IList<V >> is not something that is possible to map, for the problematic SQL statements that would be required to do so).

You'll need some intermediary entity/component. I'd probably have a ProjectAssignment entity to break up the nary association between User, Project and Role - it will probably grow extra attributes as time goes on (say you want to track changes to the assignments over time, so it gets StartDate and EndDate properties).

Something like:

public class User {
  // ... other attributes etc.
  public virtual ISet<ProjectAssignment> Assignments {get;set;}
}

public class Project {
  // ... other attributes etc.
  public virtual ISet<ProjectAssignment> Assignments {get;set;}
}

public class Role {
  // ... other attributes etc.
}

public class ProjectAssignment
{
  public virtual Int64 {get;set;}
  public virtual User User {get;set;}
  public virtual Project Project {get;set;}
  public virtual Role Role {get;set;}
  // ... other attributes etc.
}

I'd just map the ProjectAssignment as a persistent class in its own right, and the User.Assignments collections as a normal one-to-many of whatever flavour. Finally, I'd add a method to extract the details into a dictionary (or probably an ILookup if you're using Framework v3.5): something like:

ILookup<Project, Role> GetRolesByProject() {
    return Assignments.ToLookup(x => x.Project, x => x.Role);
}  

Upvotes: 1

Related Questions