2

I am trying to throw an exception in my BLL when there is no corresponding carID for the license plate number I've entered in a text box.

My DAL looks like this:

Public Class DALCar
    Private dc As New cars_modelDataContext

    Public Function getCarIdByLicenePlate(ByVal licensePlate_input As String) As String
        Dim result = (From car In dc.Cars
                    Where car.License_Plate = licensePlate_input
                    Select car.License_Plate).Single
        Return result
    End Function

End Class

And this is my BLL:

Public Class BLLCar
    Private DALcar As New DALCar

    Public Function getCarIdByLicenePlate(ByVal licensePlate_input As String) As String
        Dim carID As String = DALcar.getCarIdByLicensePlate(chassisNo_input)
    End Function

End Class

So when there is no carID with this specific license plate an exception is throwed in my DAL, but how can I throw this exception in my BLL instead of in my DAL?

Wout
  • 21
  • 1

2 Answers2

0

Because you're using Enumerable.Single in your LINQ expression. It throws an exception if there is more than one element in the sequence or if the sequence is empty.

If you can assume the sequence will always contains 0 or 1 element then you can replace Single with FirstOrDefault (see later for more on this). It'll return the first element in the sequence or Nothing if sequence is empty.

In this case you can check for Nothing in your BLL and throw the appropriate exception there.

Like this in your DAL:

Public Class DALCar
    Private dc As New cars_modelDataContext

    Public Function getCarIdByLicenePlate(ByVal licensePlate_input As String) As String
        Dim result = (From car In dc.Cars
                    Where car.License_Plate = licensePlate_input
                    Select car.License_Plate).FirstOrDefault
        Return result
    End Function
End Class

And this in your BLL:

Public Class BLLCar
    Private DALcar As New DALCar

    Public Function getCarIdByLicenePlate(ByVal licensePlate_input As String) As String
        Dim carID As String = DALcar.getCarIdByLicensePlate(chassisNo_input)
        If carId = Nothing Then
            Throw New ArgumentException("There is no match.")
        End If
    End Function
End Class

If your query may returns more than one element than you have to consider if this is an error or not. If it's allowed and you want to process (return) the first one then go on with FirstOrDefault. If it's an error then you should return an enumeration from your DAL and to check the number of items in your BLL (otherwise, using Single, you'll still throw inside DAL).

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
0

Use FirstOrDefault instead of Single

Public Function getCarIdByLicenePlate(ByVal licensePlate_input As String) As String
    Dim result = (From car In dc.Cars
                Where car.License_Plate = licensePlate_input
                Select car.License_Plate).FirstOrDefault 
    Return result


 Public Function getCarIdByLicenePlate(ByVal licensePlate_input As String) As String
    Dim carID As String = DALcar.getCarIdByLicensePlate(chassisNo_input)
    If carID = Nothing Then
       Throw New Exception(String.Format("Can't find car id for chassisNo : {0}", chassisNo_input))
    End If
End Function
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216