Skip to content

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; }
}
Clone this wiki locally