0

我和我的团队正在将java EE应用程序构建为一个学校项目,我们决定使用hibernate. 我们也想让整个项目尽可能的干净整洁,所以我们试图遵循推荐的约定。尽管如此,我还是不知道hibernate文件的约定是什么。IE 我有一个文件夹,/cz/fit/cvut/nameofmyproject/里面有包controllers,,,。在I've got中,在我想为我的实体创建模型,在Ive got for 中。现在我的问题:modelsutilscontrollersSpring controllersmodelsutilsSessionFactoryhibernate

我该如何命名model包中的类?应该是MyEntityNameDTO,还是我误解了的意思,DTO我应该只是命名它们MyEntityNameModel?我的DAO课程文件夹的正确名称应该是什么?这个简单的划分对于中等规模的项目(最多 20 个类/文件夹)是否足够,还是会太混乱?感谢 praxis 的任何提示:)

4

2 回答 2

3

DTO stands for Data Transfer Object. A DTO is a class which is more a data structure than a real class, usually, and which is created to transfer information from one layer to another, often across the network. It's not a model entity.

A DTO is often used

  • when serializing real model objects is not paractical (because the structure doesn't fit, or because the receiver doesn't have access to Hibernate classes, or because lazy-loaded entities are a problem)
  • when you want to transfer information that is an aggregation, or a complex view, over your model objects (like data of a statistical report for example)

So naming your entities DTO is not a good idea. DTOs and entities are different things. The Model suffix is also cumbersome. Entities are usually named after what they represent: Customer, Company, Player, Order, etc.

Segregating classes based on their technical role is an often used solution. But it tends not to scale when the application grows. I usually have a first level of segregation based on a functional aspect (like customer management, order management, security, etc.), and then a second level based on technical aspects (service, dao, model, etc.)

于 2013-05-03T12:04:04.280 回答
2

UserDAO- 界面

UserDAOImpl- 实现 UserDAO

这通常是我使用的。有时,如果您正在创建一个您希望其他人实现但您提供参考实现的接口,则像 Def​​aultUserDAO 这样的 Default 前缀可能更有意义。

大多数时候我觉得这两个可以互换使用,但在某些情况下,一个比另一个更清晰。

我见过两种约定:

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

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

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

Hibernate 提供了 Naming Strategy 接口,由实现来实现。

我在这里列出了几种方法。

  1. String classToTableName(String className) – 应该返回实体类的表名。
  2. String columnName(String columnName) – 用于更改映射文档中指定的列名的句柄。
  3. String tableName(String tableName) – 用于更改映射文档中指定的列名的句柄。
  4. String propertyToColumnName(String propertyName) – 将属性名称映射到列名称的句柄。
于 2013-05-03T11:47:32.017 回答