Skip to main content
New Tag API
Source Link
Rakesh Jesadiya
  • 42.4k
  • 19
  • 132
  • 184

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 ?

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 ?

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 ?

Tweeted twitter.com/StackMagento/status/731941756573523968
Source Link

Magento 2: what are the benefits of using service contracts?

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 ?