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.