Eitan
Eitan

Reputation: 173

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. but what if I don't like that name (i.e. ItemHeaderName) and insist on using "Name" instead? is there is any alternative?

Upvotes: 1

Views: 229

Answers (2)

Eitan
Eitan

Reputation: 173

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)).

Upvotes: 0

Lucian Bargaoanu
Lucian Bargaoanu

Reputation: 3516

If you call it just Name, then you need MapFrom. You can also have a custom naming convention if the default one doesn't work for you.

Upvotes: 2

Related Questions