Business Logic vs Service Layer – Differences and Best Practices

business-logicdesign-patternsobject-oriented-design

I read this answer: https://softwareengineering.stackexchange.com/a/234254/173318 please correct my understanding.

Business rules refers to list of steps of business in real world (no codes).

Business Logic refers to the process of converting the business rules into codes and these bunch/kind of codes addressed as "Business logic".

And what is the service layer used for? if I read this answer, it sounds no different with business logic https://stackoverflow.com/a/4817935/4190539

Is service layer a place for business logic and repository meet each other?

Best Answer

"Service layer" is an architectural term. It refers to a portion of the system that sits somewhere in the middle of a multi-tier architecture, below the user interaction layer but above the data access layer.

Business logic can be implemented in the service layer, thereby enforcing business rules.

Note however that there are cases where business logic ends up in other layers. For example, some business rules are enforced in the user interaction layer in order to improve user experience (e.g. validators written in Javascript so that you can check them without a round trip to the server). When that is the case, the service layer will usually duplicate the enforcement.

Other business rules can only be enforced at the database layer, for example when there are concurrency concerns (imagine an application where you can check out a library book) or performance concerns (imagine a program that computes a busy saleperson's annual commission, based on a complex fee structure).