How to map classes in NHibernate using roles or composition

I believe this is a general question / problem, but he could not find a good clean short answer.

Problem

How to match objects that appear to have an inheritance relationship:

Company Supplier Manufacturer Customer 

However, the Supplier may be a Manufacturer.

or

 Person Doctor Patient Employee 

If the patient may be a Doctor or may be an Employee.

Suggestion: Using Roles

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

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

They suggest using a composition or using Roles. However, I cannot find any examples or explanations on how to do this.

"Pay for composition over inheritance." Remember that little benefit from the class? In this case, I have to agree that you are trying to somewhat inherit - it is not possible in C # or Java (yet). I personally encourage you to think about re-modeling, so that you have a Personality object, and the person has a one-to-many collection of roles.

+6
c # nhibernate
source share
3 answers

You probably want to use roles. Thus, Role will have a set of people. Or Man will have a set of Roles, or both. This probably implies that there is an Association class that maps people to roles.

Define a Person class with all properties that are common to humans. Then define the superclass classes Role and DoctorRole, PatientRole and EmployeeRole (suppose each role has different properties).

The Person class can have a specific collection of roles, and the Role class can have a specific collection of people. Or it would be easier to create an Association class, let's call it PeopleRole.

This page explains how to do the mapping so that PeopleRole is an integral element. See the example Order / Product / LineItem. Your character is similar to Order, PeopleRole is similar to LineItem, and Role is similar to Product.

+4
source share

It seems to me that this is more a question of how to correctly model a domain, and not a NHibernate mapping problem.

After you sort your domain modeling, I think you will find that NHibernate mapping falls out relatively easily.

In one place, to look at the idea of ​​role modeling, you need to look for " Color Modeling " - Jeff de Luca has some resources , although the idea came about with Peter Coad

The main idea is to separate the participant’s personality from the role they play in their activities.

For example, you might have a Person object that captures the identity of a particular person.

Then, a completely separate object "Student", which captures additional information for recording the registration of a person as a student. Each instance of the Student would have a link to the person registered. One person can be associated with many "student" entries, one for each individual enrollment.

In parallel, you can have a separate “Tutor” object that records employment data when someone is hired to teach students in one-on-one situations. The Tutor object captures additional information about how someone is being used as a teacher.

This structure gives you great flexibility - one person (Joe Bloggs) can just be a student, another person (Jane Dow) can just be a mentor, and a third (Chuck Norris) can be both.

In addition, the introduction of another role (Lecturer, Marker, Administrator) becomes easier, because add-ons do not require changes to existing objects.

+3
source share

I came across a few more comments that may come in handy:

On the Naked Objects blog blog, several different approaches are outlined that discuss the pros and cons of each.

  • Using association associations "Any"
  • Modeling roles as classes
+1
source share

All Articles