Does Information Expert pattern contradict Service Layer Pattern?

80 Views Asked by At

While reading about GRASP patterns I stopped at Information Expert, which states that behavior on classes state should be within that same class. But that is a complete opposite with Service Layer pattern which I used for years. Services have behavior for Domain objects' state. Which confuses me

I would be grateful for a thoughtful answer.

1

There are 1 best solutions below

0
On

Good question.

Information expert is about objects that contain data (aggregates, entities, value objects) and the operation mostly depends on the data they have.

A trivial example: imagine you have a Customer class and you want to produce the customer full name. Rather than having a class that does:

   class SomethingService {
     String customerFullName(Customer customer) {
       return "${customer.firstName()} ${customer.lastName()}";
     }
   }

A much better approach is to have that method in the customer class itself, as the customer class has all the data required. In a way, this is the opposite of the Feature Envy Antipattern.

About the service layer, this could be seen as Pure Fabrication in GRASP terms. With the caveat that in things like DDD there are Application Services (very similar as a Facade from the Gang of Four book or a Port in Ports and Adapters Architecture) and Domain Services (internal services to the domain which don't really fit in any other class). The way I think about the latter is that if I need to add some functionality but there's no clear place where that should go, it ends up in a new domain service. I hope this is clear!