Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Out WITH RAVEN embedded In with litedb

5.00/5 (2 votes)
9 Jun 2016CPOL2 min read 11.2K  
Out WITH RAVEN embedded In with litedb

I recently starting working (I have now finished it) writing a small internal web site using the following things:

  • WebApi2
  • OAuth
  • JWT support
  • OWIN
  • AutoFac
  • Raven DB Embedded
  • Aurelia.io for front end

I have to say it worked out great. It was a pleasuree to work on, all the way through.

I quite like Raven embedded, for this type of app. It's completely stand alone, and does just what I need from it.

So I got the end of the project, and I was pretty sure I checked that we had licenses for everything I was using. Turns out we didn’t have one for RavenDB.

Mmm. This app was a tool really to help us internally so we did not want to spend that much on it.

Shame as I like Raven. I started to look around for another tool that could fit the bill.

This was my shopping list:

  • Had to be .NET
  • Had to support document storage
  • Had to have LINQ support
  • Had to support same set of features that I was using as Raven Embedded (CRUD + indexes essentially)
  • Had to be free
  • Had to be embedded as single Dll

It did not take me long to stumble upon LiteDB.

This ticked all my boxes and more. I decided to try it out in a little Console app to test it, and was extremely happy. I did not do any performance testing, as that is not such a concern for the app that I was building, but from an API point of view, it would prove to be very easy to replace the Raven Embedded code I had written so far.

I was happy.

Just thought I would show you all a little bit of its usage right here.

Installation

This is done via NuGet. The package is called “LiteDB”.

CRUD

Assuming we have this entity:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LiteDBDemo
{
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string[] Phones { get; set; }
        public bool IsActive { get; set; }
 
        public override string ToString()
        {
            return string.Format("Id : {0}, Name : {1}, Phones : {2}, IsActive : {3}",
                Id,
                Name,
                Phones.Aggregate((x, y) => string.Format("{0}{1}", x, y)),
                IsActive);
        }
    }
}

Here is how you would might use LiteDB to perform CRUD operation. See how it has the concept of collections. This is kind of like MongoDB if you have used that.

C#
using LiteDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LiteDBDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Open database (or create if not exits)
            using (var db = new LiteDatabase(@"MyData.db"))
            {
                //clean out entire collection, will drop documents, indexes everything
                db.GetCollection<Customer>("customers").Drop(); 
 
                Create(db);
                Read(db);
                Update(db);
                Delete(db);
            }
            Console.ReadLine();
        }
 
        private static void Create(LiteDatabase db)
        {
            Console.WriteLine("\r\nCREATE\r\n");
 
            // Get customer collection
            var customers = db.GetCollection<Customer>("customers");
 
            // Create your new customer instance
            var customer = new Customer
            {
                Name = "John Doe",
                Phones = new string[] { "8000-0000", "9000-0000" },
                IsActive = true
            };
 
            // Insert new customer document (Id will be auto-incremented)
            customers.Insert(customer);
            Console.WriteLine("Inserted customer");
        }
 
        private static void Read(LiteDatabase db)
        {
            Console.WriteLine("\r\nREAD\r\n");
 
            // Get customer collection
            var customers = db.GetCollection<Customer>("customers");
 
            // Index document using a document property
            customers.EnsureIndex(x => x.Name);
 
            // Use Linq to query documents
            var firstCustomer = customers.Find(x => x.Name.StartsWith("Jo")).FirstOrDefault();
            Console.WriteLine(firstCustomer); 
        }
 
        private static void Update(LiteDatabase db)
        {
            Console.WriteLine("\r\nUPDATE\r\n");
 
            // Get customer collection
            var customers = db.GetCollection<Customer>("customers");
            // Use Linq to query documents
            var johnDoe = customers.Find(x => x.Name == "John Doe").First();
            Console.WriteLine("Before update");
            Console.WriteLine(johnDoe);
 
            johnDoe.Name = "John Doe MODIFIED";
            customers.Update(johnDoe);
 
            var johnDoe2 = customers.Find(x => x.Name == "John Doe MODIFIED").First();
            Console.WriteLine("Read updated");
            customers.Update(johnDoe2);
            Console.WriteLine(johnDoe2); 
        }
 
        private static void Delete(LiteDatabase db)
        {
            Console.WriteLine("\r\nDELETE\r\n");
 
            // Get customer collection
            var customers = db.GetCollection<Customer>("customers");
            // Use Linq to query documents
            customers.Delete(x => x.Name == "John Doe MODIFIED");
            Console.WriteLine("Deleting Name = 'John Doe MODIFIED'");
 
            var johnDoe = customers.Find(x => x.Name == "John Doe MODIFIED").FirstOrDefault();
            Console.WriteLine("Looking for Name = 'John Doe MODIFIED'");
            Console.WriteLine(johnDoe == null ? "It's GONE" : johnDoe.ToString()); 
        } 
    }
}

You can learn more about this over at the LiteDB website:

Overall, I was very very happy with LiteDB and I particularly like the fact that was free, and it did pretty much exactly the same as RavenDB Embedded (sometimes it was easier to do as well).

I would use this library again for sure, I found it spot on to be honest.

Like a nice Gin and Tonic on a summers day.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)