is there any way to map 2 Models once in my application ( Mapper.CreateMap()) for example in global.asax and then wherever necessary just call Mapper.Map() in codes ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes you can, and in fact that’s the recommended way to configure AutoMapper. Creating the initial mappings is the (relatively) expensive part of AutoMapper, so you want to do it only once.
As you suggest,
Global.asaxis a good place to do it. Although it’s a good idea to put it in a separate class, for exampleBootstrapper, that theApplication_Startmethod calls into, such that this class and method can also be called from your unit tests.From the AutoMapper docs:
See also this question, which has some good ideas including a suggestion from Jimmy Bogard, who wrote AutoMapper.