On Sun, 21 Sep 2008 10:06:03 -0700, Mike
<ne************@newsgroups.invalidwrote:
[...]
Let's say I have a User class with 10 properties. I have a private
method
that populates all of these properties with values from the database.
However, I may only need to display two of the properties on a web page
(name and last login time, for example). Coming from a pure ADO/ADO.NET
background, where I would only retrieve these two values and plug them
into
their place on the page, this seems a bit wasteful to me. [...]
Is this part of the trade off I should accept when using an OOP
approach, or
should I be looking at it differently?
Unfortuantely, there's not enough detail in your question to allow us to
fully understand just how "wasteful" that design might be or what
alternatives might be most appropriate.
However, there's at least a few alternatives you might use:
-- You could allow the "User" class to retrieve the property values
dynamically as needed. That way, the class itself can be instantiated
without retrieving any actual data, and then as each property is accessed,
the relevant data would be retrieved. You'd probably want to cache the
data so that the retrieval happens only once per property per instance.
-- You could allow the properties to be "optional" in the sense that
when you construct the "User" instance, you pass a list of property names
to be populated. Then only the data for those properties would be
retrieved; if the client tries to access any of the other properties,
throw an exception or return some default value.
-- You could skip having properties altogether, and instead make each
value something that is retrieved by name, using a dictionary. This could
be in the form of an explicit method in the class to do the retrieval, or
an indexer on the class, or even just by having a single property that
returns the dictionary itself. As with the #2 option above, you'd pass a
list of desired properties to the constructor, and only those properties
would be put in the dictionary.
Hope that helps.
Pete