Skip to main content

.NET Pattern

Method Extension

Extension Methods - C# Programming Guide | Microsoft Docs

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

// Define Extenion
// Extension.cs

public static ItemDto AsDto(this Item item)
{
    return new ItemDto
    {
        Id = item.Id,
        Name = item.Name,
        Price = item.Price,
        CreatedDate = item.CreatedDate
    };
}

// Controller

[HttpGet]
public IEnumerable<ItemDto> GetItems()
{
    return _itemRepository.GetItems().Select(item => item.AsDto());
}

// Same as 

[HttpGet]
public IEnumerable<ItemDto> GetItems()
{
    return _itemRepository.GetItems().Select(item => new ItemDto
    {
        Id = item.Id,
        Name = item.Name,
        Price = item.Price,
        CreatedDate = item.CreatedDate
    });
}

AutoMapper

NuGet Gallery | AutoMapper.Extensions.Microsoft.DependencyInjection 8.1.1

Clean Arch

Architecting ASP.NET Core Applications: Best Practices | Pluralsight

How to use Firebase Auth with a custom (node) backend | by Daniel Peach | ITNEXT