In leveraging on this design pattern we took some time to set it up in exchange for better readability and maintainability of our codebase. Then go back to your browser and hit refresh and you should see 10 users being returned from the API. So let us create users using the factory, open to your terminal and go to tinker CLI, That means everything we did worked and it makes sense because we haven't created any users yet. So let us test that the route that we created by accessing this link in your browser We should see an empty set. So when we are using the Repository Pattern we just inject it to the class that we need it for without having to rewrite logic. And also what about if you have to use the same logic in a different Controller, without creating a repository you will have to copy and paste these code. In case you don't agree with me because I am only showing a simple example and it's for retrieving all users only, then for creating a new user and sometimes we have to put some logic into it, maybe some if-else statements, that actually is considered a code smell and we don't want to do that. Let's keep it that way, clean controllers. The Repository Pattern also allows us to write less code inside our Controllers and that makes it even better rather having a giant code in the Controller which isn't what we want if we are aiming for better maintainability and readability. In this case the index() method in the Controller is commonly known to return a list of resources, so let's make that as is. One thing we only want to override is the first parameter in our constructor, from the base repository it is the Model class, but since it is a user repository then we should replace it with the User model.Īnd now inside our user repository also contains the methods that we implemented inside the base repository.Īnd now bind the UserRepositoryInterface with the UserRepository inside the RepositoryServiceProvider class so our application will recognize this new repository that we created in the entire application.Īnd define a route inside routes/api.php,Īnd now inside our UserController let us define method index() but before that in our constructor we want to inject the UserRepository into the Contoller so that we will have all the methods available at our disposal. Now inside UserRepositoryInterface we only have to define the interface and extend on to the EloquentRepositoryInterfaceĪnd then inside the UserRepository class, we still implement the UserRepositoryInterface that we just created and extend BaseRepository class, by doing this we are applying Inheritance, we inherit all the methods that we just defined from our BaseRepository class and we will be able to access to the fields inside BaseRepository class so we do not have to rewrite everything. To your terminal and execute this command to create the class, These are the important part of our BaseRepository class since any new repositories will be extending to this base repository and they won't have to rewrite what we just wrote regarding the implementation details that we interfaced against the EloquentRepositoryInterface.īinding the interface and the repository class in RepositoryServiceProvider class In our constructor we inject the Model class and assign the instance into the $model property so it will be available to all methods. Inside the BaseRepository we created a protected property named as $model and it will receive an instance of class Model. Implementing the EloquentRepositoryInterface inside the BaseRepository class Making method names verbose actually makes everything easier. Now let us define the methods that we will be using throughout our entire application in this interface.Īs you may have noticed the method names are verbose and contains the verbs we just described. This what makes it beautiful because whenever we had to change a Data Provider (I am talking about Eloquent ORM, DB, or any 3rd party APIs if there's any), we only have to create another class specifically for it and we do not need to replace code.ĭefining methods inside the EloquentRepositoryInterface We can simply do that by type hinting it. The interface does not care about the implementation details, we only define the method names, what arguments are being passed into it and what we expect it returns. We can use the following verbs: 'find', 'get', 'remove', 'update' and etc that simply describes what it does. We also want to set standards when creating our repository interface, because we want the method names to be self-describing and that we should know what we get when we use the method. Then inside the interface EloquentRepositoryInterface.php , write an interface that will contain all the common methods that are used when we are accessing our database to retrieve, store, and remove data. Enter fullscreen mode Exit fullscreen mode
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |