CSLA for Silverlight is using MobileFormatter class to serialize and de-0serialize the data that if sent across the wire during client/server communications. General flow of the serialization process is as follows. Each object that implements IMobileObject is being asked by mobile formatter to serialize its data into a list of SerializationInfo objects. That list maintains the state of each object as well as the relationships between objects. Ultimately, the data consist of simple properties serialized into FieldData objects and relationships between objects into ChildData objects. The formatter then is using DataContactSerializer and XML Reader/Writer combination in order to create byte arrays from object data. These byte arrays are just XML blobs when all said and done of SerializationInfo objects. DataContractSerializer is doing the best job it can in order to specify enough information in the XML to make it possible to reconstruct the data on the other end of the wire. As a result, it drops hints from XML standards that specify what primate type should be used for each property value of each objects. This results in extremely verbose XML, where each property may take additional 20-30 bytes to specify the type to be used. On top of that, CSLA is using descriptive names inside Serialization Info object as well as generic dictionaries to store ChildData and FIeldData objects. Turns out, serializer puts enough metadata around generic collections as well that makes them larger.
So, we took a few steps to provide some optimization around the code that should work with any version of CSLA that has MobileFormatter. First of all, we changed DataMember and DataContract attributes to provider shorter names. For example, instead of Value property we will just have VU in the XML. We also created helper dictionaries instead of using generic dictionaries in order to be able to specify names for items and keys, all consisting of 2 letter instead of 4-10 letter. Also, we eliminated XML type hints by injecting a xmlns namespace into XML header containing the root namespace of XML standards with one letter alias, which will eliminate 20-30 bytes from each serialized property.
Your particular results may vary, but we saw overall improvements of 2-50 percent depending on the objects. Since Mobile Formatter is also used to take snapshots of objects to support Cancel button (undo) functionality, you will also see memory improvements in your Silverlight application as well.
Please read the warning message at the end of the article before you decide to use this code.
You can download attached SerializationInfo and make changes to your version of CSLA. You will also need to find the following method in MobileFormatter
public void Serialize(XmlWriter writer, object graph)
and replace it with the code below:
public void Serialize(XmlWriter writer, object graph)
{
List<SerializationInfo> serialized = SerializeAsDTO(graph);
DataContractSerializer dc = GetDataContractSerializer();
dc.WriteStartObject(writer, serialized);
writer.WriteAttributeString("xmlns", "z", null, "http://www.w3.org/2001/XMLSchema");
dc.WriteObjectContent(writer, serialized);
dc.WriteEndObject(writer);
}
WARNING – the byte arrays are not going to be compatible with previous CSLA version. So, if you are serializing objects using MobileFormatter and storing them somewhere, you will not be able to de-serialize them after you make the change.
You can also download SerializationInfo file here.