Coder Social home page Coder Social logo

Comments (6)

JordanMarr avatar JordanMarr commented on June 9, 2024 1

Personally, I will always opt for an extra type or function over SRTP for simplicity.
I think using an anonymous record would be the better play in this case:

let queryByIds<'T> (ids: int list) tableName (conn: IDbConnection) = 
  async {
    let query = 
      select {
          for row in table'<{| Id: int |}> tableName do
          where (isIn row.Id ids)
      }

    return! conn.SelectAsync<'T>(query) |> Async.AwaitTask
  }
let! dogs = queryByIds<Dog> [1;2;3] "Dogs" conn

from dapper.fsharp.

1eyewonder avatar 1eyewonder commented on June 9, 2024 1

Yep, that makes sense. Thank you so much for your help!

from dapper.fsharp.

JordanMarr avatar JordanMarr commented on June 9, 2024

I'm pretty sure that only the SelectAsync<'T> method reflects the columns to read.
So rather than using an SRTP, you could probably just create a single partial object that has only the Id field for querying, and then pass in a generic argument of the full table class that contains all the properties you actually want to read.

[<CLIMutable>]
type IdColumn = { Id: int  }

let queryByIds<'T> (ids: int list) tableName (conn: IDbConnection) = 
  async {
    let query = 
      select {
          for row in table'<IdColumn> tableName do
          where (isIn row.Id ids)
      }

    return! conn.SelectAsync<'T>(query) |> Async.AwaitTask
  }
let! dogs = queryByIds<Dog> [1;2;3] "Dogs" conn

from dapper.fsharp.

1eyewonder avatar 1eyewonder commented on June 9, 2024

Wow, I feel stupid for not thinking of this šŸ¤¦ā€ā™‚ļø. Thank you, this makes sense!

from dapper.fsharp.

JordanMarr avatar JordanMarr commented on June 9, 2024

Not stupid at all! Iā€™m just very familiar with the internals of the codebase.

from dapper.fsharp.

1eyewonder avatar 1eyewonder commented on June 9, 2024

So more of a philosophical question now. Is the SRTP support still a valid request/issue for this repo? While the 'ID' use case is very straight forward, does it make sense to suggest this route for other alternatives? Silly examples below.

[<CLIMutable>]
type IdColumn = { Id: int }

[<CLIMutable>]
type IdAndFoo1Column = { Id: int; Foo1: int; }

[<CLIMutable>]
type IdFoo1Foo2Column = { Id: int; Foo1: int; Foo2: int }

So while code quantity wouldn't really differ between the DTO and the SRTP routes, the pain of naming is placed on users(devs). The STRP would just allow users to focus on what is needed for the generic queries rather than come up with a DTO for each use case. (Granted I'm not sure how often users come across this since I haven't really dealt with it until the other day). I'm sure it may just end up being diminishing returns trying to pursue this route, but I figured I'd ask the question while the issue was still open. If you think it's not worth pursuing, I can go ahead and close the issue.

from dapper.fsharp.

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.