Scripts2026年5月23日·1 分钟阅读

AutoMapper — Convention-Based Object Mapping for .NET

A .NET library that eliminates boilerplate object-to-object mapping code by using conventions and configuration profiles.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
AutoMapper Overview
直接安装命令
npx -y tokrepo@latest install 8971216a-56c4-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

AutoMapper is a convention-based object mapper for .NET that eliminates repetitive property-by-property copying code. It maps between objects by matching property names automatically and supports custom projections, flattening, and value transformations through configuration profiles.

What AutoMapper Does

  • Maps properties between source and destination types by name convention
  • Flattens nested object hierarchies into flat DTOs
  • Supports custom value resolvers and type converters
  • Projects mappings into LINQ expressions for efficient database queries
  • Validates mapping configurations at startup to catch errors early

Architecture Overview

AutoMapper builds an execution plan for each source-destination type pair during configuration. It compiles these plans into expression trees for fast execution. Mapping profiles group related configurations, and the MapperConfiguration object is created once at startup. For DI scenarios, IMapper is registered as a singleton that reuses the compiled plans.

Self-Hosting & Configuration

  • Install via NuGet: dotnet add package AutoMapper
  • For DI: dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection
  • Create Profile classes to organize mapping configurations
  • Register with builder.Services.AddAutoMapper(typeof(Program).Assembly)
  • Call config.AssertConfigurationIsValid() in tests to verify all mappings

Key Features

  • Convention-based mapping with zero configuration for matching property names
  • Projection via ProjectTo<T>() that translates to SQL through EF Core
  • Before/after map actions for side effects during mapping
  • Conditional mapping to skip properties based on runtime logic
  • Reverse mapping support with ReverseMap()

Comparison with Similar Tools

  • Mapster — code-generation-based mapper; faster runtime performance, less mature ecosystem
  • Manual mapping — explicit property assignment; no dependencies, but verbose and error-prone
  • Mapperly — source-generator-based mapper; compile-time safety, zero runtime overhead
  • TinyMapper — lightweight mapper using IL emission; fewer features, smaller footprint
  • ExpressMapper — convention-based alternative; smaller community and less active development

FAQ

Q: Does AutoMapper affect performance? A: The initial configuration has a cost, but subsequent mapping operations use compiled expression trees and are fast. For hot paths, consider source-generator alternatives like Mapperly.

Q: Can I use AutoMapper with Entity Framework Core? A: Yes. Use ProjectTo<TDto>(mapper.ConfigurationProvider) on IQueryable to generate efficient SQL that only selects mapped columns.

Q: How do I handle complex nested mappings? A: AutoMapper flattens nested properties by convention (e.g., Order.Customer.Name maps to OrderDto.CustomerName). For non-conventional mappings, use ForMember with MapFrom.

Q: What is the licensing model? A: AutoMapper v12 and earlier are MIT-licensed. Version 13+ uses a dual license with a free Community edition for individuals and non-commercial use, and a commercial license for enterprises.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产