Validation in Data Classes
I am in the midst of building a simple application that extracts data from / saves data to an SQL Server database.
I have created a class (in this case classCompany) for loading and saving of data.
My question is, what is the best method for performing validation? For example, the class has a property called CompanyName of type string. CompanyName is not allowed to be null.
What I have thought might be a good approach is to incorporate the data validation into the Set portion of the Property. Thus, my Property looks like:
Public Property CompanyName() As String
Get
Return stringCompanyName
End Get
Set(ByVal value As String)
If String.IsNullOrEmpty(value) Then
'error, this is not allowed
MsgBox("Error Message", MsgBoxStyle.Exclamation, "Value Out of Range")
Else
stringCompanyName = value
End If
End Set
End Property
I thought to place it here, since then the validation need only occur once in my code. Is this an acceptable way of validating data, or is there a better / neater method?
Thanks in advance.
|