• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    圖解OOM之AutoMapper使用

    AutoMapper是一個對象映射器,它可以將一個一種類型的對象轉換為另一種類型的對象。AutoMapper供給了映射法則及操作方式,使我們不消過多設置裝備擺設就可以映射兩個類。

    本篇經驗將和大師介紹OOM之AutoMapper利用,但愿對大師的工作和進修有所幫忙!

    方式/步調

    1. 1

      打開Visual Studio 2015,如下圖所示:

    2. 2

      依次點擊菜單欄上的“文件”->“新建”->“項目”,如下圖所示:

    3. 3

      新建一個ASP.NET Web應用法式,項目名稱為AutoMapperWeb,建立一個ASP.NET MVC的項目,如下圖所示:

    4. 4

      點擊菜單欄中的“東西”->“NuGet保證理器”->“法式保證理器節制臺”,如下圖所示:

    5. 5

      在打開的法式保證理器節制臺中,輸入Install-Package AutoMapper,然后回車,安裝AutoMapper,當前安裝的是6.2.2,如下圖所示:

    6. 6

      如許AutoMapper對應的dll就主動添加到項目標bin目次下了,如下圖所示:

    7. 7

      新建2個類,ProductEntity和ProductDTO類,如下圖所示:

      public class ProductEntity

      {

              public string Name { get; set; }

              public decimal Amount { get; set; }

      }

      public class ProductDTO

      {

              public string Name { get; set; }

              public decimal Amount { get; set; }

      }

    8. 8

      接下來我們建立自界說的ATProfile并擔當Profile類,代碼如下圖所示:

      public class ATProfile:Profile

      {

           public ATProfile()

           {

                  //映射關系設置裝備擺設

                  CreateMap<ProductEntity,ProductDTO>();

           }

      }

    9. 9

      我們在HomeController節制器中,可以編寫三種體例進行AutoMapper的主動映射,代碼如下圖所示:

      public ActionResult Index()

              {

                  var productEntity = new ProductEntity() { Name = "Visual Studio 2017", Amount = 500 };

                  //1、Automapper利用靜態類建立映射

                  //ViewBag.Tips = "1、Automapper利用靜態類建立映射";

                  //Mapper.Initialize(cfg => cfg.CreateMap<ProductEntity, ProductDTO>());

                  //var productDTO = Mapper.Map<ProductDTO>(productEntity);

                  //2、Automapper利用實例方式建立映射

                  //ViewBag.Tips = "2、Automapper利用實例方式建立映射";

                  //MapperConfiguration configuration = new MapperConfiguration(cfg => cfg.CreateMap<ProductEntity,ProductDTO>());

                  //var mapper = configuration.CreateMapper();

                  //var productDTO = mapper.Map<ProductDTO>(productEntity);

                  //3、利用Profie設置裝備擺設實現映射關系

                  ViewBag.Tips = "3、利用Profie設置裝備擺設實現映射關系";

                  var configuration = new MapperConfiguration(cfg => cfg.AddProfile<ATProfile>());

                  var productDTO = configuration.CreateMapper().Map<ProductDTO>(productEntity);

                  return View(productDTO);

              }

    10. 10

      在Views目次下的Index視圖中,我們添加如下圖所示代碼:

      @using AutoMapperWeb.Models

      @model  ProductDTO

       @ViewBag.Tips

      <ul>

          <li>商品名稱:@Model.Name</li>

          <li>商品數目:@Model.Amount</li>

      </ul>

    11. 11

      運行法式,可以看到成果如下圖所示:

    • 發表于 2018-05-10 00:00
    • 閱讀 ( 823 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    admin
    admin

    0 篇文章

    作家榜 ?

    1. xiaonan123 189 文章
    2. 湯依妹兒 97 文章
    3. luogf229 46 文章
    4. jy02406749 45 文章
    5. 小凡 34 文章
    6. Daisy萌 32 文章
    7. 我的QQ3117863681 24 文章
    8. 華志健 23 文章

    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆