简体   繁体   中英

DAO class methods naming

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

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

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:

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

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.

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

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

Names according to function

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

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

set* (eg setActive) UPDATE Queries

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

delete* (eg deleteUser) DELETE queries

is* (eg isActive) IF check returns boolean, ie, if ($user_dao->isUserActive($id)) or if ($post_dao->isPostInStorage($id))

count* (eg countUsers) Returns integer with the item count.

Reserved functions

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

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

poke – Takes an ID as argument, “pokes” the record (sets “last seen” or whatever to now), returns update count (usually 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.

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 Methods this: Java Naming Conventions

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:

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 (ie voice(Invoice invoice))

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:

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 void method (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 update if you are not removing the invoice from the database after you void it. update can be applied to any changes you made for your invoice entry.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM