I am refactoring some code that used reflection to map a SqlCommand to a model. The new code uses the Dapper framework and works perfectly fine:
Public Shared Function GetModel(Of T As New)(cmd As SqlCommand) As T
Try
Dim params = GetParametersDictionary(cmd)
Dim result As Object
Using conn As New SqlConnection(ConnectionString)
result = conn.Query(Of T)(cmd.CommandText, params).FirstOrDefault()
End Using
Return result
Catch ex As Exception
ErrorsLogger.LogException(ex)
Throw
End Try
End Function
However in the old code I had an edge case for string properties and DbNull values. Something like:
If IsTypeOfString(prop) AndAlso IsDbNull(value) Then
prop.SetValue(obj, "")
End If
The new code does not convert DbNull (NULL) values to an empty string. Instead it leaves the value just not set aka Nothing, breaking some code elsewhere.
So my question is how to change this function to convert NULL values to an empty string? Preferably without reflection.
Things tried / considered / constraints:
Dapper is of the opinion that null
and ""
are different things, and actively tries not to conflate the two. You could post-process the results, perhaps.