As everybody knows, the point of wrapper classes is encapsulates the functionality of another class or component . Here is a simple Class that wrap a small part of PHP Predis library :
class CacheWrapper {
private $client;
public function __construct(){
$this->client = new Predis\Client();
}
public function set($key, $value){
$this->client->set($key, $value);
}
public function get($key){
return $this->client->get($key);
}
}
and here is the simple code that use this wrapper class :
$client = new CacheWrapper();
echo $client->get('key1');
Where this class would work perfectly the problem that it create the dependency inside the class which i want to avoid by injecting the dependency into the class instead of letting the class to create its dependency , and so the wrapper class will look like this :
class CacheWrapper {
private $client;
public function __construct(Predis\Client $predisObj){
$this->client = $predisObj;
}
public function set($key, $value){
$this->client->set($key, $value);
}
public function get($key){
return $this->client->get($key);
}
}
so i have to write the following code to use the wrapper class :
$predis = new Predis\Client();
$client = new CacheWrapper($predis);
echo $client->get('key1');
But i think like that there is no point to use the wrapper class since i still use the original class in my code . so my question is: does dependency injection and wrapper class concepts goes against each other and can't be used together and what is the best way to solve issue like this ?
As everybody knows, the point of wrapper classes is encapsulates the functionality of another class or component
This isn't actually accurate. "Wrapper Classes" aren't as simple as you are implying. There are Many design patterns that "wrap" a class.
The Adapter pattern 'wraps' an object when the API that is needed doesn't match the API of the class you have.
The Facade pattern provides a simplified interface to a larger body of code.
Class A{
String setValueFirstHalf(String)
String setValueSecondHalf(String)
Void getValue()
}
// Pseudo-code for 'wrapper' that is an 'adapter pattern' impl.
// This class 'wraps' A and changes the API
Class WrapsA_Adapter{
String setFirst(String) {myA.setValueFirstHalf(String)}
String setSecond(String) {myA.setValueSecondHalf(String)}
Void get() {myA.getValue()}
}
// Pseudo-code for 'wrapper' that is an 'facade pattern' impl.
// This class 'wraps' A and changes the API to 'simplify' it.
// Often the 'facade pattern' uses several classes to do its task,
// but it 'can' use just one like we did below. (this example is obviously a stretch)
Class WrapsA_Facade{
String get()
Void set(String first, String second){
myA.setValueFirstHalf (first);
myA.setValueSecondHalf(second);
}
}
To directly answer your question now:
does dependency injection and wrapper class concepts goes against each other and can't be used together and what is the best way to solve issue like this ?
They do not Inherently go against each other.
You could very easily have a WrapperFacade implementation where the Facade is injected with the 'specific' instance of an interface.
The adapter pattern with dependency injection would be valid also if the adapter was adapting an Interface, and you are injecting the specific implementation of that interface for it to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With