使用 spring jdbcTemplate 让我们的 Person 进入我们的 User 的最佳方法是什么
我有两个域对象:
- 用户(有一个人)
- 人员
仅供参考:在我们的域中,有些人(人:-)不是用户,所以我们不能将它们组合起来。
我试图找出将 Person 对象填充到 User 对象中的最佳方法。我想出了 3 种可能的解决方案,并且很好奇其他人做了什么以及其他人可以看到的优点和缺点。
选项 1:
将 UserDao 和 PersonDao 连接到 Userservice,然后让 userService.getUser(userId)
调用 userDAO.getUser(userId)
然后获取我们从中返回的用户并调用 user.setPerson(personDao.getPerson(user.getPersonId()))
优点:不必连接dao 中的 dao (不是那么糟糕...不确定)
缺点:让 dao 返回未完全初始化的域对象似乎是错误的,对吗?那么你就得关心User对象中是否有一个人。访问数据库两次。此外,您还必须向 User 对象添加 personId,而不仅仅是 Person 对象。
选项 2: 让 UserDao 的 getUser() 方法将用户表与人员表连接起来,然后在 UserRowMapper 内部设置用户,然后调用 user.setPerson(personRowMapper.mapRow(rs, rowNum))
优点:不必在一个 dao 中连接一个 dao(不是那么糟糕吗......不确定)。此外,您只需访问数据库一次,而不是在其他选项中访问数据库两次。
缺点:PersonRowMapper 将是它自己的类,而不是封装在 PersonDao 中(应该是吗?)。
选项 3:
将 PersonDao 连接到 UserDao,然后调用 user.setPerson(personDao.getPerson(rs.getLong("person_id")));
优点:personDao 负责获取 Person 域对象,而 userDao 不需要知道如何检索该人的详细信息。
缺点:personDao 与 userDao 相连(这很糟糕吗?)。访问数据库两次。
I have two domain objects:
- User (which has a Person)
- Person
FYI: In our domain there are Persons (people :-) that are not users so we can't just combine them.
I am trying to figure out the best way of getting the Person object populated into the User object. I have come up with 3 possible solutions and was curious what others have done and any pros and cons that others can see.
Option 1:
Have our UserDao and PersonDao wired into our Userservice and then have userService.getUser(userId)
call the userDAO.getUser(userId)
then take the user we get back from that and call user.setPerson(personDao.getPerson(user.getPersonId()))
Pros: Don't have to wire in a dao inside a dao (isn't that bad...not sure)
Cons: Something seems wrong about having the dao return a domain object that isn't fully initialized right? Then you have to worry about whether or not there is a person in the User object. Hitting the database twice. Also you then have to add a personId to the User object instead of just having a Person object.
Option 2:
Have the UserDao's getUser() method join the user table with the person table and then inside the UserRowMapper setup the user and then call user.setPerson(personRowMapper.mapRow(rs, rowNum))
Pros: Don't have to wire in a dao inside a dao (isn't that bad...not sure). Also you only hit the database once instead of hitting the database twice in the other to options.
Cons: The PersonRowMapper would be its own class and not encapsulated in the PersonDao (should it be?).
Option 3:
Have the PersonDao wired into the UserDao and then just call user.setPerson(personDao.getPerson(rs.getLong("person_id")));
Pros: The personDao is in charge of getting the Person domain object and the userDao doesn't need to know details about how that person was retrieved.
Cons: The personDao is wired into the userDao (is that bad?). Hitting the database twice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是第二种选择,正如您指出的那样,两次访问数据库的成本很高。
我有一个建议,不应该是,User is a Person (Inheritance) 而不是 User has a Person (Composition)
It is the second option, as you pointed out hitting the database twice is costly.
I have one suggestion, shouldn't it be, User is a Person (Inheritance) rather than User has a Person (Composition)