I have an interface whose job is to communicate with repository (that implements some interface). It doesn't seem to make sense to implement this interface without receiving a repository,so I'd like to inject it.

I then ask:where should I receive the repository? Since it is required in almost every method call it seems reasonable to inject the dependency in the constructor, and not in (practically) every method in the interface as this leads to a volition of the DRY (don't repeat yourself) principle.

On the other hand, there is no way of enforcing that every implementor would have this dependency in its constructor (moreover - how should an implementor even know this?)

I also thought about having an abstract class instead of an interface, but I soon ruled out that option as to not limit the option to derive another class.

What should I do in this situation, where should I receive the dependency?

  • 3
    The assumption that every implementer of an interface has some specific dependency is unquestionably wrong. At the very least, debugging fakes have no reason to hold the dependency. – Sebastian Redl Jan 24 '16 at 11:22
  • Something bothers me here: Why do you mention that repository implements some interface? Isn't it the same interface you've just mentioned - "whose job is to communicate" with it? – shudder Jan 24 '16 at 18:34

Interfaces are used to define a contract: implementing an interface is a way for a class to indicate that a certain set of methods can be called on it, that these methods require well-defined arguments and that the result of the method will be of a certain type.

You need to think about the significance of the methods on the interface. If the methods on the interface explicitly have to deal with the repository, you should probably pass it in every method you have. Otherwise, the fact that your implementations use a repository is not relevant to the interface itself. It's very difficult to define an interface and require that implementors work a certain way with it. That's simply not what interfaces do. Forcing a class to work a certain way means deriving the class from an abstract class that imposes certain constraints on it.

A go-between solution would be to simply put a method on the interface that allows a class calling the interface to assign a repository to it. You can still inject it via constructor-based IoC (which I would recommend), but you also make the fact that this interface works on a repository explicit. The implementation of such a method is a very simple assigment to a local variable.

|improve this answer|||||

Have both an interface and an abstract class which implements it so that only those implementations which need to derive from something else have to handle the dependency themselves, and they can look at the abstract class for guidance. Document it properly as well, of course.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.