-
Notifications
You must be signed in to change notification settings - Fork 331
Config inheritance
Atilla Lonny edited this page Aug 13, 2020
·
4 revisions
Type mappings will automatically inherit for source types. Ie. if you set up following config.
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
.Map(dest => dest.Name, src => src.Name + "_Suffix");
A derived type of SimplePoco
will automatically apply the base mapping config.
var dest = TypeAdapter.Adapt<DerivedPoco, SimpleDto>(src);
//dest.Name = src.Name + "_Suffix"
If you don't wish for a derived type to use the base mapping, you can turn it off by using AllowImplicitSourceInheritance
TypeAdapterConfig.GlobalSettings.AllowImplicitSourceInheritance = false;
And by default, Mapster will not inherit destination type mappings. You can turn it on by AllowImplicitDestinationInheritance
.
TypeAdapterConfig.GlobalSettings.AllowImplicitDestinationInheritance = true;
You can copy setting from based type explicitly.
TypeAdapterConfig<DerivedPoco, DerivedDto>.NewConfig()
.Inherits<SimplePoco, SimpleDto>();
You can also include derived type to the based type declaration. For example:
TypeAdapterConfig<Vehicle, VehicleDto>.NewConfig()
.Include<Car, CarDto>();
Vehicle vehicle = new Car { Id = 1, Name = "Car", Make = "Toyota" };
var dto = vehicle.Adapt<Vehicle, VehicleDto>();
dto.ShouldBeOfType<CarDto>();
((CarDto)dto).Make.ShouldBe("Toyota"); //The 'Make' property doesn't exist in Vehicle
- 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