Facet.Mapping 5.5.2

dotnet add package Facet.Mapping --version 5.5.2
                    
NuGet\Install-Package Facet.Mapping -Version 5.5.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Facet.Mapping" Version="5.5.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Facet.Mapping" Version="5.5.2" />
                    
Directory.Packages.props
<PackageReference Include="Facet.Mapping" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Facet.Mapping --version 5.5.2
                    
#r "nuget: Facet.Mapping, 5.5.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Facet.Mapping@5.5.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Facet.Mapping&version=5.5.2
                    
Install as a Cake Addin
#tool nuget:?package=Facet.Mapping&version=5.5.2
                    
Install as a Cake Tool

Facet.Mapping

Facet.Mapping enables advanced static mapping logic for the Facet source generator.

This package defines strongly-typed interfaces that allow you to plug in custom mapping logic between source and generated Facet types with support for both synchronous and asynchronous operations, dependency injection, all at compile time with zero runtime reflection.


What is this for?

Facet lets you define slim, redacted, or projected versions of classes using just attributes.
With Facet.Mapping, you can go further and define custom logic like combining properties, renaming, transforming types, applying conditions, or performing async operations like database lookups and API calls.


How it works

Static Mappers (No Dependencies)

  1. Implement the IFacetMapConfiguration<TSource, TTarget> interface.
  2. Define a static Map method.
  3. Point the [Facet(...)] attribute to the config class using Configuration = typeof(...).

Instance Mappers (With Dependency Injection)

  1. Implement the IFacetMapConfigurationAsyncInstance<TSource, TTarget> interface.
  2. Define an instance MapAsync method.
  3. Use the new extension methods that accept mapper instances.

Synchronous Mapping

  1. Implement IFacetMapConfiguration<TSource, TTarget> (static) or IFacetMapConfigurationInstance<TSource, TTarget> (instance).
  2. Define a Map method.

Asynchronous Mapping

  1. Implement IFacetMapConfigurationAsync<TSource, TTarget> (static) or IFacetMapConfigurationAsyncInstance<TSource, TTarget> (instance).
  2. Define a MapAsync method.
  3. Use the async extension methods to perform mapping operations.

Hybrid Mapping

  1. Implement IFacetMapConfigurationHybrid<TSource, TTarget> (static) or IFacetMapConfigurationHybridInstance<TSource, TTarget> (instance).
  2. Define both Map and MapAsync methods for optimal performance.

Install

dotnet add package Facet.Mapping

Examples

Basic Synchronous Mapping (Static)

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Id { get; set; }
}

[Facet(typeof(User), GenerateConstructor = true, Configuration = typeof(UserMapper))]
public partial class UserDto
{
    public string FullName { get; set; }
    public int Id { get; set; }
}

public class UserMapper : IFacetMapConfiguration<User, UserDto>
{
    public static void Map(User source, UserDto target)
    {
        target.FullName = $"{source.FirstName} {source.LastName}";
    }
}

Asynchronous Mapping

public class User
{
    public string Email { get; set; }
    public int Id { get; set; }
}

[Facet(typeof(User))]
public partial class UserDto
{
    public string Email { get; set; }
    public int Id { get; set; }
    public string ProfilePicture { get; set; }
    public decimal ReputationScore { get; set; }
}

public class UserAsyncMapper : IFacetMapConfigurationAsync<User, UserDto>
{
    public static async Task MapAsync(User source, UserDto target, CancellationToken cancellationToken = default)
    {
        // Async database lookup (limited - no DI)
        target.ProfilePicture = await GetProfilePictureAsync(source.Id, cancellationToken);
        
        // Async API call (limited - no DI)
        target.ReputationScore = await CalculateReputationAsync(source.Email, cancellationToken);
    }
    
    private static async Task<string> GetProfilePictureAsync(int userId, CancellationToken cancellationToken)
    {
        // Simple implementation without DI
        await Task.Delay(100, cancellationToken);
        return $"https://api.example.com/users/{userId}/avatar.jpg";
    }
    
    private static async Task<decimal> CalculateReputationAsync(string email, CancellationToken cancellationToken)
    {
        await Task.Delay(50, cancellationToken);
        return Random.Shared.Next(1, 6) + (decimal)Random.Shared.NextDouble();
    }
}

// Usage
var userDto = await user.ToFacetAsync<User, UserDto, UserAsyncMapper>();

Asynchronous Mapping with Dependency Injection

// Define your services
public interface IProfilePictureService
{
    Task<string> GetProfilePictureAsync(int userId, CancellationToken cancellationToken = default);
}

public interface IReputationService
{
    Task<decimal> CalculateReputationAsync(string email, CancellationToken cancellationToken = default);
}

// Implement services with real dependencies (DbContext, HttpClient, etc.)
public class ProfilePictureService : IProfilePictureService
{
    private readonly IDbContext _dbContext;
    private readonly ILogger<ProfilePictureService> _logger;

    public ProfilePictureService(IDbContext dbContext, ILogger<ProfilePictureService> logger)
    {
        _dbContext = dbContext;
        _logger = logger;
    }

    public async Task<string> GetProfilePictureAsync(int userId, CancellationToken cancellationToken = default)
    {
        try
        {
            var user = await _dbContext.Users.FindAsync(userId, cancellationToken);
            return user?.ProfilePictureUrl ?? "/images/default-avatar.png";
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to load profile picture for user {UserId}", userId);
            return "/images/default-avatar.png";
        }
    }
}

// Instance mapper with dependency injection
public class UserAsyncMapperWithDI : IFacetMapConfigurationAsyncInstance<User, UserDto>
{
    private readonly IProfilePictureService _profilePictureService;
    private readonly IReputationService _reputationService;

    public UserAsyncMapperWithDI(IProfilePictureService profilePictureService, IReputationService reputationService)
    {
        _profilePictureService = profilePictureService;
        _reputationService = reputationService;
    }

    public async Task MapAsync(User source, UserDto target, CancellationToken cancellationToken = default)
    {
        // Use injected services for real implementations
        target.ProfilePicture = await _profilePictureService.GetProfilePictureAsync(source.Id, cancellationToken);
        target.ReputationScore = await _reputationService.CalculateReputationAsync(source.Email, cancellationToken);
    }
}

// Register in DI container
services.AddScoped<IProfilePictureService, ProfilePictureService>();
services.AddScoped<IReputationService, ReputationService>();
services.AddScoped<UserAsyncMapperWithDI>();

// Usage with DI
public class UserController : ControllerBase
{
    private readonly UserAsyncMapperWithDI _userMapper;
    
    public UserController(UserAsyncMapperWithDI userMapper)
    {
        _userMapper = userMapper;
    }
    
    [HttpGet("{id}")]
    public async Task<UserDto> GetUser(int id)
    {
        var user = await GetUserFromDatabase(id);
        
        // NEW: Use instance mapper with injected dependencies
        return await user.ToFacetAsync(_userMapper);
    }
    
    [HttpGet]
    public async Task<List<UserDto>> GetUsers()
    {
        var users = await GetUsersFromDatabase();
        
        // NEW: Collection mapping with DI support
        return await users.ToFacetsParallelAsync(_userMapper, maxDegreeOfParallelism: 4);
    }
}

Hybrid Mapping with Dependency Injection

public class UserHybridMapperWithDI : IFacetMapConfigurationHybridInstance<User, UserDto>
{
    private readonly IProfilePictureService _profilePictureService;
    private readonly IReputationService _reputationService;

    public UserHybridMapperWithDI(IProfilePictureService profilePictureService, IReputationService reputationService)
    {
        _profilePictureService = profilePictureService;
        _reputationService = reputationService;
    }

    // Fast synchronous operations
    public void Map(User source, UserDto target)
    {
        target.FullName = $"{source.FirstName} {source.LastName}";
        target.Email = source.Email.ToLower();
    }

    // Expensive asynchronous operations with injected services
    public async Task MapAsync(User source, UserDto target, CancellationToken cancellationToken = default)
    {
        target.ProfilePicture = await _profilePictureService.GetProfilePictureAsync(source.Id, cancellationToken);
        target.ReputationScore = await _reputationService.CalculateReputationAsync(source.Email, cancellationToken);
    }
}

// Usage
var userDto = await user.ToFacetHybridAsync(hybridMapperWithDI);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Facet.Mapping:

Package Downloads
Facet.Mapping.Expressions

Expression tree transformation and mapping utilities for Facet DTOs. Transform predicates, selectors, and other expressions between source entities and their Facet projections.

Facet.Extensions.EFCore.Mapping

Advanced custom async mapper support for Facet with EF Core queries. Enables complex mappings that cannot be expressed as SQL projections.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.5.2 175 1/29/2026
5.5.1 125 1/28/2026
5.5.0 150 1/27/2026
5.4.4 121 1/27/2026
5.4.3 204 1/23/2026
5.4.2 154 1/22/2026
5.4.1 836 1/13/2026
5.4.0 128 1/12/2026
5.3.3 116 1/12/2026
5.3.2 188 1/8/2026
5.3.1 251 1/6/2026
5.3.0 329 1/2/2026
5.2.0 598 12/23/2025
5.2.0-alpha 303 12/18/2025
5.1.12 936 12/14/2025
5.1.11 696 12/9/2025
5.1.10 1,349 12/5/2025
5.1.9 803 12/3/2025
5.1.8 2,034 12/1/2025
5.1.7 586 12/1/2025
5.1.6 175 11/28/2025
5.1.5 165 11/28/2025
5.1.4 198 11/28/2025
5.1.3 192 11/28/2025
5.1.2 226 11/27/2025
5.1.1 225 11/27/2025
5.1.0 237 11/27/2025
5.0.4 1,112 11/26/2025
5.0.3 355 11/26/2025
5.0.2 269 11/26/2025
5.0.1 247 11/25/2025
5.0.0 346 11/24/2025
5.0.0-alpha5 217 11/24/2025
5.0.0-alpha4 205 11/23/2025
5.0.0-alpha3 209 11/22/2025
5.0.0-alpha2 225 11/22/2025
5.0.0-alpha 297 11/21/2025
4.4.3 426 11/21/2025
4.4.2 395 11/21/2025
4.4.1 449 11/20/2025
4.4.1-alpha4 318 11/16/2025
4.4.1-alpha3 307 11/16/2025
4.4.1-alpha2 308 11/16/2025
4.4.1-alpha 311 11/16/2025
4.4.0 500 11/15/2025
4.4.0-alpha 252 11/14/2025
4.3.3.1 286 11/14/2025
4.3.3 275 11/14/2025
4.3.2.1 264 11/14/2025
4.3.2 418 11/13/2025
4.3.1 366 11/12/2025
4.3.0 922 11/8/2025
4.3.0-alpha 182 11/7/2025
3.4.0 173 11/8/2025
3.3.0 246 11/7/2025
3.3.0-alpha.1 162 11/4/2025
3.3.0-alpha 213 11/3/2025
3.2.2 2,853 10/29/2025
3.2.1 1,071 10/27/2025
3.2.1-alpha.1 148 10/27/2025
3.2.1-alpha 201 10/26/2025
3.2.0-alpha 199 10/26/2025
3.1.14 324 10/24/2025
3.1.13 291 10/24/2025
3.1.12 541 10/21/2025
3.1.11 207 10/21/2025
3.1.5 199 10/24/2025
3.1.4 207 10/23/2025
3.1.3 205 10/23/2025
3.1.3-alpha 195 10/22/2025
3.1.2 197 10/21/2025
3.1.2-alpha 199 10/22/2025
3.1.1 213 10/21/2025
3.1.0 290 10/19/2025
3.0.0 172 10/17/2025
2.9.31 422 10/13/2025
2.9.3 373 10/8/2025
2.9.3-alpha 196 10/7/2025
2.9.2 1,439 10/6/2025
2.9.1 175 10/3/2025
2.9.0 297 10/1/2025
2.8.2 247 10/1/2025
2.8.1 1,615 9/21/2025
2.8.0 1,224 9/17/2025
2.7.0 572 9/12/2025
2.6.2 218 9/12/2025
2.6.1 370 9/10/2025
2.6.0 256 9/9/2025
2.5.0 427 9/4/2025
2.4.8 207 9/3/2025
2.4.7 449 9/1/2025
2.4.6 187 9/1/2025
2.4.5 258 8/30/2025
2.4.4 320 8/27/2025
2.4.3 231 8/27/2025
2.4.2 235 8/27/2025
2.4.0 226 8/26/2025
2.3.0 531 8/20/2025
2.2.0 191 8/20/2025
2.1.0 216 8/18/2025
2.0.1 688 8/5/2025
2.0.0 203 8/4/2025
1.9.3 177 7/4/2025
1.8.0 231 6/4/2025
1.7.0 224 5/6/2025
1.6.0 199 4/27/2025
1.5.0 189 4/26/2025
1.4.0 224 4/25/2025
1.3.0 244 4/24/2025
1.2.0 241 4/24/2025
1.1.1 243 4/23/2025
1.1.0 235 4/23/2025
1.0.0 810 4/23/2025