这是一个相当加载的问题,但在短,我已经在过去做了它的方法是使用一个共享的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当您绑定到已经实现了自动增量的现有系统时,这不是一个坏主意。 –