Skip to content

Discriminated Unions for Static Data #392

Open
@ghost

Description

Another question, but are there any plans for allowing discriminated unions to be used as the type for foreign key IDs to static data? I know entity framework supports this with enums but it would be handy to be able to use discriminated unions for obvious reasons:

type ContentTypeId =
  | Book
  | Magazine
  | Newspaper

...

let printData item =
  match item.ContentTypeId with
  | Book -> printfn "Found a book with id: %d" item.Id
  | Magazine -> printfn "Found a magazine with id: %d" item.Id
  | Newspaper -> printfn "Found a newspaper with id: %d" item.Id

It's possible to do this now by mapping the ID when projecting results:

let getContentTypeId = function
  | 1 -> Book
  | 2 -> Magazine
  | 3 -> Newpaper

...

query {
  for item in ctx.Dbo.Media do
  select
    { contentTypeId = getContentTypeId item.contentTypeId;
    }
}

This can get tedious and is pretty brittle, and an auto-generated solution would be preferable if possible. The infrastructure for the Individuals feature would probably be reusable to accomplish this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions