在另一个中调用dao用于sqlite事务

时间:2014-08-07 14:25:45

标签: android sqlite dao

我有2个dao,我需要在它们之间调用方法

我做了类似这样的事情,但它真的很糟糕,因为它做了无限循环而且它不尊重质量标准 请问这样做的正确方法是什么? 非常感谢你

DAO 1

public class InfractionDAO {


  private SQLiteDatabase database;
  private SqLiteManager dbHelper;
  private OffenderDAO offenderDAO;


  public InfractionDAO(Context context) {
    dbHelper = new SqLiteManager(context);
    offenderDAO= new OffenderDAO (context);
  }

  List<Infractions> getInfractions(int id) {
     offenderDao.getOffender(id);
  }
}

DAO 2

public class OffenderDAO {


  private SQLiteDatabase database;
  private SqLiteManager dbHelper;
  private InfractionDAO infractionDAO;

  public OffenderDAO (Context context) {
    dbHelper = new SqLiteManager(context);
    infractionDAO = new InfractionDAO(context);
  }

 Offender getOffender(int id) {
  infractionDAO.getInfractions(id);
}
}

1 个答案:

答案 0 :(得分:1)

我会做的是以下几点:
解决方案1.
为两者创建一个基类(也许是抽象?),在那里他们共享共同的东西,在每个单独的类中,offernder和infraction把不同的东西放在一起。
解决方案2.
我会创建一个单例类,在构造函数中初始化它们。 比如下面的linke:
http://pastebin.com/84g9SgFT