|
-
Aug 18th, 2017, 05:20 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Converting from an XML model object to more useful class
Hi folks,
TLDR: I have class MessyA, resulting from a deserializer, that models real data and want to have class SmartA that implements IA that is easier for the user to work with, what is the best way to move from MessyA to SmartA (and vice-versa for serialization)?
I am working on a C# DLL that serializes XML of a two different types of XML document; a configuration and a profile. I have used the XSD.exe tool to create classes that model the XML of each type as an object, types Configuration and a Profile; and I have serialization and deserialization working well.
A problem arises however that the deserialized objects of these class types are not very useful for presenting to a consumer.
Now I know that the classes created by XSD.exe are marked partial, so currently my implementation adds another file with another partial part of the same classes that adds functionality, such as, for example, the property:
Can better be accessed by the now added property:
c# Code:
public Format FormatValue;
Format being an Enum of the available formats and the FormatValue property's Get doing a conversion from the Format string.
This leaves the user seeing Format and FormatValue. When this solution is applied to several other properties things start to get messy quickly. It also means the XML-ness is exposed. My end plan for this was to have the ability to easily add other serial formats. To that End, my XmlSerializer class already implements an IDataSerializer interface, but it returns generic Object types because the Profile and Configuration types are currently tied specifically to the XML serializer.
To this end I though to introduce Interfaces for both the configuration and profile: IConfiguration and IProfile, but implementing them in the partial part of the existing Configuration and Profile classes would mean a name clash (e.g. the presentable Format Format property in the interface implementation would clash with the string Format property of the XML model). So it seems not only do I need to deserialize but also convert the resulting model classes to more friendly classes.
Now I can do this easily enough by implementing some sort of converter and essentially just copying over the bits of the model I want to the new friendlier class... but copying back and forth (deserialization and serialization) like that seems like a code smell.
Thus my question finally is this: is there a recommended practice or oft-used method for moving from the model object type to a type that is more presentable?
Thank you
Last edited by wolf99; Aug 18th, 2017 at 05:25 AM.
Thanks 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|