Muonroi.Mapping.Abstractions 2.0.2

dotnet add package Muonroi.Mapping.Abstractions --version 2.0.2
                    
NuGet\Install-Package Muonroi.Mapping.Abstractions -Version 2.0.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="Muonroi.Mapping.Abstractions" Version="2.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Muonroi.Mapping.Abstractions" Version="2.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Muonroi.Mapping.Abstractions" />
                    
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 Muonroi.Mapping.Abstractions --version 2.0.2
                    
#r "nuget: Muonroi.Mapping.Abstractions, 2.0.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 Muonroi.Mapping.Abstractions@2.0.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=Muonroi.Mapping.Abstractions&version=2.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Muonroi.Mapping.Abstractions&version=2.0.2
                    
Install as a Cake Tool

Muonroi.Mapping.Abstractions

Entity-DTO mapping contracts with template method pattern for schema-divergent multi-tenancy.

NuGet License

Overview

The Muonroi.Mapping.Abstractions package provides foundational interfaces and abstract base classes for mapping between domain entities and Data Transfer Objects (DTOs). Its primary focus is addressing the complexity of mapping in multi-tenant environments where core entity schemas remain static, but specific tenants (sites) require extended, schema-divergent fields.

Rather than relying purely on reflection or complex configuration profiles, this package encourages an explicit, code-first mapping strategy using the Template Method Pattern. By separating core mappings from site-specific mappings, developers can construct extensible mappers that adapt to varying data shapes across deployments without polluting the core mapping logic.

Use this package when you are building a product that allows tenant-specific data model extensions, and you need a standardized, interface-driven way to handle DTO transformations across the system.

Features

  • Standardized Interfaces: IEntityMapper<TEntity, TDto> establishes a uniform contract for mapping entities to DTOs, DTOs to entities, and applying updates to existing entities.
  • Template Method Implementation: EntityMapperBase<TEntity, TDto> orchestrates the mapping process, defining a strict lifecycle that guarantees both core and extended fields are processed.
  • Extensibility Hooks: Virtual methods allow derived classes to map site-specific, custom fields only when necessary, leaving the core mapping logic untouched.
  • Schema-Divergent Support: Perfect for environments where a baseline "Core" system is deployed, but individual clients have distinct columns or JSON extension bags added to their tables.

Installation

dotnet add package Muonroi.Mapping.Abstractions

Quick Start

Implementing a Basic Mapper

Inherit from EntityMapperBase and implement the abstract core mapping methods.

using Muonroi.Mapping.Abstractions;

public class UserMapper : EntityMapperBase<UserEntity, UserDto>
{
    protected override void MapCoreToDto(UserEntity entity, UserDto dto)
    {
        dto.Id = entity.Id;
        dto.Username = entity.Username;
        dto.Email = entity.Email;
    }

    protected override void MapCoreToEntity(UserDto dto, UserEntity entity)
    {
        entity.Username = dto.Username;
        entity.Email = dto.Email;
        // Notice we typically don't map IDs back to entities on updates
    }
}

Implementing a Schema-Divergent Mapper

If a specific tenant deployment requires mapping custom fields (e.g., a custom loyalty tier), inherit from your base mapper or override the virtual methods.

public class TenantAUserMapper : UserMapper
{
    // Core mappings are handled by the base class.
    
    protected override void MapSiteSpecificToDto(UserEntity entity, UserDto dto)
    {
        // Assuming Tenant A added a 'LoyaltyTier' column to their schema
        if (entity.CustomFields.TryGetValue("LoyaltyTier", out var tier))
        {
            dto.ExtendedProperties["LoyaltyTier"] = tier;
        }
    }

    protected override void MapSiteSpecificToEntity(UserDto dto, UserEntity entity)
    {
        if (dto.ExtendedProperties.TryGetValue("LoyaltyTier", out var tier))
        {
            entity.CustomFields["LoyaltyTier"] = tier;
        }
    }
}

Usage

IEntityMapper<UserEntity, UserDto> mapper = new TenantAUserMapper();

// Creating a DTO (Runs Core -> SiteSpecific)
UserDto dto = mapper.ToDto(myUserEntity);

// Applying updates to an existing entity (Runs Core -> SiteSpecific)
mapper.ApplyUpdate(myUserEntity, updateDto);

API Reference

IEntityMapper<TEntity, TDto>

The core interface that must be resolved from the DI container.

  • TDto ToDto(TEntity entity): Creates and populates a new DTO from the entity.
  • TEntity ToEntity(TDto dto): Creates and populates a new Entity from the DTO.
  • void ApplyUpdate(TEntity entity, TDto dto): Updates an existing entity instance with values from the DTO.

EntityMapperBase<TEntity, TDto>

The abstract base class implementing the template method pattern.

  • abstract void MapCoreToDto(...): Maps the baseline schema from Entity to DTO.
  • abstract void MapCoreToEntity(...): Maps the baseline schema from DTO to Entity.
  • virtual void MapSiteSpecificToDto(...): Maps tenant-specific fields from Entity to DTO (default: no-op).
  • virtual void MapSiteSpecificToEntity(...): Maps tenant-specific fields from DTO to Entity (default: no-op).

Integration

Muonroi.Mapping.Abstractions connects directly to:

  • Muonroi.Data.Abstractions: Can be used alongside repository abstractions to automatically map results before returning them from read-side repositories.

Ecosystem Combinations

Great standalone. Becomes significantly more powerful when combined.

+ Mapper → Core Mapping Engine

EntityMapperBase template uses Mapper for the actual mapping work.

+ Tenancy.SiteProfile → Per-Site Overrides

Per-site overrides for specific tenants:

class TenantAMapper : EntityMapperBase<Order, OrderDto>

Full Mapping Stack

builder.Services
    .AddMappingAbstractions(config)
    .AddSiteProfiles(config);

Samples

See the working example in Quickstart.Mapping.Abstractions.

License

Apache 2.0 — see LICENSE-APACHE.

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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Muonroi.Mapping.Abstractions:

Package Downloads
Muonroi.Services

Generic EF Core service base with virtual hook methods for schema-divergent multi-tenancy. Core provides CRUD; site overrides hooks. This is an implementation layer (couples to DbContext by design) — not a vendor-neutral contract, hence not an .Abstractions assembly.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.2 95 8/26/2026
2.0.1 100 8/26/2026
2.0.0 114 8/14/2026