At start, I have these classes in code and consequently tables in the database.
Person
Company
Address
Both Person and Company have an address. So I have to establish a relationship between them. Person one - Address many (let's say a person can have more than one address). I know the same address can be for two persons, but that's not important as this is not relationship critical (won't search by address).
Company has an address too. So in class Address I would have both relations to Person and Company. In code, this is not a problem, but in the desing model, it shows both foreign keys and columns, and this makes for me some ambiguity when displaying on the screen (I can hide it though).
I was thinking to create another class: CompanyAddress which will inherit Address and have only one property which will hold key to Address class)
So this is idea:
CompanyAddress : Address
{
}
and
PersonAddress : Address
{
}
So CompanyAddress and PersonAddress would inherit Address class and be used to hold relation (foreign key) to Company and Person classes. This will display in database in 2 possible ways (it can show foreign keys in same table (Address) or it can store foreign key in its own table CompanyAddress). I prefer to store in same table not to use joins.
I can choose in database to have Address table that stores also foreign keys of Person and Company (this is fine I think). Or I can have PersonAddress table which stores only key and all other fields are in Address table. I prefer first approach. Still OOP model can be different for obvious reasons since it can simplify desing model where you dont have to hide keys of Company when displaying Person address in UI design model. So you see benefit of different OOP models.
Code
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string ZIPCode { get; set; }
public string County { get; set; }
}
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
// relationship here one to many with class PersonAddress
// note: I cannot write code as I use some third party controls
// which code is much different for usage in relationships
}
public class Company
{
public string CompanyName { get; set; }
public string CompanyNumber { get; set; }
// relationship here one to many with class CompanyAddress
}
public class CompanyAddress : Address
{
// a relation with Company class
}
public class PersonAddress : Address
{
// a relation with Person class
}
Is this good OOP or it is a bad idea?