Comments (6)
Hello again Subhash!
The test project shows some very basic ways you can use the async functionality, for example:
let sql = "
SELECT author_id, full_name
FROM author
WHERE author_id IN (1,2)"
let authors =
conn
|> Db.newCommand sql
|> Db.Async.query Author.FromReader
|> Async.AwaitTask
|> Async.RunSynchronously
authors
|> List.map (printfn "%A")
However, if you are fortunate enough to be using FSharp.Core version 6. Then you can do:
let sql = "
SELECT author_id, full_name
FROM author
WHERE author_id IN (1,2)"
task {
let! authors =
conn
|> Db.newCommand sql
|> Db.Async.query Author.FromReader
authors
|> List.map (printfn "%A")
}
from donald.
Thanks for the example. I will try at my end and will let you know.
Thanks again @pimbrouwers
from donald.
Hi @pimbrouwers
I have a question in mind regarding the Async based implementation in your library.
You have use Async keyword like ""Db.Async.query" but the result it return is Task.
As per my understanding, the database-access is I/O operations and so it's falls under Asynchronous workflow, rather than Thread & Task based workflow which are best for CPU bound works.
Any specific reason to return Task? Maybe you are using the F# 6 task based implementation.
from donald.
I have tested according to your examples. It's working according to first example, which is Async.AwaitTask and then RunSynchronously but failing with error "Connection is closed" for the task {} of F# 6.
And Async.RunSynchronously will block the main thread while executing the request to I don't think it's helpful to execute an asynchronous task. I mean it's almost like a synchronous execution.
Please note that, the code is in repository method and I am returning this method result to the another method in workflow.
The gist of my code:
Repository.fs
exception PersistenceException of DbError
let raisePersistenceException dbErr = raise (PersistenceException dbErr)
type ApplicationDAO = { Id: Guid; Name: string }
let toDbOkOrRaise (taskResult: Task<Result<'a, DbError>>) =
task {
let! result = taskResult
return
match result with
| Ok data -> data
| Error dbErr -> dbErr |> raisePersistenceException
}
let ofDataReader (dbReader: IDataReader) : ApplicationDAO =
let id = dbReader.ReadGuid "Id"
let name = dbReader.ReadString "Name"
{ Id = id; Name = name }
let getApplicationsByName =
fun applicationName->
let sqlQuery =
"SELECT Id, Name FROM [ApplicationMaster] WHERE [Name] = @appName"
let param =
[ "appName", SqlType.String(applicationName) ]
use conn = new SqlConnection(ConnectionString)
task {
let! result =
conn
|> Db.newCommand sqlQuery
|> Db.setParams param
|> Db.Async.query ofDataReader
|> toDbOkOrRaise
return result
}
Workflow.fs
let GetDbResult() =
task { return! Repository.getApplicationsByName "TestApplication" }
from donald.
Hi again!
The problem is the "use" statement. You should pass the connection in as a param to the function allowing external control of the connection.
from donald.
Hi @pimbrouwers, That works.
Thanks for your help. I tried every other way but nothing worked.
from donald.
Related Issues (20)
- GetDateTimeOffset Signature is string -> DateTime HOT 1
- Unable to execute query without a transaction HOT 6
- Add a wrapper around ExecuteScalar HOT 6
- TransactionBuilder HOT 9
- CommandBehavior.SequentialAccess causes error with SqlHydra readers HOT 5
- Cast exception when reading DateTimeOffset with System.Data.SqlClient HOT 4
- [Help] How to parse results with sequential access? HOT 2
- Db.Async.execMany throws "A command is already in progress" with NPGSQL HOT 5
- v.7.0.0 HOT 9
- FsDto lib review HOT 2
- Who is @dysme on the landing page? HOT 3
- CancellationToken support
- Support of transaction in newCommand in Fluent syntax HOT 3
- Require help on query HOT 7
- Support or Transition to Microsoft.Data.SqlClient HOT 3
- SQLite: What SQL Type should I choose for a JSON column? HOT 2
- Possible issue with Db.Async.exec throwing instead of returning a Result HOT 1
- How to deal with DB enums in Donald? HOT 1
- Doc comment error for newSproc HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from donald.