开发者

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

I was trying to write a user authentication system in Java. So I wrote some DAO class. First I did write a class named Persistence which is abstract. It is responsible for holding some common attributes. And wrote a class named User e开发者_开发百科xtending 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.

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

Your Persistance class is really a base class for all entities -- i.e. all classes instances of which get saved, where you want to represent some common fields in one place. This is a good practice -- I wouldn't call the class Persistance, something like BaseEntity is better (IMHO). Be sure to have javadocs that explain the purpose of the class.

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; 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.

Example using an interface for DAO behavior (blammy and kpow might be webservice, oracle database, mysql database, hibernate, or anything meaningful):

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:


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. 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.


A good approach would be to use JPA with all of its features, this tutorial was really helpful. It explains how to use the @PrePersist and @PreUpdate annotations for setting create and update timestamps. Optimistic locking is supported by the @Version annotation.


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. In databases, I have mostly seen createdDate though. Between deleted and isDeleted, I prefer (again subjective) deleted. I think the getter method can be named isDeleted().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜