Announcement Announcement Module
No announcement yet.
Need advice on IoC and DAOs Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need advice on IoC and DAOs

    I'm trying to take an old DAO pattern and rewrite it for use with an IoC container.

    Here's my current architecture:
    * a util object that can hand back datasource objects
    * an "extractor" object that does the work of extracting data from my database
    * a set "model" objects that my extractor fills up with the extracted data

    so right now what I do is:
    * fire up teh extractor
    * the extractor goes and gets a datasource from the util object
    * the extractor instantiates and fills the model objects and hands those back to the caller.

    In other words, my "action" code that needs the data does this to get it:
    Model = dbExtractor.extractModel();

    Where an how can IoC help with this architecture. Should I be letting the container create the Datasource object (instead of having a util class do it) and a set of empty model objects and then injecting those into the extractor? What benefit would that provide?

    What would be ideal is if my action class didn't have to know about the extractor but could just have an instantiated and populated model injected into it. I'm a bit unclear about how that would be wired up, though.


    - Gary

  • #2
    Hi Gary

    That's pretty much what I've just done. The only difference is as I use Hibernate the datasource was already externally configured. If you're using a JDBC datasource then yes I'd use Spring to set that up as well.

    The big, big advantages I'm seeing is that I'm replacing a lot of, quite involved, setup code, with some really simple (and consistent) bean setup XML in my beans.xml file. I'm also drastically reducing the dependencys of a number of classes on other classes (now they only need to know about the top most *interface* and not the concrete classes and they don't need to know at all about the constituent objects of the top-level classes).

    Here's a (slightly simplified) version of my beans.xml

    <standard header stuff>
      <bean id="LetterTemplateDAOBean" class="com.pti.dbmulti.db.LetterTemplateDAO"/>
      <bean id="UpdateLetterTemplateActionBean" class="com.pti.dbmulti.ui.UpdateLetterTemplateAction">
        <property name="LetterTemplateDAO">
          <ref bean="LetterTemplateDAOBean"/>