常见的数据库/DAO方法是否应该是静态的



我有一种方法,该方法在我的代码中从各个位置使用了3-4次。通常,人们会为此引入静态实用方法。

但是我想知道这是否是一个很好的做法,如果此静态方法执行某些数据库/DAO逻辑?以以下方式为伪代码示例:

class DaoUtiliy {
    public static void updateToDB(List a, List b) {
        Dao dao = new Dao();
        dao.open(); //begin transaction     
        //create some variables using the list a, that are used to fetch the db entry:
        String time, String date; //some more
        Entity entity = dao.find(time, date);
        //update anything in that entity;
        entity.setProperty(b.get(0));
        dao.close(); //commit transaction
    }   
}

您是否也将其放在静态方法中?或者是创建一个new DaoService().updateToDB(a, b); ANC从需要的地方调用此方法?

我更喜欢dao中的非静态方法

  1. 使其非静态使使用诸如Spring之类的框架
  2. 的框架更容易注入值
  3. 编写单元测试用例对于静态方法并不容易,因为它们不能被覆盖
  4. 也不可能扩展静态方法的DAO覆盖的功能。

最新更新