ElHaix
ElHaix

Reputation: 13006

What is a strategy for creating C# property classes / returning specific properties for different types?

In a forthcoming application, there will be several supplier types which can include hotels, restaurants, transportation services, meeting venues.

In planning the data model, I was thinking of having one table with most of the common properties between all types. Displaying/selecting specific properties based on the supplier type would be determined by its respective supplier type class which would contain a list of which properties to return for that supplier.

In cases where a supplier has say, "meeting spaces", a foreign key relationship will be created to a 1..* table containing those properties.

It's been a while since I have done this so I would like a refresher on how to accomplish this in C#, if in fact this is a good strategy, rather than creating separate tables for each supplier type. An update on proper terminology would be useful.

Upvotes: 2

Views: 72

Answers (1)

Andrei
Andrei

Reputation: 44620

If you are going to use Entity Framework, there are 2 different ways to implement model mapping to SQL tables when you have inheritance:

Table per Type

In the TPT mapping scenario, all types are mapped to individual tables. Properties that belong solely to a base type or derived type are stored in a table that maps to that type. Tables that map to derived types also store a foreign key that joins the derived table with the base table.

Table per Concrete

In the TPC mapping scenario, all non-abstract types in the hierarchy are mapped to individual tables. The tables that map to the derived classes have no relationship to the table that maps to the base class in the database. All properties of a class, including inherited properties, are mapped to columns of the corresponding table.


Here is a great article to start. Even if you are not going to use EF, it's a good reading for you. It will give you some ideas of how to implement tables and models structure.

Upvotes: 1

Related Questions