<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何在 C# 中使用 AutoMapper

          共 4576字,需瀏覽 10分鐘

           ·

          2020-11-28 20:20

          譯文鏈接:https://www.infoworld.com/article/3192900/how-to-work-with-automapper-in-csharp.html

          AutoMapper 是一個非常流行的 object-to-object 映射庫,它的目的就是幫助你實現(xiàn)不同類型對象之間的映射,舉一個例子,在 DDD 開發(fā)模式中,你可能需要實現(xiàn)將 DTO object 映射為 Model object,在過去,你需要人肉的將這兩個類型下的屬性字段進行一一映射,現(xiàn)在 AutoMapper 就可以幫你節(jié)省 這種冗余的模板式代碼 匹配所耗費的時間。

          開始玩 AutoMapper 之前,你需要在 Visual Studio 中創(chuàng)建一個 Project 并且安裝 AutoMapper,你可以從 NuGet 上下載,也可以在 NuGet Package Manager Console 控制臺輸入如下命令:


          PM> Install-Package AutoMapper

          使用 AutoMapper 創(chuàng)建映射關(guān)系

          像 AutoMapper 這樣的 object-to-object 映射工具,它必須能夠做到將一種輸入類型轉(zhuǎn)換成另一個輸出類型,是不是很拗口,可以先考慮下面的兩個類。


          ????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;
          ????????}
          ????}

          接下來,下面的代碼段將會告知你如何使用 AutoMapper 在 AuthorModel ?和 AuthorDTO 這兩個對象之間創(chuàng)建一個 mapping 關(guān)系。


          var?config?=?new?MapperConfiguration(cfg?=>?{
          ????????????????cfg.CreateMap();
          ????????????});

          最終的 mapping 轉(zhuǎn)換,你還需要增加幾句下面的代碼,實現(xiàn)兩個類型之間的轉(zhuǎn)換。


          IMapper?iMapper?=?config.CreateMapper();
          var?source?=?new?AuthorModel();
          var?destination?=?iMapper.Map(source);

          一個 AutoMapper 的例子

          接下來可以上一些數(shù)據(jù)了,可以參考下面的代碼片段,我準備先在 source object 上賦值,然后執(zhí)行 AutoMapper 中的 Map 方法之后,在 destination object 上原樣顯示出來。


          var?config?=?new?MapperConfiguration(cfg?=>?{
          ????????????????cfg.CreateMap();
          ????????????});
          IMapper?iMapper?=?config.CreateMapper();
          var?source?=?new?AuthorModel();
          source.Id?=?1;
          source.FirstName?=?"Joydip";
          source.LastName?=?"Kanjilal";
          source.Address?=?"India";
          var?destination?=?iMapper.Map(source);
          Console.WriteLine("Author?Name:?"+?destination.FirstName?+?"?"?+?destination.LastName);

          當(dāng)你執(zhí)行完這段代碼之后,destination ?object 上的 Author Name 將會輸出到控制臺上,目標對象上的 FirstName 和 LastName 和 source object 上的這兩個屬性值保持一致,說明 automapper 已經(jīng)幫你成功映射。

          值得注意的是,AutoMapper 不僅僅可以 mapping 一個類,還可以 mapping 多個類,默認情況下,AutoMapper會按照默認約定匹配,也就是被mapping的對象之間具有相同的屬性名稱才能被成功映射,但現(xiàn)實情況下,很多被映射的屬性名稱是不相同的,這個時候就需要人工介入指定 mapping 關(guān)系讓 AutoMapper 按照你設(shè)定的執(zhí)行,假定你需要實現(xiàn) Contact 到 ContactDetails 之間的映射,下面的例子展示了如何去實現(xiàn)這種關(guān)系。


          var?config?=?new?MapperConfiguration(cfg?=>?{
          ????????????????cfg.CreateMap()
          ????????????????.ForMember(destination?=>?destination.ContactDetails,
          ???????????????opts?=>?opts.MapFrom(source?=>?source.Contact));
          ????????????});

          下面的語句可以創(chuàng)建最終的 ?destination object 對象。


          var?destination?=?iMapper.Map(source);

          有時候你已經(jīng)生成了 destination ?object,在這基礎(chǔ)上你還想二次映射,這時可以使用下面替代語句。


          iMapper.Map(sourceObject,?destinationObject);

          本質(zhì)上來說,上面的這段代碼常用于匹配兩個已存在的 object。

          使用 AutoMapping 的 projections 功能

          AutoMapper 提供了非常好的 projections 功能,projections ??的地方在于在 mapping 映射時可以無視兩者的 object 數(shù)據(jù)結(jié)構(gòu)是否一致,比如說讓 source 的多個屬性 映射到 destination 的一個屬性上,而上面我們一直討論的都是一對一的 object mapping。

          接下來我們一起學(xué)習(xí)下 projection,舉個例子,考慮如下類。


          ????public?class?Address
          ????{
          ????????public?string?City?{?get;?set;?}
          ????????public?string?State?{?get;?set;?}
          ????????public?string?Country?{?get;?set;?}
          ????}

          接下來在 AuthorModel 類中新增一個 Address 屬性用來存儲 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,下面的代碼片段展示了如何去實現(xiàn)。


          var?config?=?new?MapperConfiguration(cfg?=>?{
          ????????????????cfg.CreateMap()
          ???????????????????.ForMember(destination?=>?destination.Address,
          ??????????????map?=>?map.MapFrom(
          ??????????????????source?=>?new?Address
          ??????????????????{
          ??????????????????????City?=?source?.City,
          ??????????????????????State?=?source?.State,
          ??????????????????????Country?=?source.Country
          ??????????????????}));

          我會在后續(xù)的文章中繼續(xù)討論 AutoMapper 更多的高級特性,現(xiàn)在,你可以通過這個鏈接:http://automapper.org/ ?去學(xué)習(xí)更多的 AutoMapper 知識。


          瀏覽 31
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大黑逼伊人 | 操逼视频五月天 | 精品国产免费无码久久久 | 国内视频精品在线 | A片免费黄色电影网站 |