MapsterMapper / Mapster

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

Can not override default mapping #364

Open it4factory opened 2 years ago

it4factory commented 2 years ago

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.

dsbahr commented 1 month ago

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?