Skip to content

Instantly share code, notes, and snippets.

@ofirgeller
Forked from vbfox/Dapper.fs
Created April 26, 2016 21:48
Show Gist options
  • Save ofirgeller/831041c6760127cc4d12b90074964722 to your computer and use it in GitHub Desktop.
Save ofirgeller/831041c6760127cc4d12b90074964722 to your computer and use it in GitHub Desktop.
Minimal dapper in F#
module DapperFSharp =
open System.Data.SqlClient
open System.Dynamic
open System.Collections.Generic
open Dapper
let dapperQuery<'Result> (query:string) (connection:SqlConnection) =
connection.Query<'Result>(query)
let dapperParametrizedQuery<'Result> (query:string) (param:obj) (connection:SqlConnection) : 'Result seq =
connection.Query<'Result>(query, param)
let dapperMapParametrizedQuery<'Result> (query:string) (param : Map<string,_>) (connection:SqlConnection) : 'Result seq =
let expando = ExpandoObject()
let expandoDictionary = expando :> IDictionary<string,obj>
for paramValue in param do
expandoDictionary.Add(paramValue.Key, paramValue.Value :> obj)
connection |> dapperParametrizedQuery query expando
type User = { UserId:string }
let getUsers connection =
connection
|> dapperQuery<User> "SELECT UserID From tbUser"
let getUser userId connection =
connection
|> dapperMapParametrizedQuery<User> "SELECT UserID From tbUser WHERE UserId = @UserId" (Map ["UserId", userId])
|> Seq.head
type UserSelectArgs = { SelectedUserId:string}
let getUser' userId connection =
connection
|> dapperParametrizedQuery<User> "SELECT UserID From tbUser WHERE UserId = @SelectedUserId" {SelectedUserId=userId}
|> Seq.head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment