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 / 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 February 25, 2023 12:14
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 May 31, 2024 00:21
Technical Error vs Business Error

Technical Errors vs Business Errors

When designing or learning about APIs or microservices, it's crucial to consider error handling. 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 alway return ONLY "Functional" (or "Business") errors.
  • The API/micro-service migh crash 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.

We will explore the distinction between "real technical errors" and "fake technical errors," which 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 February 28, 2024 10:40
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):

@duongphuhiep
duongphuhiep / MediatR vs MassTransit Mediator - Part 1 - Differences.md
Last active October 11, 2024 21:43
MediatR vs MassTransit Mediator / Part 1 / Differences

Why mediator?

In a ASP.NET Web application. You don't need mediator

  • If you prefers to make controlers depends directly to the Application Codes instead of indirectly via the Mediator.
  • If you prefers to make a normal ASP.NET Web Application instead of a Mediator Application

Frankly it is not a Bad choice, no need to use a Mediator framework or make a Mediator Application just because everyone did.. Though, There are benefits in making a Mediator Application:

  • Event sourcing (Messages broadcast), CQS pattern..
  • Decouple the Controler (presentation) from Application codes, so that you could swap the presentation technology. For eg, if you make a "MassTransit" application, then you can swap the presentation layer to Mediator or RabbitMQ, or Grpc.. => you are not to be sticked with or limited by ASP.NET presentation => but rather sticked with and limited by your mediator framework!
@duongphuhiep
duongphuhiep / SampleUnitTest.cs
Created August 7, 2022 10:27
Masstransit 8 TestHarness and Moq.Automocker
using MassTransit;
using MassTransit.Testing;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Moq.AutoMock;
using System.Threading.Tasks;
using Xunit;
namespace HiepTest;