Using POCOs with Xamarin and Couchbase Lite


#1

Is it possible to save/query POCO classes (i.e. a class that doesn’t inherit from NSObject or some other base class) using Couchbase Lite/Xamarin? So far the examples I’ve seen have used NSDictionary to populate the documents, but I’d like to avoid that so I can keep my business objects platform independent.


#2

Hello cbsmith,

Know it has been awhile now and probably you have already resolved the original question.

Though I was thinking it is possible to query the POCO classes/objects after serializing to JSON objects. Can investigate more over on the Xamarin serializer discussion

Nonetheless, would love to know your work around and the examples you saw with NSDictionary.


#3

@cbsmith it sounds like you’re seeing the old iOS binding stuff, which is now retired. The current Couchbase Lite for .NET is pure .NET, so you’ll never see NSDictionary, only IDictionary<T,U>. One simple thing that you can do is add ToDictionary and FromDictionary, or even implicit/explicit operator overloads, that look like:

public static implicit operator IDictionary<string,object>(Foo model)
{
   return new Dictionary<string,object>
   {
          {"FirstName" , model.FirstName},
          {"LastName" , model.LastName},
          ... // and so on.
   }
}

public static implicit operator Foo(IDictionary<string,object> doc)
{
   return new Foo
   {
          FirstName = doc["FirstName"],
          LastName = doc["LastName"],
          ... // and so on.
   }
}