riok / mapperly

A .NET source generator for generating object mappings. No runtime reflection.

Home Page:https://mapperly.riok.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mapping from the source object

Thundarr1974 opened this issue · comments

When mapping to a Target field there's often a need to access multiple fields from the Source and today this requires creating a custom mapping method like so:

public Address OrderToAddress(Order order)
{
  var ret = MapToAddress(order);
  ret.FullName = string.Concat(order.FirstName, " ", order.LastName);
  return ret;
}

[MapperIgnoreTarget(nameof(Address.FullName))]
private partial Address MapToAddress(Order order);

I propose a new [MapSource] (or some other name) attribute that supports using the Source object as the 'from' parameter like so:

[MapSource(nameof(Address.FullName))]
public partial Address OrderToAddress(Order order);

private string MapToFullName(Order order) => string.Concat(order.FirstName, " ", order.LastName);

Originally posted by @Thundarr1974 in #1145