You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by Kerry Todyruik <ke...@todyruik.com> on 2001/12/10 22:44:24 UTC

Applying Avalon Components

I am still trying to get a handle on how to use components.  Using
components with other components seems pretty straightforward.  What I
am still having some trouble with is mixing components with
non-component classes.  In particular, how to properly propegate
resources to classes that require them like DataSources.

For example, I have a hypothetical class that does some kind of database
operation but is not a component.  Call it "MyQuery".

Is the best way to use a DataSource without breaking IOC by implementing
a setDataSource() method in MyQuery?

So..

MyQuery mq = new MyQuery();
JdbcDataSource datasource = null;
try {
  datasource = container.lookup(JdbcDataSource.ROLE);
  mq.setDataSource(datasource);
catch (ComponentException e) {
...
} finally {
  if (datasource != null)
    container.release(datasource);
}


Is there anything wrong with passing a Component object ot a
Non-Component object?

Then what about an object where it's scope isn't localized because the
object is passed to another object or a context like in Velocity.   The
only thing I can think of is to implement implement a setConnection()
method instead of a setDataSource to quickly release dependancy on the
component.

MyQuery mq = new MyQuery();
JdbcDataSource datasource = null;
try {
  datasource = container.lookup(JdbcDataSource.ROLE);
  mq.setConnection(datasource.getConnection());
  velocitycontext.put("myqueryobject",mq);
catch (ComponentException e) {
...
} finally {
  if (datasource != null)
    container.release(datasource);
}


Now, there is a possiblity that a user of MyQuery may forget to set the
Connection or DataSource so this becomes a source of errors if
incorrectly applied and I have to throw runtime exceptions.

I was also wondering if it might be possible to make the setting of
required resources like datasources more transparent.  One thought I had
might be to use a factory that is a component to create non-component
classes and set its required resources like datasources.  Does that make
sence or am I on the wrong track here?

I would really appreciate some suggestions.  The fundamentals of avalon
are pretty easy to grasp and the benefits to my code were clear right
away.  It is just some of these implementation details that are a bit
tricky.

Thanks in advance.

Kerry Todyruik


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Applying Avalon Components

Posted by Peter Donald <pe...@apache.org>.
On Tue, 11 Dec 2001 08:44, Kerry Todyruik wrote:
> Is the best way to use a DataSource without breaking IOC by implementing
> a setDataSource() method in MyQuery?

no.

>
> So..
>
> MyQuery mq = new MyQuery();
> JdbcDataSource datasource = null;
> try {
>   datasource = container.lookup(JdbcDataSource.ROLE);
>   mq.setDataSource(datasource);
> catch (ComponentException e) {
> ...
> } finally {
>   if (datasource != null)
>     container.release(datasource);
> }
>
>
> Is there anything wrong with passing a Component object ot a
> Non-Component object?

Hope not - I do it all the time ;)

> Then what about an object where it's scope isn't localized because the
> object is passed to another object or a context like in Velocity.   The
> only thing I can think of is to implement implement a setConnection()
> method instead of a setDataSource to quickly release dependancy on the
> component.
>
> MyQuery mq = new MyQuery();
> JdbcDataSource datasource = null;
> try {
>   datasource = container.lookup(JdbcDataSource.ROLE);
>   mq.setConnection(datasource.getConnection());
>   velocitycontext.put("myqueryobject",mq);
> catch (ComponentException e) {
> ...
> } finally {
>   if (datasource != null)
>     container.release(datasource);
> }



> I was also wondering if it might be possible to make the setting of
> required resources like datasources more transparent.  One thought I had
> might be to use a factory that is a component to create non-component
> classes and set its required resources like datasources.  Does that make
> sence or am I on the wrong track here?

It makes sense. What I would do is create a Query interface and then all 
implementations are created via this factory that makes sure that the query 
instances are set up correctly. Something like

interface Query
{
  ...
  void setConnection( Connection conn );
  ...
}


class MyQuery implements Query {}

class QueryManager
{
  
  Query getQuery( String name )
  {
   Query query = createQuery( name );
   JdbcDataSource datasource = container.lookup(JdbcDataSource.ROLE);
   query.setConnection(datasource.getConnection());
   return query;
  }

  void releaseQuery( Query query )
  {
    ...do release here ...
  }
}

Does that fit with what you are doing ?

-- 
Cheers,

Pete

*--------------------------------*
| Every rule has an exception,   |
| except the rule of exceptions. |
*--------------------------------*

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>