It's generally better to avoid additional work for GC, but in this case fsharp type checking forces writing box even for reference types, which is inconvenient, as params are of this type Parameters : (string * obj) list. I have do this for both ints and strings: [
p "paramInt" (box paramInt)
p "paramString" (box paramString)
]
It is possible to avoid boxing in F#, but there is no point because the value is going to get boxed anyway by the database client (SqlParameter only takes objects as values). Avoiding boxing in the F# code will just bloat the code to no benefit.
Edit: I just re-read the preceding conversation and realized I answered the wrong question. Both yours and @lanayx
previously.
No it is not a problem at all for the p helper to box the value, to avoid repeating box every time. And in fact that is exactly how I defined it in my own library. I believe it was just an oversight in the post. I will update the post.
Leaving the original response below.
I created this tiny helper due to a couple of factors.
ADO.NET boxes all parameters values to obj anyway
F# will not auto-upcast the values in the list, which can lead to type inference compile errors.
I recall there being an issue on a dotnet repo about having the option to not box sql parameters. But I am not sure if typed SQL parameters ever made it into a release.
So without the helper, the parameter list looked like this:
Not terrible, but removing characters , box to add p on the front seemed a favorable trade.
That ended up being fortuitous because later, using a tuple was not quite enough. Sometimes it is necessary to declare the database type of the parameter. For example, when the value is null. (With Npgsql also when the column is jsonb.) So the parameter tuple became a full record type.
p can be changed to work with this, and in a way that does not break existing defined queries. And it will be drastically shorter than spelling out the full SqlParam record.
Hi Kasey, thank you for the quick and helpful reply.
Moving from the F# type providers to Dapper has been a real pleasure for me. For the price of giving up the compile time type checking, I now have simple reusable data transfer objects (DTOs) and functions to convert these from domain and back.
Best regards
Roland
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
It's generally better to avoid additional work for GC, but in this case fsharp type checking forces writing box even for reference types, which is inconvenient, as params are of this type Parameters : (string * obj) list. I have do this for both ints and strings:
[
p "paramInt" (box paramInt)
p "paramString" (box paramString)
]
It is possible to avoid boxing in F#, but there is no point because the value is going to get boxed anyway by the database client (SqlParameter only takes
object
s as values). Avoiding boxing in the F# code will just bloat the code to no benefit.I see, thank you for the clarifications
Hi Kasey, thank you for the useful post.
Is there any problem with or disadvantage to defining p as:
let p name value = (name, (box value))
Which allows you to just type
[ p "paramInt" paramInt ]
Edit: I just re-read the preceding conversation and realized I answered the wrong question. Both yours and @lanayx previously.
No it is not a problem at all for the
p
helper to box the value, to avoid repeatingbox
every time. And in fact that is exactly how I defined it in my own library. I believe it was just an oversight in the post. I will update the post.Leaving the original response below.
I created this tiny helper due to a couple of factors.
obj
anywaySo without the helper, the parameter list looked like this:
Not terrible, but removing characters
, box
to addp
on the front seemed a favorable trade.That ended up being fortuitous because later, using a tuple was not quite enough. Sometimes it is necessary to declare the database type of the parameter. For example, when the value is null. (With Npgsql also when the column is
jsonb
.) So the parameter tuple became a full record type.p
can be changed to work with this, and in a way that does not break existing defined queries. And it will be drastically shorter than spelling out the full SqlParam record.Hi Kasey, thank you for the quick and helpful reply.
Moving from the F# type providers to Dapper has been a real pleasure for me. For the price of giving up the compile time type checking, I now have simple reusable data transfer objects (DTOs) and functions to convert these from domain and back.
Best regards
Roland