Link Search Menu Expand Document

Property Handler (Type Level)


This page contains the recommended way of implementing a property handler for specific type.

This property handler is based on database column type (database-bound).

PropertyHandler

Create a class that implements the IPropertyHandler interface.

public class DateTimeKindToUtcPropertyHandler : IPropertyHandler<datetime?, datetime?>
{
    public datetime? Get(datetime? input, ClassProperty property)
    {
        return input.HasValue ? DateTime.SpecifyKind(input.Value, Kind.Utc) : null;
    }

    public datetime? Set(datetime? input, ClassProperty property)
    {
        return input.HasValue ? DateTime.SpecifyKind(input.Value, Kind.Unspecified) : null;
    }
}

public class GuidToStringPropertyHandler : IPropertyHandler<Guid?, string>
{
    public string Get(Guid? input, ClassProperty property)
    {
        return input.HasValue ? input.ToString() : null;
    }

    public Guid? Set(string input, ClassProperty property)
    {
        var output = Guid.Empty;
        if (!string.IsNullOrEmpty(input) && Guid.TryParse(input, out output))
        {
            return output;
        }
        return null;
    }
}

Mapping

Explict

Use the PropertyHandlerMapper class to map the property handlers into the target types.

PropertyHandlerMapper.Add(typeof(DateTime), new DateTimeKindToUtcPropertyHandler());
PropertyHandlerMapper.Add(typeof(Guid), new GuidToStringPropertyHandler());

Implicit

Use the FluentMapper class to map the property handlers into the target types.

FluentMapper
    .Type<DateTime>()
    .PropertyHandler<DateTimeKindToUtcPropertyHandler>();

FluentMapper
    .Type<Guid>()
    .PropertyHandler<GuidToStringPropertyHandler>();

Key Take-aways

  • Ensure to always make both the TInput and TResult generic types nullable. It is handling both the properties/columns that are nullables/non-nullables.
  • Make the code snippets to both Get() and Set() method concise and highly performant.
  • Name the property handler corresponds to its purpose.
  • Mishandling of the implementation would make your application suffer in performance.