Ok this is a bit more theoretical question.
I have PlayerRepository
. This is a class that is used to make actions on my SQLite database. I've implemented there actions like select
, insert
, update
etc.
public PlayerRepository(Context context) {
super(context, com.fixus.portals.model.Player.class);
open();
}
super
in constructor is cause PlayerRepository extends Repository
which is also my class. The most important part of Repository
is this one
public class Repository<T> {
protected static SQLiteDatabase db = null;
protected static MainHelper helper = null;
protected Context context;
private Class<T> type;
public Repository(Context context, Class<T> classz) {
this.type = classz;
this.context = context;
if(helper == null) {
helper = new MainHelper(context.getApplicationContext());
}
}
public static void open() {
if(db == null) {
db = helper.getWritableDatabase();
}
}
}
As you can see when I create the repository I'm opening DB if it wasn't open before. To do that I need to pass the Context
of the application/activity. That is not a problem.
BUT sometimes I want to use my repository out side of an activity. In some kind of tool class that need to get data. So I have two ways that I can think about
-
I get the data in activity and pass it to my tool class/method so I don't need to use repository in it. This is not very flexible
-
I need to pass context to my tool class/method. But that means that every kind of operation need to receive a context and I'm not sure this is a good way
Am I missing something ? is there any better way to handle it ?
Aucun commentaire:
Enregistrer un commentaire