Discussing how exceptions can be handled in a centralized spot and how to return consistent responses. Exception logging is also centralized in the same approach.

Motivation

I adopted a REST API project today which had no exception handling or logging, making it really hard to investigate issues. So I spent an hour adding a centralized exception handler and structured logging using Serilog. In this article, I’d like to discuss how exception handling and logging can be setup at the beginning phase of a project following a best and easy concept.

Image for post
Image for post

Background

Once the business-level entities are defined, common exceptions and business-specific exceptions should be defined so that they can be shared among projects like API, Function App, Console App, etc..


Discussing a GitHub starter project to build a web API using Partitioned Repository Pattern with Azure Cosmos DB, ASP.NET Core, and Clean Architecture.

Image for post
Image for post
Clean Architecture — Onion View (From Microsoft documentation)

This article describes the GitHub project that can be used as a starting point to work with:

  • Clean Architecture (Onion Architecture)
  • ASP.NET Core 3.1
  • Azure Cosmos DB .NET SDK V3
  • Repository Design Pattern
  • Partition Key and Partitioned Repository

Popular features also supported in the project include:

  • Cosmos DB database initial creation and data seeding
  • Query data using Parameterized Query, LINQ and IQueryable, Specification Pattern in Cosmos DB
  • REST API with OData support and Swagger UI
  • MediatR Command/Query pattern
  • MediatR pipeline behaviour for exception handling
  • FluentValidation for validation

Please see a full updated feature list in the GitHub repo. If you…


How to build an ASP.NET Core API endpoint for time series anomaly detection, particularly spike detection, using ML.NET to identify interesting intraday stock price points.

Image for post
Image for post
Spikes identified by Anomaly Detection Model (Image by Author)

Background

What is Anomaly Detection?

According to Wikipedia, “In data analysis, anomaly detection (also outlier detection) is the identification of rare items, events or observations which raise suspicions by differing significantly from the majority of the data.”


How to deploy a trained sentiment analysis machine learning model to a REST API using Microsoft ML.NET and ASP.NET Core, in just 15 mins.

Image for post
Image for post
Photo by Robert Zunikoff

Why fast integration?

If you have a pure Data Scientist role, you probably care more about studying the data, going through feature engineering, choosing the correct statistical or machine learning model, and training the model to perform well on both the training dataset and validation dataset. And that’s it!


Discussing how to scale the barebones TinyURL system using Load Balancer and Database Horizontal Partitioning, and related topics like partition key design strategy, partition vs replica vs backup, SQL vs NoSQL.

Image for post
Image for post
Image credit to Jason Chen

In the last couple of articles, we discussed how to design a barebones working solution for TinyURL, and also how to improve 90% of the estimated workload by introducing cache.

The system diagram below shows what we currently have. The single web server handles all the HTTP requests from the clients, and the single database stores all the data and handles all the read and write requests. Let’s trace the workflow and see how we can scale each component in the diagram, starting from the web server.


Discussing Binary Search, and how to use its template code with Two Pointers to solve multiple interview questions in C# for a better understanding of Data Structure and Algorithms.

Goal

The goal is to share some concise notes on Binary Search, and its implementation using both recursion and two pointers algorithm. Hopefully, the high-level summary notes allow you to add Binary Search to your tool bag without getting swamped by detailed textbook explanations. The following items are covered:

  • Algorithm high-level logic
  • Typical use cases/interview questions referencing LeetCode
  • Typical bugs/gotchas in implementation

What is Binary Search?

Binary Search is an algorithm to search a sorted array for a target value. Important notes on Binary Search:

  • O(logn) time complexity, as the problem size is reduced to half after each iteration
  • O(1) space complexity
  • works on sorted…


Discussing Two Pointers algorithm, and how to use its template code to solve multiple interview questions in C# for a deeper understanding of Data Structure and Algorithms.

Goal

The goal is to share some concise notes on the Two Pointers algorithm at the high-level and its typical use cases or relevant interview questions. Hopefully, this allows you to add it to your tool bag without getting swamped by detailed textbook explanations. The following items are covered:

  • Algorithm high-level logic
  • Typical use cases/interview questions referencing LeetCode
  • Typical bugs/gotchas in implementation

Three types of Two Pointers

Depending on the starting point of the two pointers, left (L) and right (R):

  • Same Direction
  • Opposite Direction
  • Facing Direction
Image for post
Image for post
Image by Author

Facing Direction Use Cases

Below are some typical use cases and frequently asked interview questions on Two Pointers with facing direction.


Discussing how to use Serilog for both filesystem and log stream on Azure App Service for ASP.NET Core Web API in details.

Image for post
Image for post

Goal

Does this sound familiar? “XXX works perfectly locally, but does not work at all in production”. The same mystery applies to structured logging using Serilog!

Getting Started

Not to waste any time, so let’s get straight into it!

  • A new or existing ASP.NET Core API project
  • (optional) API deployed on Azure App Service if you want to test production environment

Step 1 — Add…


Discussing how to automatically create an Azure Cosmos DB database and its containers on API application startup in Clean Architecture using ASP.NET Core.

Background

It is straightforward to create an Azure database and containers in Azure Portal. But if you are developing a new application using Cosmos DB Emulator, it will be nice to be able to delete the whole database and have it automatically re-created on the application start to get a clean slate. Also, if a new developer joins the team, he/she can just run the application and be all set to go, instead of having to manually create a database and containers (annoying!).

Image for post
Image for post
Image by Author, Cosmos DB icon from Microsoft

Prerequisites

  • (optional) Cosmos DB Container Factory class library, if you want to just copy the code and be ready…


Discussing how to build a powerful factory to work with Cosmos DB containers and encapsulate the complex logic with Cosmos DB .NET SDK in Clean Architecture using ASP.NET Core.

Image for post
Image for post
Image by Author (Cosmos DB icon from Microsoft, Factory Icon from Smashicons)

Background

What is Factory Pattern? It is a design pattern in OOP that allows us to encapsulate object creation logic and hide its complexity from the calling program, such as API, Function Apps, and even users. This probably sounds very vague. My personal take-away note is that factory pattern allows me to ask the factory:

  • to make me a car, then I will get a car
  • to make me a train, then I will get a train
  • to make me a universe, then I will get a universe

I do not have to worry about how to make a car/train/universe throughout…

Shawn Shi

Software Engineer, Machine Learning Engineer. When I am not dived into data and code, I am hanging out with my young daughter or outside rock climbing!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store