Magento 2 Service Layer Breakout Session Magento Imagine 2014

This session is Magento 2 Service Layer: A Deep Dive into Customer Service presented by Chris O’Toole a Software Engineer at Magento. Chuck Choukalos, a product manager at Magento responsible for the service layer, is introducing the overall idea of the service layer.

The service layer streamlines customizations to allow easier installation & upgrades and simplifies external integrations. Consolidate business logic into the service layer.

What is a service layer?

The service layer encapsulates the domain model, data source and data store in turn. The UI and the web API interacts with the service layer. The service layer in Magento 2 sits between blocks or controllers and the models. Template -> block -> service layer -> model -> resource. For controllers, controller -> service layer -> model -> resource.

Services in Magento 2 will be stateless, making them easier to expose via a REST API. No need to make multiple calls to setup and then run an action.

Example Interactions

Before service layer, the template interacts with the block, which interacts with the model. Requires 5+ calls that aren't cleanly separated. There's the possibility and temptation to access the model directly when you shouldn't.

With the service layer, there's fewer calls and the actual models are hidden from the template and block.

Using the Customer Service

Example is writing an extension which will allow you to create customers from an outside source such as PayPal, Facebook, Google, etc.

In the customer model there's 72 public methods - can be daunting. The service layer simplifies that to 25 methods.

Chris walked us through writing this customization - much, much simpler using the service layer. Bring on Magento 2!

Writing your own Service

For instance, if you want to email store owner if a customer is created via an integration. You create a new service that your code calls.

Handling Upgrades

Services are versioned in Magento 2. When interfaces are changed in Magento 2, a new service layer version is created but the old one is still there. This allows you to upgrade and then work on the migration to the new service version at your leisure.

Creating a Web API

A simple XML file defines your API route, the service it uses and resources. Magento 2 service layer team is looking for feedback on what authentication methods developers would like to see concerning API calls in Magento 2. The service layer sits between the API framework and the models, including converting JSON to the proper data object.

Benefits Provided by the Service Layer in Magento 2

  • Consistency
  • Separation between presentation layer and business logic
  • Easier to customize Magento 2
  • Backwards compatibility to make upgrades less painful
  • Create web APIs via simple XML

Q&A

Testing - integration tests are written while writing the service layers. Unit tests are also present to test the service layers. Finally, integration testing is run on the service layers.

Versioning - both services and data objects are versioned. Take a look on Github - i.e., https://github.com/magento/magento2/blob/master/app/code/Magento/Customer/Service/V1/CustomerGroupService.php

Subscribe to Leadership in Commerce by Joshua Warren

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe