CommandBase ValidationRules

CommandBase ValidationRules

Old forum URL: forums.lhotka.net/forums/t/5545.aspx


Shaun posted on Monday, October 06, 2008

What is the best way to implement validation rules within a class that inherits from CommandBase. AuthorizationRules work the same as BusinessBase, ValidationRules do not unless I'm missing something.

Many thanks

Shaun Trennery

JonStonecash replied on Monday, October 06, 2008

In my mind, the classes/objects that inherit CommandBase "Do" something rather than "Have" values.  The only thing that might be validated are the values of the parameters passed to the command object.  In most cases, these values do not come directly from the end user.  I am not sure that the BusinessBase type of validation makes much sense here.  The BusinessBase is set up to expose the results of the validation to the presentation/gui layer.  I would think that this is not the case with the Command objects, that are typically invoked as a part of performing validation or some other business function.

I would think that any validation would be of the form: if OK, do the work of the command; else, throw an exception.  I would use an exception for the most part because I would not expect many cases where the parameters were in error.  It might be possible that the parameters could cause an error as a part of executing the command but I think of that as execution status rather than a pre-execution validation.

Jon Stonecash

Shaun replied on Monday, October 06, 2008

Thank you for your reply.

My scenario is similar to this one. I have a News Business Base object that has a Vote Command. When I execute the Vote Command from the News class, I'd like to check the member parameter passed into the command. The check needs to validate the member and determine if the member is active, not blocked etc.

Thanks

Shaun

JonStonecash replied on Monday, October 06, 2008

I would think that the particular validation logic that you are describing should be in the News class.  The command object should do or not do based upon the decision of the News object.  It should not quibble. 

 

In my mind, the command object is a private in the army that does what it is told to do.  It should make no decisions.

Jon Stonecash

 

Shaun replied on Monday, October 06, 2008

Thank you Jon. That makes perfect sense.

rsbaker0 replied on Tuesday, October 07, 2008

We struggled with this some early on, and I've come to the conclusion that if you want validation rules, authorization rules, or bindability, you should just derive from BusinessBase<T> rather than CommandBase.

Whatever you were doing in your CommandBase "DataPortal_Execute" method can be done in your BusinessBase "DataPortal_Update" method -- it's really a straightforward refactoring. You can even implement a static Execute method so that the implementation difference is not noticeable to the consumer for simple cases.

Copyright (c) Marimer LLC