Is it a good choice to use a validation class

T

Tony Johansson

Hello!

I have a GUI that have the following controls.
Two textboxes
Five ListBoxes
One combobox
One checkedlistbox
Three button
When I click on one of the buttons to add the Animal it should be added to a
collection in a class called AnimalManager that hold the collection to the
animals by using a List<T> object.

Now to my question I just wonder what is your opinion to use a
separateValidation class to check if all input has been done correctly or if
all validations should be stored in the GUI class ?


//Tony
 
A

Arne Vajhøj

I have a GUI that have the following controls.
Two textboxes
Five ListBoxes
One combobox
One checkedlistbox
Three button
When I click on one of the buttons to add the Animal it should be added to a
collection in a class called AnimalManager that hold the collection to the
animals by using a List<T> object.

Now to my question I just wonder what is your opinion to use a
separateValidation class to check if all input has been done correctly or if
all validations should be stored in the GUI class ?

I am not completely sold on the idea.

It makes sense to have the UI check format of input according
to certain rules.

It makes sense to have data/domain classes validate consistency
of their data.

A validator object is not really a good model for anything
in the real world.

But in practice you may often need it:
1) if the UI framework allow you to give it a validator
object and it will do the validation automatically
2) if you need the same functionality mane places in the UI
then you can put in a helper class and reuse it

Arne
 
J

James A. Fortune

Hello!

I have a GUI that have the following controls.
Two textboxes
Five ListBoxes
One combobox
One checkedlistbox
Three button
When I click on one of the buttons to add the Animal it should be added to a
collection in a class called AnimalManager that hold the collection to the
animals by using a List<T> object.

Now to my question I just wonder what is your opinion to use a
separateValidation class to check if all input has been done correctly orif
all validations should be stored in the GUI class ?

//Tony

Let's see how well Arne's book recommendation addresses that problem.

Effective C#: 50 Specific Ways to Improve Your C#
Bill Wagner
Addison Wesley 2005
ISBN 0-321-24566-0

From pp. 224, 225, Item 39: Use .NET Validation (Windows Application
portion):

"User input can be validated in C# code that runs in the same context
as the application. The full gamut of Windows controls is available
to you when you want to notify the user of invalid input. The general
model uses exceptions in property accessors to indicate the invalid
input. UI widgets catch those exceptions and display errors to the
user."

From pp. 224, 225, Item 39: Use .NET Validation (Web portion):

"Web applications should get data validated at the browser, using
JavaScript. The validation controls generate JavaScript in the HTML
page. It's more efficient for your users: They do not need to have
round-trips back to the server each time they change an entry. These
web controls make extensive use of regular expressions to tentatively
validate user input before the page is posted back to the server.
Even so, you'll want to perform more extensive validation at the
server, to prevent programmatic attacks."
....
"You can use five web controls to handle most of the validation tasks
in your ASP.NET applications. All five are controlled by properties
that specify the field that should be validated and the conditions for
valid input. RequiredFieldValidator forces the user to enter some
value in a given field. RangeValidator mandates that a specific field
supplies a value within a given range. This range could be the
magnitude of a number or the length of a string value.
CompareValidator lets you construct validation rules that relate two
different fields in a web page. These three are relatively simple.
The last two give you all the power you need to validate almost any
user input you are expecting. The RegularExpression validator
processes the user input using a regular expression. If the
comparison returns a match, the user input is valid.
....
If regular expressions aren't enough for you, you can add your own
validator by deriving a new class from CustomValidator."

In:

http://groups.google.com/group/comp.databases.ms-access/msg/3a252c2b9a3e7493

I said:

"For ASP pages, validation can be done at the javascript level or the
ASP level. Javascript validation can be done on the client side.
This site seems to strike the right balance by validating at the
javascript level whenever possible."

I realize that the book was written for an earlier version of the .NET
Framework and that better techniques may be available by now. Also,
just because something is written in a book doesn't make it true.
Thus, I hope that anyone with better experiences or techniques
corrects any shortcomings in Mr. Wagner's advice. I'll wait until I
finish reading the second book in the series before presenting other
questions I have about his recommendations. BTW, the prior "Item" in
the book (Data Binding) might provide an elegant way to solve the
cascading combobox problem from another thread.

James A. Fortune
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top