Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7003937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:08:02+00:00 2026-05-27T21:08:02+00:00

the title might be too generic, but I have a very specific question about

  • 0

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:08:02+00:00Added an answer on May 27, 2026 at 9:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Though the title might be either too specific or too vague, I have a
I asked a question , title of which might have been misleading so I'm
This question might appear very simple, but i haven't found an answer yet, so
The title might sound convoluted but bear with me. I have Rooms which contain
Title might be a bit confusing, so let me explain. I have a website
the title might be a little bit confusing, let me explain, ;) I have
Apologies for the long winded title but looking for a solution to what might
I know the title isn't very clear. I'm new to PHP, so there might
This question might be very easy to answer so I apologize if I missed
This might be quite a trivial question, but which of these methods is best

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.