Validation and formatting in a layered application

  • Thread starter Thread starter pablolch
  • Start date Start date
P

pablolch

Does all the validation code resides on the business layer?
Where should reside the formating code (how to show dates, numbers,
phone numbers, etc)?
I underestand that the validating rules should reside at the business
layer. But if the business layer expects information (from the
presentation layer) the information itself must be in some format. For
example if business layer expects a number as a Double the presentation
layer should send a Double, not a string. To send a Double the
presentation layer should parse a string tiped by the user and it may
contain errors. ¿Do this validation should reside on the presentation
layer?

1) Which one (presentation or business) says that "3;14" is not a
Double but "3.14" is
2) Which one says that 3.14 must be displayed as "0003.14"?
 
"3.14" is not a double. It's a string. The business layer defines the type
that is represented by the string, which the Presentation layer hands to it.
The Presentation layer must hand a double back to the business layer.
Therefore, the Presentation layer must convert the string to a double. If it
cannot, an exception will be thrown at the Presentation layer.

IOW, the Presentation layer must do validation. The business layer must
enforce business rules. So, depending upon your definition of "validation,"
the answer is "both."

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

Does all the validation code resides on the business layer?
Where should reside the formating code (how to show dates, numbers,
phone numbers, etc)?
I underestand that the validating rules should reside at the business
layer. But if the business layer expects information (from the
presentation layer) the information itself must be in some format. For
example if business layer expects a number as a Double the presentation
layer should send a Double, not a string. To send a Double the
presentation layer should parse a string tiped by the user and it may
contain errors. ¿Do this validation should reside on the presentation
layer?

1) Which one (presentation or business) says that "3;14" is not a
Double but "3.14" is
2) Which one says that 3.14 must be displayed as "0003.14"?
 
One could think of your business layer as the api to the backend. The UI
and/or UIs consumers of that api. So the business layer needs to do
validation as the entry point for backend. The UI does validation for the
user locally which can save round trips to the business layer for things you
know will not pass anyway and put up you UI dialogs, etc. It also creates
seperation as you can munge the data anyway you like in the presentation
layer, you just need to convert into something the BL api ultimately will
allow.

--
William Stacey [MVP]

Does all the validation code resides on the business layer?
Where should reside the formating code (how to show dates, numbers,
phone numbers, etc)?
I underestand that the validating rules should reside at the business
layer. But if the business layer expects information (from the
presentation layer) the information itself must be in some format. For
example if business layer expects a number as a Double the presentation
layer should send a Double, not a string. To send a Double the
presentation layer should parse a string tiped by the user and it may
contain errors. ¿Do this validation should reside on the presentation
layer?

1) Which one (presentation or business) says that "3;14" is not a
Double but "3.14" is
2) Which one says that 3.14 must be displayed as "0003.14"?
 
Hello (e-mail address removed),

Validation needs to be performed in both layers, but what and how is totally
depends on type of info.

Generally all BAL's incoming data need to be validated to be well-formed
in type(how it will be performed is up to you, but generally in message approach
systems XML Schemas serves the best of all). You need to be sure in app that
BAL will get valid data, it's crucial and very important first step.
Additional steps, for data consistensy may be performed either on BAL or
Application layer, but it may depends on your environment.
You can pre-test user input type in application (because it's very easy and
u need to calls to server) and re-test finally on BAL.

Returning to you sample. let's take "3;14" - it's a string not a number,
and it breaks type of data, this need to be validated in UI (i not saying
that u can't do it in BAL but UI is prefered for this)
Now about "0003.14", we already have a valid type - number, no compulsory
for UI to check it. But BAL might reject in due to some constrighs (and maybe
not :) ), for example value should be more then 1. I suggest you not to check
it in UI, only in BAL

Does all the validation code resides on the business layer?
Where should reside the formating code (how to show dates, numbers,
phone numbers, etc)?
I underestand that the validating rules should reside at the business
layer. But if the business layer expects information (from the
presentation layer) the information itself must be in some format. For
example if business layer expects a number as a Double the
presentation
layer should send a Double, not a string. To send a Double the
presentation layer should parse a string tiped by the user and it may
contain errors. ¿Do this validation should reside on the presentation
layer?
1) Which one (presentation or business) says that "3;14" is not a
Double but "3.14" is
2) Which one says that 3.14 must be displayed as "0003.14"?
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
Back
Top