MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.38k stars 331 forks source link

Inheritance and scanning assembly for IRegister. #726

Open vchc opened 2 months ago

vchc commented 2 months ago

If you use IRegister to define the inheritance mapping like this:

DerivedPocoProfile.cs

public class DerivedPocoProfile : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        .NewConfig<DerivedPoco, DerivedDto>()
        .Inherits<SimplePoco, SimpleDto>();
    }
}

SimplePocoProfile.cs

public class SimplePocoProfile : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        .NewConfig<SimplePoco, SimpleDto>();
    }
}

and then use scan assembly for types:

TypeAdapterConfig.GlobalSettings.Scan(typeof(DerivedPocoProfile).Assembly);

there is no guarantee that the base type will be registered first and

.Inherits<SimplePoco, SimpleDto>();

will fail silently, leaving your mapping inconsistent.

The current implementation of Inherits does nothing and simply returns when the type tuple has not yet been registered.