Re: General Question about Config Files and IoC
Ryan Hoegg <[email protected]> Mon, 16 Feb 2004 16:20:24 -0600
| Newsgroups | gmane.comp.java.jcontainer.interest |
|---|---|
| Message-ID | <[email protected]> |
Paul Hammant wrote: > Peter, Chad, > >> >>> Do I need to make a separate "ObjectAConfig" class, which just holds >>> the config settings from the file, and then have a constructor like >>> this: >>> new ObjectA(objectAConfig, objectB) >>> >>> Is this the preferred approach, or am I missing something? >> >> >> >> I believe that is the preferred approach when doing constructor-based >> dependency injection. >> >> Of course, it doesn't have to be an 'ObjectAConfig' class, it could >> be a Properties object, a DOM document, or anything else.. but there >> is the general principal of passing a representation of the config >> via the constructor. > > > > I'd always suggest not doing a Properties or DOM object via the ctor. > It is too vague. An interface to represent the the configuration, > with multiple possible implementations is my preference. > Although I've not yet tried simplifying my life with a picocontainer, I have been trying out constructor-based dependency injection quite a bit recently. I've sort of been following a rule of three; if I have more than two things to pass into my constructor I encapsulate them in a *Configuration class (or interface). Although it looked a bit heavyweight at first, I have found that it's not that bad, and it really helps with readability as well. In addition, it's nice to explicitly separate configuration concerns from application concerns; for instance, I am enjoying capturing IOExceptions and stuff during application startup and then not having to worry about them anymore. -- Ryan Hoegg ISIS Networks http://www.isisnetworks.net/