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 |
This comment has been minimized.
This comment has been minimized.
Nice! |
This comment has been minimized.
This comment has been minimized.
great! |
This comment has been minimized.
This comment has been minimized.
Can anyone comment on how this compares to FsSql? -> https://github.com/mausch/FsSql |
This comment has been minimized.
This comment has been minimized.
Awesome |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
incomplete, created these two files in f# and imported Dapper in PM and red squiggles all over! |
This comment has been minimized.
This comment has been minimized.
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 Also, I noticed that passing the Map directly in @ovatsug25, it sounds like you're only partially applying the function. Are you sure you passed all the arguments, like the |
This comment has been minimized.
This comment has been minimized.
What do you think about this? |
This comment has been minimized.
This comment has been minimized.
It would be nice if the sample script included an example of how to run the functions (including a sample connection string). |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
Definitely would be a plus to add the "chrome" around these snippets. They're very helpful. New users can add
Note: You'll also need to install Npgsql: |
This comment has been minimized.
I found this via a google search ;)