S
Scott Rymer
I'm working on a WCF service with a LINQ to SQL back end. I'm trying to
figure out how to handle exceptions in the UI that are thrown by the
OnPropertyChanging event in the dbml.
In my dbml, I have some validation logic:
Namespace MyProject.Data
Partial Public Class User
Private Sub OnBusinessPhoneChanging(ByVal value As String)
If Not Utilities.ValidatePhone(value) Then
Throw New Exception("Invalid Phone number")
End If
End Sub
Private Sub OnEmailChanging(ByVal value As String)
If Not Utilities.ValidateEmail(value) Then
Throw New Exception("Invalid Email Address")
End If
End Sub
Private Sub OnValidate(ByVal action As
System.Data.Linq.ChangeAction)
Using db As New MyProjectDataContext
Dim usr = (From u In db.Users _
Where u.Username = _Username Or u.Email = _Email
_
Select u).FirstOrDefault
If Not usr Is Nothing Then
If usr.Username = _Username Then
Throw New Exception("A user already exists with that
username")
ElseIf usr.Email = _Email Then
Throw New Exception("A user already exists with that
email address")
End If
End If
End Using
End Sub
End Class
End Namespace
In my UI (ASP.NET website), I have:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles Button1.Click
Using mbr As New MembershipServiceClient
Dim u As New MembershipService.User
u.Email = txtEmail.Text
Try
u = mbr.SaveUser(u)
GridView2.DataSource = mbr.GetUsers
GridView2.DataBind()
Catch ex As FaultException(Of MyProjectFault)
Label1.Text = ex.Detail.Message
Catch ex As Exception
Label1.Text = ex.Source & "::" & ex.Message
End Try
End Using
End Sub
And SaveUser:
Public Function SaveUser(ByVal user As MyProject.Data.User) As
MyProject.Data.User
If user.Timestamp Is Nothing Then
db.Users.InsertOnSubmit(user)
Else
db.Users.Attach(user, True)
End If
Try
db.SubmitChanges()
Catch ex As Exception
Dim err As New MyProjectFault()
err.FaultCode = FaultType.UNKNOWN_ERROR
err.Message = ex.Message
If Not ex.InnerException Is Nothing Then err.Details =
ex.InnerException.ToString
Throw New FaultException(Of MyProjectFault)(err)
End Try
Return user
End Function
When I call mbr.SaveUser(u) in the UI with an email address that exists
(hitting OnValidate), I get a proper FaultException returned to the UI which
shows the proper error message. However, if I submit an invalid email
address (hitting OnEmailAddressChanging), I get an "server unable to process
request" message instead of my "Invalid email address" message at the UI.
If I turn on includeExceptionDetailInFaults, then I get the proper message
but I will eventually need to turn this off. Where can I catch the
exception in the dbml for the OnPropertyChanging events so I can convert it
to a fault?
Should I be validating in the dbml (Data Classes) in the first place? Or
should I be checking everything in the SaveUser method in the business
layer?
Thanks!
-Scott
figure out how to handle exceptions in the UI that are thrown by the
OnPropertyChanging event in the dbml.
In my dbml, I have some validation logic:
Namespace MyProject.Data
Partial Public Class User
Private Sub OnBusinessPhoneChanging(ByVal value As String)
If Not Utilities.ValidatePhone(value) Then
Throw New Exception("Invalid Phone number")
End If
End Sub
Private Sub OnEmailChanging(ByVal value As String)
If Not Utilities.ValidateEmail(value) Then
Throw New Exception("Invalid Email Address")
End If
End Sub
Private Sub OnValidate(ByVal action As
System.Data.Linq.ChangeAction)
Using db As New MyProjectDataContext
Dim usr = (From u In db.Users _
Where u.Username = _Username Or u.Email = _Email
_
Select u).FirstOrDefault
If Not usr Is Nothing Then
If usr.Username = _Username Then
Throw New Exception("A user already exists with that
username")
ElseIf usr.Email = _Email Then
Throw New Exception("A user already exists with that
email address")
End If
End If
End Using
End Sub
End Class
End Namespace
In my UI (ASP.NET website), I have:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles Button1.Click
Using mbr As New MembershipServiceClient
Dim u As New MembershipService.User
u.Email = txtEmail.Text
Try
u = mbr.SaveUser(u)
GridView2.DataSource = mbr.GetUsers
GridView2.DataBind()
Catch ex As FaultException(Of MyProjectFault)
Label1.Text = ex.Detail.Message
Catch ex As Exception
Label1.Text = ex.Source & "::" & ex.Message
End Try
End Using
End Sub
And SaveUser:
Public Function SaveUser(ByVal user As MyProject.Data.User) As
MyProject.Data.User
If user.Timestamp Is Nothing Then
db.Users.InsertOnSubmit(user)
Else
db.Users.Attach(user, True)
End If
Try
db.SubmitChanges()
Catch ex As Exception
Dim err As New MyProjectFault()
err.FaultCode = FaultType.UNKNOWN_ERROR
err.Message = ex.Message
If Not ex.InnerException Is Nothing Then err.Details =
ex.InnerException.ToString
Throw New FaultException(Of MyProjectFault)(err)
End Try
Return user
End Function
When I call mbr.SaveUser(u) in the UI with an email address that exists
(hitting OnValidate), I get a proper FaultException returned to the UI which
shows the proper error message. However, if I submit an invalid email
address (hitting OnEmailAddressChanging), I get an "server unable to process
request" message instead of my "Invalid email address" message at the UI.
If I turn on includeExceptionDetailInFaults, then I get the proper message
but I will eventually need to turn this off. Where can I catch the
exception in the dbml for the OnPropertyChanging events so I can convert it
to a fault?
Should I be validating in the dbml (Data Classes) in the first place? Or
should I be checking everything in the SaveUser method in the business
layer?
Thanks!
-Scott