Question

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:

$this->quoteFactory->create()->load($quoteId);

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

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

$this->quoteRepository->get($quoteId);

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 ?

Was it helpful?

Solution

Benifits of using service contracts,(As per magento 2 understanding)

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

  • Module Upgradation become easy.

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

  • Reduce the conflict between modules in the system.

  • Magento upgradation 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.

OTHER TIPS

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:

$this->categoryLinkManagement->assignProductToCategories(
    $sku,
    $categoryIds
);

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

$this->stockManagement->backItemQty(
    $productId,
    $itemsToReceive
);

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top