如何映射具有IList值的IDictionary? [英] How to map IDictionary that has a IList value?

查看:86
本文介绍了如何映射具有IList值的IDictionary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用NHibernate,我如何映射以下场景:

Using NHibernate, how can I map the following scenario:

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

在数据库中,我有分别用于UserProjectRole的表,以及用于关系的第四张表.

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

任何帮助将不胜感激.

推荐答案

您不能-来自

我想指出的是,我仍然没有涵盖< map/>的所有选项, 还有更多选择(尽管IDictionary< K,IList< V >>不是 对于可能需要映射的有问题的SQL语句,可以进行映射.

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).

您将需要一些中介实体/组件.我可能会有一个ProjectAssignment实体来打破用户,项目和角色之间的纽带关系-随着时间的流逝,它可能会增加额外的属性(例如您想跟踪随时间变化的分配,因此得到StartDateEndDate属性).

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).

类似的东西:

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.
}

我只是将ProjectAssignment本身映射为持久性类,将User.Assignments集合映射为正常的一对多类型.最后,我将添加一种将详细信息提取到字典中的方法(如果使用的是Framework v3.5,则可能是ILookup):

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);
}  

这篇关于如何映射具有IList值的IDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆