简体   繁体   中英

DAOManager - should object validation be done here or in the specific DAO classes?

I have a setup of classes with a DAOManager and seperate DAO classes for the tables which I need to interact with. When I want to insert or delete an object from the DB, it's necessary to validate the objects. Should I validate the objects in the corresponding methods in the DAOManager class or in the specific DAO classes? It's of course also possible to validate the objects before the DAOManager is ever called. Which option is best?

public class DAOManager {
  public void insertIntoTable(Object obj) {
    //Validate here? 
    exampleDAO.insert(Object obj);
  }
}

public class ExampleDAO() {
  public void insert(Object obj) {
    //Or validate here?

    //Insert logic... 
  }
}

I would choice the option to create an abstract class "ObjectValidation" with a method validateOnInsert(), validateOnDelete(),...

The all yours single DAO's should implement that class and add the particular logic to the validation, if you just dont need a validation for your DAO return true and continue the Manager process.

public class ExampleDAO() extends DAOValidation{
    public void insert(Object obj) {
         if(validationOnInsert(obj)){...}
    }
    public boolean validateOnInsert(Object obj) {
         //your validation here
    }
    public boolean validateOnDelete(Object obj) {
         //your validation here
    }
}

Probably different people have different approach to solve it, but i think the DAO is the class who should know about all business logic of the class itself, the Manager should act just as a Distributor who ask actions to the DAOs

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