Tracks part 2 - Implementing Persistance with LiteDB

So now we have a console app, and we can add items and list items, but as there is no persistance it is not that much fun. We want to create a rest api to allow multiple different clients to update the tracks.

Lite up the database

ListeDB is a embedded standalone NoSQL database for .NET. I wnt fot this db as I wanted somethings small, light, and able to package it with the same app as the console app. Potential this db could be interesting in some IoT scenarios. The requirements for this app is basically non existent, so lets try this out.

I know this is not the end db so I want to be able to switch it out later. So by now I have a Model project and a Console App project. I’m splitting this out way more that I would normally do, because I know I’m going to have so many platforms in the mix, so I want to switch components in and out.

Installing LiteDB

Installing is as simple as below - “Thank you NuGet!”.

 
Install-Package LiteDB

In order to store entities in LiteDB the entities need to have an Id property, so we extend the Step and Track model classes.

Using the liteDB is quite simple as well. All you have to do is use the Library, create a db object giving it a filename, and then call db.GetCollection<Entity>("<table name>") as shown below:

using LiteDB;

namespace d6.Tracks {

public class LiteDBTracker : ITracker {

        public ITrack GetTrack(string name) {
            using (var db = new LiteDatabase(@"tracks.db")) {
                var tracks = db.GetCollection<DBTrack>("tracks");
                var result = tracks.Include(t => t.Steps).FindOne(Query.EQ("Name", name));
                return (ITrack)result;
            }
        }
}

Architecture

The complete console app with a running persistent database is looking like the diagram below.

git it to source control

Everything is of course committed to a git repo. Currently i’m using two private Azure DevOps repos for the Tracks project and the blog it self. Possibly I will consider publishing it to an open git repo at some point, when it gets cleaned up a bit more,

Next steps

The next steps will be to add unit testing, a service API and possibly another cloud based storage, hopefully without changing the client.