Link Search Menu Expand Document

Class Handlers


This is a feature that would allow you to handle the event during the serialization/deserialization process of the entity model and the data reader object. It enables you as developer to handle any kind of serialization event within the model, a validation of the serializable data and/or even trigger a workflow during and after the transformation process.

It uses the following objects.

NameDescription
IClassHandleran interface to mark your class as class handler.
ClassHandleran attribute used to map a class handler into a specific .NET CLR type.
ClassHandlerMappera mapper used to map a class handler into a specific .NET CLR type.
FluentMappera fluent mapper class used to map a class handler into a specific .NET CLR type.

How does it works?

If you are reading a data from the DB (i.e.: ExecuteQuery, Query, BatchQuery), the Get() method will be invoked after deserializing the model. On the other hand, if you are pushing a data towards the DB (i.e.: Insert, Merge, Update), the Set() method will be invoked prior the actual DB 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 various ways of mapping a class handler into an entity model.

Firstly, via the ClassHandlerMapper class.

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

Secondly, via the FluentMapper class.

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

Lastly, via an explicit ClassHandler attribute.

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