Guest User

Untitled

a guest
Jul 9th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. module dutest
  2.  
  3. open Marten
  4. open Npgsql
  5. open System
  6. open System.Reflection
  7. open Marten
  8.  
  9. type Case1 = { id: Guid; thing: int; status: bool }
  10. type Case2 = { id: Guid; butts: string; ``where``: DateTime }
  11.  
  12. type DU =
  13. | Case1 of Case1
  14. | Case2 of Case2
  15. with member x.Id =
  16. match x with
  17. | Case1 c -> c.id
  18. | Case2 c -> c.id
  19.  
  20. let connstring = "Host=localhost;Database=foo;UserName=postgres;Password=postgres"
  21.  
  22. let case1Guid = Guid.Parse "378dfc1f-98a7-4de3-a022-a4835ce9fbe7"
  23. let case2Guid = Guid.Parse "716e9e89-cae3-4603-ac0e-080c90136737"
  24.  
  25. [<EntryPoint>]
  26. let main _argv =
  27. let store = DocumentStore.For(fun store ->
  28. store.Connection(connstring)
  29. let duTy = typeof<DU>
  30. // have to filter out the auto-generated `Tags` static class.
  31. let innerTys = duTy.GetNestedTypes() |> Array.filter (fun t -> t.BaseType = duTy) |> Array.map MappedType.op_Implicit
  32. let mapping = store.Schema.For<DU>()
  33. mapping.AddSubClassHierarchy(innerTys) |> ignore
  34. ()
  35. )
  36. store.Schema.ApplyAllConfiguredChangesToDatabase ()
  37. use session = store.LightweightSession()
  38.  
  39. session.Store(Case1 { id = case1Guid; thing = 10; status = true})
  40. session.Store(Case2 { id = case2Guid; butts = "lol"; ``where`` = DateTime.UtcNow })
  41. session.SaveChanges ()
  42.  
  43. // the problem is that this works already with Marten.
  44. // we can't name the inner sealed classes of DU.Case1 or DU.Case2, which is the premise of the Marten support for hierarchical support (ie gimme all of these subclass instances)
  45. let one = session.Load<DU>(case1Guid)
  46. let two = session.Load<DU>(case2Guid)
  47.  
  48. printfn "%A" [one; two]
  49. 0
Add Comment
Please, Sign In to add comment