Issue with serialization of type names
Note: for support questions, please ask on StackOverflow: https://stackoverflow.com/questions/tagged/Akavache . This repository's issues are reserved for feature requests and bug reports.
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Depending on the structure of your classes, you may end up serializing types to Sqlite as base classes rather than the actual runtime type. This caused a problem for me in particular when using the GetAllObjects<T> method, which wasn't returning anything.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem
Run this to see the different between using typeof (compile time) vs GetType (runtime):
http://csharppad.com/gist/b19be0194baf19f2202f88ad6f46580e
What is the expected behavior?
I would expect my objects to be serialized as their actual type rather than boxed type.
What is the motivation / use case for changing the behavior?
I think this would make for more consistent serialization / de-serialization of objects.
Which versions of Akavache, and which platform / OS are affected by this issue? Did this work in previous versions of Akavache? Please also test with the latest stable and snapshot (https://www.myget.org/feed/Akavache/package/nuget/Akavache) versions.
I believe that prior versions of Akavache will have been affected. Only tested on Windows / UWP.
Other information (e.g. stacktraces, related issues, suggestions how to fix)
Replace use of typeof with GetType. I would submit a pull request but I don't have all the prerequisites installed to run Akavache anymore. I was helping a former colleague diagnose an issue and thought it might be worth raising as an issue. I also understand you may have other reasons for doing this and didn't want to presume that what I think is a solution is actually a solution :).
Workaround
A workaround is to cast an object (if the reference comes from a base class) to the actual type before calling AddObject but this isn't entirely intuitive.
Hi @natwallbank, If I understand your problem correctly, I just faced similar issue. I changed the namespace of my object and it is not deserializing/serializing anymore. Is that what you mean simply?
Hi @EmilAlipiev, not exactly, no. I'd probably expect that if the full name of the serialised type changed then existing serialised data may no longer be compatible.
The issue I had is that my reference to an object was declared as its interface for reuse/generic reasons, so calling AddObject serialised it using that type. I think it would be better if AddObject serialised as the actual runtime type, as when I wanted to de-serialise all objects of a specific derived type, it didn't work. It is possible to cast the object to is derived type first but it's a pain to have to remember to do that.