Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Generic class mapping with inheritance

Open luczito opened this issue 9 months ago • 1 comments

I am in the process of refactoring a project from automapper to mapster. I have run into an issue with mapping configs for generic classes e.g.

CreateMap(typeof(derivedDto<>), typeof(derivedDomain<>))
        .IncludeBase(typeof(baseDto), typeof(baseDomain)
        .ReverseMap();

As far as i can tell i can create a config for the base class via mapster as:

TypeAdapterConfig.GlobalSettings.ForType(typeof(derivedDto<>), typeof(derivedDomain<>))
    .Map("value", "Value");

But i (as far as i can tell) can not use .Include or .Inherits to include the inheritance information in the mappings.

Is this possible somehow or do i need to create a custom mapper to achieve this?

luczito avatar Apr 11 '25 12:04 luczito

@luczito This is not included in the standard distribution because there is no way to determine that you are actually copying settings for types within same inheritance hierarchy.

~~You can Create Custom Helper~~.

Update

As a custom Helpers it won't work either.

DocSvartz avatar Apr 11 '25 14:04 DocSvartz