Spring-Hibernate DAO 命名约定?

发布于 2024-08-20 06:38:44 字数 153 浏览 12 评论 0原文

通常以以下方式命名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦醒灬来后我 2024-08-27 06:38:44

我一般都是这么用的。有时,如果您正在创建一个希望其他人实现的接口,但您要提供参考实现,则像 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.

樱娆 2024-08-27 06:38:44

我见过两种约定:

  1. FooDao 用于接口,FooDaoImpl 用于实现
  2. IFooDao 用于接口,FooDao 用于实现

前者源于 CORBA;后者源于 CORBA。后者是 Microsoft COM/.NET 约定。 (感谢 Pascal 的更正。)

“不要重复 DAO”是个好主意。我个人认为这篇文章比它需要的更复杂。有一种方法可以做到这一点,而无需在查找器中进行反射,我碰巧更喜欢这种方法。如果您使用 Hibernate,通过示例查询可能是一种简单的好方法。界面看起来更像是这样的:

package persistence;

import java.io.Serializable;
import java.util.List;

public interface GenericDao<T, K extends Serializable>
{
   T find(K id);
   List<T> find();
   List<T> find(T example);
   List<T> find(String queryName, String [] paramNames, Object [] bindValues);

   K save(T instance);
   void update(T instance);
   void delete(T instance);
}

There are two conventions that I've seen:

  1. FooDao for the interface and FooDaoImpl for the implementation
  2. IFooDao for the interface and FooDao for the implementation

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:

package persistence;

import java.io.Serializable;
import java.util.List;

public interface GenericDao<T, K extends Serializable>
{
   T find(K id);
   List<T> find();
   List<T> find(T example);
   List<T> find(String queryName, String [] paramNames, Object [] bindValues);

   K save(T instance);
   void update(T instance);
   void delete(T instance);
}
Saygoodbye 2024-08-27 06:38:44

首先 - 您可能并不真正需要为每个类都有一个 DAO 类。 不要重复 DAO! 文章解释了什么是 DAO通用 DAO。想知道如何命名样板代码是没有成效的。

现在,当您拥有通用 DAO 时,您可以选择:

  • DAO(接口)
  • SessionDAOEntityManagerDAO - 用于使用 SessionEntityManager

当然,只能通过接口使用 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 and EntityManagerDAO - for using either Session or EntityManager

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 the Impl suffix)

淡莣 2024-08-27 06:38:44

我也是 GenericDao 和 GenericDaoImpl 约定的粉丝,如果保存或删除需要对某些持久类执行额外的操作,则

public interface PersistListener<T> {
   void onPersist(T item);
}

可以使用通用帮助器类的一些支持:类似的构造也可用于删除。如果您需要某种事件日志来写入每个活动并且您不想为此使用 AOP,那么这尤其有用。

我的 GenericDaoImpl 看起来像这样:

public class GenericDaoImpl<T> extends HibernateTemplate {
  public void setPersistListeners(List<PersistListener> listeners) {
     this.persistListeners = new GenericInterfaceHandler( listeners, 
        PersistListener.class );
  }

  // hibernate updates the key to the object itself
  public T save(T item) {
     getSession().save( item );
     List<PersistListener<T>> listeners = this.persistListeners.getAll( item );
     for ( PersistListener<T> listener : listeners )
        listener.persist( item );
  }

  // ...
}

上面示例中的 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:

public interface PersistListener<T> {
   void onPersist(T item);
}

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:

public class GenericDaoImpl<T> extends HibernateTemplate {
  public void setPersistListeners(List<PersistListener> listeners) {
     this.persistListeners = new GenericInterfaceHandler( listeners, 
        PersistListener.class );
  }

  // hibernate updates the key to the object itself
  public T save(T item) {
     getSession().save( item );
     List<PersistListener<T>> listeners = this.persistListeners.getAll( item );
     for ( PersistListener<T> listener : listeners )
        listener.persist( item );
  }

  // ...
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文