17

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?

2
  • 1
    "service layer" is a generic term, it can be or contain whatever you like. That SO question you cited was talking about the "service layer in ASP.NET MVC", which gives the term a more specific focus. Are you intentionally talking about the latter? Or did you simply miss the difference?
    – Doc Brown
    Mar 1, 2017 at 7:35
  • 1
    that is what i got so far. but i would love to hear your explanation about them all.
    – Kakashi
    Mar 1, 2017 at 8:49

1 Answer 1

20

"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).

6
  • is it fine if i have service directory and contains classes as a places where i put bussiness logic and i inject repository, another services, validation there?
    – Kakashi
    Mar 1, 2017 at 8:14
  • Yes, it is natural to inject other services, including data access, into a service layer-- it has to store data somehow, and if written properly it doesn't know how to do it by itself.
    – John Wu
    Mar 1, 2017 at 8:16
  • repository must not contain any business code right? it means repository must be clear from validation, filter or any other strijg manipulation such as strtolower for example?
    – Kakashi
    Mar 1, 2017 at 8:51
  • Not necessarily (I already gave you two examples in my post) but it is a good practice to move as much business logic into the service layer as you can.
    – John Wu
    Mar 1, 2017 at 9:01
  • 1
    the service layer will usually duplicate the enforcement - and that shouldn't be a problem, in fact it's needed. JS validations are good for improving UX, but they cannot be relied upon alone. Server must enforce the checks again. Jan 26, 2018 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.