This Gist explores three mapping strategies in .NET: AutoMapper, Manual Mapping, and Mapster. Includes benchmarks and testing guidance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ITimeProvider.cs | |
| public interface ITimeProvider | |
| { | |
| DateTime UtcNow { get; } | |
| } | |
| // SystemTimeProvider.cs | |
| public class SystemTimeProvider : ITimeProvider | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ❌ 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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // PolymorphicJsonConverter.cs | |
| using System.Text.Json; | |
| using System.Text.Json.Serialization; | |
| public interface INotification | |
| { | |
| string Type { get; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
NewerOlder