Improving MVC Three‑Tier Architecture with a Manager Layer
This article explains the classic MVC three‑tier architecture, identifies its drawbacks such as bloated service code and long‑running transactions, and proposes adding a Manager layer to separate generic business processing, improve code maintainability, and enhance performance in Java backend systems.
Introduction: MVC (Model‑View‑Controller) is a classic three‑tier architecture that separates the system into Model, View, and Controller layers.
In typical Java backend projects the three directories are controller, service, and dao, corresponding to presentation, business, and data‑access layers.
Problems of the traditional MVC three‑tier architecture include bloated service code, frequent long‑running transactions, mixing business logic into the dao layer, and increasingly complex SQL statements.
To address these issues, the Alibaba Java Development Manual suggests adding a generic business‑processing layer called the Manager layer beneath the service layer.
The Manager layer provides features such as a third‑party platform encapsulation layer, sinking common service capabilities (e.g., caching, middleware handling), and DAO composition reuse.
Typical usage patterns involve delegating complex business orchestration and transaction handling to the Manager, while the service prepares data and invokes Manager methods, keeping each layer’s responsibilities clear.
Traditional three‑tier code example:
@Transactional(rollbackFor = Throwable.class)
public Result
upOrDown(Long departmentId, Long swapId) {
// 验证 1
DepartmentEntity departmentEntity = departmentDao.selectById(departmentId);
if (departmentEntity == null) {
return Result.error("部门xxx不存在");
}
// 验证 2
DepartmentEntity swapEntity = departmentDao.selectById(swapId);
if (swapEntity == null) {
return Result.error("部门xxx不存在");
}
// 验证 3
Long count = employeeDao.countByDepartmentId(departmentId);
if (count != null && count > 0) {
return Result.error("员工不存在");
}
// 操作数据库 4
Long departmentSort = departmentEntity.getSort();
departmentEntity.setSort(swapEntity.getSort());
departmentDao.updateById(departmentEntity);
swapEntity.setSort(departmentSort);
departmentDao.updateById(swapEntity);
return Result.OK("success");
}Refactored version with Manager layer:
// DepartmentService.java
public Result
upOrDown(Long departmentId, Long swapId) {
// 验证 1
DepartmentEntity departmentEntity = departmentDao.selectById(departmentId);
if (departmentEntity == null) {
return Result.error("部门xxx不存在");
}
// 验证 2
DepartmentEntity swapEntity = departmentDao.selectById(swapId);
if (swapEntity == null) {
return Result.error("部门xxx不存在");
}
// 验证 3
Long count = employeeDao.countByDepartmentId(departmentId);
if (count != null && count > 0) {
return Result.error("员工不存在");
}
// 操作数据库 4
departmentManager.upOrDown(departmentEntity, swapEntity);
return Result.OK("success");
}
// DepartmentManager.java
@Transactional(rollbackFor = Throwable.class)
public void upOrDown(DepartmentEntity departmentEntity, DepartmentEntity swapEntity) {
Long departmentSort = departmentEntity.getSort();
departmentEntity.setSort(swapEntity.getSort());
departmentDao.updateById(departmentEntity);
swapEntity.setSort(departmentSort);
departmentDao.updateById(swapEntity);
}Introducing the Manager layer thus clarifies layer boundaries, enhances code reuse, and mitigates long‑transaction problems, leading to more maintainable and performant backend systems.
The article also promotes a “40‑lecture enterprise‑level practical summary” booklet covering JVM, databases, performance tuning, and other backend pain points, priced at 11.9 CNY.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.