I’ve built a UserRepository interface to communicate with my LINQ to SQL Data layer, but I’m trying to figure out how to implement validation.
Here is what my AddUser subroutine looks like
Public Sub AddUser(ByVal about As String, ByVal birthdate As DateTime, ByVal openid As String, ByVal regionid As Integer, ByVal website As String) Implements IUserRepository.AddUser
Dim user = New User
user.About = about
user.BirthDate = birthdate
user.LastSeen = DateTime.Now
user.MemberSince = DateTime.Now
user.OpenID = openid
user.RegionID = regionid
user.UserName = String.Empty
user.WebSite = website
dc.Users.InsertOnSubmit(user)
dc.SubmitChanges()
End Sub
And then my controller will simply call AddUser(...)
But I haven’t the foggiest idea on how to implement both client side and server side validation on this.
(I think I would prefer to use jQuery AJAX and do all of the validation on the server, but I’m totally open to opinions)
EDIT:
@vijaysylvester‘s answer was perfect, I just thought I would show a code example of how I implemented it.
UserService.vb
Imports System.ComponentModel.DataAnnotations
Namespace Domain
<MetadataType(GetType(UserMetaData))> _
Partial Public Class User : End Class
Public Class UserMetaData
<Required(ErrorMessage:="Username is required.")> _
<StringLength(30, ErrorMessage:="Username cannot exceed 30 characters.")> _
Public UserName As String
<StringLength(50, ErrorMessage:="Email Address cannot exceed 50 characters.")> _
<RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9+)*\\.([a-z]{2,4})$", ErrorMessage:="Not a valid email address.")> _
Public Email As String
<StringLength(256, ErrorMessage:="Web Address cannot exceed 256 characters.")> _
Public WebSite As String
<StringLength(4000, ErrorMessage:="Profile cannot exceed 4000 characters.")> _
Public About As String
End Class
#Region "Interface"
Public Interface IUserService
Sub UpdateUser(ByVal id As Integer, ByVal about As String, ByVal birthdate As DateTime, ByVal openid As String, ByVal regionid As Integer, ByVal username As String, ByVal website As String)
Sub UpdateUserReputation(ByVal id As Integer, ByVal AmountOfReputation As Integer)
Sub CloseUser(ByVal id As Integer)
Sub OpenUser(ByVal id As Integer)
Function GetAllUsers() As IList(Of User)
Function GetUserByID(ByVal id As Integer) As User
End Interface
#End Region
#Region "Service"
Public Class UserService : Implements IUserService
Private _UserRepository As IUserRepository
Public Sub New(ByVal UserRepository As IUserRepository)
_UserRepository = UserRepository
End Sub
Public Sub UpdateUser(ByVal id As Integer, ByVal about As String, ByVal birthdate As Date, ByVal openid As String, ByVal regionid As Integer, ByVal username As String, ByVal website As String) Implements IUserService.UpdateUser
Dim user = _UserRepository.GetUserByID(id)
user.About = about
user.BirthDate = birthdate
user.RegionID = regionid
user.UserName = username
user.WebSite = website
_UserRepository.UpdateUser(user)
End Sub
''# And the rest of my methods.
End Class
End Namespace
Though it is meaningful to do validations in server side and client side manually , I would say one can go with open source validation Frameworks such as XVal .
Check out the link below.
XVal reference
It is as easy as just configuring the properties that needs to be validated.
E.g.,
The code snippet above checks if Name is a valid string and its size is not more than 50.
There are numerous built in validators (including Regular Expressions) , which we can make use of for different data types such as Date.
Hope this helps.
Thanks ,
Vijay.