Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing DAO Pattern on Android project

I'm developing an Android 3.1 and above.

I have the following packages:

enter image description here

es.viacognita.models contain classes to store data retrieved with a web service. When I get all web service data, I need to insert it on database.

To make it right, I've thought to use DAO pattern, but I don't know how to do it.

If I use DAO pattern, may I need to use es.viacognita.models classes? I think that these classes are going to be DAO classes, isn't it?

Where I have to implement inserts, updates, or deletes? on DBManager class?

like image 603
VansFannel Avatar asked Apr 15 '12 11:04

VansFannel


People also ask

What is the DAO in Android?

Data Access Objects are the main classes where you define your database interactions. They can include a variety of query methods. The class marked with @Dao should either be an interface or an abstract class. At compile time, Room will generate an implementation of this class when it is referenced by a Database .

Why is it useful to build an application with the DAO pattern?

DAO Design Pattern is used to separate the data persistence logic in a separate layer. This way, the service remains completely in dark about how the low-level operations to access the database is done. This is known as the principle of Separation of Logic.

What is DAO implementation?

The Data Access Object (or DAO) pattern: separates a data resource's client interface from its data access mechanisms. adapts a specific data resource's access API to a generic client interface.

What is DAO in Kotlin?

A Data Access Object (DAO) defines the interface for how to perform CRUD operations on a particular entity.


2 Answers

You can implement inserts,updates,deletes and all other operations in DBManager class or create a seperate class dao which does all the operations you want to do on the database...

    public class DAO {
private SQLiteDatabase database,customdb;
private DBManager dbHelper;



public DAO(Context context) {
    dbHelper = new DBManager(context);
}
public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();

}
public void close() {
    dbHelper.close();
}
//insering,deleting and all other operations you want to perforem on the database
  }
like image 90
5hssba Avatar answered Oct 05 '22 12:10

5hssba


I would use DBManager's namespace. I treat your model classes as business entity classes and create a separate library for DAO (DAL) layer. Using this methodology you are able to separate your business logic from your Data access layer.

like image 36
Jermin Bazazian Avatar answered Oct 05 '22 13:10

Jermin Bazazian