SQLProvider icon indicating copy to clipboard operation
SQLProvider copied to clipboard

Inverse of `.MapTo<'T>`

Open bisen2 opened this issue 3 years ago • 4 comments

Is your feature request related to a problem? Please describe. This library provides the .MapTo<'T> to map an entity to a record type (which is awesome), but it is not as simple to map this record type back to an entity. Mapping back can be done manually, but it can get a little cumbersome when the entity has a lot of columns.

Describe the solution you'd like It would be cool to have some sort of inverse of the .MapTo<'T> method that could take a record and return an entity.

Describe alternatives you've considered As mentioned above, it can currently be done manually. This works fine for small entities, but scales with the number of columns in the entity.

Basic Example If we have a table MyTable with column Id that is mapped by the record MyTable_Row, you can easily grab a row based on the column Id using

let getById (id: int): MyTable_Row option =
    let foundRow = query {
        for row in ctx.Dbo.MyTable do
        where (row.Id = id)
        select (Some row)
        exactlyOneOrDefault
    }
    match foundRow with
    | Some foundRow -> Some( foundRow.MapTo<MyTable_Row>() )
    | None          -> None

This method is independent of the number of columns in MyTable (other than in the definition of MyTable_Row). But, if we want to add a row to MyTable from a MyTable_Row, we need to manually specify that each column gets populated:

let insertRow (myRow: MyTable_Row) =
    let newRow = ctx.Dbo.MyTable.Create()
    newRow.Id      <- myRow.Id
    newRow.Column1 <- myRow.Column1
    newRow.Column2 <- myRow.Column2
    newRow.Column3 <- myRow.Column3
    newRow.Column4 <- myRow.Column4
    ...
    newRow.ColumnN <- myRow.ColumnN
    ctx.SubmitUpdates()

With this, not only does the definition of the record type need to scale with the number of columns in the entity, but all functions performing inserts must too.

bisen2 avatar Apr 20 '21 17:04 bisen2

Btw, I heavily recommend the type-safe ``Create(...)``(...) over .Create()

Basically if you add a non-nullable field to the table, you want your error compile-time.

Thorium avatar Apr 20 '21 18:04 Thorium

Wouldn't you still get compile time checking at newRow.ColumnN <- myRow.ColumnN? If newRow.ColumnN is an int but myRow.ColumnN is an Option<int> there should be a compile-time error on that line.

bisen2 avatar Apr 20 '21 19:04 bisen2

Yes, but what if your database changes? You drop a column or add a new non-nullable one...

Thorium avatar Apr 22 '21 08:04 Thorium

Ah that's a good point. Thanks for the pointers!

bisen2 avatar Apr 22 '21 15:04 bisen2