Skip to content

Instantly share code, notes, and snippets.

@Dzoukr
Last active June 19, 2021 01:50
Show Gist options
  • Save Dzoukr/f5927f9f0677f5c8508fc09c7ed2a835 to your computer and use it in GitHub Desktop.
Save Dzoukr/f5927f9f0677f5c8508fc09c7ed2a835 to your computer and use it in GitHub Desktop.
F# extensions for Dapper
module Dapper.Extensions
open System
open System.Data.SqlClient
open Dapper
let extractValue (x:obj) =
match x with
| null -> null
| _ -> match x.GetType().GetProperty("Value") with
| null -> x
| prop -> prop.GetValue(x)
let (+>) (map:Map<string, obj>) (key,value) = map.Add(key, extractValue value)
let singleParam (key, value) = (Map.empty) +> (key,value)
type OptionHandler<'T>() =
inherit SqlMapper.TypeHandler<option<'T>>()
override __.SetValue(param, value) =
let valueOrNull =
match value with
| Some x -> box x
| None -> null
param.Value <- valueOrNull
override __.Parse value =
if isNull value || value = box DBNull.Value
then None
else Some (value :?> 'T)
let registerTypeHandlers() =
SqlMapper.AddTypeHandler (OptionHandler<Guid>())
SqlMapper.AddTypeHandler (OptionHandler<int64>())
SqlMapper.AddTypeHandler (OptionHandler<int>())
SqlMapper.AddTypeHandler (OptionHandler<string>())
SqlMapper.AddTypeHandler (OptionHandler<DateTime>())
let safeSqlConnection string =
registerTypeHandlers()
new SqlConnection(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment