简体   繁体   English

不使用名称约定的AutoMapper展平

[英]AutoMapper Flattening without the name convention

I'm trying to flatten my Domain model into the Contract type 我正在尝试将我的域模型展平为合同类型

This is my Model: 这是我的模型:

public interface IPrice
{
    IItem Item { get; set; }
    Money Money {get; set;}
}

public interface IItem
{
    IItemHeader Header { get; set; }
}

public interface IItemHeader
{
    string Name{ get; set; }
}

and this is my Contract: 这是我的合同:

public class Price
{
  public string Name{ get; set; }  
}

Now, I know that if i will change the contract Name field to ItemHeaderName AutoMapper will handle that and will set the correct value when I map between IPrice and Price. 现在,我知道如果将“合同名称”字段更改为ItemHeaderName,则 AutoMapper将处理该问题,并在IPrice和Price之间映射时设置正确的值。 but what if I don't like that name (ie ItemHeaderName) and insist on using " Name " instead? 但是,如果我不喜欢该名称(即ItemHeaderName)并坚持使用“ Name ”怎么办? is there is any alternative? 有没有其他选择?

If you call it just Name, then you need MapFrom. 如果仅将其命名为Name,则需要MapFrom。 You can also have a custom naming convention if the default one doesn't work for you. 如果默认命名约定不适合您,那么您还可以使用自定义命名约定。

We need to do something like that: 我们需要做这样的事情:

 CreateMap<Contract.Entities.Price, Model.Entities.IPrice>().
            ReverseMap().
            ForMember(model => model.Name, model => model.MapFrom(src => src.Item.Header.Name)).

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

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