Serializing complex types using the built in JSON Serializer of NEventStore

457 Views Asked by At

I'm using NEventStore in an application that uses CQRS/Event-Sourcing. My WireUp looks like the following:

 return NEventStore.Wireup.Init()
               .LogToOutputWindow()
               .UsingInMemoryPersistence()      
               .UsingSqlPersistence("TestConnection") 
               .WithDialect(new MsSqlDialect())
               .EnlistInAmbientTransaction()        
               .InitializeStorageEngine()               
               .UsingJsonSerialization()                             
               .Build();

Serializing events that contain built-in simple data types, such as string and int works fine. However, when I try to serialize an event that uses a struct that I have defined the deserialized value is null.

I will probably have to mark the memberrs of my event using attributes. But what scheme does NEventStore use? C# has quite a few options (the attributes from the DataContract Serializer, the attributes from the XmlSerializer, ... ).

I'm also wondering what restrictions there are on serialization. Does NEventStore require a public empty constructor? Public setters? Or can I use readonly fields (my preference).

I have been unable to figure out what Json serializer NEventStore uses. It seems its not the one by NetwonSoft as there is a separate nuget package for that one.

1

There are 1 best solutions below

0
EverPresent On

I had this same problem so here's what I found. NEventStore uses Newtonsoft's Json.net internally. Here's a link to Newtonsoft's page on how serialization works: http://www.newtonsoft.com/json/help/html/serializationguide.htm#ISerializable

You have several options to get the type serializing. I chose to implement the System.Runtime.Serialization.ISerializable interface, because I didn't want to have to reference the json.net dependency from my models assembly. Your case may be different.