I'm looking for an elegant way (a design pattern if such exists, not a library) to map two classes that share the same base class, without duplicating the code that maps the properties of the base class.
Let's say I have
public abstract class RequestBase
{
public DateTime BornAt { get; set; }
}
public class RequestV1 : RequestBase
{
public string Name { get; set; }
}
public class RequestV2 : RequestBase
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
and I want to
var record = this.MapFrom(someV2Request);
where
public PersonRecord MapFrom(RequestV1 request) => new PersonRecord
{
Name = request.Name,
BornAt = request.BornAt,
};
public PersonRecord MapFrom(RequestV2 request) => new PersonRecord
{
Name = $"{request.FirstName} {request.LastName}",
BornAt = request.BornAt,
};
but I don't want both methods to implement the mapping of the base class property BornAt
independently.
There must be some elegant solution that I can't come up with.