Designing Repository and Service tiers in CakePHP 4 for large applications

Tram Ho

Introduce

Hello guys, it’s been a while since I wrote a post. Today I share about the topic of separating the repo and service layers in Cakephp 4 . The content is just me learning and sharing to learn and save. If you have any questions or suggestions for me, please comment to share, learn and develop.

1. What is Repository?

In software programming, a Repository is a design pattern used to abstract and separate data storage from objects in the system. The Repository pattern provides a generic and abstract approach to data access, making it easy to change the way data is stored and retrieved without changing the objects that use that data.

In applications that use databases (databases), the Repository pattern is often used to retrieve and store data, and to interact with tables in the database. Repository methods usually include basic operations such as create, read, edit, delete (CRUD – Create, Read, Update, Delete), and other data query methods.

The Repository pattern is part of the SOLID design principles, which increases modularity, reduces dependencies between system components, and eases future maintenance and changes to data storage.

2. What is Service?

In programming, Service is a component of software architecture (architecture) used to handle the application’s business tasks. In other words, a Service is a class that handles certain tasks related to the application’s business. Service can call objects and methods from other classes such as Repository, Model, … and return results to Controller, View or other Service classes.

Business tasks may include data processing, data checking and validation, emailing, report generation, database queries, calculations, integration of other services,… Service helps separate business tasks from other layers in the MVC architecture, simplifying application development and maintenance.

Okay, I’ll go into the details now.

Main content

In Cakephp 4, there is support for separating the Repository and Service sections. You can use the repository tier to perform queries to the database and use the service tier to write data processing logic. Here is an example of how to use abstract class and interface to create base repository and service in CakePHP 4:

1. Create Base for Repository

2. Create Example Repo

Below, when using the service, please create a new Repo, I will create an ArticleRepository for example:

Note that you need to register the repository and service with the DI container before using them. You can register using the DI container’s set() method. For example, if you want to register an instance of ArticleRepository .

In the src/Application.php file, you can create properties to store the configurations of the repositories. For example:

3. Create Service

We will also be signing the service in src/Application.php

4. DI Service into Controller

  1. Declare the $serviceName variable in the controller’s __construct method, for example:

  1. Use the $articleService variable in your controller methods, for example:

In the index method, we use the $articleService variable to call the service’s getAllArticles() method. Note that, when using Dependency Injection in CakePHP 4, you don’t need to create the instance of the Service using new but instead, CakePHP will automatically create the instance and inject it into the Controller as needed. In the above example, when creating the ArticlesController instance, CakePHP will automatically create the ArticleService instance and pass in the __construct method.

End

Hope the above sharing will help you. Thanks for watching.

Share the news now

Source : Viblo