1

I am writing a custom domain-management class which uses various external services, sort of a wrapper class to make them work together.

Where abouts would I put the connection logic in this class? I won't need all the services at once so it doesn't make sense to put it in the constructor, I'm actually thinking some of the methods will be better off as static methods as they don't really relate to each other, the only thing they have in common is the underlying connections.

I am going to have methods along the lines of:

  • registerDomain() (contacts Nominet)
  • updateDomain(),
  • domainAvailable(), (contacts Nominet)
  • registerDNS(), (contacts Amazon & Nominet)
  • updateDNS(),

Should I check for a connection property in each call (and create it if non-existent) or connect in the classes constructor?

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • Idea: The common denominator of your methods could be set up in the wrapper class construtor. For the other methods, you could extend the wrapper class or build separate classes and pass them only what they need to work. – hornetbzz Jul 20 '11 at 09:58

1 Answers1

2

I think it would make sense to create somthing along the lines of this:

class DomainManager {
    public function __construct($domainData) {}
    public function registerDomain() {
        //connect
        //do stuff
    }
    public function updateDomain() {
        //connect
        //do stuff
    }
    public function isAvailable() {
        //connect
        //do stuff
    }
    public function registerDns() {
        //connect
        //do stuff
    }
    public function updateDns() {
        //connect
        //do stuff
    }
    private function connectToNominet() {}
    private function connectToAmazon() {}
}

Then you have a nice object that encapsulates the logic available for a domain:

$domain1 = new DomainManager('example.com', $user, $foo);
$domain->registerDomain();
$domain->registerDns();
NDM
  • 6,731
  • 3
  • 39
  • 52
  • Cool, would you store the connection in a property and something like this when it's needed: `if(!$this->nominet) { $this->connectToNominet() }` ? Or should connectToNominet return the connection? – Dunhamzzz Jul 20 '11 at 10:03
  • Depends on the type of connection, if it is stateless like a regular ajax request it won't be needed. If it is a more permanent connection however, like you would make to a database server it would make sence to store the connection. The question you need to pose yourself is: can I do multiple request to the connection without having to reconnect? – NDM Jul 20 '11 at 10:08
  • Probably "DomainController" is a better name for the class than Manager. – hakre Jul 20 '11 at 12:54
  • @Nicky: This is wrong mate. You're defining $domain1 but using $domain ;) – genesis Jul 20 '11 at 19:24