MVC分层领域模型规约 基础
-
PO:persistence Object,持久化对象。
数据最终要存储,无论以何种形式存储,都必须要持久化。加入使用关系数据库存储,一个PO对应一条数据库的记录,或者是对象从数据库查询出来的结果集的一条记录。 总结: PO : 数据库实体对象 -
DAO:Data Access Object,数据访问对象。
包含一些数据库的基本操作,CRUD,和数据库打交道。负责将PO持久化到数据库,也负责将从数据库查询的结果集映射为PO。 -
DTO : 业务层之间数据传输用的
DTO (经过处理后的PO,可能增加或者减少PO的属性): -
VO : 前端页面请求对象,以及页面返回对象 数据的封装
阿里规范:
【参考】分层领域模型规约:
DO(Data Object) :与数据库表结构一一对应,通过 DAO 层向上传输数据源对象。
DTO(Data Transfer Object) :数据传输对象, Service 或 Manager 向外传输的对象。
BO(Business Object) :业务对象。 由 Service 层输出的封装业务逻辑的对象。
AO(Application Object) : 应用对象。 在 Web 层与 Service 层之间抽象的复用对象模型,
极为贴近展示层,复用度不高。
VO(View Object) : 显示层对象,通常是 Web 向模板渲染引擎层传输的对象。
Query:数据查询对象,各层接收上层的查询请求。 注意超过 2 个参数的查询封装,禁止
使用 Map 类来传输。
总结:
控制层 用 VO ,并且在控制层进行VO到DTO的转换
业务层 用DTO,并且在业务层进行DTO到PO的转换
数据库访问层,用PO查询数据,返回到业务层
业务层在根据PO转换成DTO
控制层DTO 转换成VO
guacadmole 分层对象类型
guacamole中对数据对象做了各种抽象,利用泛型抽象了一些公共操作。
泛型标识
InternalType :内部类型相当与DTO 例如: UserGroupModle
ModelType: 模型类型相当于PO 例如: UserGroup
ExternalObject: 外部类型相当与VO,例如 APIUserGroup
VO与DTO之间的转换
如下,guacamole对每个目录菜单都进行了了抽象,如下具体连接目录菜单资源继承DirectoryResource,并且传入了泛型参数InternalType、ExternalObject分别为Connection,APIConnection
DirectoryResource抽象了目录菜单通用操作方法,如创建用户、创建用户组、创建连接
都是走createObject方法。
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ConnectionDirectoryResource
extends DirectoryResource<Connection, APIConnection> {
@POST
public ExternalType createObject(ExternalType object)
举例如下:
它定义了多个转换相关的,用于外部类型和内部类型的转换,既VO和DTO的转换,控制层到服务层数据对象的转换。
这些转换都继承与抽象类DirectoryObjectTranslator。
共如下四个具体的实现类:
ActiveConnectionObjectTranslator
ConnectionObjectTranslator
UserGroupObjectTranslator
UserObjectTranslator
这些转换类,把它当成工具类就行,实例化后,直接调响应转换方法即可,4个实现类对应4组具体的转换。
DTO 和PO 之间的转换
如下举例,利用ModeledConnection、ModeledUser这些类实例,来辅助完成PO的一些成员变量赋值。
@Override
protected ConnectionModel getModelInstance(ModeledAuthenticatedUser currentUser,
final Connection object) {
// Create new ModeledConnection backed by blank model
ConnectionModel model = new ConnectionModel();
ModeledConnection connection = getObjectInstance(currentUser, model);
// Set model contents through ModeledConnection, copying the provided connection
connection.setParentIdentifier(object.getParentIdentifier());
connection.setName(object.getName());
connection.setConfiguration(object.getConfiguration());
connection.setAttributes(object.getAttributes());
return model;
}
@Override
protected UserModel getModelInstance(ModeledAuthenticatedUser currentUser,
final User object) throws GuacamoleException {
// Create new ModeledUser backed by blank model
UserModel model = new UserModel();
ModeledUser user = getObjectInstance(currentUser, model);
// Set model contents through ModeledUser, copying the provided user
user.setIdentifier(object.getIdentifier());
user.setPassword(object.getPassword());
user.setAttributes(object.getAttributes());
return model;
}