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: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>