Encapsulate update method inside of object or have method which accepts an object to update

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2011-01-02T02:51:21Z Indexed on 2011/01/02 2:53 UTC
Read the original article Hit count: 268

Hi, I actually have 2 questions related to each other:

  1. I have an object (class) called, say MyClass which holds data from my database. Currently I have a list of these objects ( List < MyClass > ) that resides in a singleton in a "communal area". I feel it's easier to manage the data this way and I fail to see how passing a class around from object to object is beneficial over a singleton (I would be happy if someone can tell me why). Anyway, the data may change in the database from outside my program and so I have to update the data every so often. To update the list of the MyClass I have a method called say, Update, written in another class which accepts a list of MyClass. This updates all the instances of MyClass in the list.

However would it be better instead to encapulate the Update() method inside the MyClass object, so instead I would say foreach(MyClass obj in MyClassList) { obj.update(); }

What is a better implementation and why?

  1. The update method requires a XML reader. I have written an XML reader class which is basically a wrapper over the standard XML reader the language natively provides which provides application specific data collection. Should the XML reader class be in anyway in the "inheritance path" of the MyClass object - the MyClass objects inherits from the XML reader because it uses a few methods. I can't see why it should. I don't like the idea of declaring an instance of the XML Reader class inside of MyClass and an MyClass object is meant to be a simple "record" from the database and I feel giving it loads of methods, other object instances is a bit messy. Perhaps my XML reader class should be static but C#'s native XMLReader isn't static.?

Any comments would be greatly appreciated

Thanks Thomas

© Stack Overflow or respective owner

Related posts about c#

Related posts about inheritance