preface

Is the Repository layer needed in the project? This question, there seems to be no definite answer, the following is my thinking to share with you, do not like spray.

The Repository location

I understand that Repository is a large Repository with MySQL, Redis, MongoDB… Such data.

The developer who maintains this layer can be called the warehouse administrator. When the user needs to query the data, he needs to tell the warehouse administrator, and the warehouse administrator brings it to him. As for where the warehouse administrator takes the data, the user does not have any relationship.

Similarly, when data needs to be created or updated, the warehouse administrator needs to be told to manipulate the data.

Summary: Repository encapsulates the logic of querying, creating, updating, and deleting data for users to invoke.

The implementation of the Repository

  • You can configure condition query
  • Data conversion can be configured
  • You can configure data verification

Explain the “configurable data conversion” : When we need to return the privacy field, for example: mobile phone number, if the user does not have data permissions, the middle 4 bits of the mobile phone number field need to be processed with *, and the time format of the returned field.

If you are using the Laravel framework, check out Andersao /l5-repository

The Repository interface

The Repository layer interface can be understood as a contract (see Laravel Contracts directory), which is domain-driven, and the functionality defined in Repository reflects the Domain’s intent and constraints. I will provide what Domain needs and I will not provide what Domain does not need.

For example, the interface name can be searchUsersById or searchUsersByName, but not searchUsersByInfo. You are not advised to set the queried field to *. Only the required field is returned.

What is Domain? You can think of it as the domain layer.

summary

There are pros and cons to using the Repository layer, but the downside is that it is cumbersome and not as smooth as an ORM shuttle. Of course, there are many advantages, mainly the late maintainability is greatly improved.

List some advantages:

  • Replacement and upgradeORMThe engine does not affect the service logic.
  • Easy unit testing, availableMockObject instead of the actual database access;

Above, hope to be able to help you.

Recommended reading

  • Do you know what AOP is?
  • Programming is an idea, not code
  • How to do business system research and development seriously and responsibly?