HibernateUtil 类的通用名称是什么?

发布于 2024-09-17 08:35:10 字数 643 浏览 8 评论 0原文

我有一个 HibernateUtil 类,它是本书 (Hibernate in action) 类的标准。
它获取一个会话,刷新并关闭它。开始、提交和回滚事务并构建 sessionFactory。
目前,我们的包中这被错误地称为 HibernateSessionFactory。
我想将其重构为更合适的名称,但 HibernateUtil 这个名称让我烦恼,因为我想选择一个更通用的名称,例如 PersistentManagerUtil
关于这样的名字有一个好的java约定或共识吗?

编辑 我想让它更通用的原因是我想最大限度地减少对特定 ORM 实现的“暴露”。该类的大多数公共方法都将 void 作为返回类型,因此它们不返回 Hibernate 类/接口。唯一的例外是 getSession,它返回 org.hibernate.Session。我之所以考虑这个方向是因为我想切换到JPA并使用hibernate作为实现,因此有更多的自由度。 为什么不将其更改为 EntityManagerUtil?正如 hibernate 的文档中所说,EntityManager 相当于 Session,EntityManagerFactory 相当于 HibernateSessionFactory。

预先感谢,
一泰

I have a HibernateUtil class which is the standard by the book (Hibernate in action) class.
It gets a session, flushes and closes it. begins,commits and rollbacks transactions and build the sessionFactory.
Currently, by mistake, this is called HibernateSessionFactory in our package.
I want to refactor it to a more appropriate name but the name HibernateUtil bothers me as I'd like to choose a more general name something like PersistentManagerUtil.
Is there a good java convention or consensus about such a name?

EDIT
The reason I want to make it more general is that I want to minimilize the "Exposure" to the specific ORM implementation. Most of the public methods of the class have void as a return type so they do not return Hibernate classes/interfaces. The only exception to this is the getSession which returns an org.hibernate.Session. The reason I was thinking this direction is because I want to switch over to JPA and have hibernate as the implementation and so have a bit more freedom.
Why not change it to EntityManagerUtil? As in hibernate's docs they say EntityManager is equivelant to Session and that EntityManagerFactory is equivelant to HibernateSessionFactory.

Thanks in advance,
Ittai

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

青衫儰鉨ミ守葔 2024-09-24 08:35:10

不要让它变得更通用,除非这个类除了与 Hibernate 对话之外真的可以做任何事情。它在其公共 API 中使用 Hibernate 类和接口,对吧?只要它是 Hibernate 特定的(在接口和实现方面),最好通过其名称让每个人都了解它。

HibernateUtil 听起来不错。

该类的大多数公共方法都将 void 作为返回类型,因此它们不返回 Hibernate 类/接口。唯一的例外是 getSession,它返回 org.hibernate.Session。我之所以考虑这个方向是因为我想切换到JPA并使用hibernate作为实现,因此有更多的自由度。

好吧,一旦摆脱了 getSession,您就可以将名称更改为 JpaUtil

Don't make it more general, unless the class can really do anything other than talk to Hibernate. It is using Hibernate classes and interfaces in its public API, right? As long as it is Hibernate-specific (in both interface and implementation), it would be good to let everyone know about it in its name.

HibernateUtil sounds good.

Most of the public methods of the class have void as a return type so they do not return Hibernate classes/interfaces. The only exception to this is the getSession which returns an org.hibernate.Session. The reason I was thinking this direction is because I want to switch over to JPA and have hibernate as the implementation and so have a bit more freedom.

Well, you can change the name to JpaUtil once you got rid of getSession.

感悟人生的甜 2024-09-24 08:35:10

您的类是 Hibernate 特定的,因此最好将其包含在其名称中。

它会创建会话,还是从其他地方获取会话?如果它创造了它们,Factory 也是一个好名字。如果没有,听起来它管理会话,那么“HibernateSessionManager”怎么样?

你的名字“HibernateUtil”太笼统了。我总是将“util”包和类视为代码味道;它们通常会突然出现,因为设计师有一堆他/她不知道该放在哪里的东西。你的课程不是这样的,它显然专注于休眠会话的管理。

至于你关于命名的现有约定或共识的问题。有一些准则(但您可能已经知道它们):

  • 方法应该反映它们所在的类上的操作,并且如果可能的话具有逻辑参数顺序。示例:ChessBoard.swapPlayers(Player a, Player b)
  • 对于返回布尔值的方法,使用 isActivated 或 hasPlayers 等谓词
  • 另一种风格是方法链接,其中链接的方法读起来有点像英文句子:verify(mockedList, atLeastOnce()) .add("三次"); (mockito)
  • 有关方法的 JavaDoc 应该描述动作,例如:交换玩家 A 和 B,以便他们用彼此的棋子继续游戏”。
  • 类应该代表一个具有明确责任的“事物”。示例:StringBuilder、Customer、PdfServlet。如果次要责任蔓延到一个类中,这通常意味着它应该被重构为两个协作类。
  • 类上的 JavaDoc 应该描述“事物”,并且应该描述它的主要职责,因为它通常不应该描述对客户端有价值的信息。在公共 JavaDoc 中。

哦,是的,我碰巧认为命名对于良好的软件设计非常重要,但我想这已经很清楚了(-:

编辑:响应您的编辑。只要它返回 Hibernate 类型,它就可以了。仍然是 Hibernate 特定的。如果您设法完全从 Hibernate 抽象到 JPA,那么将其命名为 JpaSessionManager(如果您愿意,也可以称为 JPASessionManager)是有意义的。但这需要返回仅 JPA 的会话对象。

Your class is Hibernate specific, so it's good to have that in its name.

Does it create Sessions, or get them from elsewhere? If it creates them, Factory is also a good name. If not, it sounds like it manages Sessions so how about 'HibernateSessionManager'?

Your name 'HibernateUtil' is if anything too general. I always take 'util' packages and classes as a code smell; they usually pop up because the designer had a bunch of stuff he/she did not know where to put. Your class is not like that, it apparently focuses on the management of hibernate sessions.

As to your question about existing conventions or consensus about naming. There are some guidelines (but you probably already know them):

  • methods should reflect an action on the class they reside in and if possible have a logical argument order. Example: ChessBoard.swapPlayers(Player a, Player b)
  • for methods returning a boolean use a predicate like isActivated or hasPlayers
  • another style is method chaining, where the chained methods read a bit like an english sentence: verify(mockedList, atLeastOnce()).add("three times"); (mockito)
  • The JavaDoc on methods should describe the action, for example: swaps players A and B so that they continue the game with each others' pieces".
  • classes should represent a 'thing' with a clear responsibility. Examples: StringBuilder, Customer, PdfServlet. If a secondary responsibility creeps into a class, this usually means it should be refactored into two collaborating classes.
  • JavaDoc on classes should describe the 'thing' and its main responsibility. Behavior should be described insofar it is valuable information for the client. Implementation details should usually not be described in the public JavaDoc.

Oh yes, and I happen to think naming is extremely important for good software design. But I guess that was already clear (-:

EDIT: in response to your edit. As long as it returns a Hibernate type, it is still Hibernate specific. If you manage to totally abstract away from Hibernate, towards JPA, then it would make sense to name it JpaSessionManager (or JPASessionManager if you prefer) for example. But this would require to return a JPA-only session object.

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