java DAO 类方法命名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17580783/
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-11-01 02:22:52  来源:igfitidea点击:

DAO class methods naming

javaspring-mvc

提问by MChan

I am building a small Java web application using Spring MVC, Hibernate and I am confused about the DAO classes methods naming.

我正在使用 Spring MVC、Hibernate 构建一个小型 Java Web 应用程序,我对 DAO 类方法命名感到困惑。

For example I have an InvoiceDAO.java class which I thought should contain the following methods:

例如,我有一个 InvoiceDAO.java 类,我认为它应该包含以下方法:

Save(Invoice newInvoice);
Void(Invoice oldInvoice);
getInvoiceByID(Long invoideID);

but my boss says that best practices says that I should have methods names in DAO classes as follows:

但我的老板说,最佳实践说我应该在 DAO 类中使用方法名称,如下所示:

add(Invoice newInvoice);
update(Invoice oldInvoice);

which makes no sense for me as I am not sure how I can name voiding an invoice as Update?!!

这对我来说毫无意义,因为我不确定如何将发票作废命名为更新?!!

So can someone please guide me in this and tell me if I am wrong on my methods naming? In other words is it correct that I should only use add, update for naming or can I use any naming and still be considered as best practices.

那么有人可以在这方面指导我并告诉我我的方法命名是否有误吗?换句话说,我应该只使用添加、更新来命名还是可以使用任何命名并且仍然被视为最佳实践,这是否正确。

thanks

谢谢

回答by dan carter

Voiding an invoice is a business operation. I would say such logic lives in your service layer. You make updates to the invoice to mark it as void, and then pass it to the data layer to save.

取消发票是一项业务操作。我会说这样的逻辑存在于您的服务层中。您更新发票以将其标记为无效,然后将其传递到数据层进行保存。

The data layer should contain pure CRUD type methods, that is add/save/find.

数据层应该包含纯 CRUD 类型的方法,即添加/保存/查找。

Using many modern data frameworks, you don't even need to write the data layer ... e.g. see http://blog.springsource.org/2011/02/10/getting-started-with-spring-data-jpa/

使用许多现代数据框架,您甚至不需要编写数据层……例如参见http://blog.springsource.org/2011/02/10/getting-started-with-spring-data-jpa/

回答by Cristian Meneses

I've found this refeernce some time ago about DAO naming ...

前段时间我发现了这个关于 DAO 命名的参考......

Names according to function

按功能命名

getData*Data Parsing Methods used internally in DAO, do not use this namespace for data accessing.

getData*DAO 内部使用的数据解析方法,不使用此命名空间进行数据访问。

get*(e.g. getUsersByID) SELECT queries – It is encouraged that you try to use the noun in Singular or Plural according to single or multi-row return.

get*(例如 getUsersByID) SELECT 查询 – 鼓励您根据单行或多行返回尝试在单数或复数中使用名词。

set*(e.g. setActive) UPDATE Queries

set*(例如 setActive)更新查询

add*(e.g. addUser) INSERT Queries – It is encouraged that you try to use the noun in Singular or Plural according to single or multi-row insert.

add*(例如 addUser) INSERT 查询 – 鼓励您根据单行或多行插入尝试在单数或复数中使用名词。

delete*(e.g. deleteUser) DELETE queries

delete*(例如 deleteUser) DELETE 查询

is*(e.g. isActive) IF check returns boolean, i.e., if ($user_dao->isUserActive($id)) or if ($post_dao->isPostInStorage($id))

is*(例如 isActive) IF 检查返回布尔值,即 if ($user_dao->isUserActive($id)) 或 if ($post_dao->isPostInStorage($id))

count*(e.g. countUsers) Returns integer with the item count.

count*(例如countUsers)返回带有项目计数的整数。

Reserved functions

保留功能

insert– takes an object as argument, and inserts it to the table.

insert– 将一个对象作为参数,并将其插入到表中。

save– takes an object as an argument, and stores the data in it back to data backend

save– 将一个对象作为参数,并将其中的数据存储回数据后端

poke– Takes an ID as argument, “pokes” the record (sets “last seen” or whatever to now), returns update count (usually 1)

poke– 以 ID 作为参数,“戳”记录(设置“上次看到”或任何到现在),返回更新计数(通常为 1)

Other things to remember

其他要记住的事情

As the storage Backend may or may not be a “database”, it would be encouraged not to create methods with names that imply that the backend is using a database.

由于存储后端可能是也可能不是“数据库”,因此鼓励不要创建名称暗示后端正在使用数据库的方法。

回答by Andrei Sfat

First of all, in Java, at least, you name your methods with the first letter of each internal word capitalized, camel-case. You can see at the section Methodsthis: Java Naming Conventions

首先,至少在 Java 中,您使用大写的每个内部单词的第一个字母命名您的方法,骆驼大小写。您可以在方法部分看到:Java 命名约定

Regarding the specific naming of your methods inside the dao: I would go by creating basic crud operations that can be performed to your model classes Example:

关于 dao 中方法的特定命名:我将创建可以对模型类执行的基本 crud 操作示例:

add(Invoice invoice)
update(Invoice invoice)
// or instead 
save(Invoice invoice) // which will perform either add or update
delete(Invoice invoice) // or delete(int invoiceId)
findById(int invoiceId)
// and so forth

I would not make use of the term "void" inside the dao, since that is related to the business. Do the dao as simple as possible and after that in your service that will be using the dao, you can name your methods related to the business required (i.e. voice(Invoice invoice))

我不会在 dao 中使用“void”这个词,因为这与业务有关。尽可能简单地做 dao,然后在您将使用 dao 的服务中,您可以命名与所需业务相关的方法(即语音(发票发票))

There is another possibility to create a generic dao with the basic CRUD operations and maybe you can then start naming the methods as you want:

还有另一种可能性,可以使用基本的 CRUD 操作创建通用 dao,然后您可以根据需要开始命名方法:

public class InvoiceDAO inherits GenericDao<Invoice> {
    // all the above methods would be inherited
    // add specific methods to your dao
}

Again, if I were you I would move the naming of specific stuff in the service. Now it's up to you how you want to approach from what I showed. The idea is to keep the dao as simple as possible.

同样,如果我是你,我会移动服务中特定内容的命名。现在由您决定如何从我展示的内容中进行处理。这个想法是让道尽可能简单。

You might as well go and name your voidmethod (since you can do name it void, since in Java is a keyword -- thanks @Marco Forberg for noticing that) either delete(Void - means that it is deleted.) or performVoid. Or go simple with updateif you are not removing the invoice from the database after you void it. updatecan be applied to any changes you made for your invoice entry.

您不妨去命名您的void方法(因为您可以将其命名为void,因为在 Java 中是一个关键字——感谢@Marco Forberg 注意到这一点)delete(Void - 意味着它已被删除。)或performVoid。或者,如果您在取消发票后没有从数据库中删除发票,请使用更新进行简单处理。更新可以应用于您对发票条目所做的任何更改。

回答by Evoke

Save and add have 2 different meanings. As do Void and update. Use the term that accurately describes what the method is doing. Im not aware of any specific best practise here.

保存和添加有两种不同的含义。和 Void 和更新一样。使用准确描述该方法正在做什么的术语。我不知道这里有任何具体的最佳实践。

Also, I would tend to only pass an ID into a void method if that is enough to perform the action. This is different scenario from an update where you may expect to update multiple attributes on the invoice.

此外,如果足以执行操作,我倾向于只将 ID 传递给 void 方法。这与您可能希望更新发票上的多个属性的更新不同。