1

In my Android app I have:

  1. A SQLiteHelper class that extends SQLIteOpenHelper, and takes care of things like table-creation and upgrades.

  2. A SQLiteDatasource class that performs CRUD operations on the SQLiteHelper 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.

  • 1
    Have you thought about using an ORM instead? – Robert Harvey Mar 29 '16 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 Mar 29 '16 at 22:15
  • Have you read GOF and Fowler? – Robert Harvey Mar 29 '16 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 Mar 29 '16 at 22:16
  • 1
    Fowler. – Robert Harvey Mar 29 '16 at 22:17
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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.