Comments (6)
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.
Yep, that makes sense. Thank you so much for your help!
from dapper.fsharp.
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.
Wow, I feel stupid for not thinking of this š¤¦āāļø. Thank you, this makes sense!
from dapper.fsharp.
Not stupid at all! Iām just very familiar with the internals of the codebase.
from dapper.fsharp.
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)
- Add TypeHandler for fieldless and single case DUs
- Support union/union all cases HOT 3
- SQLite.OptionTypes.register seems to be missing `float`
- Is there a query that implements something like `QueryFirst`, `QueryFirstOrDefault`? HOT 2
- Question about the Where operator AND and OR HOT 3
- Are multi column joins no longer supported? HOT 9
- Sqlite and multiple type registration HOT 3
- Structures in columns HOT 3
- Working with auto increment IDs HOT 3
- namespace 'LinqBuilders' is not defined HOT 8
- Postgresql Full Text Searching HOT 1
- how to use transactions in DapperFSharp? HOT 3
- Adding pipeline API? HOT 1
- How do typehandlers in F# work?
- "count" problem with inner join in v4.5.1+ HOT 2
- `CLIMutable` seems to be necessary for some records HOT 4
- Record attribute to use capitalized word for record key on F# but lowercase for database HOT 2
- Batch insert support HOT 6
- Support select with `IAsyncEnumerable<'t>` result
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 dapper.fsharp.