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 7566529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:26:19+00:00 2026-05-30T14:26:19+00:00

I would like to know what’s the best code design when storing and retrieving

  • 0

I would like to know what’s the best code design when storing and retrieving data from a database when working with objects and classes.

I could do this in two ways.

In the class constructur I query the database and stores all info in instance variables inside the class and retrieve them with getters/setters. This way I can always get any information I want, but in many cases wont be needing all the information all the time.

public class Group {
    public int GroupID { get; private set; }
    public string Name { get; private set; }
    public Group(int groupID)
    {
        this.GroupID = groupID;
        this.Name = // retrieve data from database
    }
    public string getName()
    {
        // this is just an example method, I know I can retrieve the name from the getter :)
        return Name;
    }
}

The other way is to create some methods and pass in the groupID as a parameter, and then query the database for that specific information I need. This could result in more querys but I will only get the information I need.

public class Group {    
    public Group()
    {
    }
    public string getName(int groupID)
    {
        // query database for the name based on groupID
        return name;
    }
}

What do you think is the best way to go? Is there a best practice to go with here or is it up to me what I think works the best?

  • 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-30T14:26:20+00:00Added an answer on May 30, 2026 at 2:26 pm

    You don’t want to do heavy DB work in the constructor. Heavy work should be done in methods.

    You also don’t want to necessarily couple the DB work with the entity class that holds the data. What if you want a method to return two of those objects from the database? For example GetGroups() – you can’t even construct one without doing DB work. For something that returns multiple, the storage and retrieval is decouple from the entity class.

    Instead, decouple your DB work from your entity objects. One option is you can have a dataaccesslayer with methods like GetFoo or GetFoos etc… that query the database, populate the objects and return them.

    If you use an ORM, see:

    https://stackoverflow.com/questions/3505/what-are-your-favorite-net-object-relational-mappers-orm

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

Sidebar

Related Questions

Would like to know the c# code to actually retrieve the IP type: Static
I would like to know what's the best technique to do single sign-on in
I would like to know what would be the best way to do unit
Would like to know from all you guys what do you think about my
I would like know , what is the best way to sync/load tree panel
I would like know what is the best possible way to implement transactions with
I'm starting to learn Android and i would like know the best practice to
Would like to know when it is right to uses a nested classes in
Would like to know what is the best F/OSS IDE for Python Web development.
Would like to know which direction player hits terrain tile from (just a simple

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.