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.
#:package Tenon.Mapper.Mapster@0.0.1-alpha-202502241449
                    
#: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-202502241449&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Tenon.Mapper.Mapster&version=0.0.1-alpha-202502241449&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 70 2/24/2025
0.0.1-alpha-202502101554 87 2/10/2025
0.0.1-alpha-202502101448 77 2/10/2025
0.0.1-alpha-202502101434 73 2/10/2025
0.0.1-alpha-202501130258 69 1/13/2025
0.0.1-alpha-202412311524 90 12/31/2024
0.0.1-alpha-202412061617 74 12/6/2024
0.0.1-alpha-202412051527 72 12/5/2024
0.0.1-alpha-202412051432 65 12/5/2024
0.0.1-alpha-202412041445 68 12/4/2024
0.0.1-alpha-202412021409 62 12/2/2024
0.0.1-alpha-202411301019 72 11/30/2024
0.0.1-alpha-202411170525 68 11/17/2024
0.0.1-alpha-202411161308 61 11/16/2024
0.0.1-alpha-202411131604 67 11/13/2024
0.0.1-alpha-202411111439 82 11/11/2024
0.0.1-alpha-202411051434 68 11/5/2024
0.0.1-alpha-202410281339 73 10/28/2024
0.0.1-alpha-202410131500 71 10/13/2024
0.0.1-alpha-202407261457 85 7/26/2024
0.0.1-alpha-202407261325 71 7/26/2024
0.0.1-alpha-202406271301 74 6/27/2024
0.0.1-alpha-202406251508 75 6/25/2024
0.0.1-alpha-202406251310 69 6/25/2024
0.0.1-alpha-202406141611 71 6/14/2024
0.0.1-alpha-202406141550 66 6/14/2024
0.0.1-alpha-202406121515 72 6/12/2024
0.0.1-alpha-202406061553 80 6/6/2024
0.0.1-alpha-202406041519 72 6/4/2024
0.0.1-alpha-202406011613 78 6/1/2024
0.0.1-alpha-202406011238 70 6/1/2024
0.0.1-alpha-202405311458 74 5/31/2024
0.0.1-alpha-202405291213 83 5/29/2024
0.0.1-alpha-202405190458 76 5/19/2024
0.0.1-alpha-202405161229 72 5/16/2024
0.0.1-alpha-202405141510 73 5/14/2024
0.0.1-alpha-202405101323 78 5/10/2024
0.0.1-alpha-202405081356 79 5/8/2024
0.0.1-alpha-202405021337 45 5/2/2024
0.0.1-alpha-202405021336 48 5/2/2024
0.0.1-alpha-202405020452 55 5/2/2024
0.0.1-alpha-202405011443 60 5/1/2024
0.0.1-alpha-202404291541 75 4/29/2024
0.0.1-alpha-202404281218 71 4/28/2024