View on GitHub


Idiomatic data access for F#

Download this project as a .zip file Download this project as a tar.gz file


The first step is to define parameterless function creating a database connection:

let createConnection () = new SqlConnection(connectionString)
let generatorConfig = createDefaultConfig createConnection

Connections are used in two contexts - when query code is generated, and when query functions are executed.

In the first case the connection creation function is wired-up by defining some functions:

let sql commandText = sql generatorConfig commandText

and, if explicit timeouts must be specified:

let sqlWithTimeout timeout commandText = 
    sql (generatorConfig |> addCommandTimeout timeout) commandText

that generate code executing inline sql, and:

let proc name = 
    proc generatorConfig name


let procWithTimeout timeout name = 
    proc (generatorConfig |> addCommandTimeout timeout) name

generating code for stored procedure execution.

The query execution configuration code depends on query mode (i.e. whether they are synchornous or asynchronous).

For synchronous execution the run function should be defined:

let run f = createConnection f

and, additionally, when composite queries are used:

let buildQuery ctx = FinalQueryPart(ctx, generatorConfig, cleanUpTemplate)

or with timeout:

let buildQueryWithTimeout timeout ctx = 
        (generatorConfig |> addCommandTimeout timeout), 

For asynchronous execution these function should be defined as follows:

let run f = createConnection f


let buildQuery ctx = async {
    return FinalQueryPart(ctx, generatorConfig, cleanUpTemplate)


let buildQueryWithTimeout timeout ctx = async {
    return FinalQueryPart(
              (generatorConfig |> addCommandTimeout timeout),