473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Immutable object (de)serialization

vekipeki
229 Expert 100+
Hi!

I've been looking around for a simple example of the "right way" to implement immutable object xml serialization/deserialization.

The obvious problem is that the immutable object's properties are made read-only by omitting the setter, while private fields are made read-only by using the readonly keyword.

Since IXmlSerializable.ReadXml(System.Xml.XmlReader reader) requires the object inner state to be changed, I have to make private fields writable, which I would like to avoid. I've tried googling it but was unable to find the answer I was looking for.

Thanks a lot,

Veki
Aug 29 '08 #1
2 1953
vekipeki
229 Expert 100+
Just to make my question a bit clearer, one of the simple classes named UnitInfo is implemented like this:

Expand|Select|Wrap|Line Numbers
  1. class UnitInfo
  2. {
  3.    private readonly Unit _unit;   // Unit is enum
  4.    public Unit { get { return _unit; }}
  5.  
  6.    private readonly UnitPrefix _unitPrefix;   // UnitPrefix is enum
  7.    public UnitPrefix { get { return _unitPrefix; }}
  8.  
  9.    public UnitInfo(Unit unit, UnitPrefix unitPrefix)
  10.    {
  11.        _unit = unit; _unitPrefix = unitPrefix;
  12.    }
  13. }
  14.  
Since it is used at many places in code, it was made immutable to make sure that no object containing a UnitInfo can corrupt other objects' UnitInfos, and since those UnitInfos are in 80% cases equal by value, this way they can safely reference the same instance.

But the problem is that I am not sure how to implement serialization/deserialization? XmlSerializer by itself will skip the readonly properties, and I cannot change the fields outside the constructor if they are also readonly.

I am pretty sure I am not the only one using immutable objects -- someone must have had the same problem before? :)
Aug 30 '08 #2
vekipeki
229 Expert 100+
I got it at the end, so I will just post a quick answer if anyone comes across the same problem:

I created a base ImmutableObject class which implements IXmlSerializable, and then uses Reflection to serialize/deserialize private instance fields marked with a custom attribute (I named it [SerializeImmutableAs("NameOfElement")], because private fields usually have lower camel casing or who-knows-what-soft-of-casing. Since Reflection does not care about readonly attributes, it can change the state of the object outside the constructor.

Derived object don't have to implement anything, and all messing with Reflection is constrained to IXmlSerializable members in base class only.
Sep 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gavin | last post by:
Hi, I'm using the BinaryFormatter so serialize a document class hierachy to a file. I'm using rather large files (20-50mb) so I was wondering if anyone knows of a way to check the progress of the...
0
by: Sabyasachi Basu | last post by:
Most C++ object serialization techniques are intrusive, that is they require all serializable classes need to have functions to stream in and stream out their contents. I came across...
12
by: Nimmi Srivastav | last post by:
Are there any design patterns for object serialization/deserialization in C++? Where can I find information pertaining to them? Can someone post some real coding examples please? I have a need...
1
by: Marja Ribbers-de Vroed | last post by:
I'm using classic ASP with VBscript and I would like to have a facility for XML object serialization, just like the Serializer object that is available in ASP.NET. Is there such a thing for classic...
6
by: mookid8000 | last post by:
Hi group! Is it possible to scan an assembly *which has NOT been added as a reference* for types, and dynamically create instances of the loaded types? I have a program, MyProgram.exe, which...
2
by: V | last post by:
Hi, I am trying to ascertain if Object Serialization (binary or xml) has been improved (new classes, or new ways) from version 1.1 of the .net framework. I pretty much know how to do that in...
0
by: mshetty | last post by:
Hi, Is it possible to override the default de-serialization of parameters sent to a Web Method written using SoapRpcMethod keyword? Thanks and Regards, M Shetty
0
by: Igor | last post by:
Hi everyone ! I have a problem with object serialization. I created web service with number of methods. One of them receives object of the next type: public class ObjectClass private szValue...
2
by: Ed Sutton | last post by:
Is there an attribute I can add to a public get/set property to prevent serialization? I am using auto-generated object serialization for a custom type in my webs service. Thanks in advance...
11
by: William | last post by:
I'm looking for an example that would show how to serialize a c++ object at it's simplest w/o using any other api's. I have a class that I want to serialize and then pass to my obj-c class so I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.