Skip to content

Configuration

Jacek edited this page Oct 21, 2019 · 2 revisions

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 with timeout = (Some timeout) } commandText

that generate code executing inline sql, and:

let proc name = 
    proc generatorConfig name

and

let procWithTimeout timeout name = 
    proc { generatorConfig with timeout = (Some 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 = DbAction.run createConnection f

and, additionally, when composite queries are used:

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

or with timeout:

let buildQueryWithTimeout timeout ctx = 
    FinalQueryPart(
        ctx, 
        { generatorConfig with timeout = (Some timeout) }, 
        cleanUpTemplate)

For asynchronous execution these function should be defined as follows:

let run f = AsyncDb.run createConnection f

and

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

or

let buildQueryWithTimeout timeout ctx = async {
    return FinalQueryPart(
              ctx, 
              { generatorConfig with timeout = (Some timeout) }, 
              cleanUpTemplate)
}

Of course, it's possible to create DataContext and utilize it within the use statement:

let createDataContext () = 
    let c = createConnection()
    c.Open()
    c
 
use ctx = createDataContext()
let blog = getBlog id ctx
    ...

but I don't recomend this approach.

Clone this wiki locally