java DAO 包结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9981277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:08:02  来源:igfitidea点击:

DAO package structure

javadao

提问by dhalsim2

I'm writing some simple DAOs in Java using JDBC (no Spring, Hibernate or anything else).

我正在使用 JDBC(没有 Spring、Hibernate 或其他任何东西)在 Java 中编写一些简单的 DAO。

Is it better to put the implementation DAOs in the same package as their interfaces or to put them in a sub-package?

将实现 DAO 与其接口放在同一个包中还是将它们放在子包中更好?

Example:

例子:

com.mycompany.myproject.dao.MyDao
com.mycompany.myproject.dao.MyDaoImpl

OR

或者

com.mycompany.myproject.dao.MyDao
com.mycompany.myproject.dao.impl.MyDaoImpl

If you suggest the sub-package structure, what would you suggest as a sub-package name? .impl? .sql? .jdbc?

如果您建议使用子包结构,您会建议什么作为子包名称?.impl?.sql?.jdbc?

Realistically, I'm not going to have multiple implementations. Am I over-engineering this?

实际上,我不会有多个实现。我是否过度设计了这个?

采纳答案by Alonso Dominguez

When designing an application there is no standardway of structuring in packages, experience is what usually helps every one to decide what are the appropriate names for our packages.

在设计应用程序时,没有标准的包结构方式,经验通常可以帮助每个人决定我们的包的合适名称。

About packaging implementations of your interfaces in the same package or in a different one just think about how Java itself is structured: usually an implementation class is packaged in the same package that its interface, but is not all the times.

关于在同一个包中或在不同的包中打包接口的实现,只需考虑 Java 本身的结构:通常实现类与其接口打包在同一个包中,但并非总是如此。

If you were about to have several implementations of the same DAO's then it would make sense having them structured in .jdbc, .jpaor .jdosub packages. If your are only going to have one implementation both of the options you enumerate make sense in some way (same package or a .implsub package).

如果您正要具有相同的DAO的几个实现那就让让他们在层次感.jdbc.jpa.jdo子包。如果您只打算实现一个实现,那么您列举的两个选项在某种程度上都有意义(相同的包或.impl子包)。

Regarding over-engineering I would recommend you this article. Even though you are going to have just one implementation of your DAO's, it would make sense to have them defined as an interface and implementation as that will help you in a potential future to rewrite your DAOs for other frameworks whilst the code that makes use of them keeps unchanged.

关于过度工程,我会向您推荐这篇文章。即使您将只有一个 DAO 实现,将它们定义为接口和实现也是有意义的,因为这将有助于您在潜在的未来为其他框架重写您的 DAO,同时使用他们保持不变。

At the end it's up to you (or you and your peers) to reach a consensus and make the decision that makes more sense in your specific case.

最后,由您(或您和您的同行)来达成共识并做出对您的特定情况更有意义的决定。

EDIT

编辑

An application usually has one implementation per DAO interface and that isn't over-engineering at all, it simply doesn't make sense to have the same DAO interface implemented for JPA and for JDO. Some of the purposes of using the interface/implementation pattern is to ease re-factoring, testing by means of mock objects, etc..

一个应用程序通常每个 DAO 接口都有一个实现,这根本不是过度设计,为 JPA 和 JDO 实现相同的 DAO 接口根本没有意义。使用接口/实现模式的一些目的是简化重构,通过模拟对象等进行测试。

P.S.: I usually rely on JDependto distribute my application classes in packages avoiding cycles as most as I can.

PS:我通常依靠JDepend在包中分发我的应用程序类,尽可能避免循环。

回答by Johan Sj?berg

I don't think either is better, but in this case I prefer the first alternative. It would be in line with having ArrayList, LinkedList, etc. , in the same package as List.

我不认为哪个更好,但在这种情况下,我更喜欢第一个选择。这将是符合具有ArrayListLinkedList等等,在相同的封装中 List

When using additional frameworks, such as hibernateI prefer the second option with MyDaoand HibernateDaoas the implementor.

当使用额外的框架时,比如hibernate我更喜欢第二个选项MyDaoHibernateDao作为实现者。

回答by Guillaume Polet

I would go with your second option (although none is really better), because you can see immediately in your imports if an impl is imported and refactoring would be simpler if you want to move your impl in another project.

我会选择你的第二个选项(虽然没有哪个更好),因为如果你想在另一个项目中移动你的 impl,你可以立即在你的导入中看到是否导入了 impl 并且重构会更简单。

This is not over-engineering. There are multiple advantages to use DAO:

这不是过度设计。使用 DAO 有多种优势:

  1. It improves the quality of your code by decoupling database access from other considerations
  2. Testing your code is made easier and you can test it with a finer grain.
  3. If some day, you find out that Hibernate is actually a lot easier for you, it won't impact the rest of your code.
  1. 它通过将数据库访问与其他考虑因素分离来提高代码质量
  2. 测试你的代码变得更容易,你可以用更细粒度的方式测试它。
  3. 如果有一天,您发现 Hibernate 实际上对您来说要容易得多,它不会影响您的其余代码。

回答by Gordon Hopper

One purpose of packages is to improve readability for the other programmers on your team. Generally, I put the implementation in the same package as the interface, because that is the obvious place to look, and in most cases, the DAO implementation is simple. If your implementation is complex, then you should find a framework that is appropriate for your application.

包的一个目的是提高团队中其他程序员的可读性。通常,我将实现与接口放在同一个包中,因为那是显而易见的地方,而且在大多数情况下,DAO 实现很简单。如果您的实现很复杂,那么您应该找到适合您的应用程序的框架。

Other reasons to consider a separate package include: if you are writing a library that will be used by other groups, or if you want to support multiple implementations.

考虑单独包的其他原因包括:如果您正在编写一个将由其他组使用的库,或者您想要支持多个实现。

回答by nsfyn55

Namespaces and packages only exist to prevent collisions. Neither is preferable as long as they are unique.

命名空间和包的存在只是为了防止冲突。只要它们是唯一的,两者都不是优选的。