There are several articles and posts out there regarding what a Service Layer is. I’m going to throw my two cents in as to what my interpretation of the Service Layer is.
Whenever I am in conversations with other devs or giving presentations about the Service Layer, I convey the following points:
A Service Class is primarily “just another object”.
When I had first started reading up on this my mind was flooded with several definitions which are all true and “click” after you understand what the Service Layer actually does. Start off thinking of a Service Classes as a normal Class in your library.
A Service Layer is a collection of Service Classes.
A Service Layer consists of serveral Service Classes which can call upon one another. I’ll be discussing the strategies I use in a little more detail later in the series.
At this stage, we’ll start with the notion that a Service Object can call other service object to use it’s methods. Additoinally, it can provide access to other Service Object methods via proxy.
The Service Layer is all about encapsulation (of business logic)
If I ever have to explain to someone what a Service Layer is all about, I would say, it’s all about encapsulation of business logic. Mathew Weier O’Phinney once coined on his blog that the Service Layer is a gateway to the domain. The domain in most cases refer to the Model which by de facto, is tied up to an ORM in Symfony or an ORM / Data Mapper in ZF.
I create Service Classes that encapsulate business rules and implementation code for creating and setting up these instances of the Model providing an interface that can be easily mapped to Requirements.
I’ll be expanding more on these points later on, but will hold off at this stage. To summarise the post and my view of the Service Layer, you could string together the three points here and come up with a definition: “The Service Layer is a Collection of Regular Classes that encapsulate business logic”. Because your business logic is encapsulated, it is accessible from anywhere.
I do go through this, but when I’m lazy, I say “you know how Web Services open up a web application? Well, think of the Service Layer as a Web Service for your Model and related assets like Forms etc”
I’ll expand on these posts in the weeks to come so be sure to come back. Subscribe to my RSS feed (if people still use that nowadays – it was pretty big when I was coming up ).
- The Service Layer Series
- How would you handle this? – Service Layer slowly getting polluted (or so it seems)!
- My Zend Framework Model Layer: Part Service, Part ORM