亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Getting started · AutoMapper/AutoMapper Wiki

系統(tǒng) 1857 0

Getting started · AutoMapper/AutoMapper Wiki

Getting Started

What is AutoMapper?

AutoMapper is an object-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type. What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as type B follows AutoMapper's established convention, almost zero configuration is needed to map two types.

Why use AutoMapper?

Mapping code is boring. Testing mapping code is even more boring. AutoMapper provides simple configuration of types, as well as simple testing of mappings. The real question may be "why use object-object mapping?" Mapping can occur in many places in an application, but mostly in the boundaries between layers, such as between the UI/Domain layers, or Service/Domain layers. Concerns of one layer often conflict with concerns in another, so object-object mapping leads to segregated models, where concerns for each layer can affect only types in that layer.

How do I use AutoMapper?

First, you need both a source and destination type to work with. The destination type's design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up to the source type's members. If you have a source member called "FirstName", this will automatically be mapped to a destination member with the name "FirstName". AutoMapper also supports Flattening , which can get rid of all those pesky null reference exceptions you might encounter along the way.

Once you have your types, and a reference to AutoMapper, you can create a map for the two types.

              
                Mapper.CreateMap<Order, OrderDto>();

              
            

The type on the left is the source type, and the type on the right is the destination type. To perform a mapping, use the Map method.

              
                OrderDto dto = Mapper.Map<Order, OrderDto>(order);

              
            

AutoMapper also has non-generic versions of these methods, for those cases where you might not know the type at compile time.

Where do I configure AutoMapper?

If you're using the static Mapper method, configuration should only happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.asax file for ASP.NET applications. Typically, the configuration bootstrapper class is in its own class, and this bootstrapper class is called from the startup method.

How do I test my mappings?

To test your mappings, you need to create a test that does two things:

  • Call your bootstrapper class to create all the mappings
  • Call Mapper.AssertConfigurationIsValid

Here's an example:

              
                AutoMapperConfiguration.Configure();

Mapper.AssertConfigurationIsValid();

              
            

Can I see a demo?

Check out the dnrTV episode on AutoMapper .

Getting started · AutoMapper/AutoMapper Wiki


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!?。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲图片欧美 | 不卡中文字幕在线观看 | 国产激情久久久久久影院 | 亚洲精品国产成人99久久 | 久久精选 | 欧美一区二区在线视频 | 99视频在线国产 | 91成人精品视频 | 精品国产免费一区二区三区 | 色悠久久久久综合网小说 | 日本一本久 | 日本1区2区3区 | 同性女女黄h片在线播放 | 男女超爽视频免费播放在线观看 | 国产成人精品一区二三区 | 99热只有精品一区二区 | 日本欧美视频在线 | 日本中文字幕二区三区 | 欧美激情伦妇在线观看 | 91精品全国免费观看 | 欧美一级毛片高清免费观看 | 日韩欧美视频在线播放 | 国产精品一区二区久久精品涩爱 | 日韩狠狠操 | 国产视频自拍一区 | 日日添天天做天天爱 | 黄色影院网站 | 日本在线三级 | 国产区精品在线 | 最新亚洲精品国自产在线 | 国产欧美精品午夜在线播放 | 久青草国产观看在线视频 | 狠狠操狠狠 | 中文字幕精品一区二区日本大胸 | 国产午夜不卡在线观看视频666 | 国产大学生一级毛片绿象 | 亚洲伊人久久大香线蕉啊 | 手机看片日韩高清国产欧美 | 色四月| 97国内免费久久久久久久久久 | 国产精品国产精品国产专区不卡 |