简体   繁体   English

Automapper-如何为此特定值对象创建映射?

[英]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 ???? 如何创建地图以使AddressInfo得到处理?
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. 看来您的AddressInfo类没有设置器或构造函数参数,然后将它们设置为您的收件人,公司,城市后备变量。 So there is no way for AutoMapper to set the data. 因此, AutoMapper无法设置数据。

If you wanted to control how AutoMapper maps you can create a class that inherits from TypeConverter . 如果要控制AutoMapper映射方式,则可以创建一个从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: 创建类型转换器类后,可以将AutoMapper设置为在CreateMapping方法中使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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