這是一個相當加載的問題,但在短,我已經在過去做了它的方法是使用一個共享的IService
與所有的邏輯在共享庫。
此庫位於移動應用程序和API中,並負責處理同步。該API將具體實現(到您的回購),移動應用程序將具有具體實現(對Sqlite),並且還有一個代理實現HttpClient
。
至於回購,你就會有一個針對移動和一個用於服務器,並再次共享相同的IRepo
注:使用GUID的所有標識,從來沒有自動遞增,否則你會得到碰撞
例如
// everything in here lives in a shared library that the server and mobile have access to.
public interface IUserRepository {
// shared methods to talk to data
}
public interface IUserService {
UserDto GetUserById(Guid userId);
IList<UserDto> FindUsers(int skip, int take);
void DeleteUserById(Guid userId);
// etc
}
public class UserService : IUserService {
public UserService (IUserRepository userRepo) {
_userRepo = userRepo;
}
}
public class UserProxyService : IUserService {
public UserProxyService (HttpClient httpClient) {
_httpClient = httpClient; // don't dispose... make singleton
}
}
// server implementation of the repo
public namespace MyApp.Api.Data {
public class UserRepository : IUserRepository {
// sql server calls (EF maybe)
}
}
// mobile implementation of the repo
public namespace MyApp.Mobile.Data { /* (portable) */
public class UserRepository : IUserRepository {
// sqlite calls on the phone
}
}
至於處理同步。我的建議是在可能的情況下立即推送您的更改,如果以前的嘗試因網絡問題而失敗,則可能爲OnResume
。我建議立即做這件事的原因是因爲用戶會希望隨處看到這些數據。
Akavache也很棒這些種情景模式 –
一定要同意,並強調使用方法:「不要使用自動遞增」。在過去,當我使用基於整數的主鍵系統進行集成時,我已經將本地主鍵設置爲負值,直到它們已經同步到服務器,並且工作得很好。 –
@ deckertron_9000當您綁定到已經實現了自動增量的現有系統時,這不是一個壞主意。 –