Wow…It has been a while since my last post! About 5+ months. It is safe to assume that I’ve had many pints of beer since then, IPA or not! Few significant changes in private life, but not very many of those. I’m glad to be back and picking up where I left off. I’ve been craving. Let’s do it!

Updates

Code for this post is in branch: learning-unbeered-8.

  • In this update, I’ve dived into Notification part of the application. To recap quickly, notification is an event that gets published when something happens in the application. Usually published by a request handler.
  • Looking at AddEntryHandler.cs, we see that we now publish “NewEntryAddedNotification”. We’re letting any subscribers know the fact that an entry was successfully recorded.
  • Looking at RemoveAllEntriesHandler.cs, we publish AllEntriesRemovedNotification event. Letting the subscribers of the event know that all records have been removed.
  • We also have a new handler for those notifications - EntryNotificationHandlers.cs. The handler is basically a subscriber that reacts to the published events. In our case, it handles the two new notification events.
  • When NewEntryAddedNotification is published, the handler goes to the database and adds the number of beers to the existing tally! Existing tally is stored as a separate collection in the db (calculated_data).
  • When AllEntriesRemovedNotification is published, the handler wipes the collection(calculated_data) that is keeping track of current tally.

With the use of notifications, we were able to add the number of beers to existing “pre calculated” tally. In essence, this is almost like a cached version. Previously we had to retrieve all the entries in the database and do the calculation in memory. We could do that within the request handler and avoid publishing notifications and therefore implementing handlers. But here’s where this pattern shines: we can have multiple handlers subscribed to the same notification which does different things. In the next post, lets look at how to alert/warn when I consume more than 4 pints of beer in one entry! Hint: New Handler :)

  • Other improvements:
    • When the application starts, we remove all the entries from the past.
    • Only prompt for last seven days.
    • Console outputs both the cached version of tally and aggregated version.

Whats next?

  • Add a feature where if an entry has more than 4 pints, warn/yell to stop doing that :)