The Service Layer: How I understand it

July 13th, 2012 by rvdavid Leave a reply »

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

if you enjoyed this post, make sure you subscribe to my RSS feed!
You can also follow me on Twitter here.

Related posts:

  1. The Service Layer Series
  2. How would you handle this? – Service Layer slowly getting polluted (or so it seems)!
  3. My Zend Framework Model Layer: Part Service, Part ORM
Advertisement

4 comments

  1. Good intro, looking forward to seeing the rest. There’s so much confusion about what services and whatnot are that good posts are great to have.

    I think it would be great to have a sort of a “domain model vocabulary” thing to clarify all the terminology. Eg. that a “model” or a “domain model” can refer to the business logic layer as a whole, and an “entity” is what most people call models, etc.

    By the way, I recommend checking out the book “Domain Driven Design” which is focused on building domain oriented software architectures – in other words, how to accurately portray the domain model of some area of business in code in a flexible yet powerful way. It pretty much explains a lot of all the domain model related concepts along with services and such.

    • rvdavid says:

      I’ve read the book a while back and it influences a lot of the design decisions I make even when working with off the shelf Frameworks like Symfony 1 and ZF 1.

      By the way, since you and I are of the same vein of thought, I welcome any feedback.

      It would be great to throw around ideas and “shoot the breeze” per seh via comments so that if people come across these posts, like we would have had when we had first started out, can follow the thought process.

      Thanks man – I’ll be updating a little later as I’ve slowly reverted back to being “idly active” :)

  2. Vincent van Dijk says:

    Nice article rvdavid!

    Go a question about the role of the service layer.

    When editing a model. Is this done directly to the model, or also through the service layer? In other words, can other services/models use a model directly, or is everthing done through that specific service layer?

    Same for the relation of two models (a question / answer relation in a survey). Can I still pass the answer model to the question service to add the answer to the question. Or just give the answer ID to the question service to add the answer? I know the latter is better for testing (loose coupling).

    Thanks in advance!

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.