Shipeng.Mapster 4.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Shipeng.Mapster --version 4.1.0
                    
NuGet\Install-Package Shipeng.Mapster -Version 4.1.0
                    
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="Shipeng.Mapster" Version="4.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shipeng.Mapster" Version="4.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Shipeng.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 Shipeng.Mapster --version 4.1.0
                    
#r "nuget: Shipeng.Mapster, 4.1.0"
                    
#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 Shipeng.Mapster@4.1.0
                    
#: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=Shipeng.Mapster&version=4.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Shipeng.Mapster&version=4.1.0
                    
Install as a Cake Tool

Shipeng.Mapster

Shipeng.Mapster 是基于 Mapster 的对象映射集成包,提供对象映射工具和 DI 注册扩展。它适合 DTO、ViewModel、Command、Entity 之间的转换,避免每个项目重复配置 Mapster。

安装

dotnet add package Shipeng.Mapster

适用场景

  • Entity 转 DTO、DTO 转 ViewModel、Command 转 Entity。
  • 希望通过 DI 使用统一的对象映射服务。
  • 多项目需要复用 Mapster 扫描和配置方式。

主要类型

  • MapperTool:轻量反射映射辅助工具,适合同名同类型属性之间的快速复制;内部缓存属性映射关系,避免每次映射重复反射扫描。
  • ObjectMapperServiceCollectionExtensions:Mapster 服务注册扩展入口,扩展方法为 AddObjectMapper(this IServiceCollection services, params Assembly[] assemblies)
  • MapsterMapper.IMapper:调用 AddObjectMapper 后可从 DI 容器注入使用的 Mapster 映射服务。

基本用法

using MapsterMapper;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// 注册 Mapster 映射服务;assemblies 用于扫描实现 Mapster.IRegister 的映射配置。
builder.Services.AddObjectMapper(typeof(Program).Assembly);

var app = builder.Build();
app.Run();

在业务服务中注入 IMapper

using MapsterMapper;

public sealed class UserAppService
{
    private readonly IMapper _mapper;

    public UserAppService(IMapper mapper)
    {
        _mapper = mapper;
    }

    public UserDto ToDto(UserEntity entity)
    {
        return _mapper.Map<UserDto>(entity);
    }
}

如果只是做同名同类型属性复制,也可以直接使用 MapperTool

using Shipeng.Foundation.Mapster;

UserDto dto = entity.Mapper<UserDto, UserEntity>();
entity.Mapper(existingDto);
List<UserDto> dtoList = entities.ListToList<UserDto, UserEntity>();

使用建议

  • 简单字段名一致的映射可直接使用默认约定。
  • 字段名不一致、需要格式化、需要忽略敏感字段时,应显式配置映射规则。
  • 不要在循环中重复创建映射配置,配置应在应用启动时完成。
  • 大批量映射要注意对象分配成本,必要时使用投影查询减少内存压力。
  • MapperTool 只复制公开实例属性,且要求属性名忽略大小写后相同、属性类型完全相同;索引器、只读属性、ErrorItem 不会被复制。

API Reference / API 索引

完整公开类型、方法、属性签名请查看 API_REFERENCE.md。该文件从源码 public 声明生成,用于补充 README 中的场景说明和示例。

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 Shipeng.Mapster:

Package Downloads
Shipeng.Data.Hybrid

Hybrid MongoDB and SqlSugar paging helper package for read-model scenarios that query MongoDB first and fall back to SQL data when needed.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.1.1 134 6/30/2026
4.1.0 143 6/30/2026

4.1.0: Split the original monolithic foundation library into focused Shipeng.* NuGet packages. Consumers can reference authentication, MongoDB, SqlSugar, EF Core, database index, third-party integration, logging, mapping, payment, and hybrid paging capabilities on demand.