简体   繁体   English

编写数据访问对象(DAO)的最佳方法是什么?

[英]What is the best approach to write a data access object (DAO)?

I was trying to write a user authentication system in Java. 我试图用Java编写用户身份验证系统。 So I wrote some DAO class. 所以我写了一些DAO课程。 First I did write a class named Persistence which is abstract. 首先,我写了一个名为Persistence的类,它是抽象的。 It is responsible for holding some common attributes. 它负责保留一些共同的属性。 And wrote a class named User extending Persistence class. 并编写了一个名为User extends Persistence class的类。 Those classes are – 这些课程是 -

  public abstract class Persistance {

     private Date createdDate;
     private Date lastUpdatedDate;
     private long version;
     private boolean  isDeleted;


    //getter and setters
 }

and the user class 和用户类

 public class User extends  Persistance{
   private String username;
   private String password;
   private String passwordConfired;

  // getters and setters

 }

My questions are- what is the best way to write variable name, which one is good, createdDate or dateCreated, deleted or isDeleted etc. 我的问题是 - 编写变量名称的最佳方法是什么,哪一个是好的,创建日期或日期处理,删除或删除等。

And is this approach is okay or is there more good approach ? 这种方法还可以,还是有更好的方法? And how to implement data versioning? 以及如何实现数据版本控制?

To write a DAO, typically you create an interface that defines the behavior of the DAO. 要编写DAO,通常可以创建一个定义DAO行为的接口。

interface MyObjDao {

    public int save(MyObj myObj);

    public void delete (MyObj myObj);

    // as many methods as you need for data acess

}

and then you create the actual implementation 然后你创建实际的实现

class MyObjDaoImpl implements MyObjDao {
    // implement methods here

}

The advantages of this are: 这样做的好处是:

1) Because you define an interface, mocking DAOs is easy for any testing framework 2) The behavior is not tied to an implementation -- your DAOImpl could use jdbc, hibernate, whatever 1)因为你定义了一个接口,所以任何测试框架都很容易模拟DAO 2)行为不依赖于实现 - 你的DAOImpl可以使用jdbc,hibernate,等等

Your Persistance class is really a base class for all entities -- ie all classes instances of which get saved, where you want to represent some common fields in one place. 你的Persistance类实际上是所有实体的基类 - 即所有类实例都被保存,你想在一个地方代表一些公共字段。 This is a good practice -- I wouldn't call the class Persistance , something like BaseEntity is better (IMHO). 这是一个很好的做法 - 我不会称之为Persistance ,类似于BaseEntity更好(恕我直言)。 Be sure to have javadocs that explain the purpose of the class. 一定要有javadoc来解释类的目的。

With respect to variable names, as long as they make sense and describe what they are for, its good. 关于变量名称,只要它们有意义并描述它们的用途,它就是好的。

so dateCreated or createdDate are both fine; 所以dateCreatedcreatedDate都很好; they both get the idea across. 他们都得到了这个想法。

You are mixing a DAO (data access object) and a VO (value object) - also known as a DTO (data transfer object) - in the same class. 您正在混合DAO(数据访问对象)和VO(值对象) - 也称为DTO(数据传输对象) - 在同一个类中。

Example using an interface for DAO behavior (blammy and kpow might be webservice, oracle database, mysql database, hibernate, or anything meaningful): 使用DAO行为接口的示例(blammy和kpow可能是webservice,oracle数据库,mysql数据库,hibernate或任何有意义的东西):

public interface UserDTO
{
    boolean deleteUser(String userId);
    UserVO readUser(String userId);
    void updateUser(String userId, UserVO newValues);
}

package blah.blammy;
public class UserDTOImpl implements UserDTO
{
  ... implement it based on blammy.
}

package blah.kpow;
public class UserDTOImpl implements UserDTO
{
  ... implement it based on kpow.
}

Example VO: 示例VO:


public class UserVO
{
    String firstName;
    String lastName;
    String middleInitial;

    ... getters and setters.
}

I prefer to identify the target of the delete using an ID instead of a VO object. 我更喜欢使用ID而不是VO对象来识别删除的目标。 Also, it is possible that an update will change the target identified by user ID "smackdown" to have user ID "smackup", so I generally pass an id and a VO. 此外,更新可能会将用户ID“smackdown”标识的目标更改为具有用户ID“smackup”,因此我通常会传递id和VO。

A good approach would be to use JPA with all of its features, this tutorial was really helpful. 一个好的方法是使用JPA及其所有功能,本教程非常有用。 It explains how to use the @PrePersist and @PreUpdate annotations for setting create and update timestamps. 它解释了如何使用@PrePersist和@PreUpdate注释来设置创建和更新时间戳。 Optimistic locking is supported by the @Version annotation. @Version注释支持乐观锁定。

My questions are- what is the best way to write variable name, which one is good, createdDate or dateCreated, deleted or isDeleted etc. 我的问题是 - 编写变量名称的最佳方法是什么,哪一个是好的,创建日期或日期处理,删除或删除等。

createdDate or dateCreated is very subjective. createdDate或dateCreated非常主观。 In databases, I have mostly seen createdDate though. 在数据库中,我主要看过createdDate。 Between deleted and isDeleted, I prefer (again subjective) deleted. 在删除和isDeleted之间,我更喜欢(再次主观)删除。 I think the getter method can be named isDeleted(). 我认为getter方法可以命名为isDeleted()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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