Question

I have gone through a couple of tutorials in Magento 2, and this confuses me a little. I can see there are basically two ways by which we can read/write business entities:

Retrieve Data

Using A Factory Approach

$object = $this->myFactory->create();
$object->load($myId);

Using A Repository Approach

$repo   = $this->myRepository();
$object = $repo->getById($myId);

Save Data

Using A Factory Approach

$object = $this->myFactory->create();
$object->load($myId);
$object->setData('something', 'somethingDifferent')->save();

Using A Repository Approach

$repo   = $this->myRepository();
$object = $repo->getById($myId);
$object->setData('something', 'somethingDifferent');
$repo->save($object);

I can also see that, both a repository and a factory class can be injected using dependency injection. This is confusing at least for me.

When should we use a repository approach and a factory approach? What is the best practice we need to follow?

Was it helpful?

Solution

If there is a repository and it does what you need well, always prefer the repository.

Repositories are part of the Service Contracts (they are implementations of interfaces in Api), this means they are meant as a public interface to other modules.

Use Repositories for full loading

$model->load() is not part of the service contract. I had a question on that particular topic, you might find the answers useful: Is there ever a reason to prefer $model->load() over service contracts?

Use Factories to create new entities

Repositories do not come with methods to create a new entity, so in that case, you will need a factory. But use the factory for the interface, such as Magento\Catalog\Api\Data\ProductInterfaceFactory - it will create the right implementation based on DI configuration.

Then use the repository->save() method to save it.

Use Collection Factories if you need more control

The following is not official Magento best practice, but currently, repositories do not give you fine control over what to load. The search criteria API lets you define filters, but for example, there is no way to select particular EAV attributes or specify which index tables to join.

These are implementation details, hidden from the service contract APIs, but often these implementation details matter and you get poor performance if you ignore them. For that reason, as soon as the repositories are limiting me I don't hesitate anymore to use the underlying collections.

OTHER TIPS

Good question.

Even if both Repositories and Factories let us access an Entity I think we should focus on their responsibility.

From Magento documentation: "Factories are service classes that instantiate non-injectable classes, that is, models that represent a database entity. They create a layer of abstraction between the ObjectManager and business code."

From Alan Storm's article: "A repository object is responsible for reading and writing your object information to an object store"

My interpretation is: If our purpose is to work with non-injectable (so called "newable") objects we should use Factories; if our focus is on searching/reading/writing objects within an object store we should use Repositories.

This is my idealistic approach to the topic; keep in mind that actual implementation may force us to mess up things, as pointed out by Alan.

Enjoy.

I would say the way forward it's to start using repositories as they allow code separation between data reading/writing and business logic.

There is a very detailed article written by Alan Storm about this, explaining how to use repositories but also looking into some drawbacks of this new method: http://alanstorm.com/magento_2_understanding_object_repositories/

Also, from the Magento documentation, explaining the benefits of this new aproach: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/service-contracts/service-contracts.html

Hope this answer might help other extension developers as well.

We have to save model using Repository only.

  1. Factory Model in Magento 2 hold very limited data.
  2. On the Other hand, Repository Model contains all data, in case of eav attributes related to customer, products , etc.
  3. For saving model, always use Repository to save any entity, if factory model is used for saving model, it deletes all non-system eav attributes related to that entity (customer, product, etc.).

  4. For loading model purpose, Repository are best option to get model using getById() method.

I will recommend to use Repository as much as possible specially for model save purpose.

Now load,save,delete methods (models) are deprecated.So we can use resource model or repository.

Magento now using entity manager concept for save,delete,load operations.

Resource models have entity manager object to fulfil those operations.

$categoryModel = $this->_objectManager->create('\Magento\Catalog\Model\CategoryFactory')->create();        
  $categoryResource = $this->_objectManager->create('\Magento\Catalog\Model\ResourceModel\Category');        
  $categoryResource->load($categoryModel, 3);        
  echo $categoryModel->getName();

Using solely collections seems like the lazy way out. If an existing repository underperforms, simply create a new collection that meets your needs and create a new repository to leverage the new collection. In this way you are still following best practices by abstracting away the collection and you can still implement the service contract.

Many answers, including the accepted one, seem to ignore the fact that repositories use collections under the hood. There is nothing stopping you from getting finer control when using a custom repository, since you can use a custom collection.

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