Hi,
I think many of us come across same problems ..And as you said, it is always not easy 
to come with an easy solution ..


But following  approach will avoid code duplication...And also keep seperation between 
business layer and Presentation layer(web layer)..


In the forms just do the frontend level validation(compulsory fields, date formats, 
invalid strings being entered for numeric text boxes etc etc...)
And then the business level validations should be done just in the service/model 
layer...That means no dao calls in the forms.And if there are any Business level 
validation failures in Service/Model layer, it should return with some specific 
Business Exception...Which can then be caught in Action and handled 
appropriately...You may even think about some ErrorContainer to the 
Serviclayer(Similar ActionMessages object in Struts layer) which can then be returned 
along with the businessException(Embedded in BusinessException .)So the ActionLayer 
can then just have a generic Utility method to extract the BusinessException from this 
container and populate the StrutsMessages container to show the same to user...


But the important point to consider is whether your Model/service layer is going to be 
called from some other API or not?Else doing the validation in FORM or Action looks 
just fine...


HTH.
regards,
Shirish

-----Original Message-----
From: Guido Garc�a Bernardo [mailto:[EMAIL PROTECTED]
Sent: Tuesday, December 23, 2003 1:51 PM
To: Struts Users Mailing List
Subject: Re: Validations - 'format' vs 'business'


I really think that is a good aproach. At least one of the most simple 
and centralized.  BUT....

...from a "pure struts" point of view someone can think that your 
solution is mixing the model with the view.
I think ActionForm should remains as part of the view, or just a 'box' 
between the view and the controller.
What do you think?

On the other hand, I _must_ expose business logic as a API for other 
projects.  With this fact in mind, business methods should be 
implemented doing validations again (defensive programming, you need to 
think about possible errors from your users and don't assume 
anything...). HOW without duplicated code?

Thank you again,
Guido Garc�a Bernardo

Vic Cekvenich wrote:

> {repost}
> I just override validate() method on formbean and do all there, 
> including super.validate() that reads validation.xml. In here I call 
> DAO's to do business validation also, ex: what is the available credit 
> for the client to place this order.
>
> Then in action I do this:
> errors = formbean.validate();
>
> hth,
> .V
>
> Guido Garc�a Bernardo wrote:
>
>> Hi,
>> I have a design doubt... I must validate data coming from a form. 
>> This consist typically of:
>>    - 'format' validations (i.e. a field is not empty or it is 
>> numeric) that I do in the validate method of the ActionForm
>>    - 'business' validations that usually require a DB access
>>
>> I actually do 2 steps (actions) per operation. One of them prepare 
>> the data and the second one does the operation itself. And here comes 
>> my first question: ��is there any other better aproach??  Maybe 
>> something similar to a Tiles Controller to prepare the data...
>>
>>    class PreOperationAction extends Action {
>>        public ... execute ( ... ) {
>>           // Create JavaBeans to populate html selects and several 
>> inputs (requires DB access) and include them in the request
>>           // Forward to error/success jsp
>>        }
>>    }
>>
>>    class PostOperationAction extends Action {
>>        public ... execute ( ... ) {
>>           // Get data from ActionForm
>>           // Business validations (the selected values and inputs are 
>> valid from the business point of view)
>>           // Execute business logic (encapsulated in external 
>> business logic classes)
>>           // Forward to error/success jsp
>>        }
>>    }
>>
>> At this point I don't know what is better (from a MVC perspective).
>>    1. Do it as actually, that is, doing business validation before 
>> business logic. This way I think I can't expose the business logic as 
>> an API or as a web service.
>>    2. Include all the validations (business and format) into the 
>> business logic classes.  This way I must duplicate format validations
>>    3. Doing a OperationValidations class (? only a vague idea)
>>    4. Is there any pattern or any best practice related?  Does 
>> Validator Plugin allow complex business validations?
>>
>> Finally, I need your opinion about handling validation errors:
>>    1. Throwing an Exception from the business logic classes and catch 
>> it in the Action (or declare the exception in struts-config.xml)
>>    2. Returning null (or -1, or a no-sense value) from the business 
>> logic classes
>>    3. Any other way...
>>
>> Thank you very much,
>> Guido Garc�a Bernardo.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to