AutoMapper Getting started, automappergetting

Source: Internet
Author: User

AutoMapper Getting started, automappergetting

What is AutoMapper?

AutoMapper is a ing between objects and objects. The ing between objects and objects is done by converting a type object input to a different type object output, one thing that interests AutoMapper is that it provides interesting rules to remove heavy work from the type object conversion algorithm,

As long as the type object follows the rules set up by AutoMapper, no additional configuration is required for conversion between type objects.

 

Why AutoMapper?

The type conversion code is annoying, and the test type conversion code is even more annoying. AutoMapper provides a very simple configuration mechanism, and the type conversion test has become equally simple, now, why use object-object mapping ?,

Type conversion occurs in many places in the project, but most of them occur at the boundary between the layer, such as between the UI/Domain or between the Service/Domain, generally, what one layer cares about is different from what another layer cares about,

Therefore, the ing between objects and objects produces an isolated model. In this way, the changes of objects in each layer are more independent, and only affect the layer of the object and other layers.

How to Use AutoMapper?

First, you need the Source type and target type, which will be affected by the layer where the target type is designed, but AutoMappers can still work well, as long as the target object member name matches the source object member name, for example, you have a source object property named FirstName,

Then it will be automatically mapped to the attribute of the target object member name FirstName. AutoMapper also supports Flattening.

 

When ing source type to target type, AutoMapper ignores NULL reference exceptions. This is the default design. If you do not like this design, you can combine AutoMapper Custom-value-resolver, if necessary.

Once you have two types and reference AutoMapper, you can add mappings for these two types,

Mapper.CreateMap<Order, OrderDto>();

 

The type on the left is the source type, and the type on the right is the target type. Use the following method to execute a ing.

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

AutoMapper also has non-generic methods.

Where can I configure AutoMapper?

If you use the static ing method, you only need to configure it once in appDomain, which means that your best choice is to set the configuration code method in the program startup item, such as Global. asax. You must note that you have configured yourself in the bootstrapper (start loader,

This bootstrapper is also called as the startup method.

How to test my mappings?

Create a ing test. You have to do two things.

As in the following example:

AutoMapperConfiguration.Configure();Mapper.AssertConfigurationIsValid();

 

Https://github.com/AutoMapper/AutoMapper/wiki/Getting-started

Related Article

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.