Android (distributed application) primary key strategy

后端 未结 5 874
刺人心
刺人心 2020-12-24 08:34

I am going to implement a distributed application with multiple mobile clients and a web based server application. So each client and also the server are allowed to generate

5条回答
  •  醉梦人生
    2020-12-24 09:27

    I offered two bounties on this question and didn't find the answer I am looking for. But I spent some time on thinking about the best solution and maybe the question was not open enough and focused to much on the solution I had in mind.

    However there are a lot of different strategies available, now (after the second bounty) I think the first question to answer is which data model(s) do you have in your distributed environment? You might have

    1. the same (or a subset) data model on client and server
    2. differnet client data model and server data model

    If you answer with 1) then you can choose for your key strategy from

    • using GUID
    • using my approach High/Low
    • mapping keys as @user3603546 suggested

    If you answer with 2) then only the following comes in my mind

    • composite id

    I never liked composite id's, but when I think about it (and don't call it composite id's anyway) then it could be a possible solution. Following I want to sketch this solution:

    Glossary:

    • ... primary key generated at the client side, so the client chooses the implementation (long _id for Android)
    • ... primary key generated at the server side, so the server chooses the implementation
    • ... ID for identifying the client
    • ... ID for identifying the device, there is a 1-n relation between client and device

    Solution:

    • Use it only if you have a client data model and a server data model
    • The client data model has the fields
      • primary key
      • nullable data field
    • The server data model has the fields
      • as primary key
      • nullable data field
      • as mandatory data field to distinguish the client
    • When synchronizing from server to client, generate missing on the client and mark entry as dirty (so that the client id comes to the server at the end of the day)
    • When synchronizing from client to server, generate missing on the server before saving it
    • The mapping between client and server data model can be handled by specialised frameworks like dozer or Orika, however the key generation must be integrated when performing the mapping.

    I never liked this solution because I always thought in server data model terms. I have entities which live only on the server and I always wanted to create these entities on the client which would not be possible. But when I think in client data model I might have one entity eg. Product which results in two entities (Product and a ClientProduct) on the server.

提交回复
热议问题