9

I know there is a lot of topics about this question but I'm not sure to clearly understand..

When did we need to use service contract ?

  • Are they replacing the CRUD done by Model / ResourceModel ?
  • Did we need to create everytime a service contract for each entity which will use a data table on custom module ?
  • Are they really interesting for future development ?
  • Why Magento sometimes uses save / load from the Model in Controller / Block, and sometimes not ?

Thank you

  • An interesting post of M2 service contracts this – 95623 Aug 1 '16 at 17:12
  • Hi Sanpu and thanks for the link :). The vision of Alan is interesting and now, I begin to understand the logic of the service contract. It seems to reorganize all business model logic by separate with Repository / Data / Management & Builder concepts. It's not clearly explained but probably it will replace the current using of model. What do you think about it ? – TaKe_Da_ShAkEr Aug 2 '16 at 7:15
1

Basically, service contracts are a set of interfaces that are available for modules to declare standard APIs. Broadly, there are two categories of API, The data API which exposes CRUD methods, and the operational API that actually does something.

Service contracts fulfill a number of important functions, such as:

  • Improving the upgrade process
  • Formalizing the customization process
  • Decoupling modules

Now, with the use of interfaces and APIs, it is much clearer how to interact with modules in Magento's more modular system since they only communicate via API

All classes are documented via their interfaces, so that you know exactly what each does and how using it will impact your entire implementation.

I would highly recommend you take the Magento Course Fundamentals of magento Development which Magento is providing free until 31/1/2017. It covers exhaustively Service Contract overview and devolves into Framework API, Data API and web API

0

Service Contracts are very useful in Magento2. They define exactly what fields of data you need for an input. Since Magento is developed in a way that both controllers, processes and web apis consume the same repositories and models, the service contract helps to make data to be the same along all of them. It is usefull too because it gives you the chance to inject factories and other things quite easily.

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.