Skip to content

Instantly share code, notes, and snippets.

View emonarafat's full-sized avatar
🟢
Working from home

Yaseer Arafat emonarafat

🟢
Working from home
View GitHub Profile
@emonarafat
emonarafat / JsonPerformanceTuning.cs
Created July 27, 2025 15:29
💡 Optimized JSON serialization, compression, and caching strategy in .NET APIs using System.Text.Json + Redis for high-throughput, scalable performance.
using System.Text.Json;
using System.IO.Compression;
using StackExchange.Redis;
using System.Text;
public static class JsonCacheHelper
{
private static readonly JsonSerializerOptions _jsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
@emonarafat
emonarafat / Mapping_Strategies_NET_With_Comments.md
Created July 16, 2025 18:19
🧠 AutoMapper vs Manual vs Mapster — Real-World Mapping in .NET

🧠 AutoMapper vs Manual vs Mapster — Real-World Mapping in .NET

This Gist explores three mapping strategies in .NET: AutoMapper, Manual Mapping, and Mapster. Includes benchmarks and testing guidance.


🔄 AutoMapper Mapping

@emonarafat
emonarafat / ITimeProvider_Pattern.cs
Created July 14, 2025 17:21
ITimeProvider Pattern for Testable Time Management in C#
// ITimeProvider.cs
public interface ITimeProvider
{
DateTime UtcNow { get; }
}
// SystemTimeProvider.cs
public class SystemTimeProvider : ITimeProvider
{
@emonarafat
emonarafat / RateLimitingMiddlewareSamples.cs
Created July 12, 2025 18:36
Complete Minimal API setup with Fixed Window strategy and global limiter
// Program.cs — Minimal API setup for global rate limiting
var builder = WebApplication.CreateBuilder(args);
// Register native ASP.NET Core rate limiter
builder.Services.AddRateLimiter(options =>
{
options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(context =>
RateLimitPartition.GetFixedWindowLimiter(
@emonarafat
emonarafat / DotNet_Records_Good_And_Bad_Practices.cs
Created July 8, 2025 02:43
Examples of correct and incorrect usage of C# record types in real-world .NET projects — from immutability principles to EF Core gotchas.
// ❌ Misuse of Records in .NET — Don't Do This
public record User
{
public string Name { get; set; }
public int Age { get; set; }
}
// This is mutable — which defeats the purpose of records
var user = new User { Name = "John", Age = 30 };
user.Age = 31; // Mutation warning!
@emonarafat
emonarafat / PolymorphicJsonConverter.cs
Created July 4, 2025 17:16
System.Text.Json polymorphic converter for interface deserialization in .NET 6+
// PolymorphicJsonConverter.cs
using System.Text.Json;
using System.Text.Json.Serialization;
public interface INotification
{
string Type { get; }
}
@emonarafat
emonarafat / JsonSerializerComparison.cs
Created July 3, 2025 11:53
A benchmark comparison of System.Text.Json and Newtonsoft.Json in .NET
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
public class JsonSerializerComparison
{
public static void Compare(MyModel model)
@emonarafat
emonarafat / PluginHostExtensions.c
Created July 2, 2025 19:44
🔌 Simple .NET plugin architecture pattern using IPlugin and extension methods for modular service registration.
// PluginHostExtensions.cs
public static class PluginHostExtensions
{
public static IServiceCollection AddPlugin<TPlugin>(this IServiceCollection services)
where TPlugin : class, IPlugin, new()
{
var plugin = new TPlugin();
plugin.ConfigureServices(services);
return services;
}
@emonarafat
emonarafat / EFCoreMythSamples.cs
Created June 29, 2025 16:49
“EF Core Tracks Everything” — and 5 Other Lies Slowing Down Your App
// EFCoreMythSamples.cs
// Demonstrates tracked vs untracked queries and minimal EF Core usage
using Microsoft.EntityFrameworkCore;
public class BloggingContext : DbContext
{
public DbSet<Post> Posts => Set<Post>();
public DbSet<Comment> Comments => Set<Comment>();
@emonarafat
emonarafat / TaskResultDeadlockExample.cs
Created June 26, 2025 16:28
Avoid Task.Result — Async/Sync Mismatch in .NET Can Cause Deadlocks
# TaskResultDeadlockExample.cs
// ❌ Anti-pattern: Mixing sync + async causes deadlocks
public class UserService
{
public string GetUserName(Guid id)
{
var client = new HttpClient();
// This will block the thread and can cause deadlocks in ASP.NET or UI apps