In my Android app I have:
A
SQLiteHelper
class that extendsSQLIteOpenHelper
, and takes care of things like table-creation and upgrades.A
SQLiteDatasource
class that performs CRUD operations on theSQLiteHelper
object.
However my SQLiteDatasource
class is getting very bloated because it have so many methods. Lots of ways to apply CRUD to various types of objects and ways to view those objects, etc.
Is there a way to organize/refactor this better? Or do most people just organize them in some logical order and then explain the separations with comment lines? e.g. "Open/close methods", "ObjectA query methods", etc.
-
1Have you thought about using an ORM instead?Robert Harvey– Robert Harvey2016年03月29日 20:52:01 +00:00Commented Mar 29, 2016 at 20:52
-
@RobertHarvey I'm mostly interested in what's considered good OOP/design practice for this situation (as opposed to using a third party tool that takes care of it for me)AJJ– AJJ2016年03月29日 22:15:07 +00:00Commented Mar 29, 2016 at 22:15
-
Have you read GOF and Fowler?Robert Harvey– Robert Harvey2016年03月29日 22:16:23 +00:00Commented Mar 29, 2016 at 22:16
-
@RobertHarvey Is GOF = Gang of Four = Design Patterns? If so, I have the book, but have not read it yet. Fowler I don't know what that is.AJJ– AJJ2016年03月29日 22:16:56 +00:00Commented Mar 29, 2016 at 22:16
-
1Fowler.Robert Harvey– Robert Harvey2016年03月29日 22:17:55 +00:00Commented Mar 29, 2016 at 22:17
1 Answer 1
Use repository pattern. Create a repository class for each entity class in your app and put methods to manage that kind of entity there. That way you have clean separation and can take use of abstraction. I usually do something like this:
public abstract class BaseRepository<T> {
public T getById(long id){
// query on SQLite
}
// etc. more common methods here
}
And repo for entity:
public class UsersRepository extends BaseRepository<UserEntity> {
public long countAllAdministrativeUsers() {
// query here
}
// othet user-specific database actions
}
Explore related questions
See similar questions with these tags.