Introduction
Repository in one of the new design pattern introduced in magento2 along with interceptors, proxy and others. Repositories provide a way to access and manipulate the data from and into database, web services or any other means, in simple words you can say repositories acts as an API for the third party developers to access and work with the complex data of your extension.
Problem
Suppose a third party developer wants to access some data from your module, he checks in your module that how he can get data, he founds a function but he cannot use the same function because it has some dependency there and he wants to pass those dependencies as an argument, so he will simply copy that function and will use it in his module, using this will solve the problem for temporary period, now whenever any update of the original module is released the developer need to check the changes in the code and implement it in his module, otherwise the module will break or not function well, and in the complex system like getting and updating correct data is not an easy task at all.
Some of the problems that can arise if you are not using a standard approach:
- code duplicity
- higher chances of buggy code
- object mocking becomes difficult for testing
Solution
So to solve this problem magento2 has introduced repository design pattern, this pattern is new to Magento but not for programmers. It works as an intermediator between the data access and data accessor.It centralizes all the data access logic for the module to one place.It facilitates following objectives:
- simplifies data access and web services logic by providing a centralized repository like system.
- simplifies object mocking so that testing can be done easily.
- easy to understand for the developer, as he does not need to understand the complex data access and updating logic.
- repositories can be easily converted to web services, so you do not need to write web services API again.
- centralise the caching login for the data source
Repositories are very common for the programmers where they need to manage complex data, it makes developers life more easy since it is very rare that repositories code updated except major updates, so you don’t need to worry much about application break issues.
go through these links if you want to know about its implementation in code end:
https://webkul.com/blog/create-repositories-magento2/
https://webkul.com/blog/how-to-use-search-criteria-in-custom-module/
Thanks 🙂
Be the first to comment.