Class design

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    Class design

    I have always been told that I should only get the data I need for each web
    page, so my quandary is set against that backgound.

    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. I can see that in
    the event I might need to display more properties, I only need to make one
    small change in the ASP.NET page, as the values are already available, but
    should I really be thinking about creating other private methods that
    instantiate a User object but only populate some of the properties?

    Is this part of the trade off I should accept when using an OOP approach, or
    should I be looking at it differently?



  • Peter Duniho

    #2
    Re: Class design

    On Sun, 21 Sep 2008 10:06:03 -0700, Mike
    <newsgroupstuff @newsgroups.inv alidwrote:
    [...]
    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

    Comment

    Working...