Mapster
Mapster copied to clipboard
[Question] : how to pass mapper instance to `MapToTargetWith` method?
So basically i have
config...
.MapToTargetWith((a,b)=>MapMethod(a,b));
List<B> MapMethod(List<A> a,List<B> b)
{
// there comes custom list processing options. I dont want to recreate list. I wanna merge em by items id's
foreach(var item in a)
{
var candidate = b.FirstOrDefault(x=>x.Id == item.Id);
if(candidate is not null)
{
Mapper.From(item).AdaptTo(candidate);
}else
{
b.Add(Mapper.Map<B>(item));
}
}
}
How i can pass Mapper there?
Im using DI. Mapper MUST be an instance and not a static class,
RN i have idea about made clousure with config and then create mapper or use it with Adapt method.
But it would be better to pass self if MapToTargetWith
MapToTargetWith((a,b,mapper)=>MapMethod(a,b,mapper));
issue about "How to keep parameters if needed"
@ParadiseFallen Have you tried MapContext?
@devbased MapContext is map context. its not what i want. but i found how to solve this.
cfg
.MapToTargetWith((a,b)=>MappingMethod(a,b,cfg));
B MappingMethod(A a, B b, cfg)
{
var mapper = new Mapper(cfg);
}