Using Automapper ValueResolver for complex, reusable mapping

Tuesday, October 23, 2012
Automapper is a great tool to help automate the tedious task of mapping Domain Model to View Model and back.  However, sometimes, mappings are not as straightforward as a simple one to one, left to right mapping.  The example below is a real-world implementation of ValueResolver for mapping date of birth to age:

using System;
using AutoMapper;

namespace SomeCompany.SomeProject.Web.Models.ValueResolvers
{
    public class AgeValueResolver : ValueResolver<DateTime?, int>
    {
        protected override int ResolveCore(DateTime? dateOfBirth)
        {
            if (dateOfBirth.HasValue)
            {
                var age = DateTime.Today.Year - dateOfBirth.Value.Year;

                if (dateOfBirth.Value > DateTime.Today.AddYears(-age))
                {
                    age--;
                }

                return age;
            }

            return 0;
        }
    }
}

To use this within your mapping, you could do something like:

CreateMap<IPerson, PersonViewModel>()
  .ForMember(dest => dest.Age, opt => opt.ResolveUsing<AgeValueResolver>().FromMember(src => src.DOB));

21 comments:

Robert said...

Its very nice to see your contribution in this post. You have shown your talent and experience in your post. Thanks for sharing this post with all. psd to html

for ict 99 said...

Automapper is a great tool to help automate the tedious task Dot Net Training in Chennai C# real-world implementation of ValueResolver for mapping date C-Sharp Courses C# Training

Unknown said...

You shared useful post. Happy to visit your blog. Thanks for sharing.

seo training in chennai

Unknown said...

Excellent post! Thanks for sharing such a useful post.

web design course in chennai

Unknown said...

hi welcome to this blog.really you have post an informative blog.it will be really helpful to many peoples.
codedUI training

Unknown said...

Thank you admin for your valuable article. I really impressed with your blog. SAS Training in Chennai | Devops Online Training | SQL Server DBA Online Training

Unknown said...

Nice post.Thanks for sharing.
SEO Training In Chennai
SEO Training Institute In Chennai

sathyaramesh said...

Thank you for this wonderful post, It really nice and informative. Keep sharing!!
German Classes in Chennai
Big Data Training in Chennai
SEO Training in Chennai
Selenium Training in Chennai
Digital Marketing Training in Chennai
JAVA Training in Chennai
Android Course in Chennai
Android Training Chennai

Amy Wong said...
This comment has been removed by the author.
Amy Wong said...

Hello today, together with a friend found a bombed little sylochku to entertain and to spend fun time Lucky online casino canada do you want as well as we have fun come in

smart outsourcing solutions said...

outsourcingall.com "Nice and helpful information provided by you. Thanks Buddy
free seo training in dhaka bangladesh
Freelancing Training Center
Best Website Development and Design Company in Bangladesh

sangeetha sathyan said...

Learned a lot from your post and it is really good. Share more tech updates regularly.
ui ux design course in Chennai
ui design course in chennai
ui developer course in chennai
Ethical Hacking course in Chennai
Web Designing Course in chennai
Web development training in chennai
PHP Training in Chennai
ui ux design course in Anna Nagar
ui ux design course in Vadapalani
ui ux design course in Thiruvanmiyur

divi said...

thanks for your information really good and very nice web design company in velachery

Adhuntt said...

Great blog thanks for sharing Take care of all your search engine optimization SEO, graphic design, logo creation, social media marketing and digital branding need at one stop - Adhuntt Media. Customer satisfaction and service is our priority - We tread that fine line between projecting your visions into tangible reality! Why wait when you can begin your digital marketing journey with us right now at Adhuntt Media.
digital marketing company in chennai

Karuna said...

Nice blog thanks for sharing Wondering where to buy healthy garden plants in Chennai for your home? Karuna Nursery Gardens offers you with tons of variety and organic choices suitable for our tropical climate. Make your garden fabulous with Karuna Nursery.
plant nursery in chennai

Pixies said...

Excellent blog thanks for sharing Looking for the best place in Chennai to get your cosmetics at wholesale? The Pixies Beauty Shop is the premium wholesale cosmetics shop in Chennai that has all the international brands your salon deserves.
Cosmetics Shop in Chennai

Malcom Marshall said...

Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.

best digital marketing course in chennai
SKARTEC Digital Marketing
best digital marketing training in chennai
best seo training in chennai
online digital marketing training
best marketing books
best marketing books for beginners
best marketing books for entrepreneurs
best marketing books in india
digital marketing course fees
best seo service in chennai
SKARTEC SEO Services
best digital marketing resources
best digital marketing blog
digital marketing expert
how to start affiliate marketing
what is affilite marketing and how does it work
affiliate marketing for beginners

vivekvedha said...

Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
Digital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery

Anu said...

Excellent and very cool idea and great content of different kinds of the valuable information's. DevOps Training in Bangalore | Certification | Online Course Training | DevOps Training in Hyderabad | Certification | Online Course Training | DevOps Training in Coimbatore | Certification | Online Course Training | DevOps Training in Online | Certification | Online Course Training

Revathi said...

Thanks for sharing helpful information, I really like your all post. I will bookmark your blog for future updates.keep it up!!
android training in chennai

android online training in chennai

android training in bangalore

android training in hyderabad

android Training in coimbatore

android training

android online training

Unknown said...

IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes. IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble.Final Year Projects for CSE

Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining .

Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

The Angular Training covers a wide range of topics including Angular Directives, Angular Services, and Angular programmability.Angular Training

Post a Comment