Click here to Skip to main content
15,891,316 members
Articles / Mobile Apps / Windows Phone 7

Serializing the Unserializable

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
7 Mar 2011CPOL2 min read 13.1K   54   7   2
How to serialize a class that is not serializable.

I came across a post in the Windows Phone developer forums that got me thinking for a moment. The poster had asked how to serialize a class that was not serializable. The class of interest was the StrokeCollection class. The solution for this problem has actually been around for a while but under different terms. If you pick up a Design Patterns book and look up Data Transfer Object (DTO), you'll see what I'm talking about. For the sake of those that don't have a Design Patterns book handy, I'll describe the concept.

A DTO exists for the sake of storing data in an object that is suitable for crossing boundaries. More times than not, when I see some one talking about a DTO, it is for an object that is going from an application to a database. But it's not restricted to that scenario. For the sake of answering the question for the poster, I can assume that he or she wanted to transfer the data to a file. The representation of the data in a file is also a form that is suitable for transfer over a wire.

To accomplish the task, I made a few classes that have some (but not all) of the same properties as the classes I needed to serialize. I didn't need to replicate all of the properties because I'm not interested in saving all of the data that the class provides. As far as functionality goes, the only thing that I've put on these classes is functions for converting from the DTO to the original class and vice versa. The StrokeCollection class uses a number of other classes to represent its data. StrokeCollection contains instances of Stroke, which contains instances of StylusPointCollection, and that contains instances of StylusPoint. I created a similar hierarchy with the members of that hierarchy marked as serializable (with the [DataContract] attribute). For saving and loading the data, I've made use of the serialization code from a previous post. With my class hierarchy and the serialization code, I was able to load and save the strokes in a few lines of code. 

C#
//Save the Strokes
var valuesaveDto = new StrokeDtoCollection(myInkPresenter.Strokes);
_myInkSaver.SaveMyData(valuesaveDto,"MyInk.ink");

//Load the Strokes
var valueDto = _myInkSaver.LoadMyData("MyInk.ink");
myInkPresenter.Strokes = valueDto.ToStrokeCollection();

image.png

I've put together an example program (see link at the top of article) that uses the code. Word of warning, I've not implemented any tomb-stoning code in it. But draw whatever you like and then click on the Save button. After you exit from the program, when you go back into it, you'll see your picture reload.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions

 
GeneralMy vote of 1 Pin
seaDoc15-Mar-11 3:31
seaDoc15-Mar-11 3:31 
GeneralRe: My vote of 1 Pin
Joel Ivory Johnson30-Mar-11 19:50
professionalJoel Ivory Johnson30-Mar-11 19:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.