Tenon.Mapper.Mapster 0.0.1-alpha-202502101554

This is a prerelease version of Tenon.Mapper.Mapster.
There is a newer prerelease version of this package available.
See the version list below for details.
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.
<PackageVersion Include="Tenon.Mapper.Mapster" Version="0.0.1-alpha-202502101554" />
                    
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-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.
#:package Tenon.Mapper.Mapster@0.0.1-alpha-202502101554
                    
#: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=Tenon.Mapper.Mapster&version=0.0.1-alpha-202502101554&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Tenon.Mapper.Mapster&version=0.0.1-alpha-202502101554&prerelease
                    
Install 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 73 2/24/2025
0.0.1-alpha-202502101554 89 2/10/2025
0.0.1-alpha-202502101448 81 2/10/2025
0.0.1-alpha-202502101434 76 2/10/2025
0.0.1-alpha-202501130258 72 1/13/2025
0.0.1-alpha-202412311524 93 12/31/2024
0.0.1-alpha-202412061617 77 12/6/2024
0.0.1-alpha-202412051527 75 12/5/2024
0.0.1-alpha-202412051432 68 12/5/2024
0.0.1-alpha-202412041445 71 12/4/2024
0.0.1-alpha-202412021409 65 12/2/2024
0.0.1-alpha-202411301019 76 11/30/2024
0.0.1-alpha-202411170525 71 11/17/2024
0.0.1-alpha-202411161308 64 11/16/2024
0.0.1-alpha-202411131604 70 11/13/2024
0.0.1-alpha-202411111439 85 11/11/2024
0.0.1-alpha-202411051434 71 11/5/2024
0.0.1-alpha-202410281339 77 10/28/2024
0.0.1-alpha-202410131500 74 10/13/2024
0.0.1-alpha-202407261457 88 7/26/2024
0.0.1-alpha-202407261325 74 7/26/2024
0.0.1-alpha-202406271301 77 6/27/2024
0.0.1-alpha-202406251508 78 6/25/2024
0.0.1-alpha-202406251310 72 6/25/2024
0.0.1-alpha-202406141611 74 6/14/2024
0.0.1-alpha-202406141550 69 6/14/2024
0.0.1-alpha-202406121515 75 6/12/2024
0.0.1-alpha-202406061553 83 6/6/2024
0.0.1-alpha-202406041519 75 6/4/2024
0.0.1-alpha-202406011613 81 6/1/2024
0.0.1-alpha-202406011238 73 6/1/2024
0.0.1-alpha-202405311458 77 5/31/2024
0.0.1-alpha-202405291213 86 5/29/2024
0.0.1-alpha-202405190458 79 5/19/2024
0.0.1-alpha-202405161229 75 5/16/2024
0.0.1-alpha-202405141510 76 5/14/2024
0.0.1-alpha-202405101323 82 5/10/2024
0.0.1-alpha-202405081356 82 5/8/2024
0.0.1-alpha-202405021337 48 5/2/2024
0.0.1-alpha-202405021336 51 5/2/2024
0.0.1-alpha-202405020452 58 5/2/2024
0.0.1-alpha-202405011443 63 5/1/2024
0.0.1-alpha-202404291541 78 4/29/2024
0.0.1-alpha-202404281218 75 4/28/2024