Value Conversions
Value conversions let you transform property values during mapping — for example, formatting a DateTime as a string or converting between custom types.
Defining conversions
Use the Convert and ConvertBack parameters on [MapFrom]:
[Facette(typeof(Employee), "SocialSecurityNumber")]
public partial record EmployeeDto
{
[MapFrom("HireDate", Convert = nameof(FormatDate), ConvertBack = nameof(ParseDate))]
public string HiredOn { get; init; } = "";
public static string FormatDate(DateTime dt) => dt.ToString("yyyy-MM-dd");
public static DateTime ParseDate(string s) => DateTime.Parse(s);
}
Method requirements
Convert— used inFromSourceandProjection. Must be astaticmethod on the DTO type that takes the source property type and returns the DTO property type.ConvertBack— used inToSource. Must be astaticmethod on the DTO type that takes the DTO property type and returns the source property type.
You can specify one without the other:
Convertonly —ToSourcewill skip this property (or you can disableGenerateToSource)ConvertBackonly — useful whenFromSourcecan map directly but the reverse needs transformation
Generated output
// FromSource
HiredOn = EmployeeDto.FormatDate(source.HireDate)
// ToSource
HireDate = EmployeeDto.ParseDate(this.HiredOn)
// Projection
HiredOn = EmployeeDto.FormatDate(source.HireDate)
Diagnostics
If the referenced method doesn't exist or isn't a static method on the DTO type:
Tips
- Keep conversion methods simple — they're called per-property, per-mapping operation
- Remember that
Projectionconversions must be translatable by your LINQ provider if used with EF Core. Method calls in projections may not translate to SQL - For enum↔string/int conversions, see Enum Conversion — Facette handles those automatically without custom converters