LLBLGen Equivalent to get data annotation validation errors?

Posts   
 
    
ABOH
User
Posts: 148
Joined: 06-Sep-2011
# Posted on: 25-Apr-2013 17:24:10   

Hello,

We are creating a WCF Data Service RESTful service and are using the LLBLGen runtime as our ORM. Access to the underlying data will be via OData. We have applied .NET Data Annotations to fields in the entities via the LLBLGen Pro editor.

When a data validation error happens during CRUD operation in the service, we want to pass a specific error message back to the client. In EF, we would do the following to determine each of the data validation errors, so we can construct a helpful, detailed message. How can I accomplish this with LLBLGen ORM exception? I've looked at ORMEntityValidationException, but don't see the equivalent. I'd appreciate your help.

Thanks,

Mike

        if (args.Exception.GetType() ==
          typeof(DbEntityValidationException))
        {
            var ex = args.Exception as DbEntityValidationException;
            var errors = ex.EntityValidationErrors.First().ValidationErrors.ToList();
            var errorMessage = new StringBuilder();
            foreach (System.Data.Entity.Validation.DbValidationError e in errors)
            {
                errorMessage.AppendLine(e.ErrorMessage);
            }
            args.Exception = new DataServiceException(500, errorMessage.ToString());
        }
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Apr-2013 08:42:22   

You can use IDataErrorInfo:

// set the error
customer.SetEntityFieldError("City", "some error", true);

// get the error
var error = ((IDataErrorInfo)customerFromXml)["City"];

That info is serializable through WCF. You can use it at entity or field level.

David Elizondo | LLBLGen Support Team
ABOH
User
Posts: 148
Joined: 06-Sep-2011
# Posted on: 27-Apr-2013 03:21:39   

Hi David,

In my WCF Data Service, I am hoping to apply .NET Data Annotations on fields and have these annotations throw exceptions when they are violated. My plan is to catch exceptions in the following method and to prepare a more meaningful error message to return to the client.

    protected override void HandleException(HandleExceptionArgs args)
    {
       //ORMEntityValidationException e;
        //e.EntityValidated.

        // Determine whether this exception if due to a PK or UKC violation.
        if (args.Exception.GetType() == typeof(ORMQueryExecutionException))
        {
        }
}

For data annotation validation in Entity Framework, we have to do the following to get to the actual list of data validation errors, and to prepare a helpful error message for the client:

        if (args.Exception.GetType() ==
          typeof(DbEntityValidationException))
        {
            var ex = args.Exception as DbEntityValidationException;
            var errors = ex.EntityValidationErrors.First().ValidationErrors.ToList();
            var errorMessage = new StringBuilder();
            foreach (System.Data.Entity.Validation.DbValidationError e in errors)
            {
                errorMessage.AppendLine(e.ErrorMessage);
            }
            args.Exception = new DataServiceException(500, errorMessage.ToString());
        }

In LLBLGen, I've assigned a StringLength(4) data validation annotation to a string field. When I create a new record with a string that is longer than 4, I am not receiving any errors from LLBLGen or WCF Data Services. So, I am wondering whether I can use data annotations for validation in this manner with LLBLGen. If so, is there a similar list of validation error messages that I can process? If I cannot use data annotations in the WCF Data Service, then the alternative seems to be what you mentioned in your response. Right?

Thanks for your help and have a great weekend!

Mike

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Apr-2013 09:19:00   

Data annotations are just that: validation-info decorator on properties. LLBLGen Framework doesn't use them, but you can use the attribute system to generate them and use them in your code, which means you have to use System.ComponentModel.DataAnnotations.

Now, AFAIK (please correct me if I'm wrong), data annotations are part of the type, it's not serializable data, so I don't know (ref...).

I think there is a project that make them usable through WCF, its name is WCFDataAnnotations. I never tried, but maybe you want to take a look.

If you use IDataErrorInfo, you could check if there are errors at your Validator.ValidateBeforeSave() method, for instance, (but it could be anywhere) and throw an ORMEntityValidationException so you can handle it on HandleException.

David Elizondo | LLBLGen Support Team
ABOH
User
Posts: 148
Joined: 06-Sep-2011
# Posted on: 27-Apr-2013 21:54:41   

Hi David,

I read in your docs the following about validation:

Built-in field validation logic LLBLGen Pro's entities have validation logic built-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 built-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.

I looked at the generated entity code and I don't see any testing to ensure that a string, which has a length of 100 in the database, is exceeded. Can you please tell me where to look? It seems to me that the built-in validation that is mentioned takes care of our business cases, and I can intercept your exception in the WCF data service to prepare a message to return.

Thanks for your help! Mike

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 29-Apr-2013 18:49:15   

It seems to me that the built-in validation that is mentioned takes care of our business cases

Yes it does, and that's in the runtime library, not in the generated code. Over and above, you can create a Validator class to add your own business logic, which can be used with the built-In validation, or it can be used exclusively without the built-in validation.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39897
Joined: 17-Aug-2003
# Posted on: 30-Apr-2013 10:27:44   

I thought it was all documented in the validation page: http://www.llblgen.com/documentation/4.0/LLBLGen%20Pro%20RTF/hh_goto.htm#Using%20the%20generated%20code/gencode_validation.htm

Did you miss something essential there so we should add it?

Frans Bouma | Lead developer LLBLGen Pro
ABOH
User
Posts: 148
Joined: 06-Sep-2011
# Posted on: 30-Apr-2013 15:20:51   

Hi Otis,

Actually, I did review the docs, but I didn't find specifics on how .NET Data Annotations are or are not used in a WCF Data Service. I experimented and found that no exceptions were thrown for violating a Data Annotation, so I posted to clarify the issue. Now, I am trying to decide whether I want to do field level validation in the data service, which could be inefficient, or repeat it on each client(s) platform (ASP.NET and mobile).

Thanks for your help,

Mike

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Apr-2013 19:29:06   

Do you still need help from our side, or should we close this thread.

ABOH
User
Posts: 148
Joined: 06-Sep-2011
# Posted on: 30-Apr-2013 20:10:24   

Hi Walaa,

I am good to go...

Thanks for the help!

Mike