简体   繁体   中英

Automapper - how to create map for this specific value object?

This is a representation of my domain model:

public class AddressInfo
    {
        private readonly string addressee;
        private readonly string company;
        private readonly string city;



        public string Addressee
        {
            get { return addressee; }
        }

        public string Company
        {
            get { return company; }
        }

        public string City
        {
            get { return city; }
        }
    }



 public class Address
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public AddressInfo AddressInfo { get; set; }
        }

This is my entity class:

public class AddressEntity
{
    public  int Id { get; set; }
    public string Name { get; set; }
    public AddressInfo AddressInfo { get; set; }
}

This is the representation of my repository where I am able to retrieve the values but not map them in the way I want to.

 public class AddressRepository
    {
      public static void CreateMappings()
        {
         //How to construct a map here to take care of 
         //the map creation of AddressInfo??

         Mapper.CreateMap<AddressEntity, Address>();
        }

      public IEnumerable<Address> GetAllAddresses (User user)
        {
            var addressentities = GetAddresses(user.Id);
            return addressentities == null? 
                                 null: 
                                 Mapper.Map<IEnumerable<Address>>(addressentities);
        }


    }

How to create the map so that AddressInfo is taken care of ????
Currently it is not populated with the retrieved values as it is obviously not mapped.

It looks like your AddressInfo class doesn't have setters or constructors parameters who then go set to your addressee, company, city backing variables. So there is no way for AutoMapper to set the data.

If you wanted to control how AutoMapper maps you can create a class that inherits from TypeConverter .

//This is also easier to unit test if you have conversions that are not 100% automappable.
public class AddressEntityToAddress : TypeConverter<AddressEntity, Address>
{
    protected override Address ConvertCore(AddressEntity source)
    {
        if(source == null)
        {
            return null;
        }

        var destination = new Address
        {
            Id = source.Id,
            DisplayName = source.DisplayName,
            AddressInfo = source.AddressInfo
        };  

            //do other magic you may want during conversion time

        return destination;
    }
}

After you create the type converter class you can set AutoMapper to use it like this in your CreateMapping method:

public static void CreateMappings()
{
    Mapper.CreateMap<AddressEntity, Address>().ConvertUsing<AddressEntityToAddress>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM