Skip to content

Instantly share code, notes, and snippets.

@jtheisen
Last active April 30, 2026 08:46
Show Gist options
  • Select an option

  • Save jtheisen/c8d922b1ab9f2b322779558ad20bccb9 to your computer and use it in GitHub Desktop.

Select an option

Save jtheisen/c8d922b1ab9f2b322779558ad20bccb9 to your computer and use it in GitHub Desktop.
Manual mapping with extension methods
public static class OrderMappingsExtensions
{
public static void Fill(this FaithfulOrderDto target, Order source)
{
target.Total = source.Total;
target.Customer = MapTo<CustomerDto>().From(c => c.Fill(source.Customer));
// Additionally, we also should have some syntax for ignoring properties
// to allow a generic test that automatically discovers all Fill methods
// know which properties are deliberately ignored. Not sure which syntax
// I'd prefer.
// A Nicest and probably doable, but slightly magical
// Ignore(target.IgnoredProperty);
// B More basic and also ok
// Ignore(nameof(FaithfulOrderDto.IgnoredProperty));
// C Less text but too much into the AutoMapper direction?
// Ignore(() => target.IgnoredProperty);
}
}
public static class CustomerMappingsExtensions
{
public static void Fill(this CustomerDto target, Customer source)
{
target.Name = source.Name;
}
}
public static class MappingExtensions
{
public class Helper<TTarget>
where TTarget : new()
{
public TTarget From(Action<TTarget> fill)
{
var target = new TTarget();
fill(target);
return target;
}
}
public static Helper<TTarget> MapTo<TTarget>()
where TTarget : new()
{
return new Helper<TTarget>();
}
}
public class Order
{
public decimal Total { get; set; }
public Customer? Customer { get; set; }
}
public class Customer
{
public string? Name { get; set; }
}
public class FlatOrderDto
{
public decimal Total { get; set; }
public string? CustomerName { get; set; }
}
public class CustomerDto
{
public string? Name { get; set; }
}
public class FaithfulOrderDto
{
public decimal Total { get; set; }
public CustomerDto? Customer { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment