0

I have a certain function that I use in two different entity repositories. It's the exact same in both repositories, which is of course not very DRY.

I don't really know where the appropriate place would be to put this function so it could be used by both repositories. The fact that the two repositories are in different bundles doesn't make things any easier for me to figure out.

It's not a template helper, by the way. It's a function that the repositories need to use.

Where should I put this function so I'm only defining it once?

Jason Swett
  • 43,526
  • 67
  • 220
  • 351
  • How much code is it? Can you paste an example from both bundles? – Travis Jul 13 '12 at 15:52
  • It's about 40 lines, so definitely enough to be a bad idea to duplicate. (Anything more than 2 lines is probably a bad idea to duplicate, in my opinion.) The function just takes some inputs and returns a value. I can't really imagine that the content of the function should make a difference as to where would be an appropriate place to put it. – Jason Swett Jul 13 '12 at 16:37
  • (The reason I don't want to paste the code is because I don't want people to focus on the content of the code and start discussing that and get sidetracked from the original issue.) – Jason Swett Jul 13 '12 at 16:39

2 Answers2

0

I resolved something similar sometime ago, I don't know if is the better solution, but it worked.

class CommonRepository extends EntityRepository
{

     public function common(){
      ...
     }

}


class RepoThatUseCommonRepository extends CommonRepository
{

     ...
}
smoreno
  • 3,129
  • 3
  • 32
  • 49
  • I thought of this solution as well, but I wouldn't know where to put the file since the class would need to be used by two different bundles. Where would you put the file? Even if I could put the `CommonRepository` class inside one of the two bundles and have it work, it would seem weird to just arbitrarily pick one over the other, since the function isn't more closely related to one bundle than it is to the other. So hopefully there's some third option, but I don't know what it would be. – Jason Swett Jul 13 '12 at 16:58
0

I may be burned at the stake for saying this, but what about throwing the unDRY code in a static method of a utility class? Using it is then as simple as Foo::bar() from any repo.

You could also just make it a regular public method, and use setter injection to pass the utility object to each repo that needs it. This would involve a bit more work, but your app would probably be easier to run unit tests against.

On a slight side-note, @John Millikin does a great job describing "safe" and "unsafe" static methods.

Community
  • 1
  • 1
Steven Mercatante
  • 24,757
  • 9
  • 65
  • 109