Are business rules always related to validation?

14

Since I started studying object orientation I hear a lot about business rules. Basically, as I understand it to this day, an object must have methods encapsulating the business rules and modifying its state must be done through these methods and not through direct access to its attributes. From what I realized there is so much emphasis on this that an object without methods is even called anemic.

The biggest problem is that I still do not really understand what these business rules really are. All the examples I've seen, business rules have to do with validation. One of these examples is a Usuario class representing a user of a system, and the business rules imposed in the example have to do with data validations (ensure that required data is informed, ensure that the email is an email valid, etc.).

Another well-used example is the banking system, and then the business rules are again validated: for example, checking if the account balance is enough to make a withdrawal and so on. Thus it seems that the always business rules are validations to check if a certain operation can or can not be performed or to verify that the reported data is actually consistent.

Is this impression correct? Do such business rules always have to do with validations? If not, what really are the business rules?

    
asked by anonymous 28.01.2015 / 18:16

2 answers

12

In fact, it is very common for business rules to be validations, but not necessarily.

The interpretation of what is business rule and what is mechanism is not always very clear, and depending on the methodology used there may be a preference for one thing or another.

In fact, if you think about object types there are also disagreements about what should be part of the type as a business rule and what should be something external to the type still being a business rule. Often this is implemented through rules, constraints, policies, or treatments.

/ em>). That are nothing more than other auxiliary types to the main type. Sometimes a business rules engine is used for this.

Taking the bank account example, you already know the various validations that exist. But does the object only validate data? Can not he do anything?

Let's say you have a method called ObtemContasInativas() . Is not this a business rule? It is not validation. It shows how an account is considered to be inactive and how to provide data. Note that what information and in what order are also embedded business rules within this hypothetical method. It is actually possible for this method to use another method to know if an individual account is inactive or not, probably a EhInativa() .

Most likely the first method is not in the bank account class itself, it would not make sense to be. But it's still a business rule. The second method already makes more sense to be in the type of the bank account itself. But not necessarily. There are a few reasons to choose not to be. But this is not the case here.

Another method could be EncerrarConta() which could be called automatically by some event, perhaps checked by a InativoMais180Dias() method or manually by some system function. And of course she would go through a series of procedures to ensure the account is terminated. All of these are business rules.

Exactly how you are going to access the database to get this information, to record what is needed for shutdown is probably part of the mechanism and does not go into these methods. They should delegate these specificities to other methods of mechanisms, most likely in other classes.

Then one notices that business rules can rather determine how information is obtained, how to execute certain algorithms, etc. They are not just checks if the data you are trying to enter is right or wrong.

Even what to do in the event of an invalid data entry attempt can be a business rule per se. Should you log in ? Should I call some auxiliary processing? Should some object state change?

Business rule is anything that determines what you should do in any situation by manipulating (reading or writing, total or partial) that information in an abstract form. In a way that makes sense to the user.

When the manipulation becomes more concrete, such as WriteOnDataBase() or DisplayOnScreen() are probably already part of the mechanism. These are usually things that make sense to the developer only but are critical to the system.

So you can validate, guarantee (slight semantic difference), register, consult, classify, condition, relate, execute, verify, allow / prohibit, calculate, delegate, derive, signal, observe, etc. The number of possible actions, even if generic, as listed, may continue.

    
28.01.2015 / 18:39
9

Validations are, yes, business rules. But there are others.

Consider, for example, other bank business rules that go beyond checking your account balance before allowing a withdrawal:

A bank provides loans . You go to the bank and ask for 100 thousand reais to pay in 10 times. The system will automatically assess if you can take this loan and also the interest rate. To do so, the credit release business rules are triggered. These include:

  • Is a monthly installment of 10,000 reais greater than 30% of your income?
  • Have you been a good payer on previous loans?
  • Does the overall amount of clients with a profile similar to yours have paid off your debts?
  • What is the current interest rate?
  • ...

A complex calculation is made considering these and other information and the loan is released as long as it is approved by someone with higher authority than the manager of your account. Then this manager forwards the loan for approval, which goes into a queue and will be evaluated by another manager, which is also a business rule.

    
28.01.2015 / 18:39