Spring-Hibernate DAO 命名约定?
通常以以下方式命名 DAO:
UserDAO - interface
UserDAOImpl - implements UserDAO
我想知道使用后缀“Impl”进行实现是否是标准,或者更有意义的东西是否是最佳实践。谢谢。
Is it typical to name DAOs in the following way:
UserDAO - interface
UserDAOImpl - implements UserDAO
I am wondering if its standard to use the suffix 'Impl' for the implementation or if something more meaningful is the best practice. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我一般都是这么用的。有时,如果您正在创建一个希望其他人实现的接口,但您要提供参考实现,则像
DefaultUserDAO
这样的 Default 前缀可能更有意义。大多数时候,我觉得这两者可以互换使用,但在某些情况下,其中一个比另一个更清晰。
That is generally what I use. Sometimes the Default prefix like
DefaultUserDAO
might make more sense if you're creating an interface that you expect others to implement but you're providing the reference implementation.Most of the time I feel those two can be used interchangeably but in some situations one provides a little more clarity than the other.
我见过两种约定:
前者源于 CORBA;后者源于 CORBA。后者是 Microsoft COM/.NET 约定。 (感谢 Pascal 的更正。)
“不要重复 DAO”是个好主意。我个人认为这篇文章比它需要的更复杂。有一种方法可以做到这一点,而无需在查找器中进行反射,我碰巧更喜欢这种方法。如果您使用 Hibernate,通过示例查询可能是一种简单的好方法。界面看起来更像是这样的:
There are two conventions that I've seen:
The former has its roots in CORBA; the latter is a Microsoft COM/.NET convention. (Thanks to Pascal for the correction.)
"Don't Repeat the DAO" is a fine idea. I personally think that article is more complex than it needs to be. There's a way to do it without reflection in finders that I happen to prefer. If you use Hibernate, query by example can be a great way to do it simply. The interface would look more like this:
首先 - 您可能并不真正需要为每个类都有一个 DAO 类。 不要重复 DAO! 文章解释了什么是 DAO通用 DAO。想知道如何命名样板代码是没有成效的。
现在,当您拥有通用 DAO 时,您可以选择:
DAO
(接口)SessionDAO
和EntityManagerDAO
- 用于使用Session
或EntityManager
当然,只能通过接口使用 DAO。您可以轻松地在实现之间切换。
(实际上我更喜欢小写 -
Dao
,尽管它是缩写;以及Impl
后缀)First of all - you may not really need a DAO class for each of your classes. Don't repeat the DAO! article explains what is a generic DAO. Wondering how to name boilerplate code is not productive.
Now, when you have a generic DAO, you could go for:
DAO
(interface)SessionDAO
andEntityManagerDAO
- for using eitherSession
orEntityManager
And, of course, use the DAO only by interface. You can easily switch between implementations.
(I actually prefer it lowercased -
Dao
, although it's an abbreviation; and theImpl
suffix)我也是 GenericDao 和 GenericDaoImpl 约定的粉丝,如果保存或删除需要对某些持久类执行额外的操作,则
可以使用通用帮助器类的一些支持:类似的构造也可用于删除。如果您需要某种事件日志来写入每个活动并且您不想为此使用 AOP,那么这尤其有用。
我的 GenericDaoImpl 看起来像这样:
上面示例中的 persistListener 要做的是找到一个 PersistListener,其泛型类与作为参数给出的类相匹配。如果找到了,则将调用委托给适当的侦听器。我的 GenericInterfaceHandler 还可用于仅返回最具体的处理程序或仅返回给定类(如果存在)的处理程序。
如果您有兴趣,我还可以发布 GenericInterfaceHandler 实现,因为它在很多情况下都是非常强大的构造。
I've been also fan of the GenericDao and GenericDaoImpl -convention with some support from generic helper classes, should the save or delete require extra actions for some persistent classes:
Similar constructs can be used also for deletion. This is especially useful if you need some kind of event log to write each activity to and you don't want to use AOP for that.
My GenericDaoImpl would look something like this:
What the persistListener in the above example will do is to find a PersistListener with generic class matching to that of the class given as a parameter. It such is found, then call is delegated to the proper listener(s). My GenericInterfaceHandler also can be used to return only most specific handler or only handler for the given class if present.
If you are interested, I could also post the GenericInterfaceHandler implementation as it's quite powerful construct on many occasions.