Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AutoMapper?

Tags:

c#

automapper

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

like image 302
Mark Buckley Avatar asked Apr 26 '10 21:04

Mark Buckley


Video Answer


1 Answers

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); 
like image 88
Dave Walker Avatar answered Oct 11 '22 03:10

Dave Walker