CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: VB.NET

Validation across asp.net, silverlight RIA, MVC, WPF etc

583 Views
Copy Code Show/Hide Line Numbers
' I watched Karl's video about validation with custom rules, and wondered
' why Silverlight, ASP.NET and ASP.NET MVC had DataAnnotations, and
' WPF only had IDataErrorInfo. So I thought of using EF4 as the model,
' and generating a metadata class like in RIA, MVC, etc for WPF and a
' inheritable generic class to convert the validations between IDataErrorInfo
' and the DataAnnotation attributes. 
'
' This way validation can happen the same way for each type of client!
'
' Let me know what you think please?
'
' Thanks, Rick
 
 
''' <summary>
''' Validating translater class for WPF using a sidecar metadata class like RIA services
''' </summary>
''' <typeparam name="T">The model class</typeparam>
''' <typeparam name="U">The model metadata class which must have the data 
''' annotations on the property, and the property must exist.</typeparam>
''' <remarks></remarks>
Public Class Validatable(Of T, U)
    Implements IDataErrorInfo
 
    Public Overridable ReadOnly Property [Error] As String Implements System.ComponentModel.IDataErrorInfo.Error
        Get
            Throw New NotImplementedException()
        End Get
    End Property
 
    ' Needed to get the property value. Is there a better way to do this?
    Public Property ParentObject() As Object
 
    Default Public Overridable ReadOnly Property Item(ByVal columnName As String) As String Implements System.ComponentModel.IDataErrorInfo.Item
        Get
            If ParentObject Is Nothing Then Return String.Empty
 
            ' Get the property from the metadata class and DataAnnotations attributes
            Dim prop As PropertyInfo = GetType(U).GetProperty(columnName)
            Dim validationMap = prop.GetCustomAttributes(GetType(ValidationAttribute), True)
 
            For Each v As ValidationAttribute In validationMap
                Try
                    ' Get the actual value and validate it
                    Dim pp As PropertyInfo = ParentObject.GetType().GetProperty(columnName)
                    Dim value As Object = pp.GetValue(ParentObject, Nothing)
                    v.Validate(value, columnName)
                Catch ex As Exception
                    Return ex.Message
                End Try
            Next
 
            Return Nothing
        End Get
    End Property
 
End Class
 
' This is the fake model from EF4
Partial Public Class Customer
 
    Public Property FirstName As String
    Public Property LastName As String
 
End Class
 
' This is the "WPF Helper" partial class which binds the data annotations to the IDataErrorInfo
<MetadataType(GetType(CustomerMetadata))>
Partial Public Class Customer
    Inherits Validatable(Of Customer, CustomerMetadata)
 
    Public Sub New()
        ParentObject = Me
    End Sub
 
End Class
 
' The metadata class for Customer, with DataAnnotations attributes
Public Class CustomerMetadata
 
    Public Sub New()
    End Sub
 
    <Required(AllowEmptyStrings:=False, ErrorMessage:="First name is required.")>
    <StringLength(10, MinimumLength:=3, ErrorMessage:="Enter a value between 3 and 10 characters.")>
    Public Property FirstName As String
 
    <Required(AllowEmptyStrings:=False, ErrorMessage:="Last name is required.")>
    <StringLength(10, MinimumLength:=3, ErrorMessage:="Enter a value between 3 and 10 characters.")>
    Public Property LastName As String
 
End Class
by rickrat
  July 02, 2010 @ 3:54am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate