Coder Social home page Coder Social logo

Comments (6)

pimbrouwers avatar pimbrouwers commented on May 26, 2024

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.

AnalyzeNCode avatar AnalyzeNCode commented on May 26, 2024

Thanks for the example. I will try at my end and will let you know.
Thanks again @pimbrouwers

from donald.

AnalyzeNCode avatar AnalyzeNCode commented on May 26, 2024

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.

AnalyzeNCode avatar AnalyzeNCode commented on May 26, 2024

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.

pimbrouwers avatar pimbrouwers commented on May 26, 2024

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.

AnalyzeNCode avatar AnalyzeNCode commented on May 26, 2024

Hi @pimbrouwers, That works.
Thanks for your help. I tried every other way but nothing worked.

from donald.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.