Tenon.Mapper.AutoMapper 0.0.1-alpha-202502101554

This is a prerelease version of Tenon.Mapper.AutoMapper.
dotnet add package Tenon.Mapper.AutoMapper --version 0.0.1-alpha-202502101554                
NuGet\Install-Package Tenon.Mapper.AutoMapper -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.AutoMapper" 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.AutoMapper --version 0.0.1-alpha-202502101554                
#r "nuget: Tenon.Mapper.AutoMapper, 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.AutoMapper as a Cake Addin
#addin nuget:?package=Tenon.Mapper.AutoMapper&version=0.0.1-alpha-202502101554&prerelease

// Install Tenon.Mapper.AutoMapper as a Cake Tool
#tool nuget:?package=Tenon.Mapper.AutoMapper&version=0.0.1-alpha-202502101554&prerelease                

Tenon.Mapper.AutoMapper

基于 AutoMapper 的对象映射实现,提供了简单且灵活的对象映射功能。

项目特点

  • 基于 AutoMapper 实现
  • 支持依赖注入
  • 支持配置文件扫描
  • 统一的映射接口

快速开始

  1. 安装 NuGet 包:
dotnet add package Tenon.Mapper.AutoMapper
  1. 添加服务:
// 使用类型标记扫描配置
services.AddAutoMapperSetup(typeof(Startup));

// 或使用程序集扫描配置
services.AddAutoMapperSetup(Assembly.GetExecutingAssembly());
  1. 创建映射配置:
public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<UserDto, User>()
            .ForMember(dest => dest.FullName, 
                      opt => opt.MapFrom(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 MappingProfile : Profile
{
    public MappingProfile()
    {
        // 基本映射
        CreateMap<Source, Destination>();

        // 自定义成员映射
        CreateMap<Source, Destination>()
            .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.Name))
            .ForMember(dest => dest.Age, opt => opt.Ignore());

        // 双向映射
        CreateMap<Source, Destination>().ReverseMap();
    }
}

高级配置

public class AdvancedMappingProfile : Profile
{
    public AdvancedMappingProfile()
    {
        // 条件映射
        CreateMap<Source, Destination>()
            .ForMember(dest => dest.Status,
                      opt => opt.MapFrom(src => src.IsActive ? "Active" : "Inactive"));

        // 集合映射
        CreateMap<Order, OrderDto>()
            .ForMember(dest => dest.Items,
                      opt => opt.MapFrom(src => src.OrderItems));

        // 值转换器
        CreateMap<Source, Destination>()
            .ForMember(dest => dest.Date,
                      opt => opt.ConvertUsing(new DateTimeTypeConverter()));
    }
}

依赖项

  • AutoMapper
  • AutoMapper.Collection
  • Tenon.Mapper.Abstractions
  • Microsoft.Extensions.DependencyInjection.Abstractions

项目结构

Tenon.Mapper.AutoMapper/
├── Extensions/
│   └── ServiceCollectionExtension.cs  # 服务注册扩展
├── AutoMapperObject.cs               # AutoMapper 实现
└── Tenon.Mapper.AutoMapper.csproj    # 项目文件

注意事项

  1. 性能考虑:

    • AutoMapper 会在启动时编译映射
    • 建议在应用程序启动时注册所有映射
  2. 最佳实践:

    • 将映射配置放在单独的 Profile 类中
    • 使用依赖注入管理映射器实例
    • 避免在运行时动态创建映射
  3. 调试提示:

    • 启用 AutoMapper 的调试功能来诊断映射问题
    • 使用 AssertConfigurationIsValid() 验证映射配置
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Tenon.Mapper.AutoMapper:

Package Downloads
Mortise.UiaAccessibility

UI Automation Framework

Mortise.ChromiumAccessibility

UI Automation Framework

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.1-alpha-202502101554 37 2/10/2025
0.0.1-alpha-202502101448 27 2/10/2025
0.0.1-alpha-202502101434 36 2/10/2025
0.0.1-alpha-202501130258 35 1/13/2025
0.0.1-alpha-202412311524 73 12/31/2024
0.0.1-alpha-202412061617 55 12/6/2024
0.0.1-alpha-202412051527 57 12/5/2024
0.0.1-alpha-202412051432 54 12/5/2024
0.0.1-alpha-202412041445 48 12/4/2024
0.0.1-alpha-202412021409 47 12/2/2024
0.0.1-alpha-202411301019 55 11/30/2024
0.0.1-alpha-202411170525 59 11/17/2024
0.0.1-alpha-202411161308 53 11/16/2024
0.0.1-alpha-202411131604 57 11/13/2024
0.0.1-alpha-202411111439 64 11/11/2024
0.0.1-alpha-202411051434 58 11/5/2024
0.0.1-alpha-202410281339 74 10/28/2024
0.0.1-alpha-202410131500 87 10/13/2024
0.0.1-alpha-202407261457 96 7/26/2024
0.0.1-alpha-202407261325 65 7/26/2024
0.0.1-alpha-202406271301 162 6/27/2024
0.0.1-alpha-202406251508 70 6/25/2024
0.0.1-alpha-202406251310 56 6/25/2024
0.0.1-alpha-202406141611 151 6/14/2024
0.0.1-alpha-202406141550 59 6/14/2024
0.0.1-alpha-202406121515 67 6/12/2024
0.0.1-alpha-202406061553 81 6/6/2024
0.0.1-alpha-202406041519 61 6/4/2024
0.0.1-alpha-202406011613 68 6/1/2024
0.0.1-alpha-202406011238 65 6/1/2024
0.0.1-alpha-202405311458 66 5/31/2024
0.0.1-alpha-202405291213 72 5/29/2024
0.0.1-alpha-202405190458 82 5/19/2024
0.0.1-alpha-202405161229 55 5/16/2024
0.0.1-alpha-202405141510 59 5/14/2024
0.0.1-alpha-202405101323 64 5/10/2024
0.0.1-alpha-202405081356 73 5/8/2024
0.0.1-alpha-202405021337 28 5/2/2024
0.0.1-alpha-202405021336 26 5/2/2024
0.0.1-alpha-202405020452 46 5/2/2024
0.0.1-alpha-202405011443 49 5/1/2024
0.0.1-alpha-202404291541 60 4/29/2024
0.0.1-alpha-202404281218 67 4/28/2024