Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A read only (immutable) serializeable class

I've designed a class that has with two properties - as string type and a list of objects. I'm loading some xml and deserializing it into an instance of the class, which works very well. What I want is for everything about each instance to be immutable. These classes are exposed as an API, and for integrity, I don't want values of the object to be changed. If a programmer wants something different they should be creating new instances and setting the values then.

Normally I would do this with the ReadOnlyCollection and readonly properties, but that messes up the deserialization. What kinds of things can I do here?

like image 635
Jeremy Avatar asked Dec 20 '25 22:12

Jeremy


1 Answers

It's not worth polluting your domain model just be be more xml serializer friendly so you can implement ISerializable Interface and write your own serialization routine for any collection that is not serialization friendly.

void GetObjectData(SerializationInfo info, StreamingContext context)
{
  // ...
}
like image 196
Teoman Soygul Avatar answered Dec 23 '25 11:12

Teoman Soygul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!