Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 ?

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.