So as some of you may know, Magento 2 recommended way to deal with models/collections for CRUD actions is to use service contracts.

But still according to the Magento SE Q&A it seems like most people tend to use the model/resource model/collection directly instead.

As an example to load a quote I can do it directly via the factory like this:


Where $this->quoteFactory is an instance of \Magento\Quote\Model\QuoteFactory

But I can also do it via service contract like this:


Where $this->quoteRepository is an instance of \Magento\Quote\Api\CartRepositoryInterface

So my questions is what are the benefits of using service contracts over factories ?


3 Answers 3


Benifits of using service contracts, (as per Magento 2 understanding)

Service contracts have a number of important functions for Magento 2, such as:

  • Upgrading modules becomes easy.

  • Simplify customizations to the module without digging into the core files.

  • Reduce the conflict between modules in the system.

  • Magento upgrades are safer using service contract.

  • For services will remain unchanged within the new releases of it, making upgrade in future are easy for existing module.

  • For model/collections this case are not true within new releases.


I think the greatest benefit is that modules can determine which functionality can be used by other modules. In Magento 1 you had helpers that where often kind of misused for this purpose (but that's a whole other discussen), but in Magento 2 your module can provide functionality to other modules (for example by 3rd party developers) and have it separated and self-contained.

Dependency Injection provides a system where you can use an interface in your construction so you only have access to those public methods.

Some examples:

Want to link a product to multiple categories? Use \Magento\Catalog\Api\CategoryLinkManagementInterface:


Want to increase a products' stock quantity? Use Magento\CatalogInventory\Api\StockManagementInterface:


These two example show perfectly the proper use of service contracts. Besides that, they provide a uniform interface to communicate with:

  • Other Modules (as described above)
  • Console Commands
  • API Calls
  • etc.

Benefits of service contract:

  • Enhance the modularity of Magento

  • Ensure a well-defined, durable API that other modules and third-party extensions can implement

  • Make it easy to configure services as web APIs.

  • Data entities reveal a simpler data model than the data model in an underlying relational database schema

  • Use different storage technologies for different data collections

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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