Mapping Data Annotations

23 views
Skip to first unread message

jwil...@gmail.com

unread,
May 11, 2017, 3:31:38 PM5/11/17
to AutoMapper-users
Based on experiment and observation, I'm 99% certain that AutoMapper does not map data annotations, at least by default.  Can't find anything pertaining to this in the documentation and my searches on Stack Overflow found limited results.  The most popular answer is extensions to DataAnnotationsModelMetadataProvider and DataAnnotationsModelValidatorProvider, but when I implemented the provided code, it gives me errors about duplicate "required" annotations.  This is a fairly old SO answer, too, which makes me hopeful it's been improved on within AutoMapper. 

Before I dig farther into the complicated (at least to me) solution, I thought I'd ask if there's something similar I'm missing.  This seems like an important issue that there should be an accepted approach for, but I can't seem to find it. 

Jimmy Bogard

unread,
May 11, 2017, 3:50:54 PM5/11/17
to automapper-users
What is the solution? Custom metadata providers?

I just don't apply data annotations to my persistence/domain model. Only to DTOs. Then there's rarely duplication.

On Thu, May 11, 2017 at 2:31 PM, <jwil...@gmail.com> wrote:
Based on experiment and observation, I'm 99% certain that AutoMapper does not map data annotations, at least by default.  Can't find anything pertaining to this in the documentation and my searches on Stack Overflow found limited results.  The most popular answer is extensions to DataAnnotationsModelMetadataProvider and DataAnnotationsModelValidatorProvider, but when I implemented the provided code, it gives me errors about duplicate "required" annotations.  This is a fairly old SO answer, too, which makes me hopeful it's been improved on within AutoMapper. 

Before I dig farther into the complicated (at least to me) solution, I thought I'd ask if there's something similar I'm missing.  This seems like an important issue that there should be an accepted approach for, but I can't seem to find it. 

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jwil...@gmail.com

unread,
May 11, 2017, 4:49:43 PM5/11/17
to AutoMapper-users
Thanks, Jimmy.

That's been my leading option, but for Code First design in Entity Framework, don't you need at least some of the annotations in the model?  Also, I expect to need different view models for editing a record vs. displaying a grid of them, so I don't see a way around some duplication.  Maybe I just need to give it more thought.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.

Jimmy Bogard

unread,
May 11, 2017, 4:57:05 PM5/11/17
to automapper-users
Oh yeah, I guess we use the class map way of doing it and not the attributes. That's why it tends to work.

But now that I think about it, there is still some duplication....

To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages