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:
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.
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)
{
using (var db = new LiteDatabase(@"MyData.db"))
{
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");
var customers = db.GetCollection<Customer>("customers");
var customer = new Customer
{
Name = "John Doe",
Phones = new string[] { "8000-0000", "9000-0000" },
IsActive = true
};
customers.Insert(customer);
Console.WriteLine("Inserted customer");
}
private static void Read(LiteDatabase db)
{
Console.WriteLine("\r\nREAD\r\n");
var customers = db.GetCollection<Customer>("customers");
customers.EnsureIndex(x => x.Name);
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");
var customers = db.GetCollection<Customer>("customers");
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");
var customers = db.GetCollection<Customer>("customers");
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.