Understand Dao layer and Service layer (take GoFrame as an example)

The MVC framework has been talked about thousands of times, but its understanding is still conceptual.
As a model of classic architecture design, MVC must have its unique charm in the ever-changing software industry for decades.

1. The concept of Dao layer and Service layer

Dao is the abbreviation of Data Access Object (Data Access Object). From its name, it can be seen that it must be related to the operation of the database.
Service is a service. Judging from the name, it should be more abstract than the Dao layer, but it is also related to data operations.

Second, it is all operational data, why do we need to add a layer?

2.1 Different division of labor

The Dao layer is called the data access layer . It mainly involves some underlying and basic database operations. Specifically, a table is the CRUD (addition, deletion, modification, query) of the Entity (entity).
The Service layer is called the service layer , which focuses on business logic, and the operation of the database needs to be implemented with the help of the Dao layer. It can be considered that several operations are combined to achieve business.

The Dao layer is the bottom layer. When it comes to CRUD for a certain table, it can be simply understood as there are as many Dao layers as there are tables.
The Service layer can be understood as the boss of the Dao layer, and implements business logic by controlling the Dao layer through the Service layer .

A Service layer can control one or more Dao layers.

2.2 Code decoupling

As the project grows larger, code decoupling must be put on the agenda.
The Dao layer is only responsible for executing SQL statements, specifically adding, deleting, checking and modifying a certain table. Ensure that the Dao layer is correct, and the Service layer can focus on business logic with peace of mind.
Newcomers can also get started quickly when iterating code or making projects in the future.

Of course, for some small projects, it is possible not to use Service. The business logic is simple, and only Dao is enough.

At Last

Maybe the theory is boring, but it is indeed an effective summary of actual combat. The level of programmers can be seen from the directory structure of a project.

3. Some implementations of GoFrame

3.1 gf gen dao

gf gen daoIt is a productivity tool of GoFrame. The dao command is the most frequently used command in the CLI, and it is also the key command for whether the engineering specifications of the frame design can be accurately implemented.
This command is used to generate Go code files of daodata access object, dodata transformation model and entityinstance data model. Due to the many parameters and options of this command, it is officially recommended to use configuration files to manage generation rules.

The basic principle of gf gen dao is to automatically generate dao, do and entity according to the existing table structure in the database .
The development of this part of code is usually trivial and stylized. With this tool, programmers can focus on the development of upper-level business logic.

Some of these commonly used parameters are as follows:

  1. -p, --path indicates the directory where the generated files (dao, do ...) are stored
  2. -l, --link indicates the configuration of the database, such as-l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  3. -t, --tables means to generate models only for the given table, and multiple table names are separated by ",", such as-t user,products
  4. -g, --group means to specify the configuration group name of the database for the generated ORM instance, which is not required, and the default value is "default".

3.2 How to decouple dao and service

Create basic CRUD methods in the dao layer to interact with the database, including Create(), Reterive(), Update() and Delete(), where Reterive can be replaced by Get.
On the basis of the basic methods provided by the dao layer, the service calls the methods of the dao layer to implement business logic or basic operations with relatively complex logic. If a table is permModule, then gf gen daogenerated by

// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================

package dao

import (
	...
)

// internalPermModuleDao is internal type for wrapping internal DAO implements.
type internalPermModuleDao = *internal.PermModuleDao

// permModuleDao is the data access object for table perm_module.
// You can define custom methods on it to extend its functionality as you wish.
type permModuleDao struct {
    
    
	internalPermModuleDao
}

var (
	// PermModule is globally public accessible object for table perm_module operations.
	PermModule = permModuleDao{
    
    
		internal.NewPermModuleDao(),
	}
)

// Fill with you ideas below.

// Fill with you ideaas below.Create CRUD methods below, and then the service layer calls dao.PermModule.Create()these methods via .

Guess you like

Origin blog.csdn.net/weixin_45651194/article/details/129019652