Problem Validating Object with E.L.

Topics: Validation Application Block
Mar 29, 2010 at 5:47 PM

Hello All,

Im having some issues with my validation.

I created a base class from which all of my business objects will inherit from.

I added a method called Validate and attempted to validate the class, but it always returns IsValid = True even though I purposely violated several rules.

Here is a small example of the scenario.

Base Domain Object

Public MustInherit Class BaseDomainObect
        Public Function Validate() As ValidationResults
            Return Validation.Validate(Me)
        End Function
End Class

Test Business Object

    Public Class TestObject : Inherits BaseDomainObect

        <NotNullValidator()> _
        Public Property TestProperty() As String
            Get
                Return Nothing
            End Get
            Set(ByVal value As String)
                'do nothing
            End Set
        End Property
    End Class

and here is how I was trying to use it.

 Dim results, results2 As ValidationResults

        Dim testo As New TestObject
        results = Validation.Validate(testo) 'returns the correct result
        results2 = testo.Validate 'always returns true

        For Each r As ValidationResult In results
            MessageBox.Show(r.Key & "   " & r.Message)
        Next

When I call the Validation method of the business object it always returns true even when a rule has clearly been violated.

Any ideas what I did wrong?

Thanks

Mar 30, 2010 at 2:16 AM

Hi intertekdev,

Please see this FAQ item, there is a workaround suggested, please see if it is feasible for you http://entlib.codeplex.com/wikipage?title=Why%20do%20validators%20that%20are%20configured%20in%20the%20base%20class%20not%20work%20in%20its%20subclasses%3f&referringTitle=EntLib%20FAQ .

Also, see if this may help http://entlib.codeplex.com/Thread/View.aspx?ThreadId=204421.

HTH,

~ginkapitan

 

Mar 30, 2010 at 2:33 AM

Thanks but the links you pointed me to do not help.

I think you misunderstood my problem.

The links you provided talk about people having an issue when they inherit from a base class that contains properties that are decorated with attributes.

This is clearly not my problem.

My base class only provides a validation method as shown above in the example.

The problem is the Validate method in the base class does not work correctly. The IsValid property of ValidationResult that is returned is always True, even when I purposely violated a rule, as shown again in my example above.

Clearly the issue lies in the base class, and I believe it has to do with the Validation.Validate(Me) line.

As shown in the example above when I validate the object directly I get the correct result, but I try the validation method within the business object it fails.

 

Mar 30, 2010 at 3:38 AM

try using this as your Validate Method in your Base class

Public Function Validate(ByVal type As Type) As ValidationResults
    Dim val As Validator = ValidationFactory.CreateValidator(type)
    Dim results As New ValidationResults()
    val.Validate(Me, results)
    Return results
End Function

then executing the Validate method in your client code by

     Dim testo As New TestObject

      Dim resultsFromBase As ValidationResults = testo.Validate(GetType(TestObject))

Hope this helps.

 

 

 

Mar 30, 2010 at 5:24 PM

Thanks,

I modified your code so I dont have to pass in the type.

    Public MustInherit Class BaseDomainObect
        Public Function Validate() As ValidationResults
            Return Validation.Validate(Me.GetType)

        End Function
    End Class

And this works great.