Search code examples
c#databaseinheritancerelational

C# Object Oriented Programming, Inheritance, Foreign Keys and DB


the title might be too generic, but I have a very specific question about how to design C# classes based on a relational database.

Let's say we have a table called TPerson and a table called TComment, both tables have one column in common (PERSON_ID, PK on TPerson and FK in TComment).

Let's say we have a web app where we are displaying a list showing all comments from everyone, in this list we are showing the comment and the first name and last name of the author (TPerson) and the date the comment was created as well. I think it is not appropriate to use inheritance (Base class TPerson, derived class TComment) because I don't need the alias for example, I don't want to drag the other columns with me if I only need first name and last name (column TPerson might have lots of columns).

I want a class design that is able to:

  1. Add Person objects and save to DB
  2. Add Comment objects and save to DB
  3. Retrieve the list of comments with first name and last name of the person

Is there a way to create re-usable code by only retrieving or using parts of an object ?

The only way to do this would be to use inheritance and every time I retrieve a Comment, I would also create the Person object that goes with it, but in some parts it would be overkill to retrieve the entire thing when I only need certain parts of it...

If I were to create classes to represent the data, I would go with something like this, Any ideas ?, thanks for your help !:

class Person
{
    int personId;
    string firstName;
    string lastName;
    string alias;  
    DateTime creationDate;        

    public int PersonId
    {
        get { return personId; }
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    public string Alias
    {
        get { return alias; }
        set { alias = value; }
    }

    public DateTime CreationDate
    {
        get { return creationDate; }
    }

    //for adding new person object
    public Person(string first_name, string last_name, string alias)
    {

    }
    //internal usage
    public Person()
    {

    }

    public void Save()
    {
        //save new person object in DB or update...
    }

    public static Person GetPerson(int personId)
    {
        Person p = null;

        //call sproc and load from database...
        p = new Person();
        p.personId = 10;
        p.firstName = "First Name";
        p.lastName = "Last Name";
        p.alias = "Alias";

        return p;
    }
}

class Comment
{
    int commentId;
    int personId;
    string comment;
    DateTime creationDate;

    public int CommentId
    {
        get { return commentId; }
        set { commentId = value; }
    }

    public int PersonId
    {
        get { return personId; }
        set { personId = value; }
    }

    public string Comment1
    {
        get { return comment; }
        set { comment = value; }
    }

    public DateTime CreationDate
    {
        get { return creationDate; }
        set { creationDate = value; }
    }

    public Comment(int person_id, string comment)
    {

    }

    public Comment()
    {

    }

    public void Save()
    {
        //save or update to DB
    }

    public static List<Comment> GetComments()
    {
        List<Comment> comments = null;

        //get data from db and load...
        comments = new List<Comment>();

        comments.Add(new Comment() { 
            commentId = 1,
            personId = 10, 
            comment = "this is one comment", 
            CreationDate = DateTime.Now });
        comments.Add(new Comment() { 
            commentId = 1, 
            personId = 11, 
            comment = "this is another comment", 
            CreationDate = DateTime.Now });

        return comments;
    }
}

Solution

  • This is definitely not a classic case of inheritance - typically, you want to use inheritance to reflect an "is a" relationship - "car is a vehicle", for instance. There's no logical way in which you might say "comment is a person"; you could, of course, say that both comment and person are "things you can store into a database".

    Whilst I agree with both King Chan and ken2k that you would probably want to use an existing ORM tool for this, it would help to read up on the underlying concepts of OO design first. I'd recommend Craig Larman's book "Applying UML and patterns" - it's technology-agnostic, but has a great description of how to map objects to a database.