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 / CSharp Best Practices.md
Created October 29, 2025 21:08
My personal experiences collected on Codes Review

1. try/catch the right way

a) Don’t try/catch whenever you feel like it

[!info] 👍 Rule of thumb: Avoid try/catch as much as possible

try/catch is BAD. It hurts the application performance. Hence, all try/catch must to be justified.

You should put a comment to justify why would you have to try/catch here?

For example a BAD Code:

@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 September 22, 2025 14:39
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 Request (or Merge Request) in an unfamiliar C# codebase, what should you check? Here is my checklist for reviewing a C# codebase without fully knowing what it does:

Since we don’t know much about what the code is doing, the review should focus on best practices:

  • Are there tests that cover all the changes? If not, is there a justified reason why the changes are not unit-testable? (Yes, that can happen.)
  • Challenge any unjustified try/catch blocks.
  • Challenge the use of Generics or Reflection (often a sign of over-engineering).
  • Challenge the use of Inheritance (may indicate poor design or over-engineering).
@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):