3

I've a query for you relating to best use of EF6 when using Winforms yet not having justification for changing things to something more modern.

I've got half a dozen classes that are based on DbContext so there's no change tracking (I've yet to understand why but that's a different question). I've a databound form that uses these classes in various ways.

At the moment I've partial classes built along the following lines - yet the duplication of code really sticks in my mind.

I'd like to make save and update generic but can't rely on the primary key naming - and as this is only a small area, can't justify attempting to do an extra layer of redirection. So, given these constraints, am I missing the obvious in how to apply interfaces/inheritance to make this work across 4 or 5 types?

    Public Shared Sub Save(TaskSession As TaskSession)
        Using db As New StaffAbsenceEntities
            If TaskSession.JobTaskSessionID = 0 Then
                db.TaskSessions.Add(TaskSession)
            Else
                Update(TaskSession, db)
            End If
            db.SaveChanges()
        End Using
    End Sub

    Private Shared Sub Update(TaskSession As TaskSession, ByRef db As StaffAbsenceEntities)
        Dim oldAbsence = db.TaskSessions.Find(TaskSession.JobTaskSessionID)
        For Each p In TaskSession.GetType.GetProperties
            If p.Name = "JobTaskSessionID" Then Continue For 'Avoid primary key or any damned entity property!
            If p.PropertyType.Name.Contains("Nullable") Then
                If Not Nullable.Equals(p.GetValue(TaskSession), p.GetValue(oldAbsence)) Then p.SetValue(oldAbsence, p.GetValue(TaskSession))
            Else
                If p.GetValue(TaskSession) <> p.GetValue(oldAbsence) Then p.SetValue(oldAbsence, p.GetValue(TaskSession))
            End If
        Next
    End Sub
End Class

Partial Class StaffAbsence
    Public Shared Sub Save(staffabsence As StaffAbsence)
        Using db As New StaffAbsenceEntities
            If staffabsence.AbsenceID = 0 Then
                db.StaffAbsences.Add(staffabsence)
            Else
                Update(staffabsence, db)
            End If
            db.SaveChanges()
        End Using
    End Sub

    Private Shared Sub Update(staffabsence As StaffAbsence, ByRef db As StaffAbsenceEntities)
        Dim oldAbsence = db.StaffAbsences.Find(staffabsence.AbsenceID)
        For Each p In staffabsence.GetType.GetProperties
            If p.Name = "AbsenceID" Then Continue For 'Avoid primary key or any damned entity property!
            If p.PropertyType.Name.Contains("Nullable") Then
                If Not Nullable.Equals(p.GetValue(staffabsence), p.GetValue(oldAbsence)) Then p.SetValue(oldAbsence, p.GetValue(staffabsence))
            Else
                If p.GetValue(staffabsence) <> p.GetValue(oldAbsence) Then p.SetValue(oldAbsence, p.GetValue(staffabsence))
            End If
        Next
    End Sub
End Class

I realise if I'd figured out how to do a further layer, I could have made a small inheritance hierarchy but I'm hoping there is a neat solution that doesn't necessarily need this as I've not sufficient time to implement it!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.