Tenon.Mapper.Mapster 0.0.1-alpha-202502101554

This is a prerelease version of Tenon.Mapper.Mapster.
dotnet add package Tenon.Mapper.Mapster --version 0.0.1-alpha-202502101554                
NuGet\Install-Package Tenon.Mapper.Mapster -Version 0.0.1-alpha-202502101554                
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="Tenon.Mapper.Mapster" Version="0.0.1-alpha-202502101554" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tenon.Mapper.Mapster --version 0.0.1-alpha-202502101554                
#r "nuget: Tenon.Mapper.Mapster, 0.0.1-alpha-202502101554"                
#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.
// Install Tenon.Mapper.Mapster as a Cake Addin
#addin nuget:?package=Tenon.Mapper.Mapster&version=0.0.1-alpha-202502101554&prerelease

// Install Tenon.Mapper.Mapster as a Cake Tool
#tool nuget:?package=Tenon.Mapper.Mapster&version=0.0.1-alpha-202502101554&prerelease                

Tenon.Mapper.Mapster

基于 Mapster 的对象映射实现,提供了高性能的对象映射功能。

项目特点

  • 基于 Mapster 实现
  • 超高性能
  • 支持依赖注入
  • 支持编译时类型映射
  • 统一的映射接口

快速开始

  1. 安装 NuGet 包:
dotnet add package Tenon.Mapper.Mapster
  1. 添加服务:
// 使用程序集扫描配置
services.AddMapsterSetup(Assembly.GetExecutingAssembly());
  1. 配置映射规则:
public class MappingRegister : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<UserDto, User>()
            .Map(dest => dest.FullName, 
                 src => $"{src.FirstName} {src.LastName}");
    }
}
  1. 使用映射服务:
public class UserService
{
    private readonly IObjectMapper _mapper;

    public UserService(IObjectMapper mapper)
    {
        _mapper = mapper;
    }

    public User MapToUser(UserDto dto)
    {
        return _mapper.Map<UserDto, User>(dto);
    }

    public List<User> MapToUsers(List<UserDto> dtos)
    {
        return _mapper.Map<List<UserDto>, List<User>>(dtos);
    }
}

配置说明

基本配置

public class BasicMappingRegister : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        // 基本映射
        config.NewConfig<Source, Destination>();

        // 自定义成员映射
        config.NewConfig<Source, Destination>()
            .Map(dest => dest.FullName, src => src.Name)
            .Ignore(dest => dest.Age);

        // 双向映射
        config.NewConfig<Source, Destination>()
            .TwoWays();
    }
}

高级配置

public class AdvancedMappingRegister : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        // 条件映射
        config.NewConfig<Source, Destination>()
            .Map(dest => dest.Status, 
                 src => src.IsActive ? "Active" : "Inactive");

        // 集合映射
        config.NewConfig<Order, OrderDto>()
            .Map(dest => dest.Items, src => src.OrderItems);

        // 自定义转换
        config.NewConfig<Source, Destination>()
            .MapWith(src => new Destination 
            { 
                Date = ConvertDate(src.Date) 
            });
    }
}

依赖项

  • Mapster
  • Mapster.DependencyInjection
  • Tenon.Mapper.Abstractions
  • Microsoft.Extensions.DependencyInjection.Abstractions

项目结构

Tenon.Mapper.Mapster/
├── Extensions/
│   └── ServiceCollectionExtension.cs  # 服务注册扩展
├── MapsterObject.cs                  # Mapster 实现
└── Tenon.Mapper.Mapster.csproj       # 项目文件

注意事项

  1. 性能优势:

    • Mapster 比 AutoMapper 性能更好
    • 支持编译时生成映射代码
    • 适合高性能要求的场景
  2. 最佳实践:

    • 使用 IRegister 接口组织映射配置
    • 利用编译时类型检查
    • 使用依赖注入管理映射器实例
  3. 编译优化:

    • 使用 Mapster.Tool 生成编译时映射代码
    • 配置 Source Generator 提升性能
    • 利用 ValueTask 支持异步映射
Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
0.0.1-alpha-202502101554 35 2/10/2025
0.0.1-alpha-202502101448 33 2/10/2025
0.0.1-alpha-202502101434 29 2/10/2025
0.0.1-alpha-202501130258 37 1/13/2025
0.0.1-alpha-202412311524 71 12/31/2024
0.0.1-alpha-202412061617 56 12/6/2024
0.0.1-alpha-202412051527 56 12/5/2024
0.0.1-alpha-202412051432 50 12/5/2024
0.0.1-alpha-202412041445 51 12/4/2024
0.0.1-alpha-202412021409 47 12/2/2024
0.0.1-alpha-202411301019 58 11/30/2024
0.0.1-alpha-202411170525 54 11/17/2024
0.0.1-alpha-202411161308 46 11/16/2024
0.0.1-alpha-202411131604 52 11/13/2024
0.0.1-alpha-202411111439 69 11/11/2024
0.0.1-alpha-202411051434 54 11/5/2024
0.0.1-alpha-202410281339 58 10/28/2024
0.0.1-alpha-202410131500 59 10/13/2024
0.0.1-alpha-202407261457 71 7/26/2024
0.0.1-alpha-202407261325 60 7/26/2024
0.0.1-alpha-202406271301 60 6/27/2024
0.0.1-alpha-202406251508 60 6/25/2024
0.0.1-alpha-202406251310 55 6/25/2024
0.0.1-alpha-202406141611 61 6/14/2024
0.0.1-alpha-202406141550 57 6/14/2024
0.0.1-alpha-202406121515 59 6/12/2024
0.0.1-alpha-202406061553 69 6/6/2024
0.0.1-alpha-202406041519 60 6/4/2024
0.0.1-alpha-202406011613 66 6/1/2024
0.0.1-alpha-202406011238 60 6/1/2024
0.0.1-alpha-202405311458 62 5/31/2024
0.0.1-alpha-202405291213 70 5/29/2024
0.0.1-alpha-202405190458 63 5/19/2024
0.0.1-alpha-202405161229 60 5/16/2024
0.0.1-alpha-202405141510 60 5/14/2024
0.0.1-alpha-202405101323 63 5/10/2024
0.0.1-alpha-202405081356 69 5/8/2024
0.0.1-alpha-202405021337 29 5/2/2024
0.0.1-alpha-202405021336 30 5/2/2024
0.0.1-alpha-202405020452 45 5/2/2024
0.0.1-alpha-202405011443 48 5/1/2024
0.0.1-alpha-202404291541 63 4/29/2024
0.0.1-alpha-202404281218 61 4/28/2024