In fact it is very common that business rules are validations, but not necessarily.
The interpretation of what is a business rule and what is a mechanism is not always very clear (I asked a question and I did not get satisfactory answers), and depending on the methodology used there may be a preference for one thing or another.
In fact, if we think about the types of objects 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 (Rules), restrictions (constraints), policies (policies) or treatments (traits). That are nothing more than other auxiliary types to the main type. Sometimes a mechanism of business rules is used for this.
Taking the example of the bank account, you already know the various validations that exist. But the object only validates data? It can not perform anything?
Let’s say you have a method called ObtemContasInativas()
. Wouldn’t this be a business rule? It’s not validation. It states how an account is considered inactive and how to provide data about it. Note that what information and in what order are also business rules embedded within this hypothetical method. In fact it is possible that this method makes use of another to know whether 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 is still a business rule. The second method already makes more sense to be in the type of own bank account. But not necessarily. There are a few reasons why you 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 verified by a method InativoMais180Dias()
or manually by some system function. And she would obviously do a series of procedures to ensure that the account closes. These are all business rules.
Exactly how will access the database to get this information, to record what is necessary for the closure is probably part of the mechanism and does not enter these methods. They should delegate these specificities to other methods of mechanisms, most likely to other classes.
So it turns out that business rules can rather determine how information is obtained, how to perform certain algorithms, etc. It’s not just checks whether the data you’re trying to enter is right or wrong.
Even what to do in the case of an invalid input attempt can be a business rule by itself. It should log in? Should you call some auxiliary processing? You must change some state of some object?
Business rule is anything that determines what you should do in any situation by manipulating (reading or writing, full or partial) that information in an abstract form. In a way that makes sense to the user.
When manipulation becomes more concrete, such as WriteOnDataBase()
or DisplayOnScreen()
probably already part of the mechanism. Usually they are things that make sense to the developer only, but are fundamental for the system to work.
So you can validate, guarantee (slight semantic difference), record, query, classify, condition, relate, perform, verify, permit/prohibit, calculate, delegate, derive, signal, observe, etc. The amount of possible actions, even if generic, as listed, may continue.
So methods like,
obterClientesInativos()
orlistarContaSalario()
which obtains specific information from some object can be considered business rules, certain?– gato
Generally speaking they are yes.
– Maniero