[英]Data transfer object in dao design pattern
I am bit confused about what data should a DTO contain. 我对DTO包含哪些数据感到困惑。 For example let's assume that we have two tables: User, and Orders.
例如,假设我们有两个表:User和Orders。 Orders table contains id_users, which is foreign key to user table.
Orders表包含id_users,它是用户表的外键。
Obviously I have two DAOs, MysqlUserDao and MysqlOrdersDao, with crud operations, and two transfer objects User, and Order, in which I store jdbc rowset. 显然我有两个DAO,MysqlUserDao和MysqlOrdersDao,带有crud操作,以及两个传输对象User和Order,其中我存储了jdbc rowset。
If I want to get the list of users and for each user all his orders how should I do: 如果我想获取用户列表以及每个用户的所有订单,我应该怎么做:
1) In my MysqlUserDao create a function: getUsersAndOrders(select users. ,orders. from users join orders) And my User DTO should have a OrderList property in where i put orders ? 1)在我的MysqlUserDao中创建一个函数:getUsersAndOrders(选择用户。 ,订单。来自用户加入订单)我的用户DTO应该有一个OrderList属性在哪里我下订单?
2) In my MysqlUserDao i create a function getAllUsers(select * from users), and foreach user I use MysqlOrdersDao function getOrder(id_user); 2)在我的MysqlUserDao中我创建了一个函数getAllUsers(select * from users),而foreach用户我使用MysqlOrdersDao函数getOrder(id_user);
And some clarifications: 还有一些澄清:
1) For each table in database I need to create a DAO object? 1)对于数据库中的每个表,我需要创建一个DAO对象? or just for complex ones?
或只是复杂的? For example products and images, should be 2 dao or just one?
例如产品和图像,应该是2 dao还是只有一个?
2) a DTO object should have only properties and setter getter, or it is possible to have other methods like convertEuroToUsd etc. 2)DTO对象应该只有属性和setter getter,或者可以有其他方法,如convertEuroToUsd等。
thanks 谢谢
In your scenario #1 is the best option because #2 generates too much overhead. 在您的场景中,#1是最佳选择,因为#2会产生过多的开销。
1) In my MysqlUserDao create a function: getUsersAndOrders(select users.,orders. from users join orders) And my User DTO should have a OrderList property in where i put orders ? 1)在我的MysqlUserDao中创建一个函数:getUsersAndOrders(选择用户。,订单。来自用户加入订单)我的用户DTO应该有一个OrderList属性在哪里我下订单?
Clarifications: 1: If your database has a good Design, then a DAO for each table is a good approach. 说明:1:如果您的数据库具有良好的设计,那么每个表的DAO是一个很好的方法。 There some cases where you can merge DAOs together (eg: inheritance).
在某些情况下,您可以将DAO合并在一起(例如:继承)。
2: Yes. 2:是的。 It should be a plain bean (or POJO if you want).
它应该是一个普通的bean(或POJO,如果你想)。 I suggest creating another layer where you can define your workflow.
我建议创建另一个图层,您可以在其中定义工作流程。 I've seem people calling this extra layer as model, sometimes DataManager, sometimes just Manager.
我似乎有人将这个额外的层称为模型,有时称为DataManager,有时只是管理器。
For instance: When creating a order you should insert a record in Order table and also insert a record in the Notification table (because end users will be notified via email every time a order is created) 例如:创建订单时,您应在Order表中插入记录,并在Notification表中插入记录(因为每次创建订单时都会通过电子邮件通知最终用户)
class OrderManager {
private OrderDAO oDao;
private NotificationDao nDao;
public saveOrder(OrderDTO o) {
Long orderId = oDao.save(o);
NotificationDTO n = new NotificationDTO();
n.setType(NotificationType.ORDER_CREATED);
n.setEntityId(orderId);
nDao.save(n);
}
}
UPDATE: In most cases we can say that: 更新:在大多数情况下,我们可以说:
There is an important idea of LAZY or EAGER load when it comes to handling collections. 在处理集合时,有一个重要的想法是LAZY或EAGER加载。 But this is another subject :D
但这是另一个主题:D
Disclaimer: + The following assumes that these DTOs are used mainly for persistence, ie, for use with DAOs. 免责声明:+以下假设这些DTO主要用于持久性,即用于DAO。 + this approach is very oriented towards a relational database persistence + it is assumed a user can have placed orders, but that an order can have at most one user + also, that you want to query/process separatedly orders and users
+这种方法非常面向关系数据库持久性+假设用户可以下订单,但订单最多只能有一个用户+,您希望分别查询/处理订单和用户
I would have done the following: 我会做以下事情:
The resulting code would be as follows: 结果代码如下:
class OrderManagerServiceApplication {
private OrderDAO oDao;
private UserDao uDao;
private UserOrderDao uoDao;
public saveOrder(OrderDTO o, String userId) {
// Save the order
Long orderId = oDao.save(o);
// Save the association to the user who ordered
UserOrderDTO uodto=new UserOrderDTO(orderId,userId);
uoDao.save(uodto);
}
public List<OrderDTO> getOrdersForUser(String userId) {
// get the orders associated to the user
List<String> orderIds=uoDao.getAllForUser(userId);
// retrieve the order DTOs
ArrayList<OrderDTO> result=new ArrayList<OrderDTO>();
for (String orderId:orderIds){
result.add(oDAO.getOrder(orderId));
}
return result;
}
public UserDTO getUserForOrder(Stirng orderId) {
// get the user associated with the order
String userId=uoao.getUserForOrder(orderId);
// retrieve the user DTO
return uDAO.getUser(userId);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.