Page 1 of 1

DSN vs Constructor parameters and/or setup methods

Posted: Sun Mar 22, 2009 12:37 am
by Chris Corbyn
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:

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);
I may use a DSN instead since nearly always a username and password will be required (unlike with SMTP).

Code: Select all

$mailbox = new Swift_Mailbox(
  new Swift_Mailbox_Drivers_ImapDriver('imaps://username:password@imap.gmail.com')
);
 
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:

Code: Select all

$driver = Swift_Mailbox_Drivers_ImapDriver::fromDsn( ... );
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.

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
 
The most fun part of projects like this is all of these early design decisions :)

Re: DSN vs Constructor parameters and/or setup methods

Posted: Tue Mar 24, 2009 6:18 pm
by Ambush Commander
Since the DSN is not the most natural form of the authentication mechanism, I would recommend against making it the end-all be-all. Conceptually, createDriverFromDsn() is the purest. You might also want to add a little bit of syntax sugar for us lazy developers ;-)