Property Handlers
Custom, per-property conversion logic for the cases where the default type mapping isn't enough.
What it is
A property handler is a small class that controls how a single property is converted going into and coming out of the database, for cases the default type mapper can’t handle on its own — encrypted columns, enums stored as strings, or JSON-serialized value objects.
Writing a handler
public class JsonPropertyHandler<T> : IPropertyHandler<string, T>
{
public T Get(string input, ClassProperty property) =>
JsonSerializer.Deserialize<T>(input);
public string Set(T input, ClassProperty property) =>
JsonSerializer.Serialize(input);
}
Attaching it to a property
public class Order
{
[PropertyHandler(typeof(JsonPropertyHandler<ShippingAddress>))]
public ShippingAddress Address { get; set; }
}
Global vs. per-property scope
Handlers can be attached to a single property with an attribute, as shown above, or registered globally for every property of a given CLR type via PropertyHandlerMapper.Add, which is the better choice when the same conversion applies across many entities.