简体   繁体   中英

Relationship with another Entity when using inheritance on parent class or on child classes

I have the following inheritance. A doctor is a parent class. A doctor can be a paediatrician, a dentist or an orthopaedic. Each doctor type have different properties (different domain logic) but they all share the same Id, Name and they also all can have many appointments.

public abstract class Doctor
{
    public long Id { get; set; }

    public string Name { get; set; }
}

public class Paediatrician : Doctor
{
    // add some other properties specific only to Paediatrician.
}

public class Orthopaedic : Doctor
{
    // add some other properties specific only to Orthopaedic.
}

public class Dentist : Doctor
{
    // add some other properties specific only to Dentist.
} 

public class Appointment
{
    public long Id { get; set; }
    public DateTime DateOfAppointment { get; set; }
}

public class Country
{
    public long Id { get; set; }
    public string Name { get; set; }
}

There is a one to many relationship here (a doctor can have many appointments). Question here is where do I add the Appointment relationship? Inside each child class or on the parent class? I have to set this relationship to my ORM so adding this relationship into each child class would produce three different relationships into my db whereas if I add it to the parent class only one? The same stands for Country table. Where do I add Country relationship? Only on the parent class or child classes?
One of the systems requirement that might affect my selection is that I have is to show a list view with the name of the doctor and the appointment date. So there are two ways of doing this. Either by calling each child class and aggregate the results or by calling parent class directly and get the appointments if I add the Appointment relationship to the parent class. I am stuck and don't know what is the correct way of doing this fundamentally.

The question is not specific for C#.Net or entity framework, but it's generally about object relationships.

The first point is inheritance. A derived class inherits all public and protected properties (and has access to methods with the same qualifiers) from the base class. Ie A Dentist will also have an Id and a Name , although you don't declare them in the class. They are inherited from Doctor .

The second point is object relations. Clearly a Dentist can make an Appointment and so can a Paediatrician . And both are a type of Doctor . As you state, there's a one-to-many relationship. The "many" is the Appointment , but what do you want the "one" to be? Of course you can choose it to be Dentist or Paediatrician . But you'll have to make an appointment a generic class

    public class Appointment<T> where T : Doctor
    {
        [Key]
        public long Id { get; set; }
        [Required]
        public DateTime DateOfAppointment { get; set; }
        [Required]
        public T DoctorNavigation { get; set; }
    }

Each derived class would then have its own appointment list

    public class Dentist : Doctor
    {
        ...
        public List<Appointment<Dentist>> DentistAppointments { get; set; }
    }

However, I would expect that there's only one list of Appointment s, for which the "one" relationship is a Doctor . Of course, you could still give each Derived class its own list of appointments, but that would cause a strange Appointment entity, which has an optional navigation property to each of the derived classes.

More logically, you would put the list of Appointment s in the Doctor (base) class.

You will need to add a Discriminator to be able to map back from Doctor to a derivative. Hmm, the discriminator doesn't work for TPT... trying to figure out how to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM