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.

Tulains Córdova
39.6k13 gold badges102 silver badges157 bronze badges
asked Mar 29, 2016 at 18:16
9
  • 1
    Have you thought about using an ORM instead? Commented 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) Commented Mar 29, 2016 at 22:15
  • Have you read GOF and Fowler? Commented 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. Commented Mar 29, 2016 at 22:16
  • 1
    Fowler. Commented Mar 29, 2016 at 22:17

1 Answer 1

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
}
answered Apr 3, 2016 at 12:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.