I have an object to populate with xml. I think that I might use a contructor which accept this xml to build the object. But, is there a better design pattern to do it ? Is it to the object to handle all the xml parsing ?
Thank you !
XML schemas contain numerous design patterns, the most common of which are Russian Doll, Salami Slice, Venetian Blind, and Garden of Eden. The patterns vary according to the number of their global elements or types. A global element or type, which is a child of the schema, contains a target namespace.
Explanation: Command pattern is a data driven design pattern.
Iterator pattern lets us do just that. Formally it is defined as below: The iterator pattern provides a way to access the elements of an aggregate object without exposing its underlying representation.
No you should not send the xml to the constructor because you are coupling the model with deserilizing logic. You should create a class that handles the deserlization for you and return an instance. Why this is important might be unclear. But imagine in the future you may have the data coming from a database, you don't want to pass the database connection to the constructor, do you? Instead you can create a different class that uses databases to generate an instance of your class.
Here are some good read:
http://en.wikipedia.org/wiki/Builder_pattern
http://en.wikipedia.org/wiki/Immutable_object
The simplest approach would be to let XmlSerializer do it for you. Just decorate it (if necessary) so that it knows how to map the xml, and call Deserialize.
if you receive XML as a string which requires special parsing, you can, I guess, create a manager/factory to construct your object:
var myObject = MyObjectCustomXmlSerializer.Deserialize(xmlString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With