F# Custom generic dapper TypeHandler for Discriminated Union Types

56 views Asked by At

I'm trying to write a custom Generic Dapper TypeHandler for discriminated Union types.

For reference see this question : Dapper generic typehandler for F# Union types

I'm unable to write the Parse method without running into static coercion limitation.

A non generic type handler that works:

type DapperIdTypeHandler() =
    inherit SqlMapper.TypeHandler<Id>()

    override this.SetValue(parameter, Id id) = parameter.Value <- id

    override this.Parse(value) = Id(value :?> Guid)

I'm wondering if there is a way to write the parse method in a generic way?

Here is a code snippet of the generic function that doesn't work:

type DapperUnionTypeHandler<'t>() =
    inherit SqlMapper.TypeHandler<'t>()
    override x.Parse(value) =
        // Stuck at this step
        't <| (value :?> Guid)

    override this.SetValue(param, value) =
        param.Value <- value
        ()

Union type I'm using:

type Id =
    | Id of Guid
0

There are 0 answers

Related Questions in F#