How to use AutoMapper?

10,028

Ok so I can see a few things you are doing that probably won't help.

Firstly this AutoMapper is used to copy Properties in one object to Properties in a diff object. Along the way it might interrogate or manipulate them to get the end result viewmodel in the correct state.

  1. The properties are named 'Get...' which sounds more like a method to me.
  2. The setters on your properties are private so AutoSetter won't be able to find them. Change these to minimum internal.
  3. Use of a parametrized constructor is no longer needed when you use AutoMapper - as you are converting directly from one object to another. The parametised constructor is there mainly to show what is explicitly required by this object.

    CreateMap<Address, AddressEditViewModel>()
             .ForMember( x => x.GetOneAddressByDistrictGuid , 
                              o => o.MapFrom( m => m."GetOneAddressByDistrictGuid") )
             .ForMember( x => x.GetZipCodes, 
                              o => o.MapFrom( m => m."GetZipCodes" ) );
    

What Automapper is really good for is copying from DataObjects into POCO objects, or View Model objects.

    public class AddressViewModel
    {
              public string FullAddress{get;set;}
    }

    public class Address
    {
              public string Street{get;set;}
              public string Suburb{get;set;}        
              public string City{get;set;}
    }

    CreateMap<Address, AddressViewModel>()
             .ForMember( x => x.FullAddress, 
                              o => o.MapFrom( m => String.Format("{0},{1},{2}"), m.Street, m.Suburb, m.City  ) );

    Address address = new Address(){
        Street = "My Street";
        Suburb= "My Suburb";
        City= "My City";
    };

    AddressViewModel addressViewModel = Mapper.Map(address, Address, AddressViewModel); 
Share:
10,028
Mark Buckley
Author by

Mark Buckley

Updated on June 15, 2022

Comments

  • Mark Buckley
    Mark Buckley almost 2 years

    First time using AutoMapper and I'm have a hard time figuring out how to use it. I'm trying to map a ViewModel to my Database Tables.

    My ViewModel looks like this...

    public class AddressEditViewModel
    {
        public AddressEdit GetOneAddressByDistrictGuid { get; private set; }
        public IEnumerable<ZipCodeFind> GetZipCodes { get; private set; }
    
        public AddressEditViewModel(AddressEdit editAddress, IEnumerable<ZipCodeFind> Zips)
        {
            this.GetOneAddressByDistrictGuid = editAddress;
            this.GetZipCodes = Zips;
        }
    }   
    

    The Mapping I'm trying to use is...

    CreateMap<Address, AddressEditViewModel>();  
    

    When I run this test...

    public void Should_map_dtos()
    {
        AutoMapperConfiguration.Configure();
        Mapper.AssertConfigurationIsValid();
    }  
    

    I get this error...

    AutoMapper.AutoMapperConfigurationException: The following 2 properties on JCIMS_MVC2.DomainModel.ViewModels.AddressEditViewModel are not mapped: GetOneAddressByDistrictGuid GetZipCodes Add a custom mapping expression, ignore, or rename the property on JCIMS_MVC2.DomainModel.Address.

    I'm not sure how I am supposed to map those 2 properties. I would appreciate any direction. Thanks

    Mark