Is it bad practice to autowire and call a repository from within a mapper class? I have a mapper class that maps a model to an entity for JPA. In order to keep repository calls within my service, it requires additional loops/code in order to get inside the right data, whereas my mapper already loops in through my models in such a way that I can just make a few calls inside there to my repository in order to get additional values and set them to the entity models accordingly.
Asked
Active
Viewed 1,700 times
1 Answers
0
Hmm. I would say yes.
A repository should be where you store your Models. So its public methods should accept and output your Model classes. Therefore you should never need to map its output.
If you do use entities and a mapper, I would say that these should be internal to your repository.
I guess it would be possible to both use it internally AND have it reference the repository. But I think such a use would probably cause tight coupling or circular dependencies.

Ewan
- 70,664
- 5
- 76
- 161
-
Thanks yes a couple lines of code saved is probably not worth sacrificing the separation. – MeesterMarcus Feb 25 '17 at 04:05
-
But how about calling a service which performs some kind of validation ? Would you concider that bad practice ? – bvdb Jul 13 '20 at 13:08
-
you should ask a new question with all the details bob – Ewan Jul 13 '20 at 15:56