Android From Local DB (DAO) to Server sync (JSON) - Design issue
Posted
by
Taiko
on Programmers
See other posts from Programmers
or by Taiko
Published on 2014-06-13T02:38:11Z
Indexed on
2014/06/13
3:38 UTC
Read the original article
Hit count: 272
I sync data between my local DB and a Server.
I'm looking for the cleanest way to modelise all of this.
I have a com.something.db package That contains a Data Helper and couple of DAO classes that represents objects stored in the db (I didn't write that part)
com.something.db
--public DataHelper
--public Employee
@DatabaseField e.g. "name" will be an actual column name in the DB
-name
@DatabaseField
-salary
etc... (all in all 50 fields)
I have a com.something.sync package That contains all the implementation detail on how to send data to the server. It boils down to a ConnectionManager that is fed by different classes that implements a 'Request' interface
com.something.sync
--public interface ConnectionManager
--package ConnectionManagerImpl
--public interface Request
--package LoginRequest
--package GetEmployeesRequest
My issue is, at some point in the sync process, I have to JSONise and de-JSONise my data (E.g. the Employee class).
But I really don't feel like having the same Employee class be responsible for both his JSONisation and his actual representation inside the local database. It really doesn't feel right, because I carefully decoupled the rest, I am only stuck on this JSON thing.
What should I do ?
Should I write 3 Employee classes ?
EmployeeDB
@DatabaseField e.g. "name" will be an actual column name in the DB
-name
@DatabaseField
-salary
-etc... 50 fields
EmployeeInterface
-getName
-getSalary
-etc... 50 fields
EmployeeJSON
-JSON_KEY_NAME = "name" The JSON key happens to be the same as the table name, but it isn't requirement
-name
-JSON_KEY_SALARY = "salary"
-salary
-etc... 50 fields
It feels like a lot of duplicates.
Is there a common pattern I can use there ?
© Programmers or respective owner