Results 1 to 6 of 6

Thread: delaying/rippling a dependency

  1. #1
    Join Date
    Feb 2007
    Posts
    4

    Question delaying/rippling a dependency

    I've searched for an answer to this question without success, so I'll post the problem I'm facing. It's simple really, but how do I do this?

    I have a DAO that needs a DataSource. Then, I have a Service that uses this DAO. Finally, I want to provide the Service to other users, and let them specify the DataSource. How do I "forward" the DataSource that the user provided, to the DAO?

    If my design is wrong, do not hesitate to point this out and suggest a better design.

    Ideally I want the user to be able to do this:

    Code:
    <bean id="myService" parent="service">
      <property name="dataSource" ref="dataSource"/>
    </bean>
    where the user has defined his own "dataSource" definition. Then they can inject "myService" wherever they need to use it.

    In the module I am providing, I have:

    Code:
    <bean id="userDao" class="p.UserDaoImpl">
      <!-- property name="dataSource" ref="dataSource"/ -->
    </bean>
    
    <bean id="service" class="p.Service" abstract="true">
      <property name="userDao" ref="userDao"/>
    </bean>
    So userDao needs a DataSource to be configured, but I want this to be left "abstract" until the external user provides it to the service. When that happens, I want to "forward" this dataSource to the userDao. I can't figure out how to do this.

    Any help or suggestion of a better approach to achieve this result would be greatly appreciated.

    Thanks in advance.

    freddy

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Have you had a look at this? Might give you some ideas.
    http://blog.interface21.com/main/200...ource-routing/

  3. #3
    Join Date
    Feb 2007
    Posts
    4

    Smile

    Thanks for your fast and helpful reply!

    At first reading it doesn't look like exactly what I need but as you said, the goal was to give me some ideas and that it does. I will look more into it and I think that I'll be able to figure something out, based on the explanations to which you refer.

    If I come up with an elegant solution to my problem I'll post it back here. In the meantime, any other suggestions are most welcome.

    Thanks again.

    freddy

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Not a problem, hope it was of some help to you! I would be very interested to hear what you come up with! There was another blog posted a while ago that proposed something similar, but with a different implementation. Might also be useful.
    http://www.deinum.biz/2007/01/05/one...ient-database/

  5. #5
    Join Date
    Feb 2007
    Posts
    4

    Lightbulb

    Hi again,

    Here's a possible solution: I use a proxy (wrapper, delegate, whatever you want to call it) DataSource in my module, and when the user specifies the real datasource, I forward this definition to the proxy.

    I tried using Spring's DelegatingDataSource, but it's not happy about not having a target DataSource at construction time.

    So I wrote my own wrapper which just forwards all calls to the target, but accepts not having the target specified at construction time. It gets specified when the user supplies it.

    I didn't look into Spring's other classes, perhaps there's one already that achieves this.

    Generally speaking this seems to be an acceptable solution to forward a dependency that gets specified outside of a module.

    What do you think?

    cheers,
    freddy

  6. #6
    Join Date
    Feb 2007
    Posts
    4

    Lightbulb

    I guess I could simply extend DelegatingDataSource and override afterPropertiesSet() not to throw an exception..

    freddy

Posting Permissions

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