What is Repository in Magento2? Why use this. I want to know more detail about Repository.


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


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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.