Use of AutoMapper

Source: Internet
Author: User

1.AutoMapper Brief Introduction

Official website: http://automapper.org/

Source: Https://github.com/AutoMapper/AutoMapper

NuGet Installation:

Pm> Install-package AutoMapper

AutoMapper is a mapping tool based on object-to-object conventions, often used (but not limited to) to turn complex object models into DTOs, typically for viewmodel patterns and cross-service categories.

AutoMapper provides users with a convenient configuration API, just like using conventions to accomplish automatic mapping.

AutoMapper includes the following features: Flattening, projection, configuration validation, list and array, nested mappings, custom type converters, custom value converters, custom value formatter, null value substitution

The AutoMapper is a one-way mapper. This means that it does not have an built-in mapping object that supports writing back and forth to the original source unless the user explicitly creates reverse reflection after updating the mapped object. This needs to be done by design, because having a dto write back to, say, a domain model or something, changes its durability and is considered anti-pattern. In this solution, command messages tend to be better choices in bidirectional mapping. However, in some specific environments, one might justify a bidirectional mapping, such as a very simple crud application. A framework that supports bidirectional mapping is glue.

Introduction from: Http://www.oschina.net/p/automapper/similar_projects

2. How to use
    • "AutoMapper Official Document" DTO and Domin model Mutual conversion (UP)
    • "AutoMapper Official Document" DTO and Domin Model conversion (middle)
    • "AutoMapper Official Document" DTO and Domin model mutual conversion (bottom)

Simple Example 1 (mapping between two types)

Configure Automappermapper.createmap<order, orderdto> ();//execute Mappingorderdto dto = Mapper.map<order, OrderDto> (order);

Simple Example 2 (two mapped objects have partial field names that are not the same)

Mapper.createmap<addressdto, Address> (). Formember (d = d.country, opt = = opt. Mapfrom (s = s.countryname));

Simple example 3 (mapping between list types)

automapper.mapper.createmap< Address, Addressdto > () var addressdtolist = automapper.mapper.map<list< Address, list< addressdto >> (AddressList);

Reference article: Http://www.cnblogs.com/smileberry/p/3838143.html (also packaged with Automapperhelper, simpler to use)

3.AutoMapper + EF6

Automatic introduction of AutoMapper and Automapper.ef6 class libraries using NuGet installation

Pm> Install-package Automapper.ef6

Overview of extension methods

     public static tdestination[] Projecttoarray<tdestination> (this IQueryable queryable); public static tdestination[] Projecttoarray<tdestination> (This IQueryable queryable, Iconfigurationprovider         config);        public static task<tdestination[]> projecttoarrayasync<tdestination> (this IQueryable queryable); public static task<tdestination[]> projecttoarrayasync<tdestination> (this IQueryable queryable,        Iconfigurationprovider config);        public static Tdestination projecttofirst<tdestination> (this IQueryable queryable); public static Tdestination projecttofirst<tdestination> (this IQueryable queryable, Iconfigurationprovider         config);         public static task<tdestination> projecttofirstasync<tdestination> (this IQueryable queryable); public static task<tdestination> projecttofirstasync<tdestination> (this IQueryable queryable,        Iconfigurationprovider config); public static TDEstination projecttofirstordefault<tdestination> (this IQueryable queryable); public static Tdestination projecttofirstordefault<tdestination> (this IQueryable queryable,        Iconfigurationprovider config);         public static task<tdestination> projecttofirstordefaultasync<tdestination> (this IQueryable queryable); public static task<tdestination> projecttofirstordefaultasync<tdestination> (this IQueryable queryable,        Iconfigurationprovider config);        public static list<tdestination> projecttolist<tdestination> (this IQueryable queryable); public static list<tdestination> projecttolist<tdestination> (this IQueryable queryable,        Iconfigurationprovider config); public static task<list<tdestination>> projecttolistasync<tdestination> (this IQueryable queryable         ); public static task<list<tdestination>> projecttolistasync<tdestination> (this IQueryable queryable , iconfIgurationprovider config);        public static iqueryable<tdestination> projecttoqueryable<tdestination> (this IQueryable queryable); public static iqueryable<tdestination> projecttoqueryable<tdestination> (this IQueryable queryable,        Iconfigurationprovider config);        public static Tdestination projecttosingle<tdestination> (this IQueryable queryable); public static Tdestination projecttosingle<tdestination> (this IQueryable queryable, Iconfigurationprovider        config);        public static task<tdestination> projecttosingleasync<tdestination> (this IQueryable queryable); public static task<tdestination> projecttosingleasync<tdestination> (this IQueryable queryable,        Iconfigurationprovider config);        public static Tdestination projecttosingleordefault<tdestination> (this IQueryable queryable); public static Tdestination projecttosingleordefault<tdestination> (this IQueryable queryable, ICoNfigurationprovider config); public static task<tdestination> projecttosingleordefaultasync<tdestination> (this IQueryable queryable)        ; public static task<tdestination> projecttosingleordefaultasync<tdestination> (this IQueryable queryable, Iconfigurationprovider config);

This makes it easy to convert to data, lists, get the first data, and so on, and can support asynchronous

Use of AutoMapper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.