Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Repository Pattern - Accessing other Repositories [closed]

I have one repository that internally (on some methods) will need to use another repository.

I am trying to put all dependencies on the constructor, passing interfaces as arguments. However I don't know how to deal with this specific scenario.

Should I pass also this repository as an argument? Even though I will not use it on every method inside?

Thanks

like image 412
Hugo Hilário Avatar asked Oct 21 '25 13:10

Hugo Hilário


1 Answers

You don't have any problem for making a repository depend on another repository. And, yes, pass it as a parameter

I'm assuming you're making dependency injection, specifically constructor injection.

Instancing an extra repository, which some times won't be used (because not all method use it) it's not such a terrible overhead that makes you to avoid it. If it was a more expensive resource (like opening a file or DB connection) you could use some alternative technique. For example expose the second repository in a property with a backing field which is populated in the first call to the property getter using service location, i.e. finding it directly in your container, or an smarter solution, provided but some of the DI frameworks, which does this kind of thing automatically, like Unity's Lazy and similar solutions.

But I insist, in this case, the overhead doesn't justify it.

NOTE: you could also use the property or Lazy technique if you had a dependency loop (circular dependency), to break the loop and make it work. However, int this case, it's much better to refactor your classes, (extracting a thrid class) to avoid the circular references. This is not your case.

like image 115
JotaBe Avatar answered Oct 24 '25 09:10

JotaBe