Search code examples
objectoopdesign-patternsvalue-objects

Business logic inside of value object


I think value object should not have business logic.

That maybe confused other programmers.

for exmaple,

public class PersonVO {

    private String name;
    private int age;

    public void somethingBusinessLogic() {
        // Do very complecated logic -> Using Reflection, Conversion
    }
    
}

If i use this VO, have to look vo logic how to work.

many of programmers put their business logic inside of VO.

I wonder what is best practice?


Solution

  • It depends on what architecture you are using in application. If you have layered architecture:

    • Service layer should have business logic
    • Data layer is responsible for CRUD operation and this layer should be very simple. This layer should not have any logic, except base CRUD logic. Model classes should not have any logic as ORM prevents you from having logic in model classes.
    • UI layer should be responsible only for presentation and deal with DTOs or ViewModels

    Read more about other architectures here in this beautiful answer