Using DependencyInjection and validation causing

Posts   
 
    
SamRose
User
Posts: 20
Joined: 24-Jun-2008
# Posted on: 27-May-2009 15:30:29   

<DependencyInjectionInfo(GetType(myEntity), "Validator", _
                         ContextType:=DependencyInjectionContextType.Singleton)> _
Public Class myEntityValidator
    Inherits ValidatorBase
    Public Overrides Sub ValidateEntityBeforeSave(ByVal involvedEntity As IEntityCore)
        Dim toValidate As myEntity = CType(involvedEntity, myEntity)
        If toValidate.LastUpdatedBy Is Nothing Then
            Throw New ORMEntityValidationException("Last Updated By cannot be null", toValidate)
        End If

        MyBase.ValidateEntityBeforeSave(involvedEntity)
    End Sub
End Class

Throws an error when the validation is thrown, saying....

"ORMEntityValidationException was unhandled by user code"

Any ideas?

I've set the

    
<appSettings>
        <add key="autoDependencyInjectionDiscovery" value="true"/>
</appSettings>

Attachments
Filename File size Added on Approval
errorexample.png 15,763 27-May-2009 15:30.52 Approved
Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 27-May-2009 15:45:33   

Please post the stack trace.

SamRose
User
Posts: 20
Joined: 24-Jun-2008
# Posted on: 27-May-2009 15:47:37   

at BusinessObjects.myEntityValidator.ValidateEntityBeforeSave(IEntityCore involvedEntity) in C:\test\testLLBLGEN\BO\BusinessObjects\Class1.vb:line 33 at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.OnValidateEntityBeforeSave() at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.CallValidateEntityBeforeSave() at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.PersistQueue(List`1 queueToPersist, Boolean insertActions, Int32& totalAmountSaved) at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, IPredicateExpression updateRestriction, Boolean recurse) at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave) at BusinessObjects.myEntity.Save() in C:\test\testLLBLGEN\BO\BusinessObjects\Class1.vb:line 154

SamRose
User
Posts: 20
Joined: 24-Jun-2008
# Posted on: 27-May-2009 15:58:57   

actually I was thinking...

is there an object provided by llblgen that can have multiple validation errors in?

So that i can do something like this


<DependencyInjectionInfo(GetType(myEntity), "Validator", _
                         ContextType:=DependencyInjectionContextType.Singleton)> _
Public Class myEntityValidator
    Inherits ValidatorBase

    Public Overrides Sub ValidateEntityBeforeSave(ByVal involvedEntity As IEntityCore)

        Dim toValidate As myEntity = CType(involvedEntity, myEntity)

        If toValidate.LastUpdatedBy Is Nothing Then
            'Add error to object
        End If

        If toValidate.LastUpdatedOn Is Nothing Then
            'Add error to object
        End If

        If toValidate.LastUpdatedOn Is Nothing Then
            'Add error to object
        End If

if object.errors.count > 0 then
throw object
else
        MyBase.ValidateEntityBeforeSave(involvedEntity)
end if


    End Sub
End Class


Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 27-May-2009 16:01:41   

Manual wrote:

IDataErrorInfo implementation The .NET interface IDataErrorInfo is now implemented on EntityBase. Two methods have been added to the entities: SetEntityError and SetEntityFieldError, which allows external code to set the error of a field and/or entity. If append is set to true with SetEntityFieldError, the error message is appended to an existing message for that field using a semi-colon as separator.

Entity field validation, which is triggered by the entity's method SetNewFieldValue() (which is called by a property setter), sets the field error if an exception occurs or when the custom field validator fails. The error message is appended to an existing message

SamRose
User
Posts: 20
Joined: 24-Jun-2008
# Posted on: 27-May-2009 17:20:08   

I'm now having a look at this...

Local.DataErrorInfo = DirectCast(_ORMEntityValidationException.EntityValidated, System.ComponentModel.IDataErrorInfo)

but trying to figure out the best way to return this back to the UI, i guess like this

 For Each _IEntityField2 As SD.LLBLGen.Pro.ORMSupportClasses.IEntityField2 In _myEntity.Fields
           If Not IsNothing(myEntity.DataErrorInfo.Item(_IEntityField2.Name.ToString)) Then
' If error then display next to field that has error
          End If
Next

still getting the "ORMEntityValidationException was unhandled by user code" though

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 28-May-2009 05:14:01   

I recommend you to download the Validation Example at download section of the site (http://www.llblgen.com/pages/examples.aspx). It shows some ways to manage the IDataErrorInfo at GUI.

David Elizondo | LLBLGen Support Team
SamRose
User
Posts: 20
Joined: 24-Jun-2008
# Posted on: 28-May-2009 12:02:17   

thanks Daelmo,

Although the demo brings errors...basically if I try to update a field with more than the correct number of characters I get the attached error.

The value specified will cause an overflow error in the database. Value length: 114. Column max. length: 40 Parameter name: CompanyName

So I'm trying to achieve the built in validation to appear in the customerValidator.cs in the example so that it brings up a handled error.

stacktrace

[ArgumentOutOfRangeException: The value specified will cause an overflow error in the database. Value length: 114. Column max. length: 40 Parameter name: CompanyName] SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.ValidateValue(IEntityField2 fieldToValidate, Object& value, Int32 fieldIndex) +3472 SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.SetValue(Int32 fieldIndex, Object value, Boolean performDesyncForFKFields) +501 SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.SetValue(Int32 fieldIndex, Object value) +42 SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.SetNewFieldValue(Int32 fieldIndex, Object value) +40 SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2.SetNewFieldValue(String fieldName, Object value) +101 SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceViewBase.SetEntityValues(IDictionary values, IEntityCore entity) +1091 SD.LLBLGen.Pro.ORMSupportClasses.LLBLGenProDataSourceView2.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +398 System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +92 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +907 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +704 System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +123 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

SamRose
User
Posts: 20
Joined: 24-Jun-2008
# Posted on: 28-May-2009 14:35:06   

I'm almost there simple_smile

Can I call this Build-in field validation logic from code instead of it being executed every time an entity field's value is set, either through the properties or through entity.SetNewFieldValue().

i know i can override it by using this...

<add key="buildInValidationBypassMode" value="2"/>

but I'd still like to be able to call it from my own Validator class.

Build-in field validation logic LLBLGen Pro's entities have validation logic build-in for field values. This logic is executed every time an entity field's value is set, either through the properties or through entity.SetNewFieldValue(). The validation logic is meant to prevent database errors when the entity is persisted so it rejects every value which doesn't match the field's aspects: for string based fields and byte-array based fields, the length of the value is checked compared to the length set for the field. For numeric fields the precision is checked and for fields with a fraction like Decimal, Single/float and Double based fields the scale is also checked. The build-in validation logic also checks if a field which isn't nullable is set to a null value. When an overflow is detected, an exception is thrown, the IDataErrorInfo error message for the field is set and the value is rejected.