Property Handler (Property Level))
This page contains the recommended way of implementing a property handler for the class properties.
PropertyHandler
Create a class that implements the IPropertyHandler interface.
public class PersonAgePropertyHandler : IPropertyHandler<int?, string>
{
public string Get(int? input, ClassProperty property)
{
return input.HasValue ? $"{input.Value} years old" : "N/A";
}
public int? Set(string input, ClassProperty property)
{
return !string.IsNullOrEmpty(input) ? $"{Regex.Replace(key, "[^0-9]", string.Empty)}" : null;
}
}
Use the Newtonsoft.Json package if you are using JSON conversion.
public class PersonAddressPropertyHandler : IPropertyHandler<string, Address>
{
public Address Get(string input, ClassProperty property)
{
return !string.IsNullOrEmpty(input) ? JsonConvert.Deserialize<Address>(input) : null;
}
public string Set(Address input, ClassProperty property)
{
return input != null ? JsonConvert.Serialize(input) : null;
}
}
Mapping
Explict
Use the PropertyHandler attribute to map the property handlers into the class properties.
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
[PropertyHandler(typeof(PersonAgePropertyHandler))]
public string Age { get; set; }
public DateTime DateOfBirth { get; set; }
[Map("Address"), PropertyHandler(typeof(PersonAddressPropertyHandler))]
public Address Location { get; set; }
public DateTime CreatedDate { get; set; }
}
Implicit
Use the FluentMapper class to map the property handlers into the class properties.
FluentMapper
.Entity<Person>()
.PropertyHandler<PersonAgePropertyHandler>(e => e.Age)
.Column(e => e.Location, "Address")
.PropertyHandler<PersonAddressPropertyHandler>(e => e.Location);
Key Take-aways
- Ensure to make the
TInput
generic typenullable
if the database column isnullable
. - Make the code snippets to both
Get()
andSet()
methods concise and highly performant. - Name the property handlers as correspond into its own purpose.
- Mishandling of the implementation would make your application suffer in performance.