如何使用角色或组合来映射NHibernate中的类 [英] How to map classes in NHibernate using Roles or Composition

查看:62
本文介绍了如何使用角色或组合来映射NHibernate中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这是一个常见的问题,但是还没有找到一个简洁明了的答案.

I believe this is a common question / problem but have not been able to find a good clean concise answer.

问题

如何映射似乎具有继承关系的实体:

How to map entities that appear to have an inheritance relationship:

Company
  Supplier
  Manufacturer
  Customer

但是,供应商可以是制造商.

However, a Supplier can be a Manufacturer.

Person
  Doctor
  Patient
  Employee

患者可以是医生或雇员的情况.

Where a Patient can be a Doctor or can be an Employee.

建议:使用角色

在NHibernate论坛上的讨论中,通常会回答这是多重继承.

In discussions on the NHibernate forums, the response is often that this is multiple inheritance.

http://forum.hibernate.org/viewtopic.php?t=959076

他们建议的解决方案是使用成分或使用角色".但是,我找不到任何示例或说明如何完全做到这一点.

They solution suggested is to use composition or to use "Roles". However, I cannot find any examples or explanation on how exactly to do that.

偏重于继承的偏爱". 记得那个小东西 班级?在这种情况下,我必须 同意您尝试多个 继承-在C#或 Java(尚未).我个人会 鼓励你考虑 重新建模,所以你有一个人 对象和一个人有一对多 角色集合.

"Favor composition over inheritance." Remember that little goodie from class? In this instance I have to agree that you are trying multiple inheritance-- not possible in C# or Java (yet). I, personally, would encourage you to think about re-modeling so you have a Person object and a person has a one-to-many collection of Roles.

推荐答案

您可能想考虑使用角色.因此,角色将具有一组人.或一个人将具有一组角色或两者兼有.这可能意味着存在一个将人员映射到角色的Association类.

You probably want to consider using the Roles. So a Role will have a set of Persons. Or a Person will have a set of Roles or both. This would probably imply that there is an Association class that maps persons to roles.

定义一个Person类,该类具有人们共有的所有属性.然后定义一个角色超类和DoctorRole,PatientRole和EmployeeRole子类(假设每个角色具有不同的属性).

Define a Person class with all properties that are common to people. Then define a Role super class and DoctorRole, PatientRole and EmployeeRole sub classes (Assuming that each role has different properties).

Person类可以定义角色集合,而Role类可以定义人员集合.或者,创建关联类可能会更容易,将其称为PeopleRole.

The Person class can have a Collection of roles defined and the Role class can have a Collection of people defined. Or it might be easier to create an Association class, lets call it PeopleRole.

页面介绍了如何进行映射PeopleRole是一个复合元素.查看Order/Product/LineItem示例.您的人就像订单,PeopleRole就像LineItem,角色就像产品.

This page explains how to do the mapping so that PeopleRole is a composite element. Look at the Order/Product/LineItem example. Your Person is like Order, PeopleRole is like LineItem and Role is like Product.

这篇关于如何使用角色或组合来映射NHibernate中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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