Again, Peter asks JOS readers about where to put validation code in MVC: http://discuss.joelonsoftware.com/default.asp?design.4.354410.6
I recently had to deal with validation issue building Register / Sign In routines for Blogoforum.
Blogoforum has UserRepository (almost equal to Factory) where User entities are to be built. So when user submits new registration, Register page transfers request to UserRepository. UserRepository had internal validation checking for no user existing with given name, password strength, and such. UserRepository simply threw an exception if something was wrong.
And this was the problem. If something goes wrong, my Register page needs to show "Please Enter User Name", "Choose Another UserName", or "Passwords are not equal" messages. I definitely do not want to create separate class for each of these validation exceptions. Actually, even if I would want to - this would not help much because Blogoforum can report "Choose Another UserName" and "Passwords are not equal" at the same time, and UserRepository can't throw 2 exceptions at the same time.
Another idea just visited me that I can alternate UserValidationException somehow so it would be able to answer questions like isUserNameTaken() or arePasswordsNotEqual(). Maybe it is worth something. But I went another way.
The module of code absolutely responsible for validation is business logic. Currently in Blogoforum, the object absolutely responsible for new user creation validation is UserRepository. What I did was:
- created UserCreationValidator class and moved validation code there
- let UserRepository to create UserCreationValidator object to serve UserRepository's needs for validation.
Then I've added isUserNameTaken() and arePasswordsNotEqual() public methods to UserCreationValidator. Then I let my Register page to ask for UserCreationValidator, and my UserRepository to provide it.
Now, when the user submits new registration, Register page asks UserRepository for validator, validates input using it, and shows error messages or transfers input to UserRepository as needed. UserRepository then does its own validation.
As a result, Register page contains only validation logic related to presentation part. For most of the job, Register page asks UserCreationValidator. UserCreationValidator is the class which holds validation logic.
Another technique I use for good OO code is Procedural Design before OO design: http://blogoforum.com/tag/design+development+oo+programming/procedural-design-for-good-oo-design-1299.html
I recently had to deal with validation issue building Register / Sign In routines for Blogoforum.
Blogoforum has UserRepository (almost equal to Factory) where User entities are to be built. So when user submits new registration, Register page transfers request to UserRepository. UserRepository had internal validation checking for no user existing with given name, password strength, and such. UserRepository simply threw an exception if something was wrong.
And this was the problem. If something goes wrong, my Register page needs to show "Please Enter User Name", "Choose Another UserName", or "Passwords are not equal" messages. I definitely do not want to create separate class for each of these validation exceptions. Actually, even if I would want to - this would not help much because Blogoforum can report "Choose Another UserName" and "Passwords are not equal" at the same time, and UserRepository can't throw 2 exceptions at the same time.
Another idea just visited me that I can alternate UserValidationException somehow so it would be able to answer questions like isUserNameTaken() or arePasswordsNotEqual(). Maybe it is worth something. But I went another way.
The module of code absolutely responsible for validation is business logic. Currently in Blogoforum, the object absolutely responsible for new user creation validation is UserRepository. What I did was:
- created UserCreationValidator class and moved validation code there
- let UserRepository to create UserCreationValidator object to serve UserRepository's needs for validation.
Then I've added isUserNameTaken() and arePasswordsNotEqual() public methods to UserCreationValidator. Then I let my Register page to ask for UserCreationValidator, and my UserRepository to provide it.
Now, when the user submits new registration, Register page asks UserRepository for validator, validates input using it, and shows error messages or transfers input to UserRepository as needed. UserRepository then does its own validation.
As a result, Register page contains only validation logic related to presentation part. For most of the job, Register page asks UserCreationValidator. UserCreationValidator is the class which holds validation logic.
Another technique I use for good OO code is Procedural Design before OO design: http://blogoforum.com/tag/design+development+oo+programming/procedural-design-for-good-oo-design-1299.html
by dkrukovsky
June 18, 2006 7:18 AM+ add to your
Readings [?]
Blogoforum -
Your Reply
del.icio.us