dragonwell
Programmer
Thanks to all for the input on my previous question(s).
I have a more specific question now.
Let's say I have a "Car" class that essentially mirrors a row from a database, with properties such as "Year", "Make", "Model", "Mileage", etc. and an "ID" which is an integer matching the record's primary key in the database. Class Car has a constructor that takes an ID integer and sets this.ID to that number which can, at some point, be used to query the data needed for the other properties.
So, when it comes time to instantiate a Car object, I construct it using an ID integer. Should I not go ahead and run my database query in the constructor and set the rest of the properties then? I have heard that's not a good idea, and it makes sense to me for performace reasons.
My next thought is that perhaps the Car class should have a Fill() method, which runs the query and populates the rest of the fields. I could then instantiate the object, call it's Fill() method, and then go about accessing the properties. Is that a pattern ( accepted way of doing things)?
Is this the same as lazy initialization?
Hopefully someone can understand my question and help me out.
Thanks,
David
I have a more specific question now.
Let's say I have a "Car" class that essentially mirrors a row from a database, with properties such as "Year", "Make", "Model", "Mileage", etc. and an "ID" which is an integer matching the record's primary key in the database. Class Car has a constructor that takes an ID integer and sets this.ID to that number which can, at some point, be used to query the data needed for the other properties.
So, when it comes time to instantiate a Car object, I construct it using an ID integer. Should I not go ahead and run my database query in the constructor and set the rest of the properties then? I have heard that's not a good idea, and it makes sense to me for performace reasons.
My next thought is that perhaps the Car class should have a Fill() method, which runs the query and populates the rest of the fields. I could then instantiate the object, call it's Fill() method, and then go about accessing the properties. Is that a pattern ( accepted way of doing things)?
Is this the same as lazy initialization?
Hopefully someone can understand my question and help me out.
Thanks,
David