Strategies for model to dictionary to/from conversion? (For saving and querying mutable docs)


#1

Hello all. CB Lite 2.0 user, for Xamarin Forms .net shared project in C# (just for a frame of reference)

Initial/Overall Question:

Am I missing anything baked into CB Lite that would automatically convert models to and from their necessary dictionary format? We’ve had to do a goodly amount of work to get this done, and for complex models (models with collections of other models within them) this has been squirrelly to achieve. I would have thought there would have been something generic available within CB Lite to handle this back and forth while saving and querying docs. I totally understand some companies likely wanting to bake their own for customization sake, but, for general use, I’m just a little surprised you cannot just feed CB Lite a List, say which field is the primary key equivalent for the resulting json(no-sql), and then auto generate the needful unto itself back and forth. If you have found anything third party or general public use that you found helpful, would love to review them.

Related Question:

I’m just curious in general, how other users of CB Lite 2.0+ have approached the model to dictionary to/from conversions. We have approached it by baking in ConvertToDictionary and ConvertFromDictionary, ProcessDictionary and similar methods into our models to get things working back and forth ultimately as List<Dictionary<string,object>>. I’m sure there are other more generic ways to create your own back and forth conversion tools (or any other sdks libraries, etc in c# land) that you have found useful when interacting with CB Lite. If you want, share them below. We have been toying with just taking lists of complex objects, json encode , then json decode as JsonConvert.DeserializeObject<List<Dictionary<string, object>>

I’ll play my own devil’s advocate:
“Why are you asking this? Just figure it out and do the needful.”

If you are attempting to attract new couchbase customers, in particular for mobile development, a vast majority of them use SQLite for database interaction. They are not used to having to bake their own model conversions for CRUD operations.

Regards…
Peter Robbins - CB Lite Fan


#2

We use 1.4 and are doing the same thing. Convert from a dictionary using JsonConvert/JObject.

This has drawbacks, however. This imposes the requirement on all of your client applications that the data model is the same version 100% of the time, otherwise you potentially lose data when you save the model back to CBLite.

Let’s say you sync a document that has 2 fields to a device whose data model is the “old” version which only has 1 field. When you deserialize, you lose the second new field. If you save that representation of the data model back to CBLite, then you’ve lost the second field.

Hence, using dictionaries/JObjects allows your data model to evolve without having to keep all of your client’s up to date with the same definition, or to have to build logic that prevents some client’s from mutating the document if it is a newer version.