Recently, my colleague asked me a question, is there a possibility to work with RIA Service context on the client in offline mode (when network connection is lost)? That is, when there is no connection between the client and the server machine. The reasons for this are numerous. Well, for example, when user works with his data, network fails and internet/intranet became unavailable. Or for example, a user working on a mobile device, went on a fishing trip, but forgot to pay for the internet. Of course, to work on fishing trip, come home and syncronize all of his data by simple click, is not that nice?
Searching the internet, you can find a project that offers a library "RisServiceContrib". It, in turn, contains extenders for exports and imports EntitySet to IsolatedStorage. I tried it, and suprisingly, it works fine :)
I decided to gently ease the task of saving and load data by this library.
For exporting data:
Searching the internet, you can find a project that offers a library "RisServiceContrib". It, in turn, contains extenders for exports and imports EntitySet to IsolatedStorage. I tried it, and suprisingly, it works fine :)
I decided to gently ease the task of saving and load data by this library.
For exporting data:
public static void ExportToStorage(this IEnumerable collection, string fileName) where T : Entity, new() { using (var isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (var isfs = new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, isf)) { var serializer = new DataContractSerializer(typeof(List)); serializer.WriteObject(isfs, collection.Export()); isfs.Close(); } } }
For importing data:
public static void ImportFromStorage(this EntitySet collection, string fileName) where T : Entity, new() { using (var isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (var isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf)) { var serializer = new DataContractSerializer(typeof(List)); var list = (List)serializer.ReadObject(isfs); collection.Import(list); isfs.Close(); } } }
Use it like this:
public class MyViewModel { ... private bool isNetworkAvailable; private void LoadEmployees() { var fileName = "cacheFile.name"; if (isNetworkAvailable) { context.Load(context.GetEmployeesQuery(), LoadBehavior.RefreshCurrent, op => context.Employees.ExportToStorage(fileName), null); } else { context.Employees.ImportFromStorage(fileName); } } ... }
If you are confused by the work of the flag isNetworkAvailable, just look at this post
Link to RiaServiceContrib dll
Link to RiaServiceContrib dll
Комментариев нет:
Отправить комментарий