SyZero.AutoMapper
1.1.4-dev.1
This is a prerelease version of SyZero.AutoMapper.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package SyZero.AutoMapper --version 1.1.4-dev.1
NuGet\Install-Package SyZero.AutoMapper -Version 1.1.4-dev.1
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="SyZero.AutoMapper" Version="1.1.4-dev.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.AutoMapper" Version="1.1.4-dev.1" />
<PackageReference Include="SyZero.AutoMapper" />
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 SyZero.AutoMapper --version 1.1.4-dev.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SyZero.AutoMapper, 1.1.4-dev.1"
#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 SyZero.AutoMapper@1.1.4-dev.1
#: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=SyZero.AutoMapper&version=1.1.4-dev.1&prerelease
#tool nuget:?package=SyZero.AutoMapper&version=1.1.4-dev.1&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SyZero.AutoMapper
基于 AutoMapper 的对象映射组件,提供简洁的对象转换功能。
📦 安装
dotnet add package SyZero.AutoMapper
✨ 特性
- 🚀 自动配置 - 自动扫描程序集中的映射配置
- 🎯 简洁 API - 通过
IObjectMapper接口进行对象转换 - ⚡ 高性能 - 基于 AutoMapper,编译时生成映射代码
- 🔧 灵活配置 - 支持自定义映射规则
🚀 快速开始
1. 注册服务
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加 AutoMapper 服务
builder.Services.AddSyZeroAutoMapper();
var app = builder.Build();
app.Run();
2. 定义映射配置
创建一个继承自 Profile 的类来定义映射规则:
using AutoMapper;
public class UserProfile : Profile
{
public UserProfile()
{
// 简单映射
CreateMap<User, UserDto>();
// 反向映射
CreateMap<User, UserDto>().ReverseMap();
// 自定义映射
CreateMap<User, UserDto>()
.ForMember(dest => dest.FullName,
opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"));
}
}
3. 使用 IObjectMapper
public class UserService
{
private readonly IObjectMapper _objectMapper;
public UserService(IObjectMapper objectMapper)
{
_objectMapper = objectMapper;
}
public UserDto GetUserDto(User user)
{
// 对象转换
return _objectMapper.Map<UserDto>(user);
}
public void UpdateUser(User user, UserDto dto)
{
// 更新已有对象
_objectMapper.Map(dto, user);
}
}
📖 API 说明
IObjectMapper 接口
| 方法 | 说明 |
|---|---|
Map<TDestination>(object source) |
将源对象映射为目标类型 |
Map<TSource, TDestination>(TSource source, TDestination destination) |
将源对象映射到已有的目标对象 |
使用示例
// 单个对象映射
var userDto = _objectMapper.Map<UserDto>(user);
// 集合映射
var userDtos = _objectMapper.Map<List<UserDto>>(users);
// 更新已有对象
_objectMapper.Map(sourceDto, existingEntity);
🔧 高级配置
自定义映射规则
public class OrderProfile : Profile
{
public OrderProfile()
{
CreateMap<Order, OrderDto>()
// 忽略某个属性
.ForMember(dest => dest.InternalId, opt => opt.Ignore())
// 条件映射
.ForMember(dest => dest.Status, opt => opt.Condition(src => src.IsActive))
// 值转换
.ForMember(dest => dest.TotalPrice,
opt => opt.MapFrom(src => src.Items.Sum(i => i.Price)))
// 空值处理
.ForMember(dest => dest.Description,
opt => opt.NullSubstitute("暂无描述"));
}
}
嵌套对象映射
public class CustomerProfile : Profile
{
public CustomerProfile()
{
CreateMap<Customer, CustomerDto>();
CreateMap<Address, AddressDto>();
// 嵌套对象会自动映射
// Customer.Address -> CustomerDto.Address
}
}
集合映射
public class ProductProfile : Profile
{
public ProductProfile()
{
CreateMap<Product, ProductDto>();
// 集合会自动映射
// List<Product> -> List<ProductDto>
}
}
📁 项目结构
SyZero.AutoMapper/
├── ObjectMapper.cs # IObjectMapper 实现
└── SyZeroAutoMapperExtension.cs # 依赖注入扩展方法
🔗 与其他组件集成
在应用服务中使用
public class ProductAppService : IProductAppService
{
private readonly IObjectMapper _objectMapper;
private readonly IRepository<Product> _repository;
public ProductAppService(
IObjectMapper objectMapper,
IRepository<Product> repository)
{
_objectMapper = objectMapper;
_repository = repository;
}
public async Task<ProductDto> GetAsync(long id)
{
var product = await _repository.GetAsync(id);
return _objectMapper.Map<ProductDto>(product);
}
public async Task<ProductDto> CreateAsync(CreateProductDto input)
{
var product = _objectMapper.Map<Product>(input);
await _repository.InsertAsync(product);
return _objectMapper.Map<ProductDto>(product);
}
}
⚠️ 注意事项
- 映射配置类 - 确保映射配置类(Profile)在被扫描的程序集中
- 循环引用 - 避免在映射配置中产生循环引用
- 性能优化 - 对于大量数据映射,考虑使用
ProjectTo进行查询优化
📄 许可证
MIT License - 详见 LICENSE
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. net10.0 was computed. 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. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- AutoMapper (>= 9.0.0)
- AutoMapper.Collection (>= 6.0.0)
- AutoMapper.Extensions.Microsoft.DependencyInjection (>= 7.0.0)
- SyZero (>= 1.1.4-dev.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.4 | 37 | 1/2/2026 |
| 1.1.4-dev.2 | 41 | 1/2/2026 |
| 1.1.4-dev.1 | 42 | 12/30/2025 |
| 1.1.3 | 79 | 12/30/2025 |
| 1.1.3-dev.6 | 40 | 12/30/2025 |
| 1.1.3-dev.3 | 101 | 1/19/2024 |
| 1.1.3-dev.2 | 170 | 11/3/2023 |
| 1.1.3-dev.1 | 181 | 3/21/2023 |
| 1.1.2 | 373 | 3/15/2023 |
| 1.1.2-dev.108.29344 | 183 | 3/15/2023 |
| 1.1.2-dev.108.28054 | 173 | 3/15/2023 |
| 1.1.2-dev.108.27487 | 178 | 3/15/2023 |
| 1.1.1 | 312 | 3/15/2023 |
| 1.1.1-dev.108.14980 | 181 | 3/15/2023 |
| 1.1.1-dev.108.13289 | 176 | 3/15/2023 |
| 1.1.1-dev.107.27144 | 174 | 3/14/2023 |
| 1.1.0 | 330 | 3/14/2023 |
| 1.1.0-workflow-dev.107.22552 | 175 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21746 | 174 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21506 | 169 | 3/14/2023 |
| 1.1.0-workflow-dev.107.20979 | 173 | 3/14/2023 |
| 1.1.0-dev.107.26364 | 171 | 3/14/2023 |
| 1.1.0-dev.107.24396 | 174 | 3/14/2023 |
| 1.1.0-dev.107.22787 | 173 | 3/14/2023 |
| 1.0.6 | 608 | 3/5/2022 |
| 1.0.4 | 639 | 6/13/2020 |
| 1.0.1 | 650 | 2/20/2020 |
| 1.0.0 | 670 | 11/5/2019 |