What is the difference between a validation rule and a business rule ?
As per my understanding, 'if the state of the business object/objects is not as expected, then throw an error message' is a validation rule, and 'when the state of the business object/objects is or is not equal to something, then change the state of some business object/objects [or take some action/event but not just throw an error]' is a business rule.
Validation can be UI validations - validate values of UI fields or application validations - validate business object states.
I am not sure whether my understanding above is correct. In my project, we have a validation framework, where a simple validate call takes business objects to validate against something and an error collector that collect all errors. The errors are displayed on the screen afterwords.
In addition to that, we have rules that falls in second category as described above i.e check the business object/objects state and take some action such as change the state of another business object. I am trying to find out the strategy to implement such rules either using some framework [not a validation framework] or a rule engine.
Can you please help me understanding the distinction between the above 2 kind of rules and if th开发者_开发问答ere are any implementation strategies/ recommendations, it would be helpful.
A validation is a check that the value entered is legitimate for the context of its field (from technical perspective), for example: is 5 as a numeric value acceptable for Age(v.s. -5)?, while -5 is acceptable as Temperature for example.
The business rule is more of a business perspective. It is a check that the values (that passed the validation) are acceptable by the policies and procedures of the business. E.g. the person who is allowed to register has to be a resident, and 18 years old or more..etc. The business rule might check one (or more) field(s) value(s), and might consult data stored in a database and/or do some calculation(s) to ensure that the value(s) pass the business rules.
So, for the example posted above by hanna, the value 15 should pass the field validation (as it is a valid value for Age), but it will not pass the business rule check that the married person's age must be >15.
In short; a validation rule determines basic validity; "is this a valid email address?" A business rule determines what to do with the valid data; "can I set the user's confirmation email to the submitted value?" Business rules can migrate into validation logic; but typically, validation is not done by the business rule engine.
Validation usually refers to rules that it is not required to query database for validating them. For example minimum password length. Business rules usually require a database query for validating. For example you can not withdraw money more than your account balance. And this is a business rules. So for example, minimum acceptable age for registering for school that is hard coded in the application is a validation rule.
A business rules, from its name, it is a data check from business point view, for example, the marital status can not be "married" and the person age is less than 15, it is basically rules that change from business to business.
validation, it is usually related to user interface , can be common in more than on business,and can be derived by common sense, for example, the email format should have a specific form, or if there is a start date and end date, the end date inserted should be bigger than the one inserted in the start date.
It all depends on perspective. Both validation rules and business rules are really the same thing. We sometimes logically categorise how complex rules are, the most basic being Validation then more complex being Business rules. That's not correct, as it depends! Example of "complexity rule hierarchy":-
Basic datatype
Usually conversion from a string into strong type, examples
- A number cannot be "hello"
- Boolean is yes/no or True/False.
Metadata on type/common sense
Now we have a strong type, check bounds, getting more blurry
- Future booking date is not historic (before now)
- Year in century, number in range 1 to 100
- Notes max 100 chars.
Business rules
Now fully ambiguous...it depends!
- If System Health Check account then Email is mandatory but must be malformed "not-an-email".
- Cannot place order more than 1000USD, if credit limit is "Basic"
- Notes must be min 200 chars if is Manager.
Did you notice a Manager must make 200 chars of Notes minimum, whilst others zero to max 100. Thus some may argue max chars is validation, but clearly the business requires Managers do more. So which is it?
Maybe call everything a Business Rule as the ultimate goal of validation is to ensure "data correctness in all business contexts".
精彩评论