C# 中AutoMapper的使用方法
譯文鏈接: https://www.infoworld.com/art...
AutoMapper 是一個(gè)非常流行的 object-to-object 映射庫(kù),它的目的就是幫助你實(shí)現(xiàn)不同類型對(duì)象之間的映射,舉一個(gè)例子,在 DDD 開發(fā)模式中,你可能需要實(shí)現(xiàn)將 DTO object 映射為 Model object,在過去,你需要人肉的將這兩個(gè)類型下的屬性字段進(jìn)行一一映射,現(xiàn)在 AutoMapper 就可以幫你節(jié)省 這種冗余的模板式代碼 匹配所耗費(fèi)的時(shí)間。
開始玩 AutoMapper 之前,你需要在 Visual Studio 中創(chuàng)建一個(gè) Project 并且安裝 AutoMapper,你可以從 NuGet 上下載,也可以在 NuGet Package Manager Console 控制臺(tái)輸入如下命令:
PM> Install-Package AutoMapper
使用 AutoMapper 創(chuàng)建映射關(guān)系
像 AutoMapper 這樣的 object-to-object 映射工具,它必須能夠做到將一種輸入類型轉(zhuǎn)換成另一個(gè)輸出類型,是不是很拗口,可以先考慮下面的兩個(gè)類。
public class AuthorModel
{
public int Id
{
get; set;
}
public string FirstName
{
get;set;
}
public string LastName
{
get; set;
}
public string Address
{
get; set;
}
}
public class AuthorDTO
{
public int Id
{
get; set;
}
public string FirstName
{
get; set;
}
public string LastName
{
get; set;
}
public string Address
{
get; set;
}
}
接下來,下面的代碼段將會(huì)告知你如何使用 AutoMapper 在 AuthorModel 和 AuthorDTO 這兩個(gè)對(duì)象之間創(chuàng)建一個(gè) mapping 關(guān)系。
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<AuthorModel, AuthorDTO>();
});
最終的 mapping 轉(zhuǎn)換,你還需要增加幾句下面的代碼,實(shí)現(xiàn)兩個(gè)類型之間的轉(zhuǎn)換。
IMapper iMapper = config.CreateMapper(); var source = new AuthorModel(); var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);
一個(gè) AutoMapper 的例子
接下來可以上一些數(shù)據(jù)了,可以參考下面的代碼片段,我準(zhǔn)備先在 source object 上賦值,然后執(zhí)行 AutoMapper 中的 Map 方法之后,在 destination object 上原樣顯示出來。
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<AuthorModel, AuthorDTO>();
});
IMapper iMapper = config.CreateMapper();
var source = new AuthorModel();
source.Id = 1;
source.FirstName = "Joydip";
source.LastName = "Kanjilal";
source.Address = "India";
var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);
Console.WriteLine("Author Name: "+ destination.FirstName + " " + destination.LastName);
當(dāng)你執(zhí)行完這段代碼之后,destination object 上的 Author Name 將會(huì)輸出到控制臺(tái)上,目標(biāo)對(duì)象上的 FirstName 和 LastName 和 source object 上的這兩個(gè)屬性值保持一致,說明 automapper 已經(jīng)幫你成功映射。
值得注意的是,AutoMapper 不僅僅可以 mapping 一個(gè)類,還可以 mapping 多個(gè)類,默認(rèn)情況下,AutoMapper會(huì)按照默認(rèn)約定匹配,也就是被mapping的對(duì)象之間具有相同的屬性名稱才能被成功映射,但現(xiàn)實(shí)情況下,很多被映射的屬性名稱是不相同的,這個(gè)時(shí)候就需要人工介入指定 mapping 關(guān)系讓 AutoMapper 按照你設(shè)定的執(zhí)行,假定你需要實(shí)現(xiàn) Contact 到 ContactDetails 之間的映射,下面的例子展示了如何去實(shí)現(xiàn)這種關(guān)系。
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<AuthorModel, AuthorDTO>()
.ForMember(destination => destination.ContactDetails,
opts => opts.MapFrom(source => source.Contact));
});
下面的語句可以創(chuàng)建最終的 destination object 對(duì)象。
var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);
有時(shí)候你已經(jīng)生成了 destination object,在這基礎(chǔ)上你還想二次映射,這時(shí)可以使用下面替代語句。
iMapper.Map(sourceObject, destinationObject);
本質(zhì)上來說,上面的這段代碼常用于匹配兩個(gè)已存在的 object。
使用 AutoMapping 的 projections 功能
AutoMapper 提供了非常好的 projections 功能,projections 🐂👃的地方在于在 mapping 映射時(shí)可以無視兩者的 object 數(shù)據(jù)結(jié)構(gòu)是否一致,比如說讓 source 的多個(gè)屬性 映射到 destination 的一個(gè)屬性上,而上面我們一直討論的都是一對(duì)一的 object mapping。
接下來我們一起學(xué)習(xí)下 projection,舉個(gè)例子,考慮如下類。
public class Address
{
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
接下來在 AuthorModel 類中新增一個(gè) Address 屬性用來存儲(chǔ) Author 的地址信息,修改后的 AuthorModel 類如下:
public class AuthorModel
{
public int Id
{
get; set;
}
public string FirstName
{
get;set;
}
public string LastName
{
get; set;
}
public Address Address
{
get; set;
}
}
然后再更新一下 AuthorDTO 類
public class AuthorDTO
{
public int Id
{
get; set;
}
public string FirstName
{
get; set;
}
public string LastName
{
get; set;
}
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
接下來我們需要將 AuthorDTO 映射到 AuthorModel,下面的代碼片段展示了如何去實(shí)現(xiàn)。
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<AuthorDTO, AuthorModel>()
.ForMember(destination => destination.Address,
map => map.MapFrom(
source => new Address
{
City = source .City,
State = source .State,
Country = source.Country
}));
我會(huì)在后續(xù)的文章中繼續(xù)討論 AutoMapper 更多的高級(jí)特性,現(xiàn)在,你可以通過這個(gè)鏈接: http://automapper.org/ 去學(xué)習(xí)更多的 AutoMapper 知識(shí)。
更多高質(zhì)量干貨:參見我的 GitHub: dotnetfly
以上就是C# 中AutoMapper的使用方法的詳細(xì)內(nèi)容,更多關(guān)于C# AutoMapper使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法
這篇文章主要介紹了C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法,本文總結(jié)了Convert.ToDateTime(string)、Convert.ToDateTime(string, IFormatProvider)、DateTime.ParseExact()三種方法,需要的朋友可以參考下2015-07-07
詳解C#如何為某個(gè)方法設(shè)定執(zhí)行超時(shí)時(shí)間
這篇文章主要為大家詳細(xì)介紹一下C#如何為某個(gè)方法設(shè)定執(zhí)行超時(shí)時(shí)間,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,有需要的小伙伴可以學(xué)習(xí)一下2023-10-10
C#中Override關(guān)鍵字和New關(guān)鍵字的用法詳解
這篇文章主要介紹了C#中Override關(guān)鍵字和New關(guān)鍵字的用法,需要的朋友可以參考下2016-01-01
C#實(shí)現(xiàn)時(shí)間戳的簡(jiǎn)單方法
這篇文章主要介紹了C#實(shí)現(xiàn)時(shí)間戳的簡(jiǎn)單方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
基于C#實(shí)現(xiàn)的仿windows左側(cè)伸縮菜單效果
這篇文章主要介紹了基于C#實(shí)現(xiàn)的仿windows左側(cè)伸縮菜單效果,比較實(shí)用的功能,需要的朋友可以參考下2014-08-08

