Я создал интерфейс UserRepository для взаимодействия со своим уровнем данных LINQ to SQL, но я пытаюсь выяснить, как реализовать проверку.
Вот как выглядит моя подпрограмма AddUser
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
И тогда мой контроллер просто позвонит AddUser(...)
Но я не имею ни малейшего представления о том, как реализовать проверку на стороне клиента и на стороне сервера.
(думаю, я бы предпочел использовать jQuery AJAX и выполнить всю проверку на сервере, но я полностью открыт для мнений)
EDIT:
@ vijaysylvester s ответ был идеальным, я просто подумал, что покажу пример кода того, как я его реализовал.
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