Magento 2 provides multiple ways to interact with models and data. Two of the most common approaches are using Repositories and Factories. Understanding when to use each is important for writing clean, upgrade‑safe, and performant code.
What is a Repository?
A Repository is part of Magento’s Service Contracts. It is an implementation of an interface defined in the Api folder. Repositories act as a public API for modules, ensuring consistency and backward compatibility.
Key points:
- Repositories are used for full loading of entities.
- They provide methods like
getById(),getList(),save(), anddelete(). - They hide implementation details (EAV, joins, etc.) and expose only the contract.
Example:
$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$product = $productRepository->getById($productId);
What is a Factory?
A Factory is used to create new instances of entities. Repositories do not provide methods to create new objects, so you need a factory when instantiating a fresh entity.
Key points:
- Factories are generated automatically by Magento (e.g.,
ProductFactory). - They are used to create new objects that can later be saved via a repository.
- Factories respect dependency injection and create the correct implementation.
Example:
$productFactory = $objectManager->get(\Magento\Catalog\Api\Data\ProductInterfaceFactory::class);
$product = $productFactory->create();
$product->setSku('test-sku');
$product->setName('Test Product');
$productRepository->save($product);
When to Use Repository vs Factory
| Use Case | Repository | Factory |
|---|---|---|
| Load existing entity | ✔️ getById(), getList() |
❌ Not suitable |
| Create new entity | ❌ Cannot create | ✔️ Use create() |
| Save entity | ✔️ save() |
❌ Must pass to repository |
| Delete entity | ✔️ delete() |
❌ Not applicable |
Collections vs Repositories
Repositories are the official way to interact with entities, but they can be limiting. For example, the SearchCriteria API allows filtering, but you cannot select specific EAV attributes or control joins. In such cases, developers often use Collection Factories for fine‑grained control.
Example:
$collection = $collectionFactory->create();
$collection->addAttributeToSelect(['name', 'sku']);
$collection->addFieldToFilter('status', 1);
Best Practices
- Prefer repositories when they provide the functionality you need.
- Use factories to create new entities, then save them via repositories.
- Use collection factories only when repositories are too limited.
- Avoid using
$model->load()directly — it is not part of service contracts. - Always code against interfaces (
ProductRepositoryInterface,ProductInterfaceFactory) for upgrade safety.
Conclusion
In Magento 2, repositories and factories serve different but complementary roles. Use repositories for loading, saving, and deleting entities, and factories for creating new ones. Collections can be used when you need fine‑grained control over queries. Following these practices ensures your code is clean, efficient, and aligned with Magento’s service contract architecture.
Happy Coding & Clean Architecture!