Question

We have lot of object with this kind of design : Interface and several implementations, and use of several object by composition. Exemple : Foo implements IFoo and have a Bar object who implements IBar Foo also have a setBar(IBar bar) method for injection of dependancie.

My question is : the setter sould't be in the interface ? ( For Testing, Mocking... i'm stuck ! )

Was it helpful?

Solution

If you are using polymorphism, i.e., calling the setter on the interface type, then obviously you need it in the interface.

OTHER TIPS

To have a setter in the interface just for mocking and testing is not good. Thus you permit the users of that interface to arbitrary set components, even though the properties of that object probably shouldn't be modifiable after construction. The interface shouldn't reveal how to construct object.

Either inject Bar to Foo using setter Injection. In a context where Foo cannot exist without Bar it would more appropriate to inject Bar with a constructor.

Further Read: Types of dependency injection

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top