简体   繁体   中英

Android Database, CRUD operations for multiple tables in MVC

A newbie to Android and trying to save and view some info into/from a sql lite database. My tables are as follows:

Student    Course     Registration
-------    ------     ------------
id (PK)    id(PK)     sid, cid (PK)         
name       name       term
email      credit

As far as I understand, the Model part of MVC will store all the business logic, thus my 'Student', 'Course' and 'Registration' classes will have all the get/set methods. I have a DBHelper class that extends SQLiteOpenHelper and as I saw in many tutorial, this class contains all the CRUD operations in general.

But my question is, as I have multiple tables to work with, where should I place the CRUD operations like addStudent(), addCourse(), addRegistration(). I suppose it would not be feasible to have a very large DBHelper class containing all the CRUD methods in the universe. In some examples, I have seen view operations(viewStudent() for example) in MainActivity as well. Is it a good way to do it? An example (with Classes and and their methods) will be highly appreciated.

You CRUD operations should be in a Repository or DAO layer.

studentRepository.save(student);

Your business logic should be in a Service layer, or in your Domain models, if using Domain-Driven Design. The service layer uses the repository layer to save and retrieve data.

studentService.registerStudentInCourse(student, course);

The Models in MVC can represent non-domain models too, such as a LoginForm. They are slightly different than domain models in that they represent data for a View. An MVC model can be a domain model, but doesn't have to be. An MVC model can combine multiple domain models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM