Skip to content

Instantly share code, notes, and snippets.

View duongphuhiep's full-sized avatar

DUONG Phu-Hiep duongphuhiep

  • lemonway.fr
  • Paris, France
View GitHub Profile
@duongphuhiep
duongphuhiep / Why REST API is bad and it is OK to return HTTP 200 in case error.md
Last active July 30, 2025 13:28
REST vs OpenAPI - Why REST API is bad and it is OK to return HTTP 200 in case error?

REST vs OpenAPI - Why REST is bad and We should return HTTP 200 in case error

Do you know that OpenAPI (formerly "Swagger") and REST are two different things?

  • A "pure" REST API should be self-descriptive (i.e., it doesn't require any documentation) by implementing the HATEOAS (Hypermedia as the Engine of Application State) principle. Clients interact with the API entirely through URLs provided by the server, without needing to know the structure of URLs in advance. This is similar to how browsers follow links.
  • Most REST APIs today are not "pure". Clients still need documentation to use the API's endpoints effectively.

OpenAPI helps describe (or document) "impure REST APIs." But please don't confuse the two! OpenAPI doesn't enforce as many conventions as REST. For example:

| REST (or impure REST)

@duongphuhiep
duongphuhiep / radzen-material.css
Created July 18, 2025 23:24
Radzen's Material theme complementary: supporting dark, light variant, react to prefers-color-scheme and data-theme attributes
/* Default value */
:root {
color-scheme: light;
--rz-alert-message-margin: 0.125rem 0;
--rz-alert-icon-margin: 0.125rem 0;
--rz-base: #eeeeee;
--rz-base-50: #fafafa;
--rz-base-100: #f5f5f5;
--rz-base-200: #eeeeee;
--rz-base-300: #e0e0e0;
@duongphuhiep
duongphuhiep / Registered Services in a blank .NET 9 Web API application.htm
Last active October 22, 2024 08:15
Registered Services in a blank .NET 9 Web API application
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></head><body><h1>Registered Services in a blank .NET 9 Web API application</h1><table border="1"><thead><tr><th>Counter</th><th>Abstraction Type</th><th>Lifetime</th><th>Concrete Implementation</th></tr></thead><tbody><tr><td>1</td><td>Microsoft.Extensions.Hosting.IHostingEnvironment</td><td>Singleton</td><td>Microsoft.Extensions.Hosting.Internal.HostingEnvironment</td></tr><tr><td>2</td><td>Microsoft.Extensions.Hosting.IHostEnvironment</td><td>Singleton</td><td>Microsoft.Extensions.Hosting.Internal.HostingEnvironment</td></tr><tr><td>3</td><td>Microsoft.Extensions.Hosting.HostBuilderContext</td><td>Singleton</td><td>Microsoft.Extensions.Hosting.HostBuilderContext</td></tr><tr><td>4</td><td>Microsoft.Extensions.Configuration.IConfiguration</td><td>Singleton</td><td>System.Func`2[[System.IServiceProvider, System.ComponentModel, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a],[Microsoft.Extensions.Configurat
@duongphuhiep
duongphuhiep / C# Codes Review.md
Last active October 20, 2024 14:27
How to Review PRs / MRs of unfamiliar C# codes base?

How to Review PRs (or MRs) of unfamiliar C# codes base?

If you have to review a Pull (or Merge) request in an unfamiliar C# Code bases, what will you check? Here is "my method" (or check list) to review a C# codes base without knowing what it is doing.

As we don't know much about what the code is doing, we can only review on the "best pratice" aspect:

  • where are the tests which would cover all the changes? justify if the changes are not unit-testable (yes, it can happen)
  • challenge if there are unjustified try / catch
  • challenge if "Generic" or "Reflection" are used (over-engineering smell)
@duongphuhiep
duongphuhiep / Logging best practices.md
Last active August 14, 2024 11:23
Logging best practices

Logging best practices

When generate a log message

  • Major branching points in your code
  • When errors or unexpected values are encountered
  • Any IO operations: calling database / stored proc or make http request
  • Significant domain events
  • Request failures and retries
  • Beginning and end of time-consuming operations
@duongphuhiep
duongphuhiep / AesEncryptionHelper.cs
Created July 10, 2023 14:37
AesEncryptionHelper
using System.Security.Cryptography;
using System.Text;
namespace Lemonway.TransactionService.Application
{
public static class AesEncryptionHelper
{
public static string Encrypt(string payload, string secret)
{
byte[] iv = new byte[16];
@duongphuhiep
duongphuhiep / keybindings.json
Last active May 10, 2023 07:07
vscode Hiep's keymap
// Place your key bindings in this file to override the defaultsauto[]
[
{
"key": "ctrl+t",
"command": "workbench.action.togglePanel"
},
{
"key": "ctrl+shift+f4",
"command": "workbench.action.closeOtherEditors"
},
@duongphuhiep
duongphuhiep / How to perform Request Reply communication between Iframe & Host.md
Last active December 25, 2024 21:07
How to perform Request Reply communication between Iframe & Host

Problem

Your HTML page (the host page) has an Iframe. The Iframe source is on other domain, so you will have to use the window.postMessage() for cross-origin communication. But this communication is only in 1 direction (The Iframe can inform the Host or the Host can inform the Iframe)

This article shows you how to make the Request/Reply communication using postMessage() and MessageChannel combination.

The article did not show you how to add a timeout to the communication, so the Iframe might wait forever if the Host did not response.

I will resume the technique by some codes snippets and at the same time add the missing timeout implementation.

@duongphuhiep
duongphuhiep / Technical Error vs Business Error.md
Last active July 30, 2025 07:57
Technical Error vs Business Error

Technical Errors vs Business Errors

API's designer often categorize errors into "Technical" and "Functional" (or "Business") errors. However, this distinction may not make sense:

  • An API/micro-service should always return ONLY "Functional" (or "Business") errors.
  • The API/micro-service migh be crashed while processing a consumer's request and so the consumer will naturally get a technical error. But the API/micro-service should not deliberately return a "Technical" error for consumer. The simple fact that the App built a nice response called "Technical error", then gracefully return it, make it no longer a technical error, but a "fake" one.

We will explore the distinction between "real technical errors" and "fake technical errors". "Fake technical errors" are initially technical but later manifest as business errors, thus becoming "fake technical errors."

  • HTTP 404 Business error (or Fake technical error)
@duongphuhiep
duongphuhiep / MediatR vs MassTransit Mediator - Part 2 - Simplify MassTransit Consumers.md
Last active March 13, 2025 20:18
MediatR vs MassTransit Mediator - Part 2 - Simplify MassTransit Consumers

Read Part 1: MediatR vs MassTransit Mediator - Differences

The MassTransit Mediator implementation is more powerful than MediatR implementation, but also more complicate to use. Unit Testing a MassTransit Consumer is not a nice experience.

In this article I will propose some techniques to simplify the MassTransit Consumer and to make them look as straight forward as a MediatR handler.

Case study

We will study a very common use case (the most common use case which I can think of):