ClassHandler

Learn on how to use the RepoDB ClassHandler attribute.

This attribute marks a class for evaluation by the library compiler during serialization and deserialization. When present, the compiler automatically invokes the class handler’s Get() and Set() methods for custom handling.

Creating a Class Handler

Given a customized IClassHandler implementation:

private class PersonClassHandler : IClassHandler<Person>
{
    public Person Get(Person entity, ClassHandlerGetOptions options)
    {
        EnsureConvertedProperties(entity);
        return entity;
    }

    public Person Set(Person entity, ClassHandlerSetOptions options)
    {
        ValidatePerson(entity);
        return entity;
    }
}

Using the Class Handler

Apply this attribute to the class to use PersonClassHandler during transformation.

[ClassHandler(typeof(PersonClassHandler))]
public class Person
{
	public long Id { get; set; }
	public string Name { get; set; }
	public int Age { get; set; }
	public Address Address { get; set; }
	public DateTime CreatedDateUtc { get; set; }
}

Fluent Mapping

To configure via FluentMapper:

FluentMapper
    .Entity<Person>()
    .ClassHandler<PersonClassHandler>();

Retrieval

Retrieve the mapped class handler via ClassHandlerMapper:

var classHandler = ClassHandlerMapper.Get(typeof(Person));