Where should I put a calculation method? In the entity itself or in the business class?

2

I have an architectural doubt, I believe.

I need to create a method that receives a date range and an entity on which to perform a search in the period quoted. In fact, to ask this question it matters little what the parameters of this method are. It is important to know that a calculation will be made on this entity using any algorithm.

The question is: where to put this method? In the class itself that represents the entity or in the business class of that entity? I do not know if I could express myself correctly.

I've heard of anemic entities, which would be entities only with the representations of columns and nothing else, without business rules. How to decide this?

    
asked by anonymous 27.04.2016 / 21:52

3 answers

4

In the object-oriented paradigm, an object in its most basic form has behaviors.

If an object has no behaviors, it is no more than a data structure (whose declaration and instance in Java have the same form as the declaration and instance of objects, making the difference in this language, therefore only conceptual) / p>

If your entities are anemic (devoid of behaviors), leaving all business behavior delegated to other objects (which by the probable absence of business state itself will in fact be just classes grouping functions), you are actually adopting the Anemic Domain Model , what, according to some view, is something far removed from Object Guidance

Now, if your entities publish all behavior relative to them, being delegated to other objects only behaviors that do not belong naturally to any specific entity, then you are probably closer to the Object Orientation paradigm.

If you want something more object-oriented, so to speak, then the decision whether this method belongs to the entity itself or to another class depends rather on considering what the function of the method is, what the method will operate and what results it should produce. You can not decide where the method should reside without knowing what its function is.

    
28.04.2016 / 19:45
4

I think the answer is in the question. If you have one class for the entity and one for the business rules, you have no reason to put this method in the entity. The rule class is there just to have all methods that affect that entity. In this case you maintain the entity as anemic class .

But nothing in Hibernate requires this and many people find it overkill to have a separate class for business rule. If you think it is good to reduce this complexity and have only the entity with the rules there, the decision becomes easy:)

What is the best for your case I can not say. Try experimenting with both, even if you can not implement or put into production.

    
27.04.2016 / 22:37
0

There is no right or wrong in this case, the architecture you define yourself according to the solution you need to implement. If you extract all business rules from your entity and create business / service classes then the most interesting thing would be that this calculation would also be outside your entity.

But nothing prevents you from including business rules in entities, there are situations in which an entity is developed so that it can "resolve" without relying on other classes, so the rules would remain within the entity itself. / p>     

27.04.2016 / 23:47