HibernateUtil 类的通用名称是什么?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要让它变得更通用,除非这个类除了与 Hibernate 对话之外真的可以做任何事情。它在其公共 API 中使用 Hibernate 类和接口,对吧?只要它是 Hibernate 特定的(在接口和实现方面),最好通过其名称让每个人都了解它。
HibernateUtil 听起来不错。
好吧,一旦摆脱了
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.Well, you can change the name to
JpaUtil
once you got rid ofgetSession
.您的类是 Hibernate 特定的,因此最好将其包含在其名称中。
它会创建会话,还是从其他地方获取会话?如果它创造了它们,Factory 也是一个好名字。如果没有,听起来它管理会话,那么“HibernateSessionManager”怎么样?
你的名字“HibernateUtil”太笼统了。我总是将“util”包和类视为代码味道;它们通常会突然出现,因为设计师有一堆他/她不知道该放在哪里的东西。你的课程不是这样的,它显然专注于休眠会话的管理。
至于你关于命名的现有约定或共识的问题。有一些准则(但您可能已经知道它们):
哦,是的,我碰巧认为命名对于良好的软件设计非常重要,但我想这已经很清楚了(-:
编辑:响应您的编辑。只要它返回 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):
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.