Lycoris.AutoMapper.Extensions
6.0.0
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 Lycoris.AutoMapper.Extensions --version 6.0.0
NuGet\Install-Package Lycoris.AutoMapper.Extensions -Version 6.0.0
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="Lycoris.AutoMapper.Extensions" Version="6.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lycoris.AutoMapper.Extensions" Version="6.0.0" />
<PackageReference Include="Lycoris.AutoMapper.Extensions" />
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 Lycoris.AutoMapper.Extensions --version 6.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Lycoris.AutoMapper.Extensions, 6.0.0"
#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 Lycoris.AutoMapper.Extensions@6.0.0
#: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=Lycoris.AutoMapper.Extensions&version=6.0.0
#tool nuget:?package=Lycoris.AutoMapper.Extensions&version=6.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
AutoMapper自定义扩展
引入扩展
- 1. 注册扩展,手动引入全局扩展
var builder = WebApplication.CreateBuilder(args);
// AutoMapper注册
builder.Services.AddAutoMapperService();
// Add services to the container.
var app = builder.Build();
// 使用AutoMapper全局扩展
app.UseAutoMapperExtensions();
app.UseRouting();
//
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();
- 2. 注册扩展,自动引入全局扩展(不推荐使用,因为就一句话,没必要,做这个的目的是因为作者朋友经常忘记这一句话)
var builder = WebApplication.CreateBuilder(args);
// AutoMapper注册
builder.Services.AddAutoMapperService(builder =>
{
builder.AddMapper<TestB, TestA>();
builder.EnableAutoReferenceExtensions();
});
// Add services to the container.
var app = builder.Build();
app.UseRouting();
//
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();
创建映射关系
- 1. 注册扩展时同时创建映射关系
builder.Services.AddAutoMapperService(builder =>
{
// 单向映射:只能将 TestB 映射为 TestA,不能将 TestA 映射为 TestB
builder.AddSingleMapper<TestB, TestA>();
// 双向映射:支持 TestB 映射为 TestA,也支持 TestA 映射为 TestB
builder.AddMapper<TestB, TestA>();
// 添加自定义AutoMapper映射关系实体,CusotmeProfile 需要继承 Profile
builder.AddMapperProfile<CusotmeProfile>();
});
- 2. IServiceCollection扩展注册
//单向映射
builder.Service.AddSingleMapper<TestB, TestA>();
builder.Service.AddMapper<TestB, TestA>();
builder.Service.AddMapperProfile<CusotmeProfile>();
以上提供的扩展方式,仅建议小项目映射关系少的并且没有复杂的映射逻辑使用,中大型项目还是建议使用AutoMapper
提供的映射关系实体处理,然后使用AddMapperProfile
添加映射关系实体处理
使用方法
- 1. 实体映射
var a = new TestA()
{
A = 1,
B = "123"
};
var b = a.ToMap<TestB>();
- 2. 集合映射
var alist = new List<TestA>()
{
new TestA(){ A = 1, B = "123" },
new TestA(){ A = 1, B = "123" }
};
var blist = alist.ToMapList<TestB>();
复杂的映射逻辑请使用AddMapperProfile
添加映射关系实体处理,以下代码仅举例简单的配置
public class ServerServiceProfile : Profile
{
public ServerServiceProfile()
{
// 基础映射(注意此处只是单向映射)
CreateMap<TestDto, TestViewModel>();
// 双向映射需要分别写出
CreateMap<TestServiceDto, TestServiceViewModel>();
CreateMap<TestServiceViewModel, TestServiceDto>();
// 指定映射属性
CreateMap<TestServiceDto, TestServiceViewModel>()
.ForMember(x => x.StringList, opt => opt.MapFrom(src => src.JsobString));
// 指定映射属性并处理成需要的内容
CreateMap<TestServiceDto, TestServiceViewModel>()
.ForMember(x => x.ObjectText, opt => opt.MapFrom(src => new TestObj()
{
Number = int.Parse(src.StringText),
Title = src.Title
}));
// 忽略某些字段的映射
CreateMap<TestServiceDto, TestServiceDto>()
.ForMember(x => x.Info, opt => opt.Ignore()))
.ForMember(x => x.Detail, opt => opt.Ignore()));
}
}
使用小技巧:
AutoMapper
会自动转换以下不同的类型的属性
int
与enum
的相互转换,但需要注意的是,由int
转换为enum
的时候,需要注意int
值是否存在于enum
上
例:当int
值为1
的时候转为enum
就会出现异常
public enum Test
{
EN = 0,
EB = 2
}
string
类型与数值类型(int
、long
,其他数值类型博主平时使用没怎么需要转换,所以没有测试过)的转换,同样的当string
转换成数值类型的时候要注意string
是否是正确的可以转换的数值
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- AutoMapper (>= 12.0.0)
- AutoMapper.Extensions.Microsoft.DependencyInjection (>= 12.0.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
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 |
---|---|---|
8.1.1 | 222 | 8/30/2025 |
8.1.0 | 175 | 7/10/2025 |
8.0.0-rc1.0 | 111 | 8/13/2024 |
8.0.0-rc | 155 | 3/14/2024 |
6.0.0 | 450 | 11/28/2022 |