I have a really contrived example to as how my business objects load their data. However I’d like to prevent the situation demonstrated below. I know there are shortcomings here, but it’s just how it is (so far)! How can I achieve this without restructuring to interfaces etc?
Public Class Car
Public Property Passengers As Integer
Public Shared Sub Fill(c As Car, r As SqlDataReader)
Passengers = r.GetInt32(0)
End Sub
End Class
Public Class UsedCar
Inherits Car
Public Property PreviousOwner As String
Public Shared Sub FillUsed(l As Limo, r As SqlDataReader)
Passengers = r.GetInt32(0)
PreviousOwner = r.GetString(1)
End Sub
End Class
Public Sub CreateVehicles()
Dim c As New Car()
Car.FillWithStuff(c, GetSqlReader(carId))
Dim l As New UsedCar()
UsedCar.FillUsed(l, GetSqlReader(usedCarId)) 'Great
Car.Fill(l, GetSqlReader(usedCarId)) 'No! << Stop this at once!
UsedCar.Fill(l, GetSqlReader(usedCarId)) '<<< Even more wrong!
End Sub
There is no way to require that except to throw an exception. For instance:
However, the simplified example you provide doesn’t adequately explain why, in your situation, a limo is not a valid car. If, in fact, it is not a valid car, then it should not inherit from car. It is likely that your code ought to be refactored so that car and limo are two unrelated classes that both share the same dependency business class for common functionality.