Skip to content

Conversation

@stormaref
Copy link
Contributor

Creating IMapFrom interface
now destination type can inherit from an interface named IMapFrom and they can configure mapping in the same class like this:

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, _ => DesireValues.Number);
    }
}

and even if you don't implement the method it will automatically create a new config for desire types
ex:

public class DestinationModel : IMapFrom<SourceModel>
{
    public string Type { get; set; }
}

I've added a new extension method to TypeAdapterConfig named ScanInheritedTypes like this:

TypeAdapterConfig.GlobalSettings.ScanInheritedTypes(Assembly.GetExecutingAssembly());

I've created tests for all situations and tests are passed
as soon as this pull request merged I will update wiki page

@stormaref stormaref changed the title add IMapFrom interface Add IMapFrom interface for automatic mapping configuration Feb 2, 2023
@andrerav andrerav merged commit 65b6466 into MapsterMapper:master Feb 7, 2023
@andrerav
Copy link
Contributor

andrerav commented Feb 7, 2023

Thanks @stormaref!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants