What is Repository in Magento2? Why use this. I want to know more detail about Repository.
1 Answers
Repositories are part of the Service Contracts (they are implementations of interfaces in Api), this means they are meant as public interface to other modules.
In general, the purpose of a repository is to hide the storage related logic.A client of a repository should not care whether the returned entity is held in memory in an array, is retrieved from a MySQL database, fetched from a remote API or from a file.
Repositories generaly have methods like findById(), findByName(), put() or remove().but in Magento we call it as getbyId(), save() and delete() etc.
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.
You can use this link for more information.
- 14,814
- 9
- 44
- 75
- 1,076
- 6
- 10