|
An ordinary VB developer shares his own successes and failures |
|
| Home News Articles Resources Tips Downloads About me | ||
Real world .NET serializationDo you know what Jeff Richter didn't tell you about .NET serialization? Read on and learn what I've learned the hard way applying serialization to a real world .NET project.This article can be regarded as a "free continuation" of the article "My first .NET project", because the issues explained here are drawn from that same project. For those of you who didn't read the article, I'd recommend to read at least the first few paragraphs in order to get familiar with the project's requirements and architecture...In a hurry? OK, click this link to read the relevant paragraphs right here:
Now let's go back to the project. Shortly after I released my first BETA, the customer requested a new feature that would allow the editors to work offline, i.e. disconnected from the central database. Suddenly I had to implement a file-based persistence.
I've researched some of the design options and I've quickly
discovered the
I was a bit lazy so I took the easier path of "automatic"
serialization. I've rushed through the source code adding
the
I've launched the application, created a simple
Of course, the I can only speculate that this has something to do with security and I'd really appreciate if someone can explain the reasoning behind this "non-public delegates" exception. I've even searched through the MONO and Rotor project's source code and I've found that the deserialization code just checks if the delegate being deserialized is public and if not, it throws an exception. Without any comments.
(Rotor: See the
At this point I stopped struggling with the "automatic"
serialization approach opting to implement Everything worked great!
The application eventually went to production and the users
were happily saving and loading their There you have it - VERSIONING!
Here is how I did it with the
This code shows the deserialization constructor
before the new
Protected Sub New( _
ByVal info As SerializationInfo, _
ByVal context As StreamingContext)
...
_dateFrom = info.GetDateTime("_dateFrom")
_dateTo = info.GetDateTime("_dateTo")
_caption = info.GetString("_caption")
...
End Sub
This is the new code:
Protected Sub New( _
ByVal info As SerializationInfo, _
ByVal context As StreamingContext)
...
_dateFrom = info.GetDateTime("_dateFrom")
_dateTo = info.GetDateTime("_dateTo")
_caption = info.GetString("_caption")
Try
_notes = info.GetString("_notes")
Catch
_notes = String.Empty
End Try
...
End Sub
Simple, isn't it? Elegant? Far from it, IMHO.
I'd expect the (I know that the latter approach violates the rule that one should never use exceptions to handle the "normal" code flow, but I can live with that:-). To wrap things up, here is the message I'd like you to remember:
Always implement Serializable attribute to your class. Please, don't do
it. (Remember, we've been talking about real applications,
not about code samples or quick starts or whatever...) Sooner
or later you will have to extend the class (by adding a
property, changing a property's type, removing property...)
and your code will have to read serialization
streams created with the old code.
This kind of versioning simply cannot be reasonably
implemented without using
To better illustrate the differences between the "automatic" and
© Palo Mraz - Sunday, July 06, 2003 |
||
|
|
||
| ©2003-2008 Palo Mraz. All Rights Reserved. See my 'new browser window' policy | ||