Couchbase Lite 2.0 is a powerful NoSQL embedded data store for iOS, Android and .Net Mobile Platforms. Couchbase Lite provides functionality such as database and document CRUD operations, brings powerful Query API with SQL-like semantics and Full-Text-Search (FTS) capabilities.
Couchbase Lite Extensibility
The Couchbase.Lite and Couchbase.Lite.Enterprise Nuget packages provide a broad array of capabilities for easily creating, configuring, and manipulating the data within Couchbase Lite databases on the platforms that support the Couchbase Lite .NET SDK. While these packages already contain a large amount of functionality out of the box, an enormous benefit of using Nuget packages is the potential for extensibility. The ability to extend existing functionality provides us, as a development community, with an avenue to create and maintain projects that supplement existing Couchbase Lite features.
One example of an opportunity to extend Couchbase Lite is to add the ability to dynamically convert objects to and from MutableDocument
objects. Currently, as seen below, each model object property name and value must be added manually to MutableDocument
objects through various type-specific methods.
1 2 3 |
var mutableDocument = new MutableDocument(); mutableDocument.SetString("Name", userProfile.Name); mutableDocument.SetString("Email", userProfile.Email); |
Subsequently, to convert a MutableDocument
back into the original object the following must be done.
1 2 3 4 5 |
var userProfile = new UserProfile { Name = mutableDocument.GetString("Name"), Email = mutableDocument.GetString("Email") }; |
Introducing Couchbase.Lite.Mapping
As you can see, the code for converting an object into a MutableDocument
and back again is simple, but as every object must be mapped developers can find themselves writing many similar lines of code. So, it’s with much excitement that Couchbase Labs is introducing a new Nuget package to help decrease the amount of code written to map model objects to and from MutableDocument
objects.
1 2 3 4 5 6 7 8 9 10 11 |
var userProfile = new UserProfile { Name = "Robert Hedgpeth", Email = "robert.hedgpeth@couchbase.com" }; // Converting an object to a MutableDocument var mutableDocument = userProfile.ToMutableDocument(); // Converting a MutableDocument to an object var newUserProfile = mutableDocument.ToObject<UserProfile>(); |
Couchbase.Lite.Mapping can be found on Nuget.org, and is a completely open-source project maintained in Couchbase Labs on Github. It is important to note that the Couchbase.Lite.Mapping
library is not officially supported by Couchbase, Inc., but is, instead, a community effort. Please give it a try, and let us know what you think!
Also, as this is an ongoing effort, please feel free to contribute by submitting comments, issues, and/or pull-requests.
Great info. At least for me there seemed to be a dependency on Newtonsoft.Json.