I'm designing the db functionality of my app and I will implement repository pattern. So far I have this interface:
public interface IRepository<T> {
T get(long id);
long insert(T entity);
void update(T entity);
void delete(long id);
}
Currently, I have this class to represent an entity:
public class User {
public static final String Table = "users";
public static final String ColumnId = "user_id";
public static final String ColumnName = "name";
public long id;
public String name;
}
And finally the user repository:
public class UserRepository implements IRepository<User> {
private DatabaseAdapter dbAdapter;
public UserRepository(Context context) {
dbAdapter = new DatabaseAdapter(context);
}
public User get(long id) {}
public long insert(User entity) {}
public void update(User entity) {}
public void delete(long id) {}
}
This Android learning article explains using a Contract
class which also has an inner abstract class which purpose is similar to my User
class above.
What is the best practice to implement repository? Furthermore, is it possible to separate the column names from the User
class so that developers won't see the column names by doing User.ColumnId
?
Also, how is it possible to implement an abstract generic class that implements IRepository<T>
so that it will be the class that manages the db, instead of implementing CRUD functionality in even repository class?
Aucun commentaire:
Enregistrer un commentaire