Due to the licensing changes for AutoMapper, we decided to make the switch to Mapster. Although most changes where rather obvious and easy to achieve, there was one question we couldn’t answer immediatelly:
"How do I organize my mapping configurations like I did with AutoMapper Profiles?"
While Mapster doesn't have a direct equivalent to AutoMapper's Profile class, it offers enough flexibility to create your own alternative.
The AutoMapper Profile pattern
In AutoMapper, we organized our mappings like this:
We can add the profiles by automatically scanning for profiles:
Mapster's Approach: Configuration classes
Mapster allows you to specify the mapping configuration through the TypeAdapterConfig
class. Here is the same mapping as shown above:
But using this approach doesn’t allow us to scan for configuration classes automatically.
An approach that does work is by creating a class that implements the Mapster IRegistration
interface:
Now we can use the Scan
method to find and register IRegistration
instances:
More information
MapsterMapper/Mapster: A fast, fun and stimulating object to object Mapper