Wednesday, May 23, 2012

Architecture and Serialisation advice

I need a bit of a architecture help, I think what I've been doing may not necessarily be the best approach.



Things to note:




  • This is not an android specific question.

  • Android does however require that I need to serialise objects across 'screens'.



Consider the following:



I'm currently working on a game for android where a character class needs to be passed between different screens. In order to do this I need to serialise the objects. Taking into account that are different types of characters that will be needed I'd like to create a class that inherits for each of these. Where I have static characters I'd like to inherit from the Character class and set the defaults such as 'Name', and 'Description' within the constructor.



Current implementation:



I'm passing the serialised sub class objects around. ( Eg: UserCharacter with a base of Character ) Ideal situation would for me to be able to deserialise the object as it's base class however that doesn't seem to me work. The only working solution I have for this at the moment is by doing the following when trying to do serialise:



new XmlSerializer(typeof (Character, new [] { typeof(CharacterUser)}));


This allows me to pass in multiple sub types. This however is not feasible in the long term as it just means code duplication across my application as well as everytime a new character sub class is created I need to add at it any point I would need to deserialise. I'd rather have the code written and working where I leave it to do it's own thing. I'm sure you can understand that.



Another thing I've tried is to do is use IXmlSerializable on the character object and deal with the Reading and Writing independantly. I think that this however requires implementation of a list to work correctly? Currently my ReadXml never gets called. (Great idea, but it's not worked for me so far)



Anyone got any ideas I could try?



I think it's quite an open question, please let me know if I need to scope a little differently.





No comments:

Post a Comment