Tips for using SetEntityError/SetEntityFieldError

Posts   
 
    
Posts: 19
Joined: 08-Jan-2007
# Posted on: 13-Feb-2007 15:22:18   

Hi

I like the idea of using these methods for building up validation errors and passing them back to the UI, but I'm finding things a little tricky.

I have created a validator class (inheriting from ValidatorBase) and overridden the ValidateEntityBeforeSave() method. In here I perform my validation, and for each validation error I call SetEntityFieldError(). However this in itself doesn't prevent the entity from being saved, so I also have to throw an ORMEntityValidationException after all this validation code if any errors occurred, to prevent the entity from being saved, but this feels a bit unnecessary. Is there a better technique? (I'm using the Adapter model).

Also, I want to display these errors in the UI, so I figured that I could catch the ORMEntityValidationException, grab the entity in question (which is a property of the exception) then interrogate the DataErrorInfoError/DataErrorInfoErrorsPerField properties, however they are protected. I could create a partial class for the entity and create some public properties which wrap these protected properties to make them accessible to the UI, but again this seems a bit of an unnecessary overhead.

Does anyone have experiences and advice for using these methods?

Thanks in advance

Andy

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 13-Feb-2007 16:27:52   

Hello,

throwing an exception allow you to know exactly what happend because in your ORMEntityValidationException you can defined a custom message and show it directly in your UI.

Posts: 19
Joined: 08-Jan-2007
# Posted on: 13-Feb-2007 16:44:31   

Hi

The problem is I want to return all of an entity's validation errors, and thought the SetEntityFieldError would be the way to go. This is my code:


        protected override void OnBeforeEntitySave()
        {
            bool isValid = true;

            if (Title == null)
            {
                SetEntityFieldError(
                    Fields[(int)DocumentFieldIndex.Title].Name,
                    "Title must be specified",
                    true);
                isValid = false;
            }
            if (Price < 0)
            {
                SetEntityFieldError(
                    Fields[(int)DocumentFieldIndex.Price].Name,
                    "Price must be a valid amount.",
                    true);
                isValid = false;
            }
            // ...etc...

            if (!isValid)
            {
                throw new ORMEntityValidationException("Document validation failure", this);
            }
        }


As you can see I'm building up a list of entity field errors, then throwing an exception if there were any, to prevent the save from going ahead.

In my UI I do something like this:


try
{
     Call busines tier save logic to save the entity
}
catch (ORMEntityValidationException eve)
{
    // Here I want to extract the errors in eve.EntityValidated.DataErrorInfoErrorsPerField
    // and display in a label, but that property is protected!
}

As mentioned in my previous post, I could extend the entity with a partial class that contains a new public property to expose what's in DataErrorInfoErrorsPerField, but this seems a bit unnecessary! Is there a better way of doing all this?

Thanks again

Andrew

epitka
User
Posts: 19
Joined: 13-Feb-2007
# Posted on: 13-Feb-2007 19:53:47   

Well, might be ignorant from me but can't you not build a string of messages, let's say pipe delimited, and then parse it (split it) out in UI and display messages, something like:

errror = message1|message2|message3

throw new ORMEntityValidationException(error,Me)

UI dim errors() as string = error.split("|"c)

Posts: 19
Joined: 08-Jan-2007
# Posted on: 14-Feb-2007 09:51:32   

I would have preferred to use the properties and methods already provided by the entity classes, but I can't understand why the LLBLGen team chose to make the DataErrorInfoError & DataErrorInfoErrorsPerField properties protected. Used in conjunction with the SetEntityError & SetEntityFieldError methods it seemed a good way of passing validation messages back to the UI tier.

Posts: 19
Joined: 08-Jan-2007
# Posted on: 14-Feb-2007 12:30:10   

Apologies, I've just realised that in my UI I can do the following to extract the errors via the IDataErrorInfo interface (assuming the same validation code as quoted in my earlier msg):


            catch (ORMEntityValidationException eve)
            {
                IDataErrorInfo dataError = (IDataErrorInfo)eve.EntityValidated;
                // do something with the field-level errors, e.g. dataError["Price"]
            }

Thanks for everyone's input though. This area of the LLBLGen documentation is a little vague (as is the .Net documentation on IDataErrorInfo - very little on the 'net either). Perhaps some better examples would be useful...

Andy