Caching mobile object in WinRT

Caching mobile object in WinRT

Old forum URL: forums.lhotka.net/forums/t/12173.aspx


jteneglio posted on Sunday, October 06, 2013

Im looking to Cache CSLA objects in WinRT, can I use the same serialization used to copy mobile object from the server to the windows 8 application?  Is there an example of that somewhere?

 

RockfordLhotka replied on Sunday, October 06, 2013

When you say 'cache' do you mean in memory or to disk?

If you mean to disk, that's a little tricky because the serializer (MobileFormatter) is version sensitive in terms of the assemblies. So if you update your app code to a newer assembly version the cache will be unreadable.

If you are OK with that restriction (and code for the exception you'll get when trying to deserialize the cache once the version numbers don't match) then you can look at the Clone implementation in BusinessBase. It creates an in-memory clone, but that code will show you how to serialize an object into a MemoryStream (the intermediate step) and deserialize from a MemoryStream.

All you need to do is take the data from the MemoryStream and write it to a binary file, or read a binary file into a MemoryStream. The serialization/deserialization code is the same as in the Clone method.

skagen00 replied on Monday, October 07, 2013

Just to back up the notion of this - we do this with Silverlight and Isolated Storage - and if deserialization fails (for instance in the case of versions changing), well we just consider the cache invalid.

With Silverlight the initial way to deal with large representations of serialized objects was to compress the byte array - we similarly would do this when caching objects too: serialize then compress / decompress then deserialize.  With MobileFormatter being better about serialized object size you may not need to compress the seriailzed byte array but it may be something to think about if there are any space considerations.

(With compression I had been seeing 93-95% compression, with just the mobile formatter optimizations alone it was more like 80% for me - so a three to four-fold difference in the end-result byte array.  That is what I recall, at least.

Copyright (c) Marimer LLC