Comments (7)
For your convenience, this is the one I created for my current project.
I considered adding some of this stuff to the library, but wanted to wait and see what is included in F# 6.0 first.
/// Utility functions for working with tasks.
module Task
open FSharp.Control.Tasks.V2
open System.Threading.Tasks
let bind (f : 'a -> Task<'b>) (x : Task<'a>) =
task {
let! x = x
return! f x
}
/// Maps the result of Task<'a> to Task<'b>
let map<'a, 'b> (f : 'a -> 'b) (tsk: Task<'a>) =
task {
let! taskValue = tsk
return f taskValue
}
let mapSeq<'a, 'b> (f : 'a -> 'b) (itemsTask: Task<'a list>) =
task {
let! items = itemsTask
return items |> Seq.map f
}
let mapArray<'a, 'b> (f : 'a -> 'b) (itemsTask: Task<'a seq>) =
task {
let! items = itemsTask
return items |> Seq.map f |> Seq.toArray
}
let awaitIgnore (tsk: Task<'T>) =
task {
let! result = tsk
result |> ignore
}
:> Task
let fromResult value =
System.Threading.Tasks.Task.FromResult value
from sqlhydra.
Thank you for the insights, very helpful for my understanding. π
from sqlhydra.
I almost always add my own Task.fs
with Task.map
to my data projects since itβs only a few lines.
Alternatively, there is also the non async overload of Insert
if you donβt care about async which blocks for you and automatically unwraps the task.
Also, you are still free to use TaskBuilder
in your project -- I just removed the dependency so that you are not required to use it (since some people are already using F# 6.0, which would make TaskBuilder
redundant for them).
from sqlhydra.
Hi!
That is a great question. I chose to work with tasks because
- The ADO.NET libraries use tasks
- SqlKata also uses tasks
- F# 6 comes with a brand new TaskBuilder out-of-the-box which will make working with tasks even better
All of other the QueryContext
data methods use tasks. The method you referenced happen to be the few places where I actually needed to await a result, therefore it was necessary to call Async.AwaitTask
(see the F# Async Programming docs).
For those cases, I also had to call StartImmediateAsTask
; otherwise, they would be the only methods that return async instead of task.
One other point of interest is that I initially used a third party TaskBuilder
library, but then decided that there weren't enough calls to justify forcing that dependency.
from sqlhydra.
Is this different from accessing Task.Result
? "If the Result property is accessed before the computation finishes, the property blocks the calling thread until the value is available."
from sqlhydra.
Yes, this approach is actually asynchronous, whereas Task.Result
would not be because it would block (wait) for the database response before returning the result.
from sqlhydra.
And thus it's up to the caller of InsertAsync
to block or not? For lack of a Task.map
or similar (which a task builder lib or v6 would provide). Makes sense. π
from sqlhydra.
Related Issues (20)
- Add documentation and setup configuration on how to clone the repo, build the project, and run tests. HOT 2
- Creating sqlhydra file updates the wrong .fsproj file HOT 9
- SQL Server Temporal Tables Select Issue HOT 8
- Retrieve Multiple result sets (UNION ALL) using SqlHydra.Query HOT 1
- Insert and Update Builders break when column name is the same as another table. HOT 2
- Mapping object with list of objects 1 to n relationship HOT 4
- Best practice for multiple environments and the config.toml file HOT 7
- Design patterns for DDD HOT 8
- Is it possible to support materialized views from Postgres? HOT 22
- Generate types in GitHub workflow generates no table types HOT 5
- Show filter info in SqlHydra.Cli command line output HOT 1
- Custom pooling with SqlHydra HOT 8
- SqlHydra.Cli doesn't map Sqlite longvarchar column types HOT 5
- Error after upgrading V2.5.0: 'Microsoft.FSharp.Core.FSharpOption`1 is not a F# record type. HOT 9
- Public Schema Not Properly Escaped for NpgSQL in v2.4+ HOT 2
- Query with `leftJoin` breaks in v2.5.0 HOT 3
- Is it possible call `date_trunc` from PostgreSQL? HOT 7
- NoWarn 3391 HOT 3
- System.InvalidOperationException: The input sequence has an insufficient number of elements. HOT 3
- Support arrays HOT 3
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 sqlhydra.