I’d just like to run this past some of you fine folk, I’m sure the solution is obvious but despite a rewrite or two I always end up and the same place.
I have a connection object, it can(could?) use many different sources to perform general CRUD operations. Soap, Pdo, XML etc…
So, I made this object pretty much a decorator around an adaptor object, and this is where i think it gets messy.
As such, here’s some pseudo code (we all like code).
class Connection
{
protected
$adapter;
public function __construct(Connection_Adapter_Interface $adapter){
$this->adapter = $adapter;
}
public function login(Connection_Credentials $credentials){
$this->adapter->login($credentials);
}
}
interface Connection_Adapter_Interface
{
public function login(Connection_Credentials $credentials);
}
class Connection_Adapter_Soap implements Connection_Adapter_Interface
{
public function login(Connection_Credentials $credentials){
#login n stuff
}
}
class Connection_Adapter_Pdo implements Connection_Adapter_Interface
{
public function login(Connection_Credentials $credentials){
#login n stuff
}
}
Firstly, I’m worried about the maintenance of it. If I want to add a new feature, I have to add a method to the Connection object then to the Interface, and then to every Adapter.
Messy.
Secondly, I’m worried about exceptions. Say for example the PDO object throws an exception in the adapter, I’ll catch it and throw a generic Adapter_Exception but I lose the backtrace.
Even worse, what if the Soap Client throws an exception? I cannot do the same because Soap likes throwing exceptions for everything! In this instance, it throws an exception for invalid login details.
This is context which I’d like to get back to the connection object, but how aware should it be of these exceptions. It feels wrong to be letting a Soap exception bubble up out of the Adapter context.
So, given this example, Pdo throws an exception, I catch it and re-throw as an Adapter exception, I catch this in the Connection object and do what?
I’m struggling a little, and maybe because I just have me, myself and I to discuss this with.
This is where you come in…
Suggestions, comments, chastisement most welcome.