AutoMapper Getting started

Source: Internet
Author: User

    1. What is AutoMapper?
    2. Why do you use AutoMapper?
    3. How do I use AutoMapper?
    4. Where do I configure the automapper?
    5. How do I test my mappings?

What is AutoMapper?

AutoMapper is an object-to-object mapping relationship, and object-to-object mapping works by converting a type object input into a different type object output. One thing that makes AutoMapper interesting is that he offers some more interesting rules to remove heavy work from the type Object conversion algorithm,

As long as the type object follows the rules established by AutoMapper, there is basically no need for additional configuration to do conversions between types of objects.

Why do you use AutoMapper?

The type conversion code is annoying, the test type conversion code is more annoying, AutoMapper provides a very simple configuration mechanism, type conversion testing has become the same simple, now the really left to be concerned about the question is why use object-object mapping?

Type conversions occur in many parts of the project, but most occur between layers and layers, such as between Ui/domain, or service/domain, where a layer of care is often inconsistent with another layer of concern,

So object-to-object mapping produces an isolated model, so that objects in each layer become more independent, affecting only the layer on which the object resides, without affecting the other layers.

How do I use AutoMapper?

First, you need the source type and the target type, the target type will be affected by its layer at the beginning of the design, but Automappers can still work well, as long as the target object member name and the source object member name match, for example you have a source object property called FirstName,

Then he will automatically map to the properties of the target object member named FirstName, AutoMapper also supports flattening.

When mapping the source type to the target type, AutoMapper ignores the null reference exception, which is the default design, and if you do not like this design, you can combine AutoMapper custom-value-resolver if necessary.

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

Mapper.createmap<order, orderdto> ();

The type on the left is the source type, the right type is the target type, and the following method is used to perform a mapping

Orderdto DTO = mapper.map<orderdto> (order);

AutoMapper also have non-generic methods.

Where do I configure the automapper?

If you use a static mapping method, you only need to configure it once in the AppDomain, which means that your best choice is to put the configuration code method in the program startup item, such as Global.asax. It should be stated that you configure yourself within the bootstrapper (boot loader),

This bootstrapper is also called as a startup method.

How do I test my mappings?

Create a mapping test, you have to do two things

    1. To invoke a method that creates a map in a startup item
    2. Calling the Mapper.assertconfigurationisvalid method

As in the following example:

Automapperconfiguration.configure (); Mapper.assertconfigurationisvalid ();

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

AutoMapper Getting started

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.