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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Tenon.Mapper.AutoMapper
基于 AutoMapper 的对象映射实现,提供了简单且灵活的对象映射功能。
项目特点
- 基于 AutoMapper 实现
- 支持依赖注入
- 支持配置文件扫描
- 统一的映射接口
快速开始
- 安装 NuGet 包:
dotnet add package Tenon.Mapper.AutoMapper
- 添加服务:
// 使用类型标记扫描配置
services.AddAutoMapperSetup(typeof(Startup));
// 或使用程序集扫描配置
services.AddAutoMapperSetup(Assembly.GetExecutingAssembly());
- 创建映射配置:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<UserDto, User>()
.ForMember(dest => dest.FullName,
opt => opt.MapFrom(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 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 # 项目文件
注意事项
性能考虑:
- AutoMapper 会在启动时编译映射
- 建议在应用程序启动时注册所有映射
最佳实践:
- 将映射配置放在单独的 Profile 类中
- 使用依赖注入管理映射器实例
- 避免在运行时动态创建映射
调试提示:
- 启用 AutoMapper 的调试功能来诊断映射问题
- 使用
AssertConfigurationIsValid()
验证映射配置
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
- AutoMapper (>= 13.0.1)
- AutoMapper.Collection (>= 10.0.0)
- 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 (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.