"Locking" writable properties after deserialization

T

tinman31337

Dear list,
I have a collection of "notes" and want to deserialize them:
<notes>
<note>
<from>Gertie Garterbelt</from>
<to>El Douche</to>
<heading>hey</heading>
<body>don't forget me</body>
</notes>

As you might guess, I have a class "Note", made of
properties "From", "To", "Heading" and "Body".
Since I'm a friend of immutable data when I can get
it, I'd like to have those properties readonly. My domain
would support this, but deserialization clearly does not.
After all, how is the deserializer supposed to populate
readonly properties?

How does a professional handle this? Does .NET provide
a mechanism for setting properties readonly at runtime?
Or am I supposed to introduce a "_locked" flag into my
"Note" class that is checked by the setter of each property?

Kind regards
Tin
 
P

Pavel Minaev

Dear list,
I have a collection of "notes" and want to deserialize them:
<notes>
  <note>
  <from>Gertie Garterbelt</from>
  <to>El Douche</to>
  <heading>hey</heading>
  <body>don't forget me</body>
</notes>

As you might guess, I have  a class "Note", made of
properties "From", "To", "Heading" and "Body".
Since I'm a friend of immutable data when I can get
it, I'd like to have those properties readonly. My domain
would support this, but deserialization clearly does not.
After all, how is the deserializer supposed to populate
readonly properties?

It depends on what kind of serialization you're using. Binary
serializer, for example, will work directly with the backing fields,
so you can expose properties as readonly. XML serializer is much less
flexible, and indeed requires properties to be writable.
How does a professional handle this? Does .NET provide
a mechanism for setting properties readonly at runtime?
No.

Or am I supposed to introduce a "_locked" flag into my
"Note" class that is checked by the setter of each property?

Either that, or, if you're using XML serializer, avoid serializing
your (readonly) domain objects directly. Instead, serialize to/from
helper private classes, and once you get a graph of those, manually
convert it to a graph of your domain objects.
 
O

OmegaSquared

Hello, Tin,

It is the local fields that will be deserialized, so I guess that you just
need to separate the ideas of "fields" and "properties". That is, define the
local fields of your Class as Private, and then define Public ReadOnly
Properties to expose them to the outside world.

Something like (in VB):

Public Class ExampleClass

Private mHeading As String
'...

Public ReadOnly Property Heading() As String
Get
Return mHeading
End Get
End Property

'...

End Class

Cheers,
Randy
 
M

miher

Dear list,
I have a collection of "notes" and want to deserialize them:
<notes>
<note>
<from>Gertie Garterbelt</from>
<to>El Douche</to>
<heading>hey</heading>
<body>don't forget me</body>
</notes>

As you might guess, I have a class "Note", made of
properties "From", "To", "Heading" and "Body".
Since I'm a friend of immutable data when I can get
it, I'd like to have those properties readonly. My domain
would support this, but deserialization clearly does not.
After all, how is the deserializer supposed to populate
readonly properties?

How does a professional handle this? Does .NET provide
a mechanism for setting properties readonly at runtime?
Or am I supposed to introduce a "_locked" flag into my
"Note" class that is checked by the setter of each property?

Kind regards
Tin


Hi,

To control xml serialization You can implement the IXmlSerializable
interface on Your class. This also works if a given property does not have a
public setter since the deserialization method is part of the same class.

Hope You find this useful.
-Zsolt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top