Tenon.Mapper.Mapster 0.0.1-alpha-202502241449

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

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

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-202502241449 62 2/24/2025
0.0.1-alpha-202502101554 73 2/10/2025
0.0.1-alpha-202502101448 70 2/10/2025
0.0.1-alpha-202502101434 65 2/10/2025
0.0.1-alpha-202501130258 56 1/13/2025
0.0.1-alpha-202412311524 81 12/31/2024
0.0.1-alpha-202412061617 64 12/6/2024
0.0.1-alpha-202412051527 63 12/5/2024
0.0.1-alpha-202412051432 56 12/5/2024
0.0.1-alpha-202412041445 58 12/4/2024
0.0.1-alpha-202412021409 54 12/2/2024
0.0.1-alpha-202411301019 64 11/30/2024
0.0.1-alpha-202411170525 59 11/17/2024
0.0.1-alpha-202411161308 54 11/16/2024
0.0.1-alpha-202411131604 58 11/13/2024
0.0.1-alpha-202411111439 73 11/11/2024
0.0.1-alpha-202411051434 57 11/5/2024
0.0.1-alpha-202410281339 64 10/28/2024
0.0.1-alpha-202410131500 63 10/13/2024
0.0.1-alpha-202407261457 75 7/26/2024
0.0.1-alpha-202407261325 65 7/26/2024
0.0.1-alpha-202406271301 66 6/27/2024
0.0.1-alpha-202406251508 65 6/25/2024
0.0.1-alpha-202406251310 59 6/25/2024
0.0.1-alpha-202406141611 64 6/14/2024
0.0.1-alpha-202406141550 60 6/14/2024
0.0.1-alpha-202406121515 62 6/12/2024
0.0.1-alpha-202406061553 73 6/6/2024
0.0.1-alpha-202406041519 63 6/4/2024
0.0.1-alpha-202406011613 70 6/1/2024
0.0.1-alpha-202406011238 64 6/1/2024
0.0.1-alpha-202405311458 67 5/31/2024
0.0.1-alpha-202405291213 74 5/29/2024
0.0.1-alpha-202405190458 66 5/19/2024
0.0.1-alpha-202405161229 63 5/16/2024
0.0.1-alpha-202405141510 64 5/14/2024
0.0.1-alpha-202405101323 66 5/10/2024
0.0.1-alpha-202405081356 74 5/8/2024
0.0.1-alpha-202405021337 34 5/2/2024
0.0.1-alpha-202405021336 36 5/2/2024
0.0.1-alpha-202405020452 48 5/2/2024
0.0.1-alpha-202405011443 51 5/1/2024
0.0.1-alpha-202404291541 66 4/29/2024
0.0.1-alpha-202404281218 65 4/28/2024