Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to avoid the N+1 problem? #210

Open
gebekov opened this issue Nov 29, 2024 · 2 comments
Open

How to avoid the N+1 problem? #210

gebekov opened this issue Nov 29, 2024 · 2 comments

Comments

@gebekov
Copy link

gebekov commented Nov 29, 2024

Hi! Thanks for the great library 🙂!

I have a question:
Is there a way to avoid N+1 problems when mapping collections from DTO using transformers/providers (e.g. I keep identifiers in the DTOs and I want them to be loaded from the DB)? Unfortunately I couldn't find a clear answer to my question in the documentation.

The most obvious solution is to retrieve the required objects from the database in advance, before mapping, and pass them to the context. And then pass those objects from the context, but it seems to me that this is not a good solution. Is there another way to map collections?

Thanks in advance!

@damienalexandre
Copy link
Member

Doctrine EntityManager is capable of that, so it could be possible to:

  • craft a query to fetch all necessary entities at once
  • use find(ID) in the transformer, and let the EntityManager returns the already known objects

Anyway, the question is, should we expect the automapper to deal with that?

@gebekov
Copy link
Author

gebekov commented Feb 5, 2025

Hi @damienalexandre. Thank you for your reply!

Your comment seems to be correct: indeed, your trick can be used to achieve the desired result. But what about if the ORM Doctrine is not used, or if, for example, the objects are stored in another service's API?

Perhaps it's not Automapper's area of responsibility to solve these kinds of problems, but I think it could be a very handy feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants