Dzoukr / Dapper.FSharp

Lightweight F# extension for StackOverflow Dapper with support for MSSQL, MySQL, PostgreSQL, and SQLite
MIT License
374 stars 35 forks source link

Synchronous/False Synchronous Example #23

Closed dredgy closed 3 years ago

dredgy commented 3 years ago

Not really an issue, just would like a good syntactical example of how to use these queries in a synchronous or false synchronous (await) way. I.e - I have a select{} statement and I want to get the results collection in the same function. I am new to .NET and F# so it’s not always easy to follow these things or even to know what to learn in specific cases like this! My app is very synchronous and linear - nothing can happen until the query is executed in most cases.

Dzoukr commented 3 years ago

Hi @dredgy, I understand ... and welcome to F# community! 🚀

I would still strongly suggest to get used to async style as soon as possible, because it's the default way of handling IO in nearly any language these days. But if you really need synchronous example, I can give you one:

insert {
    table "Persons"
    value {| Name = "Roman"; Surname = "Provaznik" |}
} |> conn.Insert // <-- note here, no Insert instead of InsertAsync

Also check the examples in test project https://github.com/Dzoukr/Dapper.FSharp/tree/master/tests/Dapper.FSharp.Tests

dredgy commented 3 years ago

Thanks! That’s interesting, my connection doesn’t have that as an option, will investigate further! Though I am perfectly happy to use async, just in other languages I can use it in a way that simulate sync without taking up too many lines and haven’t figured out how to do that in F# yet, will read the tests now. Thanks for the help, I really appreciate the library!

Dzoukr commented 3 years ago

Oh, my fault! You are absolutely right - I didn't implement sync versions (probably on purpose to force people to use - correct - async version) 😄

Dzoukr commented 3 years ago

But yeah... async in F# is really easy! You can use one of the great libraries like https://github.com/rspeele/TaskBuilder.fs to have task computation expression available, where you can just use let! instead of let and it works.

Or you can do it "manually":

insert {
    table "Persons"
    value {| Name = "Roman"; Surname = "Provaznik" |}
} 
|> conn.InsertAsync 
|> Async.AwaitTask 
|> Async.RunSynchronously
dredgy commented 3 years ago

Thanks! |> Async.AwaitTask |> Async.RunSynchronously is what I needed. Appreciate all the help!