Well, I just could not unbeer! I just had to have some refreshing IPAs. I downed a few of good Lucious Lupalin IPAs from Migration Brewing. It felt very good and I did not have any guilt doing it. That was my Faturday :)

Now back to the app! In this iteration I’ll add in some validations and persist the data. I’ll change ForDate to be of type DateTime instead of String.

Persist the data

I’ll use LiteDB to save the data. From the official description - ‘LiteDB is a small, fast and lightweight NoSQL embedded database.’. This is a perfect solution for me to get started. Eventually I’d want to move to something like Postgres using Marten.

LiteDB stores data to a datafile. It is very easy to get started. In the following snippet, I instantiate LiteDatabase by passing in a datafile name. If the file doesn’t exist, it is created automatically. Then I get the beered collection and insert newly created data to it. Finally, I can gel all the data in a collection and calculate the sum of all the beers I had using Linq! How cool and easy was that! Impressed :)

using (var db = new LiteDatabase(@"unbeered_store.db"))
{
    // Get beered collection
    var beereds = db.GetCollection<Beered>("beereds");

    // Insert each instance of beered to the database
    foreach (var datum in data)
    {
        beereds.Insert(datum);
    }

    Console.WriteLine($"You had a total of {beereds.FindAll().Sum(x => x.Count)} beer(s) since {januaryFirst2018:yyyy MMMM dd}");
}

Validation

I’ll validate the date and count when creating Beered object using Ensure.That for this. Validating the data in constructor is a fine solution for now. Unfortunately I’ll have to have a parameterless constructor for LiteDB to work properly. That allows creating data that may not be valid :( I’ll revisit this when I use Marten to persist data.

The code for this post is at branch: learning-unbeered-2

Whats next?

  • Find a better name for Beered class. I’m still not happy with the name!
  • Move Beered class to a separate project and use console application just for the interaction.

Until next time, have a wonderful day!