Best way to validate server side using llblgen datasource

Posts   
 
    
Krish
User
Posts: 91
Joined: 02-Jan-2008
# Posted on: 19-Apr-2008 09:35:55   

I would like some opinion on the best way to validate server side when using llblgen datasource bound to a details view (asp.net) control.

e.g. the user either tries to update or insert a record. Most of the fields are selected from drop down controls. But say a combination of two field values results in a unique constraint error violation. We cant validate for this, client side. (or can we?) What is the best way to handle this and return gracefully to the user?

I am using LLBLGen Pro 2.5 Final, self servicing two class templates.

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 19-Apr-2008 20:25:34   

Hello Krish. LLBLGenPro comes with a Validation mechanism.

What you can validate - Specify FieldValidation level. - Specify EntityValidation level (before save, after save, before delete, after load). Here you can put multi-field validation.

How to inform about the errors You can pass the error info to the GUI via ORMEntityValidationException. Also you can use the _IDataErrorInfo _implementation to pass information inside the entity about the errors.

How to set the validation objects You can choose between: 1. Setting the Validator property of an entity object manually. 2. By overriding the Entity method CreateValidator. 3. By using Dependency Injection. Using the Dependency Injection mechanism build into LLBLGen Pro, the defined Validators are injected into the entity objects for you. This option is the least amount of work.

Recomendation Implement validation at all levels as possible (DB, Entities, BusinessLogic, GUI). If you can do client-validation for some fields (required, regularExpressions, etc.) then do it! so you can save roundtrips to the server.

More info.... Please read LLBLGenPro Help - Using the generated code - Validation per field or per entity. Also download the LLBLGenPro Validator examples.

Cheers.

Krish
User
Posts: 91
Joined: 02-Jan-2008
# Posted on: 23-Apr-2008 13:54:40   

I decided to use DI mechanism to implement validation but have run in to the follwing problem :

When the user tries to do an update, and the update fails validation, the control (e.g. details view) remains in edit mode which is good but all the user changes have been lost. Instead the original values are displayed.

How do I continue to show the changed values after a failed validation?

Here is how I am doing validation:

I am using LLBLGen datasource. I set the filter for the datasource in the page load only if its not a post back.

I am using DI (auto discovery) to do some validation and throw an error. e.g. throw new ORMEntityValidationException(strExceptionMessage, toValidate);

My issue is : When updating a record, say it fails validation. The details view control stays in edit mode as expected but the values have reverted back to the original values and so the user has lost all her changes even though only one field might have failed validation.

The reason the original values are restored is I think, the PerformSelect event is fired and the llblgen datasource has got the original values.

I do not want the edited values lost unless the user cancels the edit. How do I achieve this?

Here are the events I am using :

Note : dvAddress is the name of the asp details view control. ldsAddress is the name of the llblgen datasource instance.


protected void dvAddress_ModeChanging(object sender, DetailsViewModeEventArgs e) 
    { 
        
        // if there is errors, cancel the DetailsView mode change, so the user could fix the values 
        if (blAddressErrors.Visible && !e.CancelingEdit) 
        { 
            e.Cancel = true; 
        } 

        // if the user is canceling, hide the control that show the errors. 
        if (e.CancelingEdit) 
        { 
            HideEntityErrorControls(); 
        } 
    } 

 protected void ldsAddress_PerformWork(object sender, PerformWorkEventArgs e) 
    { 
        try 
        { 

            using (Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "PerformWork")) 
            { 
                e.Uow.Commit(trans, true); 
            } 
            // no errors, so hide the errors. 
            HideEntityErrorControls(); 
        } 

        // some validation rules have been broken! 
        catch (ORMEntityValidationException ex) 
        { 
            // Show entity errors to the user. 
            ShowEntityErrors(ex.Message); 
        } 
        
    } 

protected void ldsAddress_PerformSelect(object sender, PerformSelectEventArgs e) 
    { 

        e.ContainedCollection.GetMulti(e.Filter, e.MaxNumberOfItemsToReturn, e.Sorter, 
                e.Relations, e.PrefetchPath, e.PageNumber, e.PageSize); 
    }

I am using LLBLGen 2.5 Final, self servicing 2 class templates. ASP.NET 2.0.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Apr-2008 16:06:52   

I guess the following thread might be useful to you: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=12938

Also this one is relevant: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=12654