AutoMapperProfiles code example

Example: AutoMapperProfiles

public class PersonToPersonViewModelMapper : IMapper<Person, PersonViewModel>
    {
        private NoteToNoteViewModelMapper _noteToNoteViewModelMapper = new NoteToNoteViewModelMapper();
        private AddressToAddressViewModelMapper _addressToAddressViewModelMapper = new AddressToAddressViewModelMapper(); 
        public PersonToPersonViewModelMapper()
        {
            AutoMapper.Mapper.CreateMap<Person, PersonViewModel>();
        } 
        public PersonViewModel Map(Person source)
        {
            var destination = AutoMapper.Mapper.Map<Person, PersonViewModel>(source); 
            destination.Address = source.Address.MapUsing(_addressToAddressViewModelMapper); 
            destination.Notes = source.Notes.MapAllUsing(_noteToNoteViewModelMapper); 
            return destination;
        }
    }
    public class AddressToAddressViewModelMapper : IMapper<Address, AddressViewModel>
    {
        public AddressToAddressViewModelMapper()
        {
            AutoMapper.Mapper.CreateMap<Address, AddressViewModel>()
                .ForMember(x => x.PersonAddressLineOne, opt => opt.MapFrom(source => source.FirstLine))
                .ForMember(x => x.PersonCountryOfResidence, opt => opt.MapFrom(source => source.Country));
        } 
        public AddressViewModel Map(Address source)
        {
            var destination = AutoMapper.Mapper.Map<Address, AddressViewModel>(source);
            return destination;
        }
    }
    public class NoteToNoteViewModelMapper : IMapper<Note, NoteViewModel>
    {
        public NoteToNoteViewModelMapper()
        {
            AutoMapper.Mapper.CreateMap<Note, NoteViewModel>();
        } 
        public NoteViewModel Map(Note source)
        {
            var destination = AutoMapper.Mapper.Map<Note, NoteViewModel>(source);
            return destination;
        }
    }

Tags:

Abap Example