-
Notifications
You must be signed in to change notification settings - Fork 333
Mapping Configuration With "IMapFrom" Interface
Aref Azizian edited this page Feb 9, 2023
·
2 revisions
Before using this feature you have to add this line:
TypeAdapterConfig.GlobalSettings.ScanInheritedTypes(Assembly.GetExecutingAssembly());
With adding above line to your Startup.cs or Program.cs or any other way to run at startup, you can write mapping configs in the destination class that implements IMapFrom interface
Example:
public class InheritedDestinationModel : IMapFrom<SourceModel>
{
public string Type { get; set; }
public int Value { get; set; }
public void ConfigureMapping(TypeAdapterConfig config)
{
config.NewConfig<SourceModel, InheritedDestinationModel>()
.Map(dest => dest.Value, source => int.Parse(source.Value));
}
}
Even if your destination model doesn't have a specific configuration (you don't want to customize anything), you can just inherit from IMapFrom interface
Example:
public class DestinationModel : IMapFrom<SourceModel>
{
public string Type { get; set; }
}
- 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