sign in | register

Blogoforum Blogoforum - Where to Put Validation in MVC

recent > design > development > oo > mvc and validation

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
by dkrukovsky
June 18, 2006 7:18 AM
+ add to your Readings [?]

Your Reply Your Reply

have your browser's cookies enabled
! You are not signed in. Sign in or Register to get the post published into your own blog.






(html will be shown as text)
Verification Image


type the characters you see in the picture above
Spam links posted here may be put into "spam" category which may result in your site rank decreased by Google

people think this discussion is about

wrong? assign your keywords [?]
Google

Explore Blogoforum - The Better Forum Experiment

Blogoforum is looking for UI Designer.

Tell the World

Blogoforum will become more useful as its user base grows. If you think the idea is interesting, bookmark it on del.icio.us and tell your friends.