aelveborn
aelveborn

Reputation: 357

Best practice when storing/retrieving data from database inside class

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?

Upvotes: 0

Views: 2895

Answers (3)

jmoreno
jmoreno

Reputation: 13551

Lazy loading versus early loading, which is what this really boils down to, is best determined by usage.

Mostly this means related entities -- if you are dealing with an individual address for instance, splitting the read for the city from the read for the state would be crazy; OTOH when returning a list of company employee's reading their address information is probably a waste of time and memory.

Also, these aren't mutually exclusive options -- you can have a constructor that calls the databases, and a constructor that uses provided data.

Upvotes: 1

bryanmac
bryanmac

Reputation: 39296

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

Upvotes: 1

Marc
Marc

Reputation: 257

If it is a relational database the best way would be to do it with ORM (object-relational mapping). See here for a list of ORM-mappers:

https://en.wikipedia.org/wiki/List_of_object-relational_mapping_software

Upvotes: 0

Related Questions