0

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.

1 Answer 1

1

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 Customer class derived from Person or / and Company - you need separate Customer class derived from Person and another Customer class derived from Company but 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 both Employee and Customer you will be ready to delete whole your application because with inheritance there will be no way to achieve that. Changing Contact from Customer to Employee will be possible only with direct SQL because EF doesn't allow that.

Inheritance is not solution for your problem - you must use composition (relations).

Sign up to request clarification or add additional context in comments.

2 Comments

Also called the Strategy Design Pattern.
I ended up creating a Customer with a foreign relationship to Contact (which is either a Company or a Person).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.