Skip to content

Instantly share code, notes, and snippets.

@vbfox vbfox/Dapper.fs
Last active Jun 6, 2019

Embed
What would you like to do?
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
@MCord

This comment has been minimized.

Copy link

commented Dec 22, 2015

I found this via a google search ;)

@lessismore1

This comment has been minimized.

Copy link

commented Feb 8, 2016

Nice!

@fairjm

This comment has been minimized.

Copy link

commented Feb 22, 2016

great!

@jackmott

This comment has been minimized.

Copy link

commented Mar 1, 2016

Can anyone comment on how this compares to FsSql? -> https://github.com/mausch/FsSql
Seems similar at a glance.

@Fagro

This comment has been minimized.

Copy link

commented Aug 2, 2016

Awesome

@mucholove

This comment has been minimized.

Copy link

commented Aug 24, 2016

This looks great! I was wondering whether you could help me out here. When I run this in and try to print out something like the "getUsers" over my data all I get is<fun:users@187>. I've tried iterating with Seq.iter and Seq.map for individuals printing. I even had to cast the items with Seq.cast and I'm getting errors that my expression isn't finished even when I call something like Seq.head on the last pipe. What am I not understanding?

@yetanotherlogonfail

This comment has been minimized.

Copy link

commented Sep 12, 2017

incomplete, created these two files in f# and imported Dapper in PM and red squiggles all over!

@cowlike

This comment has been minimized.

Copy link

commented Jan 9, 2018

I found this example very helpful but I needed one more thing to get it all working with Dotnet Core 2.1.3: the result types have to be annotated with [<CLIMutable>] in order to give the record a default constructor with property getters/setters.

Also, I noticed that passing the Map directly in dapperMapParametrizedQuery works fine without having to create an Expando. That function turns into a one-liner. Not sure if this is specific to the 2.1.3.

@ovatsug25, it sounds like you're only partially applying the function. Are you sure you passed all the arguments, like the connection argument as well?

@SentryWard

This comment has been minimized.

@ScottHutchinson

This comment has been minimized.

Copy link

commented Mar 4, 2018

It would be nice if the sample script included an example of how to run the functions (including a sample connection string).

@matthewblott

This comment has been minimized.

Copy link

commented Mar 9, 2018

This looks useful. I'm trying to port a C# Dapper library using the Unit of Work pattern which is how I ended up here. What's missing (for me) is a way to wrap this up with transactions.

@nobleach

This comment has been minimized.

Copy link

commented Mar 18, 2018

Definitely would be a plus to add the "chrome" around these snippets. They're very helpful. New users can add

open System.Data.SqlClient
open Dapper
open Npgsql

let connString = "Host=localhost;Database=exampleDB;Username=postgres;Password=example"
let connection = new NpgsqlConnection(connString)
connection.Open()

Note: You'll also need to install Npgsql: dotnet add package Npgsql --version 3.2.7 if you're using Postgres, like I am.

@mtrsk

This comment has been minimized.

Copy link

commented Jun 8, 2018

Many thanks @vbfox & @nobleach, the script works and saved me hours of frustration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.