I have an abstract base class Contact that has two subclasses: Person and Company.
I want to have a Customer, Vendor or other types that can be either a Company or a Person (all sharing the same primary key ContactId).
My question is if it’s possible to inherit all these types from Contact? If the answer is no, is there another option of utilizing the Contact property from the PK? What’s the recommended design for this scenario?
Note that I want an Employee/Customer etc. to also be able to be a User.
How would you achieve that in C#? That is the first question you must answer yourselves because .NET doesn’t support multi-inheritance so you cannot have single
Customerclass derived fromPersonor / andCompany– you need separateCustomerclass derived fromPersonand anotherCustomerclass derived fromCompanybut every time you see this you should know that you are doing something wrong. Also if you in the future find that you need to have Contact which is bothEmployeeandCustomeryou will be ready to delete whole your application because with inheritance there will be no way to achieve that. ChangingContactfromCustomertoEmployeewill be possible only with direct SQL because EF doesn’t allow that.Inheritance is not solution for your problem – you must use composition (relations).