AutoMapper Map and dynamicmap--experts focus on detail, thought and summary

Source: Internet
Author: User

Recently in the project, encountered a strange problem, about the details of AutoMapper, but also not for the General people's attention.

I study automapper also not much time, and the process of research also wrote about AutoMapper series of basic tutorials, but after all, AutoMapper is an open source project, not a simple series of tutorials can explain clearly, can only explain a general, the project in real time , the details of the problems have to be privately re-researched and summarized.

First of all, I want to explain that this blog writing order is in the order of things to write, that is, when I want to write this blog, the root cause of the problem has not been found, but at this time, I look back, and then to see the process of the solution of each problem, I think, at this time the details of the problem is clear Let's share a little bit.

First of all, let's look at the approximate configuration of my automapper process:

    • The AutoMapper configuration class corresponding to the entity class is created first, and the naming specification is entityname (entity class name) +profile, such as Personprofile,provinceperfile.
 Public class provinceprofile:profile{    protectedoverridevoid  Configure ()    {         = cfg. Createmap<provinces, provincedto>()            . Forsourcemember (src=>src. Updateddate,opt=>opt. Ignore ())            . Reversemap ());}    }
    • Create a static class, name Automapperconfig, then initialize the mapper in his static method, add all the configuration classes, and here I add other configuration classes.
 Public Static classautomapperwebconfig{ Public Static voidConfigure () {mapper.reset (); Mapper.initialize (CFG={cfg. Addprofile<ProvinceProfile>(); Cfg. Addprofile<CityProfile>(); Cfg. Addprofile<StationProfile>(); Cfg. Addprofile<TerminalDeviceProfile> ();//terminaldeviceprofile Dependent ProvinceprofileCfg. Addprofile<operatorprofile>();        }); Mapper.assertconfigurationisvalid ();//Verify that all mapping configurations are correct    }}
    • Finally, when the project starts (the ASP. Application_Start method in the. asax file) calls Automapperwebconfig.configure ();

It all feels so smooth, but often the smoother it is, it means it's not coming. Next, an error like the following is blowing at me.

When I use the Mapper.map () method to map an entity class to a DTO class using the application tier, the error is as follows:

This is obviously a automapper mapping error.

Next on the various search errors, found the following blog, the original

TA's solution is that

See here, I am very happy, hurriedly changed their code, found that success! But the blogger did not give a why.

However, I am not angry, I have created a mapping between two classes, Ah, why Mapper.map () method does not work, I am puzzled, I have to figure out the relationship between the two.

I am thinking, literally, that one is "mapping" and one is "dynamic mapping", what difference does it make?

So various search between the difference between, to StackOverflow found the following answer: The original link

It says here that Dynamicmap is used when compiling without knowing the source type, then, accordingly, the map is used in case the source type is known at compile time. The simple explanation does not give a hint to those who have not been in contact with AutoMapper for a long time.

And the problem is, I have created a map before, so at compile time should be able to determine the source type, not to mention that my two classes are simple, it is not possible because of inconsistent data types caused by the mapping failed AH! The code is as follows:

 Public classprovinces:entity{ Public Virtual stringcode{Get;Set; }  Public Virtual stringname{Get;Set; }  Public Virtual stringupdatedby{Get;Set; }  Public VirtualDatetime? updateddate{Get;Set; }  Publicprovinces () {}}
 Public class provincedto:entitydto{    publicstringgetset;}      Public string Get Set ; }}

So, still not get the answer that you want, go to GitHub on AutoMapper project open a issue, click to see my mention of issue, here, I got the answer I want.

The answer to my answer, he guessed me in the corresponding profile file for each entity class, should use Createmap directly instead of using Createmap in Mapper.initialize. At his prompt, I changed the code, and there was no error in the future.

================================================== should summarize the ========================================================= =========

Now looking back at this question again, is completely clear to the chest feeling. Because I have mapper.initialize () in the static method of Automapperwebconfig static class, this is the initialization of automapper, and it is used once in the corresponding profile class of each entity class. Try to think about it, for the first time, all the text configuration classes are initialized to mapper (it is first understood as a container), and the second call to Mapper.initialize () may erase the previous content. So the use of Mapper.map when the error will be able to understand, and the use of Dynamicmap, where seen before Dynamicmap want to be Pantheon Createmap, then map, so, our previous configuration is dispensable, irrelevant, Because the Dynamicmap will erase the previous configuration, the mapping above is successful. As for other configuration classes, the answer is "this could be an accident!" “。 In this bumpy and tortuous problem-solving process, still learned a lot of things. Here, we are also obviously, can see the blog Park that the park friend use Dynamicmap success without giving why, nine out of ten is also my problem.

================================================== Summary Completed ========================================================= =========

AutoMapper Map and dynamicmap--experts focus on detail, thought and summary

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.