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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Tenon.Mapper.Mapster
基于 Mapster 的对象映射实现,提供了高性能的对象映射功能。
项目特点
- 基于 Mapster 实现
- 超高性能
- 支持依赖注入
- 支持编译时类型映射
- 统一的映射接口
快速开始
- 安装 NuGet 包:
dotnet add package Tenon.Mapper.Mapster
- 添加服务:
// 使用程序集扫描配置
services.AddMapsterSetup(Assembly.GetExecutingAssembly());
- 配置映射规则:
public class MappingRegister : IRegister
{
public void Register(TypeAdapterConfig config)
{
config.NewConfig<UserDto, User>()
.Map(dest => dest.FullName,
src => $"{src.FirstName} {src.LastName}");
}
}
- 使用映射服务:
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 # 项目文件
注意事项
性能优势:
- Mapster 比 AutoMapper 性能更好
- 支持编译时生成映射代码
- 适合高性能要求的场景
最佳实践:
- 使用 IRegister 接口组织映射配置
- 利用编译时类型检查
- 使用依赖注入管理映射器实例
编译优化:
- 使用 Mapster.Tool 生成编译时映射代码
- 配置 Source Generator 提升性能
- 利用 ValueTask 支持异步映射
Product | Versions 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.
-
net9.0
- Mapster (>= 7.4.0)
- Mapster.DependencyInjection (>= 1.0.1)
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Tenon.Mapper.Abstractions (>= 0.0.1-alpha-202502101554)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.