AutoMapper using Notes

Source: Internet
Author: User

AutoMapper is a. NET object mapping tool.

Project address: Https://github.com/AutoMapper/AutoMapper.

Help Document: Https://github.com/AutoMapper/AutoMapper/wiki

Main purpose

The transformation between the domain object and the DTO, and the database query results are mapped to the entity object.

Using notes

Scenario 1: Source type Blogentry, target type blogpostdto, specified attribute is mapped (blogentry.id corresponds to Blogpostdto.postid).

Code:

Automapper.mapper.createmap<blogentry, Blogpostdto> ()
. Formember (dto = dto). PostID, opt = opt. Mapfrom (entity = entity.id));


Scene 2:idatareader mapping to entity classes

Code:

using (IDataReader reader = _db. ExecuteReader (command))
{
if (reader. Read ())
{
return automapper.mapper.dynamicmap<blogconfig> (reader);
}
}

Scenario 3: Mapping between list types such as: source type list<blogsite>, Target type list<blogsitedto>

The code is as follows:

Automapper.mapper.createmap<blogsite, blogsitedto> ();
var blogsitedto = Automapper.mapper.map<list<blogsite>, list<blogsitedto>> (BlogSite);

Note: The mapping relationship between Blogsite and Blogsitedto must first be established through Createmap.

Scenario 4: Specify values for the properties of the target instance at map time

The code is as follows:

var blogsitedto = new Blogsitedto ();
Automapper.mapper.createmap<blogentry, Blogpostdto> ()
. Formember (dto = dto). Blogsitedto, opt = opt. Usevalue (blogsitedto));

Note: Blogsitedto is a property of Blogpostdto.

Add:

AutoMapper configuration (such as Automapper.mapper.createmap<blogsite, blogsitedto> ();) is recommended when the program starts, such as Global.asax application _start, BootStrapper.

AutoMapper using Notes

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.