Re: WebObjects and dependency injection (DI)
Re: WebObjects and dependency injection (DI)
- Subject: Re: WebObjects and dependency injection (DI)
- From: Kieran Kelleher <email@hidden>
- Date: Mon, 21 Sep 2009 17:56:35 -0400
To keep stuff like this below 'clean', I define a list of properties
and lookup the property to get the class name........
er
.extensions.sqlHelper.className.mysql=er.extensions.jdbc.MySQLSQLHelper
er.extensions.sqlHelper.className.frontbase
=er.extensions.jdbc.FrontBaseSQLHelper
etc.
.. and create it like this....
Class.forName( "er.extensions.sqlHelper.className." +
databaseProductName ).newInstance()
YMMV, Kieran
On Sep 21, 2009, at 5:30 PM, Anjo Krank wrote:
U-huh. So how about a real world example and not these cooked up
things. Take a look at the ERXSQLHelper. Depending on various types
of input it creates a concrete subclass. Can DI change this to sth
more "clean"?
Cheers, Anjo
public static ERXSQLHelper newSQLHelper(String databaseProductName) {
synchronized (_sqlHelperMap) {
ERXSQLHelper sqlHelper = _sqlHelperMap.get(databaseProductName);
if (sqlHelper == null) {
try {
String sqlHelperClassName =
ERXProperties.stringForKey(databaseProductName + ".SQLHelper");
if (sqlHelperClassName == null) {
if (databaseProductName.equalsIgnoreCase("frontbase")) {
sqlHelper = new FrontBaseSQLHelper();
}
else if (databaseProductName.equalsIgnoreCase("mysql")) {
sqlHelper = new MySQLSQLHelper();
}
else if (databaseProductName.equalsIgnoreCase("oracle")) {
sqlHelper = new OracleSQLHelper();
}
else if
(databaseProductName.equalsIgnoreCase("postgresql")) {
sqlHelper = new PostgresqlSQLHelper();
}
else if
(databaseProductName.equalsIgnoreCase("openbase")) {
sqlHelper = new OpenBaseSQLHelper();
}
else if (databaseProductName.equalsIgnoreCase("derby")) {
sqlHelper = new DerbySQLHelper();
}
else if
(databaseProductName.equalsIgnoreCase("microsoft")) {
sqlHelper = new MicrosoftSQLHelper();
}
else {
try {
sqlHelper = (ERXSQLHelper)
Class.forName(ERXSQLHelper.class.getName() + "$" +
databaseProductName + "SQLHelper").newInstance();
}
catch (ClassNotFoundException e) {
sqlHelper = new ERXSQLHelper();
}
}
}
else {
sqlHelper = (ERXSQLHelper)
Class.forName(sqlHelperClassName).newInstance();
}
_sqlHelperMap.put(databaseProductName, sqlHelper);
}
catch (Exception e) {
throw new NSForwardException(e, "Failed to create sql
helper for the database with the product name '" +
databaseProductName + "'.");
}
}
return sqlHelper;
}
}
Am 21.09.2009 um 23:24 schrieb Andrew Lindesay:
Hi Anjo;
I guess this could be helpful in _some_ situations; I take for
example, the Jetty server. Jetty can have a number of "handlers"
added to it. Each handler does something like re-writes, feeds
disk-based content, runs servlets etc.. etc.. The Jetty authors
could not have envisaged all of the possible handlers that might
have been written for Jetty, but because of the configuration style
which follows the same line of thinking as Andrus is describing, it
is possible to configure additional handlers. So that's a good
example of where this seems quite helpful and Andrus' other
examples also feel like good examples, but I agree that it seems
like over-kill for many situations.
cheers.
Thanks for the write-up, but yeah, this can all be achieved w/o
it. I really don't see why I shouldn't configure my app with
if(Configration.isStaging())... and instead use DI. At least my
way I easily find all the occurrences and have full logic support
if(!Configuration.isStaging()). How is DI "cleaner" in any way when:
- I have any number of DI containers and their various syntax to
chose from
- I can't *find* the dependencies when I really want or need them.
Have you ever tried to debug such an app that wasn't written by
yourself? Take a look at the Red5 Media server for some fun...
___
Andrew Lindesay
www.lindesay.co.nz
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden