AutoMapper是一种流行的对象到对象映射库,可用于映射属于不同类型的对象。例如,您可能需要将应用程序中的DTO(数据传输对象)映射到模型对象。
AutoMapper省去了手动映射此类不兼容类型的一个或多个属性的繁琐工作。
一、在Visual Studio中创建一个项目
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:6107e04b-5e60-4e21-af33-ab1128ca4249
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:d0576e3d-08e2-450b-b7fe-8b7e197648b3
然后安装AutoMapper。可以在NuGet软件包管理器窗口安装AutoMapper
二、使用AutoMapper创建映射
诸如AutoMapper之类的对象到对象映射器将一种类型的输入对象转换为另一种类型的输出对象。考虑以下两个类。
//实体
public class AuthorModel
{
public int ID { get; set; }
public string AuthorName { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
}
//映射实体DTO
public class AuthorDTO
{
public int ID { get; set; }
public string AuthorName { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
}
配置类型AuthorModel和AuthorDTO之间创建映射关系。
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<authormodel, authordto>();
});
</authormodel,>
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:6f981bc6-0149-444e-9e34-e680c4a70524
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:2688d8cc-a592-4bfa-a0fa-e49ff410a7d6
IMapper iMapper = config.CreateMapper();
var model = new AuthorModel();
var MapAuthor = iMapper.Map<authormodel, authordto>(model);//映射实体
</authormodel,>
三、使用AutoMapper完整示例
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:4ae12dbb-fc7b-43cf-8660-18f256315c30
[En]
[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:b99e02de-c924-4e14-a193-ed11759c3ff1
static void Main(string[] args)
{
//注册配置
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<authormodel, authordto>();
});
//创建映射
IMapper iMapper = config.CreateMapper();
var model = new AuthorModel();
model.ID = 1;
model.AuthorName = "LaoPaoEr";
model.Sex = "男";
model.Age = 23;
model.Phone = "18276250000";
model.Address = "中国上海市浦东新区XXXXX";
var MapAuthor = iMapper.Map<authormodel, authordto>(model);//映射实体
Console.WriteLine(MapAuthor);
Console.WriteLine($"Author Name: {model.AuthorName}");
Console.ReadLine();
}
</authormodel,></authormodel,>
当您执行上述代码时,将显示存储在目标对象中的作者姓名,使用AutoMapper成功映射了对象!
更多AutoMapper用法请转到http://automapper.org/
Original: https://www.cnblogs.com/LaoPaoEr/p/14490328.html
Author: 无昵称老炮儿
Title: C#中使用AutoMapper
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/562364/
转载文章受原作者版权保护。转载请注明原作者出处!