RequiredMethod for MapperFor Attribute - lobodava/artisan-orm GitHub Wiki

The MappingManager looks for the methods by their names: CreateObject, CreateObjectRow, CreateDataTable and CreateDataRow. It is not necessary to include all four reserved methods. If the object is "read only", a Mapper for it may have CreateObject method only.

Because Mappers are static and the only link to the target class is the MapperFor attribute, the compiler does not raise any errors if it cannot find a method with the reserved name.

To make sure the MappingManager checks for the existing of a certain method, the MapperFor attribute may have additional parameter of RequiredMethod enum:

Value Meaning
All Check if all four methods exists: CreateObject, CreateObjectRow, CreateDataTable and CreateDataRow.
AllMain Check if all three main methods exists: CreateObject, CreateDataTable and CreateDataRow.
BothForObject Check if both CreateObject and CreateObjectRow methods exist.
BothForDataTable Check if CreateDataTable and CreateDataRow methods exist.
CreateObject Check if CreateObject method exists.
CreateObjectRow Check if CreateObjectRow method exists.

Examples:

[MapperFor(typeof(RecordType), RequiredMethod.CreateObject)]

[MapperFor(typeof(User), RequiredMethod.AllMain )]

[MapperFor(typeof(User), RequiredMethod.CreateObject, RequiredMethod.BothForDataTable )]

In that case, should the required method not be found, the MappingManager throws NullReferenceException on the first call of any mapper.