Skip to content

Writing a custom XML serializer

Mark Seemann edited this page Sep 30, 2014 · 1 revision

AtomEventStore takes care of serializing and deserializing all well-known Atom XML types, but these are ultimately only envelopes. The actual content (the events) is supplied by the client, and being a general-purpose library, AtomEventStore has no knowledge of the custom event types being written and read.

AtomEventStore comes with a few built-in XML serializer implementations. These should be enough for most purposes, but you can also implement your own XML serializer if you have special needs.

IContentSerializer

In order to implement a custom XML serializer for AtomEventStore, you must implement the IContentSerializer interface, which only defines two methods:

public interface IContentSerializer
{
    void Serialize(XmlWriter xmlWriter, object value);

    XmlAtomContent Deserialize(XmlReader xmlReader);
}

As their names imply, these methods serialize and deserialze content.

Serializing content

Before AtomEventStore writes events to the underlying storage mechanism, it serializes the relevant Atom Feed page to XML. The content of each Atom Entry is serialized using IContentSerializer.Serialize.

In order to implement this method, you must write the value argument to the provided xmlWriter. The XML you write must be in a format that subsequently enables your implementation of IContentSerializer.Deserialize to interpret it and rehydrate the object with the correct values.

Deserializing content

When AtomEventStore reads events from the underlying storage mechanism, it deserializes the Atom XML to objects. The content of each Atom Entry element is deserialized using IContentSerializer.Deserialize.

Your implementation must be able to correctly interpret the XML written by your implementation of IContentSerializer.Serialize.

Examples

The source code for AtomEventStore contains various implementations of IContentSerializer, including

These are fully functioning implementations, and not particularly complex, and since they are actual production code, they provide better examples than any toy example that could be provided here.

Clone this wiki locally