Ciao a tutti,
Ho notato con piacere che gestire le cose in oggetto è una gran scocciatura.
Io ho impostato le cose in questo modo :
Le proprietà delle mie entity in questo modo :
codice:
Public Class contatore
Public id As Integer?
Public idMarca As Integer?
Public idModello As Integer?
Public matricola As String
Public tipoContatore As String
Public annoScadenza As Integer?
Public dataInstallazione As Date
Public dataRimozione As Date
Public annoCostruzione As Integer?
Public omologazione As String
Public nCifre As Integer?
Public idPdr As Integer?
.....
Poi nella mia classe db mi sono avvalso di 3 metodi :
codice:
'---------------------------------------------------
' Funzioni utili a trattare variabili da e verso DB
'---------------------------------------------------
Public Function getValueOrNothing(ByVal val)
If Convert.IsDBNull(val) Then
Return Nothing
Else
Return val
End If
End Function
Public Function setValueOrDbNull(ByVal val)
If IsNothing(val) Then
Return DBNull.Value
Else
Return val
End If
End Function
Public Function getValueOrDefault(ByRef val, ByVal defaul)
If IsNothing(val) Or IsDBNull(val) Then
Return defaul
Else
Return val
End If
End Function
getValueOrNothing la uso dopo ogni select andandomi a valorizzare le mie proprietà delle entita :
Es :
codice:
Private Function load() As Boolean
Try
Dim dr As SqlDataReader
dr = db.getDataReader()
If dr.Read() = False Then
Return False
End If
Me.id = db.getValueOrNothing(dr("cont_id"))
Me.idMarca = db.getValueOrNothing(dr("cont_idMarca"))
Me.idModello = db.getValueOrNothing(dr("cont_idModello"))
Me.matricola = db.getValueOrNothing(dr("cont_matricola"))
Me.tipoContatore = db.getValueOrNothing(dr("cont_tipoContatore"))
......
Catch ex As Exception
Me.lastError = ex.Message
Return False
End Try
End Function
setValueOrDbNull questa la uso quando devo comporre la insert o update sui parametri :
codice:
Dim parameters() As SqlParameter = New SqlParameter() _
{
New SqlParameter("@idMarca", SqlDbType.Int) With {.Value = setValueOrDbNull (Me.idMarca)},
New SqlParameter("@idModello", SqlDbType.Int) With {.Value = setValueOrDbNull Me.idModello)},
New SqlParameter("@matricola", SqlDbType.VarChar, Me.matricola.Length) With {.Value = setValueOrDbNull Me.matricola)},
New SqlParameter("@tipoContatore", SqlDbType.VarChar, Me.tipoContatore.Length) With {.Value = setValueOrDbNull (Me.tipoContatore)},
......
Infine getValueOrDefault quando devo stampare un valore dalla mia entità :
tipoContatore(me.tipoContatore,"")
Impressioni???
Voi come agite??? A me pare tutto un pò macchinoso.