mark-gerarts/automapper-plus

DataLoader for avoid N+1 problems

Gtvar opened this issue · 2 comments

Gtvar commented

I need to avoid a N+1 problem uses DataLoader or similar solution.

I have collection of Entities and each model load from DB or from REST some data.

->forMember('path', function (Category $category, AutoMapperInterface $mapper) {

    $paths = $this->pathProvider->findByPath($category->path);

    return $mapper->mapMultiple($paths, CategoryPathDTO::class);
})

I want to collect all paths, load in batch and then resolve for each object. Do you have any suggestions how I can do this?

I don't think there is a nice way do to this. You would have to loop over all your entities, collect the paths in batch, and then use that data instead of direct DB/API calls. Once you have that data, you can use it in the forMember callback.

I don't see how this library could do something else to remedy this situation, unless I'm misunderstanding something?

Gtvar commented

OK, I see, I will try to load all data before mapping or as options use custom mapper, tnx!