NEventStore / NEventStore.Persistence.RavenDB

RavenDB Persistence Engine for NEventStore
MIT License
2 stars 10 forks source link

Adding support for user customizations of ravendb's serializer #6

Closed charlessolar closed 8 years ago

charlessolar commented 9 years ago

There is a property off of _store.Conventions that allows users to customize the way RavenDb serializes. This is of particular use to me because I use NServicebus which handles events as interfaces which Json.NET can't deserialize properly (always a dynamic type). I have to hook into Json.NET's contract resolver to message map the event back to its interface definition so aggregate Handle methods can process it correctly.

This patch adds a SerializerCustomizations property to RavenPersistenceOptions which is an Action<JsonSerializer> - exactly the same thing _store.Conventions.CustomizeJsonSerializer accepts.

Which allows me to inject custom binders and contract resolvers into ravendb like so:

public static RavenPersistenceWireup UsingAggregatesRavenPersistence(
            this Wireup wireup,
            string connectionName)
        {
            var options = new RavenPersistenceOptions(serializerCustomizations: s =>
            {
                s.Binder = new EventSerializationBinder();
                s.ContractResolver = new EventContractResolver();
            });

            return wireup.UsingRavenPersistence(connectionName, options);
        }

This behavior is much cleaner then what the elliotritchie/NES project does, which is here: https://github.com/elliotritchie/NES/blob/v5/src/NES.NEventStore.Raven/CustomizedRavenSerializerWireup.cs

he actually has to use reflection to pull the IDocumentStore from your object. Figured this was a better solution