Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping to derived type not working as expected #664

Closed
luczito opened this issue Nov 24, 2023 · 0 comments
Closed

Mapping to derived type not working as expected #664

luczito opened this issue Nov 24, 2023 · 0 comments

Comments

@luczito
Copy link

luczito commented Nov 24, 2023

Suppose I have 2 base classes with derived classes as:

public abstract class Base{
      public int Value {get; set;}
}

public abstract class DtoBase{
         public int Value {get; internal set; }

         protected DtoBase(int value)
        {
                 Value = value;
        } 
}

public class DerivedBase{

         public DerivedBase()
         {}

         public DerivedBase(int value)
          {
                Value = value;
          }
}

public class DerivedDtoBase{

            public DerivedDtoBase()
            {}

             public DerivedDtoBase(int value)
            {
                Value = value;
            }
}

With the config:

TypeAdapterConfig.NewConfig<Base, DtoBase>()
           .Include<DerivedBase, DerivedDtoBase>();
TypeAdapterConfig.NewConfig<DtoBase, Base>()
           .Include<DerivedDtoBase, DerivedBase>();

When mapping from DerivedBase to BaseDto i expect it to map to the DerivedDtoBase, instead i get the error:

Error while compiling source=DerivedBase
destination=DtoBase
type=Map
Cannot convert immutable type, please consider using 'MapWith' or 'ConstructUsing' method to create mapping.

Is there a way to configure the mapping logic to automatically map to the corresponding derived Dto class and vice versa since this is a scenario that is common on our platform?

Or do i need to specifically define the config as:

TypeAdapterConfig.NewConfig<DerivedBase, DtoBase>()
          .ConstructUsing(src => new DerivedDtoBase(src.Value));

Which works but is quite a lot of custom configuration considering the amount of times we perform this type of derived mapping.

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

No branches or pull requests

1 participant