DSN vs Constructor parameters and/or setup methods
Posted: Sun Mar 22, 2009 12:37 am
I'm considering writing the counterpart for Swift Mailer and providing components for accessing a mailbox over IMAP/POP3 or any other provided driver (say NNTP)... just playing right now and it will be a separate project if it happens.
I'm considering, rather than specifying authentication parameters etc, such as this:
I may use a DSN instead since nearly always a username and password will be required (unlike with SMTP).
Does anybody have any convincing arguments against not using a DSN? I can see that for one thing it's not so easy to automate the instantiation of the Mailbox via a factory or otherwise, but it's not exactly difficult to generate the DSN string.
Perhaps the underlying implementation should use parameters in the constructor, but a factory method allows creation from a DSN:
Grr... I'm just procrastinating and thinking out loud
The advantage of the DSN approach is that a higher level DriverFactory may be provided that uses the DSN to create the underlying Drivers such as IMAP or POP3.
The most fun part of projects like this is all of these early design decisions 
I'm considering, rather than specifying authentication parameters etc, such as this:
Code: Select all
$mailbox = new Swift_Mailbox(
new Swift_Mailbox_Drivers_ImapDriver('imap.gmail.com', 993, 'ssl', 'username', 'password')
);
//Or even
$driver = new Swift_Mailbox_Drivers_ImapDriver('imap.gmail.com', 993, 'ssl');
$driver->setUsername('username');
$driver->setPassword('password');
$mailbox = new Swift_Mailbox($driver);Code: Select all
$mailbox = new Swift_Mailbox(
new Swift_Mailbox_Drivers_ImapDriver('imaps://username:password@imap.gmail.com')
);
Perhaps the underlying implementation should use parameters in the constructor, but a factory method allows creation from a DSN:
Code: Select all
$driver = Swift_Mailbox_Drivers_ImapDriver::fromDsn( ... );The advantage of the DSN approach is that a higher level DriverFactory may be provided that uses the DSN to create the underlying Drivers such as IMAP or POP3.
Code: Select all
$driver = Swift_Mailbox_DriverFactory::createDriverFromDsn('imap://user:pass@imap.hostname.tld');
// $driver is an instance of Swift_Mailbox_Drivers_ImapDriver
$driver = Swift_Mailbox_DriverFactory::createDriverFromDsn('pop3://user:pass@imap.hostname.tld');
// $driver is an instance of Swift_Mailbox_Drivers_Pop3Driver