Tuesday, January 31, 2012

using .ResolveUsing with AutoMapper

If you had an AutoMapper map like so:

Mapper.CreateMap<Person, PersonInputModel>().ForMember(personInputModel => personInputModel.FullName, person => person.ResolveUsing<PersonNameResolver>());

 
 

.ResolveUsing would allow you to reach out to another method to undertake a tricky translation.

using System.Linq;

using OurApp.Core.Entities;

using AutoMapper;

namespace OurApp.Web.UI.Config.AutoMapper.Resolvers

{

   public class OrderStatusReasonResolver : ValueResolver<Person, string>

   {

      protected override string ResolveCore(Person source)

      {

         var fullName = source.FirstName + " " + source.LastName;

         return fullName.Trim();

      }

   }

}

No comments:

Post a Comment