Skip to content

Instantly share code, notes, and snippets.

@bartsokol
Last active July 5, 2017 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartsokol/150dc54de69d267231b1475b11ad6fa2 to your computer and use it in GitHub Desktop.
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
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