Apr 15 2025
public static class MyExtensions
{
extension(TargetType instance)
{
public ReturnType PropertyOrMethod => ...;
}
extension static
{
public static ReturnType StaticHelper() => ...;
}
}
public static class MultiTenantHttpContextExtensions
{
extension(HttpContext ctx)
{
private string? _tenantId;
private Guid? _userId;
public string TenantId =>
_tenantId ??=
ctx.User.Claims.FirstOrDefault(c => c.Type == "tenant_id")?.Value
?? throw new UnauthorizedAccessException("Tenant ID missing");
public Guid UserId =>
_userId ??=
Guid.TryParse(
ctx.User.Claims.FirstOrDefault(c => c.Type == "user_id")?.Value, out var id)
? id
: throw new UnauthorizedAccessException("User ID invalid");
public bool IsTenantAdmin =>
ctx.User.IsInRole("Admin") || ctx.Request.Headers["X-Tenant-Admin"] == "true";
public string? GetHeader(string name) =>
ctx.Request.Headers.TryGetValue(name, out var value)
? value.ToString()
: null;
}
extension static
{
public static string DefaultTenantId => "public";
public static bool IsValidTenantId(string? id) =>
!string.IsNullOrWhiteSpace(id) && id.All(char.IsLetterOrDigit);
}
}
public class OrderDto
{
public List<OrderItemDto> Items { get; set; }
public string Status { get; set; }
}
public static class OrderExtensions
{
extension(OrderDto order)
{
public decimal TotalAmount =>
order.Items.Sum(i => i.Quantity * i.PricePerUnit);
public bool IsComplete =>
order.Status == "Completed";
public int TotalItems =>
order.Items.Sum(i => i.Quantity);
}
}
if (order.TotalAmount > 1000 && order.IsComplete)
{
// Do something
}
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 15,250+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 15,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.