Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

[Question] : how to pass mapper instance to `MapToTargetWith` method?

Open ParadiseFallen opened this issue 3 years ago • 1 comments

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));

ParadiseFallen avatar Sep 19 '22 08:09 ParadiseFallen

issue about "How to keep parameters if needed"

ParadiseFallen avatar Sep 19 '22 08:09 ParadiseFallen

@ParadiseFallen Have you tried MapContext?

devbased avatar Sep 26 '22 18:09 devbased

@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);
    }

ParadiseFallen avatar Sep 27 '22 07:09 ParadiseFallen