Facet.Mapping 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Facet.Mapping --version 2.0.0
                    
NuGet\Install-Package Facet.Mapping -Version 2.0.0
                    
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="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Facet.Mapping" Version="2.0.0" />
                    
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 2.0.0
                    
#r "nuget: Facet.Mapping, 2.0.0"
                    
#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@2.0.0
                    
#: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=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Facet.Mapping&version=2.0.0
                    
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, 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

Synchronous Mapping

  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(...).

Asynchronous Mapping

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

Hybrid Mapping

  1. Implement the IFacetMapConfigurationHybrid<TSource, TTarget> interface.
  2. Define both Map and MapAsync methods for optimal performance.

Install

dotnet add package Facet.Mapping

Examples

Basic Synchronous Mapping

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
        target.ProfilePicture = await GetProfilePictureAsync(source.Id, cancellationToken);
        
        // Async API call
        target.ReputationScore = await CalculateReputationAsync(source.Email, cancellationToken);
    }
    
    private static async Task<string> GetProfilePictureAsync(int userId, CancellationToken cancellationToken)
    {
        // Implementation details...
        await Task.Delay(100, cancellationToken); // Simulated async operation
        return $"https://api.example.com/users/{userId}/avatar";
    }
    
    private static async Task<decimal> CalculateReputationAsync(string email, CancellationToken cancellationToken)
    {
        // Implementation details...
        await Task.Delay(50, cancellationToken); // Simulated async operation
        return 4.5m;
    }
}

// Usage
var user = new User { Id = 1, Email = "john@example.com" };
var userDto = await user.ToFacetAsync<User, UserDto, UserAsyncMapper>();

Hybrid Mapping (Sync + Async)

public class ProductHybridMapper : IFacetMapConfigurationHybrid<Product, ProductDto>
{
    // Fast synchronous operations
    public static void Map(Product source, ProductDto target)
    {
        target.DisplayName = $"{source.Name} - {source.Category}";
        target.FormattedPrice = $"${source.Price:F2}";
    }

    // Expensive asynchronous operations
    public static async Task MapAsync(Product source, ProductDto target, CancellationToken cancellationToken = default)
    {
        target.AverageRating = await GetAverageRatingAsync(source.Id, cancellationToken);
        target.StockLevel = await CheckStockLevelAsync(source.Id, cancellationToken);
    }
    
    private static async Task<decimal> GetAverageRatingAsync(int productId, CancellationToken cancellationToken)
    {
        // Simulated database query
        await Task.Delay(100, cancellationToken);
        return 4.2m;
    }
    
    private static async Task<int> CheckStockLevelAsync(int productId, CancellationToken cancellationToken)
    {
        // Simulated inventory service call
        await Task.Delay(75, cancellationToken);
        return 25;
    }
}

// Usage - applies both sync and async mapping
var product = new Product { Id = 1, Name = "Laptop", Category = "Electronics", Price = 999.99m };
var productDto = await product.ToFacetHybridAsync<Product, ProductDto, ProductHybridMapper>();

Collection Mapping

// Map collections asynchronously
var users = new List<User> { /* ... */ };

// Sequential async mapping
var userDtos = await users.ToFacetsAsync<User, UserDto, UserAsyncMapper>();

// Parallel async mapping for better performance
var userDtosParallel = await users.ToFacetsParallelAsync<User, UserDto, UserAsyncMapper>(
    maxDegreeOfParallelism: 4);

API Reference

Interfaces

Interface Description
IFacetMapConfiguration<TSource, TTarget> Synchronous mapping configuration
IFacetMapConfigurationAsync<TSource, TTarget> Asynchronous mapping configuration
IFacetMapConfigurationHybrid<TSource, TTarget> Combined sync/async mapping configuration

Extension Methods

Method Description
ToFacetAsync<TSource, TTarget, TAsyncMapper>() Maps single instance with async configuration
ToFacetWithConstructorAsync<TSource, TTarget, TAsyncMapper>() Uses generated constructor + async mapping
ToFacetsAsync<TSource, TTarget, TAsyncMapper>() Maps collection sequentially with async configuration
ToFacetsParallelAsync<TSource, TTarget, TAsyncMapper>() Maps collection in parallel with async configuration
ToFacetHybridAsync<TSource, TTarget, THybridMapper>() Maps with hybrid sync/async configuration

Requirements

  • .NET 8.0+
  • Facet v1.9.0+

Performance Considerations

  • Sync mapping: Zero overhead, compile-time optimized
  • Async mapping: Use for I/O-bound operations (database, API calls)
  • Hybrid mapping: Combine both for optimal performance
  • Parallel mapping: Use for independent operations that can be parallelized

Facet.Mapping � Define less, map more efficiently.

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 was computed.  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 was computed.  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.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.5.0 0 9/4/2025
2.4.8 38 9/3/2025
2.4.7 96 9/1/2025
2.4.6 50 9/1/2025
2.4.5 148 8/30/2025
2.4.4 214 8/27/2025
2.4.3 176 8/27/2025
2.4.2 174 8/27/2025
2.4.0 175 8/26/2025
2.3.0 274 8/20/2025
2.2.0 131 8/20/2025
2.1.0 156 8/18/2025
2.0.1 481 8/5/2025
2.0.0 157 8/4/2025
1.9.3 124 7/4/2025
1.8.0 154 6/4/2025
1.7.0 166 5/6/2025
1.6.0 141 4/27/2025
1.5.0 125 4/26/2025
1.4.0 145 4/25/2025
1.3.0 174 4/24/2025
1.2.0 165 4/24/2025
1.1.1 176 4/23/2025
1.1.0 162 4/23/2025
1.0.0 277 4/23/2025

Version 2.0.0:
- Added async mapping support with IFacetMapConfigurationAsync interface
- Added hybrid sync/async mapping with IFacetMapConfigurationHybrid interface
- Added comprehensive async extension methods
- Added parallel collection mapping capabilities
- Full cancellation token support throughout