I’m been working on a GetRuleViolations() method for my User class, but I’m getting a little hung up on on something:
What happens when different actions require different business rules?
My User table has the following columns: Id, UserRoleId, Username, and Password. Several actions involving User are possible (create new user, edit user, set/reset password, and log in), and the business rules for each are not always the same.
For example, when users log in, they are required to enter a password, but when admins create a new user, entering a password is not even an option. And in the case of setting/resetting a password, the password needs to be entered twice, and the two values need to be an exact match. What is the best way to handle this complexity? Is there some kind of design pattern to allow the right GetRulesViolations() method to be selected for the right circumstance?
I’m finding that a “service layer” to handle validation makes a lot of sense.
I found this tutorial very helpful:
http://www.asp.net/learn/mvc/tutorial-38-cs.aspx
Also, it looks as though some nice enhancements to validation are in the works for MVC 2.0 (see http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx). Using a
UserServiceorUserValidatorshould allow the 2.0 features to be folded in without having to change class design too significantly.