19

In Magento 2, is there a concrete example of something that's built using the Service Contract concept? I've seen this term thrown around a lot, but looking at Magento 2 as it exists now it's not clear to me if Service Contracts are more guiding principles, or if they actually tie back to specific implementations of things in Magento 2.

8

As I understand it all the interfaces defined in the Api folder are the Service Contracts. So anywhere the interface is used instead of the actual implementation of the class it uses the Service Contract.

An example would be this plugin implementation here https://github.com/magento/magento2/blob/develop/app/code/Magento/GiftMessage/Model/Plugin/OrderGet.php

It uses

protected function getOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)

instead of \Magento\Sales\Model\Order

6

Services (also called service contracts) are one of our core development patterns in Magento 2 to ensure stable interfaces for easy customization/extension. They take 2 forms in the code base ( both are annotated with @api on the class or class methods to identify them as stable interfaces that you can customize and or expose as a web API ): API or SPI. The API's are defined in the API folder and take up two forms - a fully refactored service and just an API only module.

Fully refactored services are reflected in Customer, Inventory, Tax, and Quote* modules (Customer being the service to emulate, Quote has a areas remaining that need to be refactored). An API only module can be seen in Catalog, Sales and CMS. For fully refactored services you should only have to do a plugin on the service method to impact both web apis and the GUI. For API only modules you'd need to plugin on service method to impact web apis but would still need to do 1x style customizations to impact the GUI.

SPI's are basically interfaces within the code annotated with @api that are intended spots that 3rd parties would implement to provide some business functionality. An example of an SPI (CarrierInterface) defined in the Shipping module that you'd implement in your shipping module (i.e. Ups ).

The service framework provides a number of interesting advantages. Easy exposure as a web api (and coming post 2.0 via message queues) vi webapi.xml configuration (as SOAP and REST style). In the near term (post 2.0) we'll be adding API call outs (sync calls, or Webhooks if configured to fire async, messages out) that can all be managed/exposed via configuration. Safer install/upgrades - you can programmatically identify problem situations ( 2 or more extensions implementing the same interface ). Streamlined customization that impacts both web apis and gui as there's just one method/service to customize ( for fully refactored module or new modules/services created by the community ).

1

Check usages of these methods:

  • \Magento\Customer\Api\AccountManagementInterface::createAccount
  • \Magento\Customer\Api\CustomerRepositoryInterface::getById

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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