LiteDB on Xamarin Forms as an Alternative to SQLite

Hello guys what’s up ?. When building a Xamarin Forms application, persisting data locally is done mostly with SQLite. SQLite is widely used to persist data in mobile applications. While SQLite is widely used and there are libraries which function as ORM for interactions with SQLite databases in .Net applications, you may need another solution to persist your data. For one reason or another, you may want to persist your data with a NoSQL database instead. I have been using SQLite, on every mobile app which I needed to persist data on. But I needed something different, which works better for me.

Then I asked the all mighty Google. He gave me the perfect solution to LiteDB.

What is LiteDB

As stated on the LiteDB Website, LiteDB is serverless database delivered in a single DLL (less than 350kb) fully written in .NET C# managed code (compatible with .NET 3.5, 4.x, NETStandard 1.3 and 2.0).

Lite DB was inspired from MongoDB and it is extremely easy to get started with it. And obviously, it is a NoSQL database. LiteDB stores documents in the BSON (Binary JSON) data format. Enough introduction, let’s get started with implementing LiteDB on Xamarin Forms.

LiteDB on Xamarin Forms

LiteDB is compatible with .Net Standards 2.0, Xamarin.iOX, Xamarin.Android, and UWP thus can be used to build Xamarin Forms applications. Here is how it is done.

Create a new Xamarin Forms app and install LiteDB

Once you have installed the package, we need to determine where exactly our databases will be stored. And we do so on every platform. We will do this with the Dependency service provided by Xamarin Forms. (NB: I used this approach per platform to safely get a valid location for my database. And avoid any issue related to where the database file is stored on each platform).

Lets Build a very simple Todo App

In other to really demonstrate how to use LiteDB on Xamarin Forms we need to integrate data access in a real app. So we will do so with a very simple to do application. This application is almost the same as that which we built to demostrate ReactiveUI on Xamarin.Forms. If you want a detail guide on building the sample app or to learn more about building a Xamarin.Forms app with Reactiveui, check it here.

This service is just the base, now let us customize it to suit data operations on todo items. to do so, we create a class which inherits from out LiteDb service.

TodoLiteDBService : LiteDBService<Todo>

Each item saved in LiteDB has an Id property to uniquely identify it. So You need to tell LiteDB which property on our object will act as it’s ID. You can do so using attributes or a mapper. Here we will use Mappers. For our todo items, the ID property has to be considered as its ID, so let’s say it to LiteDB.

Before saving an Item in LiteDB, we have to assign its ID a value, this will be done using GUIDS which are randomly created in our code. And, this is done before saving the Todo Item. Here is the full code for this class.

When a new Todo item is created, this object saves it. When it is set as done, the object updates it. And when the view model is created, the todo items in the database are loaded. Here is the code for these operations.

Conclusion

With this, you have a sample application which shows how to use LiteDB on Xamarin Forms to persist data. Here is what this sample should look like on UWP.

LiteDB on Xamarin Forms Demo

You also have to possiblity to access the data stored in your database and manipulate it outside the app. Use the LiteDB explorer. Access the database files from the paths which we precised per platform above and open these files with LiteDB explorer.

LiteDB on Xamarin Forms LiteDB Explorer

If you liked this post, or it was useful to you, please 👍 like it, share it on twitter, facebook or other social media… in case you want to get updated on any new useful post, follow me on twitter and like my page on facebook. And every other social media by clicking on the buttons at the end of this post.