Reputation: 4747
Consider a Book table structure:
BookId, IsEbook, ISBN, PubDate, EbookFormat
A book can either be a hard copy or an ebook based on IsEbook column value.
The output of a method will have two collection one for Books and other for EBooks.
BookId and EBookId are different properties which take their values from BookId column.
Do I create two classes, one for Book and other for Ebook or can I use inheritance in some way because the BookId and EBookId are the same in the table with different name in the output.
Thanks in advance.
Upvotes: 1
Views: 548
Reputation: 9610
Only common/shared entities should be together in a base class.
If a property has a different name, then it is not shared, so should not be in a common base class, even if it is very, very similar. So in this case you only have ISBN & PubDate in common.
You can make your base class abstract so that it cannot be instantiated itself.
But you could also use a different approach and specify commonality in an interface, and make your book & EBook classes use this instead of a base class. Like this:
public interface IBook
{
DateTime PubDate { get; set; }
string ISDN { get; set; }
}
public class Book: IBook
{
public DateTime PubDate { get ; set ; } //Comes from IBook
public string ISDN { get ; set ; } //Comes from IBook
public int BookId { get; set; }
}
public class EBook : IBook
{
public DateTime PubDate { get; set; } //Comes from IBook
public string ISDN { get; set; } //Comes from IBook
public int EBookId { get; set; }
public string EbookFormat { get; set; }
}
Upvotes: 2
Reputation: 466
I would create two classes, Book and EBook, but you could put the BookId, ISBN and PubDate in an interface, e.g. IBook
. In that way, you can write logic that applies to both, without having the same property names.
Upvotes: 0