Creating a data model class for database processing

I was just starting to work on a database application when I realized that I had to implement the MVC , because the application is quite complex and includes several database operations.
In this regard, I decided to create a separate model class for processing database operations. This class will have all the methods that will return data to me after executing the Sqlite command (select for example) OR just execute the SQLite command (for example, Delete). But I want to separate this class from the Database Adapter class, where I open, create and close my database.
Let me put my concept in code:

 public class DataModel { /* Private members */ // Method to Select data from Student table public ArrayList<String> FetchStudents (parameter 1) { private ArrayList<String> arrStudent; DatabaseAdapter objDB= new DatabaseAdapter(); objDB.open(); /* Some code */ objDB.close(); return arrStudent } //Method to delete record from Student table public DeleteStudent(parameter 1) { DatabaseAdapter objDB= new DatabaseAdapter(); objDB.open(); //Some code objDB.close(); } /* Rest of methods */ } 

// DatabaseAdapterClass

 private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } /** * onCreate method is called for the 1st time when database doesn't exists. */ @Override public void onCreate(SQLiteDatabase db) { Log.i(TAG, "Creating DataBase: " + CREATE_STUDENT_TABLE); db.execSQL(CREATE_STUDENT_TABLE); } /** * onUpgrade method is called when database version changes. */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion); } } 

Question :
What I want to ask is the right approach to implementation? Is this fine if you create a separate class for the database methods? What are the limitations or problems that you think may bother me later? Also, is there a better way to implement the above concept?

thanks
A rock

+7
source share
3 answers

What you call a “model class” is more commonly known as a data access object (DAO). Your model is usually a collection of classes that store your data and business logic. In your case, it is likely that the Student class has the properties ID, name, GPA, etc. If you want to share data access with your model, you will have access to your data access class ( DatabaseHelper ) in the database and use the data to return Student or List<Student> objects. There is very little point in separating the data access class from the database assistant; it is best to use all the code associated with the database in one place.

Using model classes (only), however, may not always be practical on Android, as it has built-in support for retrieving and displaying data from Cursor ( CursorAdapter , etc.). If you want to use any of this, you will have to expose your data not to model objects, but like Cursor . As for content providers, look at them too, but if you don't need to expose your data to other applications, the ContentProvider entry may be excessive.

In another note, you do not want to open and close the database for each request. It is actually safe to leave it open; it automatically closes when your application process dies.

+11
source

I do this in my application, and it works fine, the code is clean, and it does not affect performance at all, especially with hardware phones today. I tried all other approaches and even used a content provider, but, in my opinion, it was just over complicated things.

+1
source

Android modeling approach data is content providers. Link

it's kind of like an abstract type of data source.

I did it the same way. but again its also subjective.

0
source

All Articles