Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Can not override default mapping

Open it4factory opened this issue 4 years ago • 1 comments

I would like to have a default mapping used always and then I would like to be able to override it when required.

Here is the modified example code

 public SchoolController(IMapper mapper, SchoolContext context)
    {
        _mapper = mapper;
        _context = context;

        // the defaut mapping used always
        TypeAdapterConfig<Course, CourseDto>.ForType()
            .Map(dest => dest.CourseIDDto, src => src.CourseID)
            .Map(dest => dest.CreditsDto, src => src.Credits)
            .Map(dest => dest.TitleDto, src => src.Title + " DEFAULT")
            .Ignore(d => d.IgnoreTest)
            .Map(dest => dest.EnrollmentsDto, src => src.Enrollments);

    }
    
    // OData Sample
    [HttpGet("course")]
    [EnableQuery]
    public IQueryable<CourseDto> GetCourses()
    {
        var query = _context.Courses.AsNoTracking();

        TypeAdapterConfig cloneConfig = TypeAdapterConfig.GlobalSettings.Clone() ; 

        TypeAdapterConfig clonedConfig = cloneConfig
            .ForType<Course, CourseDto>()
             .Map(dest => dest.AnotherTestDto, src => (src.Title + " NEW-CLONE"))
             .Map(dest => dest.TitleDto, src => (src.Title + " over-CLONE"))
             .Map(dest => dest.IgnoreTest, src => (src.Title + " over-IGNORE-CLONE"))
          .Config
             ;

        
        return query
   .ProjectToType<CourseDto>(clonedConfig)
   .AsQueryable();
      
    }

Here is the result

{
"courseIDDto":1045,
"titleDto":"Calculus DEFAULT",         // **EXPECTED "Calculus over-CLONE"
"anotherTestDto":"Calculus NEW-CLONE",
"creditsDto":4,
"ignoreTest":null,                            // **EXPECTED "Calculus over-CLONE"
"enrollmentsDto":[{"enrollmentID":5},{"enrollmentID":11}]
}

Basically it adds new mappings but does not override the existing one.

it4factory avatar Aug 13 '21 17:08 it4factory

Any solutions to this? I'm trying something similar, to override a default mapping using .ForType.

I was of the impression, that I could override a property mapping using something like:

public class MyDocumentMapper 
{
    private readonly TypeAdapterConfig _typeAdapterConfig;

    public MyDocumentMapper(TypeAdapterConfig typeAdapterConfig) // dependency injected
    {
         _typeAdapterConfig = typeAdapterConfig;
    }

    public ConvertedDocument ConvertDocument(Document document) 
    {
         _typeAdapterConfig.ForType<Document, ConvertedDocument>()
             .Map(dest => dest.Id, _ => "ID OVERRIDE");

         return document.Adapt<ConvertedDocument>(_typeAdapterConfig);
    }
}

However what happens is that my default rule in my IRegister implementation is used, and not the overriden mapping.

I wanted something that allowed me to define a set of default rules in an IRegister implementation, and then the posbility to override the config if necessary.

Is that possible?

dsbahr avatar May 17 '24 11:05 dsbahr

In order to identify issues that are still active, we are closing issues that we believe are either resolved or are dormant. If your issue is still active then please reopen. Thanks.

stagep avatar Jan 07 '25 18:01 stagep