Link Search Menu Expand Document

Class Handlers


This feature allows you to intercept the serialization and deserialization process of an entity model and the data reader object. It enables you to handle serialization events, validate serializable data, or trigger workflows during and after the transformation process.

It uses the following objects:

NameDescription
IClassHandlerAn interface to mark your class as a class handler.
ClassHandlerAn attribute used to map a class handler to a specific .NET CLR type.
ClassHandlerMapperA mapper used to map a class handler to a specific .NET CLR type.
FluentMapperA fluent mapper class used to map a class handler to a specific .NET CLR type.

How does it works?

When reading data from the database (e.g., ExecuteQuery, Query, BatchQuery), the Get() method is invoked after deserializing the model. When pushing data to the database (e.g., Insert, Merge, Update), the Set() method is invoked before the actual database operation.

Implementing a Class Handler

Create a class that implements the IClassHandler interface.

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

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

Mapping a Class Handler

There are several ways to map a class handler to an entity model.

Via the ClassHandlerMapper class:

ClassHandlerMapper
    .Add<Person, PersonClassHandler>(true);

Via the FluentMapper class:

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

Via the ClassHandler attribute:

[ClassHandler(typeof(PersonClassHandler))]
publi class Person
{
    ...
}