Results 1 to 3 of 3

Thread: Trying to better understand IOC using DAO's as an example

  1. #1
    Join Date
    Aug 2004
    Posts
    2

    Default Trying to better understand IOC using DAO's as an example

    I have this in a Struts action. For some reason it feels "wrong" to me.

    CategoryDAO dao = (CategoryDAO) WebApplicationContextUtils.getWebApplicationContex t(this.servlet.getServletContext()).getBean("Categ oryDAO");
    Category category = dao.createCategory(form.getName());

    Both CategoryDAO and Category are interfaces.

    I would have preferred using a prototype bean so that I can do something like this:

    Category category = ...getBean("Category");

    But then I need to setup the bean using:

    category.setName(form.getName());

    If there are a lot of properties I need to set this is going to get tedious.

    Somewhere I am not understanding IOC very well. Please help!

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    CategoryDAO dao = (CategoryDAO) WebApplicationContextUtils.getWebApplicationContex t(this.servlet.getServletContext()).getBean("Categ oryDAO");
    You can use org.springframework.web.struts.ActionSupport, then the previous line will read
    Code:
      CategoryDAO dao = (CategoryDAO) webApplicationContext.getBean("CategoryDAO");
    I would have preferred using a prototype bean so that I can do something like this:

    Category category = ...getBean("Category");

    But then I need to setup the bean using:

    category.setName(form.getName());
    why would you use Spring DI to create your business objects? It is (I think) easier to use
    Code:
      Category category = new CategoryImpl ();
    But then I need to setup the bean using:

    category.setName(form.getName());
    well, you can use Jakarta Commons BeanUtils. Struts heavely uses/recommends it
    Code:
      BeanUtils.copyProperties (category, form);
    no additional jars will be needed as struts requires BeansUtils.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Location
    Germany, Magdeburg
    Posts
    279

    Default

    I would have preferred using a prototype bean so that I can do something like this:

    Category category = ...getBean("Category");

    But then I need to setup the bean using:

    category.setName(form.getName());

    If there are a lot of properties I need to set this is going to get tedious.
    I think this isn't a good way to utilize prototype beans. I think your code would benifit from using a factory.

    Like this:

    Code:
    MyFactory factory=(MyFactory)context.getBean("MyFactory");
    Code:
    Category category=getFactory().createCategory("name");
    So your factory can be set up properly using Spring and also decouple implementation and usage. [/code]

Similar Threads

  1. Replies: 2
    Last Post: Dec 1st, 2005, 02:51 PM
  2. DAOs with dynamic data source
    By rhasselbaum in forum Data
    Replies: 9
    Last Post: Jan 27th, 2005, 02:37 PM
  3. Replies: 1
    Last Post: Jan 11th, 2005, 12:21 PM
  4. Combining Hibernate and JDBC DAOs
    By rosco in forum Data
    Replies: 8
    Last Post: Jan 4th, 2005, 01:34 PM
  5. Should Business Objects have References to DAOs?
    By sethladd in forum Architecture
    Replies: 9
    Last Post: Aug 25th, 2004, 01:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •