Serialization is the process of taking the state of an object graph and converting into a byte stream that can be transferred over a network or used for n-level undo. That byte stream can later be deserialized to create a copy or clone of the orignal object graph.
CSLA .NET relies on deep serialization to do this work. Most serializers do shallow serialization: operating only on public properties. Deep serialization serializes at the field level, including non-public field values. Deep serialization also preserves the shape of the object graph upon deserialization, where shallow serialization usually changes the shape of the object graph on deserialization, often creating new instances of objects that didn't exist in the original graph.
The .NET Framework provides two built-in deep serializers:
BinaryFormatter
NetDataContractSerializer
(NDCS)
CSLA .NET provides its own highly optimized serializer:
MobileFormatter
⚠️ Microsoft has deprecated the use of theBinaryFormatter
andNetDataContractSerializer
, so starting with CSLA 6 onlyMobileFormatter
is supported.
Where possible, CSLA .NET supports all three serializers, but not all .NET implementations include BinaryFormatter
or NDCS. The only serilizer available on all .NET implementations is MobileFormatter
.
In CSLA 6 the default serializer is MobileFormatter
. In previous versions, the default serializer used by CSLA .NET is different for .NET Framework and .NET Core.
- .NET Framework defaults to
BinaryFormatter
- .NET Core defaults to
MobileFormatter
- Mono (Xamarin/WebAssembly) defaults to
MobileFormatter
The MobileFormatter
imposes some constraints on your code beyond BinaryFormatter
or NDCS. These constraints are why MobileFormatter
works on all .NET implementations and why we've been able to optimize it for CSLA .NET.
- All types must be
[Serializable]
(same as other serializers) - All types must implement
Csla.Serialization.Mobile.IMobileObject
- More commonly types subclass CSLA base types like
Csla.Core.MobileObject
orBusinessBase
that already implement the interface - Primitive types are a special case, in that
MobileFormatter
knows how to serialize primitive .NET types, plus some other "primitive" types likestring
- More commonly types subclass CSLA base types like
- Properties must either use managed backing fields or manually get/set private backing fields in the serialization stream
- It is recommended to use managed backing fields, as this allows CSLA .NET to do all the work on your behalf
- If you strongly desire to use private backing fields, you need to override
OnGetState
andOnSetState
and get/set every private backing field value in the serialization stream
This information, with more detail, is covered in the Using CSLA 4 book series.