Last active
July 5, 2017 19:58
-
-
Save bartsokol/150dc54de69d267231b1475b11ad6fa2 to your computer and use it in GitHub Desktop.
Register F# DU (Discriminated Union) cases as event types in Marten event store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module EventStore = | |
open Marten | |
open Microsoft.FSharp.Reflection | |
open System.Reflection | |
type Events = | |
| Event1 of string | |
| Event2 of int | |
let registerUnionCasesAsEvents<'a> (opts: StoreOptions) = | |
typedefof<'a>.GetNestedTypes(BindingFlags.Public ||| BindingFlags.NonPublic) | |
|> Array.filter FSharpType.IsUnion | |
|> Array.iter opts.Events.AddEventType | |
let getStore connectionString schemaName = DocumentStore.For (fun opts -> | |
opts.Connection(connectionString) | |
opts.DatabaseSchemaName <- schemaName | |
opts.AutoCreateSchemaObjects <- AutoCreate.All | |
opts |> registerUnionCasesAsEvents<Events> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment