-
Notifications
You must be signed in to change notification settings - Fork 333
Config validation & compilation
To validate your mapping in unit tests and in order to help with "Fail Fast" situations, the following strict mapping modes have been added.
Forcing all classes to be explicitly mapped:
//Default is "false"
TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = true;
//This means you have to have an explicit configuration for each class, even if it's just:
TypeAdapterConfig<Source, Destination>.NewConfig();
Forcing all destination properties to have a corresponding source member or explicit mapping/ignore:
//Default is "false"
TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;
Both a specific TypeAdapterConfig<Source, Destination> or all current configurations can be validated. In addition, if Explicit Mappings (above) are enabled, it will also include errors for classes that are not registered at all with the mapper.
//Validate a specific config
var config = TypeAdapterConfig<Source, Destination>.NewConfig();
config.Compile();
//Validate globally
TypeAdapterConfig<Source, Destination>.NewConfig();
TypeAdapterConfig<Source2, Destination2>.NewConfig();
TypeAdapterConfig.GlobalSettings.Compile();
Mapster will automatically compile mapping for first time usage.
var result = poco.Adapt<Dto>();
However, you can explicitly compile mapping by Compile
method.
//Global config
TypeAdapterConfig.GlobalSettings.Compile();
//Config instance
var config = new TypeAdapterConfig();
config.Compile();
Calling Compile
method on start up helps you validate mapping and detect problem on start-up time, not on run-time.
NOTE: After compile, when you change setting in config, it will generate errors. Therefore, make sure you finish configuration before calling Compile
.
- Configuration
- Config inheritance
- Config instance
- Config location
- Config validation & compilation
- Config for nested mapping
- Custom member matching logic
- Constructor mapping
- Before & after mapping
- Setting values
- Shallow & merge mapping
- Recursive & object references
- Custom conversion logic
- Inheritance