Showing results for 
Search instead for 
Did you mean: 

Repository vs ResourceModel

Repository vs ResourceModel

Hello Magento Core Team,


After few months working on Magento 2, we have a question regarding Repositories and ResourceModels.


We consider the following :

  • The "Model" represents the Entity, so we should only use it as a getter/setter object, without "_init".
  • The repository purpose is to handle storage and retrieval of Entities

If so, what is the purpose of ModelResource ? Using it on Magento 2 imply using old method naming (_construct, _init, etc).


We finaly decided to create a custom Abstract Repository handling storage and retrieval in place of ModelResource for our custom entities. Now, we have only two objects to create for custom Model : Our Entity (Model) and our Repository.


Is this the good way ? Should we still implement ResourceModel ?


Best Regards.





Re: Repository vs ResourceModel

Your understanding of model and repository and model is correct.


Resource Model represents lower level persistence mechanism that should be used by repositories. It is the only recommended persistence mechanism for now. It is pretty old, and some of its parts do not follow our current guidelines, but we will not be changing them for backward compatibility. Magento team is working on new persistence mechanism.


Although it is acceptable to use alternative persistence mechanism, I would recommend using Resource Models to reuse all of Magento standard persistence and customization logic.