-
Notifications
You must be signed in to change notification settings - Fork 331
Configuration
You can easily create settings for a type mapping by using: TypeAdapterConfig<TSource, TDestination>.NewConfig()
.
When NewConfig
is called, any previous configuration for this particular TSource => TDestination mapping is dropped.
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
.Ignore(dest => dest.Age)
.Map(dest => dest.FullName,
src => string.Format("{0} {1}", src.FirstName, src.LastName));
As an alternative to NewConfig
, you can use ForType
in the same way:
TypeAdapterConfig<TSource, TDestination>
.ForType()
.Ignore(dest => dest.Age)
.Map(dest => dest.FullName,
src => string.Format("{0} {1}", src.FirstName, src.LastName));
ForType
differs in that it will create a new mapping if one doesn't exist, but if the specified TSource => TDestination
mapping does already exist, it will enhance the existing mapping instead of dropping and replacing it.
Use global settings to apply policies to all mappings.
TypeAdapterConfig.GlobalSettings.Default.PreserveReference(true);
Then for individual type mappings, you can easily override the global setting(s).
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig().PreserveReference(false);
To set the setting at a more granular level. You can use the When
method in global settings.
In the example below, when any source type and destination type are the same, we will not the copy the Id
property.
TypeAdapterConfig.GlobalSettings.When((srcType, destType, mapType) => srcType == destType)
.Ignore("Id");
In this example, the config would only apply to Query Expressions (projections).
TypeAdapterConfig.GlobalSettings.When((srcType, destType, mapType) => mapType == MapType.Projection)
.IgnoreAttribute(typeof(NotMapAttribute));
Setting also be able to done without knowing the source type, by using ForDestinationType
. For example, you can do AfterMapping
setting to validate after mapping.
TypeAdapterConfig.GlobalSettings.ForDestinationType<IValidator>()
.AfterMapping(dest => dest.Validate());
NOTE: ForDestinationType
above will always apply to all types assignable to IValidator
. If destination class implements IValidator
, it will also apply the AfterMapping
config.
If mapping type is generic, you can create setting by passing generic type definition to ForType
.
TypeAdapterConfig.GlobalSettings.ForType(typeof(GenericPoco<>), typeof(GenericDto<>))
.Map("value", "Value");
- 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