'Design pattern for checking two modes (online/offline) in multiple places
I'm working on a Xamarin application. Currently the app works in Offline mode. In Offline mode, user download a SQLite database from a server. The app then queries data from this SQLite database to display and performs CRUD actions. Changes will be synced to server when user clicks sync button.
Now, I want to implement an Online mode. In Online mode, whenever the user go to a screen, it will make a WebApi request to the server. Same goes for CRUD actions.
UI will be the same in two modes (Online/Offline). User can choose which mode they want to work on. I want to prevent the use of mode checking conditional statement in all places.
For example, when user goes to screen A conditional statement will look like
if (mode == "Online") {
getDataOnline();
} else if (mode == "Offline") {
getDataOffline();
}
This check will be required in most places and it's not very elegant.
What would be a design pattern or best way to address this?
Solution 1:[1]
Microsoft has some solutions that make offline data sync fairly straight-forward in Xamarin. I would recommend checking these out rather than building them yourself:
Solution 2:[2]
create a class that handles all of your data requests for you. You VM would request data using this service, without having to worry about the details of whether or not the user was online
public class WidgetService : IWidgetService
{
public List<Widget> GetAll()
{
if (online)
return RemoteWidgetService.GetAll();
else
return LocalWidgetService.GetAll();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Glenn |
| Solution 2 | Jason |
