Discussion:
question about expected behavior for implementation hiding container
Mike Simpson
2008-05-16 03:54:25 UTC
Permalink
Hi --
MutablePicoContainer mpc = new PicoBuilder().withConstructorInjection()
.withHiddenImplementations()
.withConsoleMonitor()
.build();
mpc.addComponent( picotest.model.Thing.class,
picotest.model.impl.DefaultThing.class );
Thing t = (Thing) mpc.getComponent( picotest.model.Thing.class );
t.setStuff( "foo" );
System.out.println( "this thing's stuff = " + t.getStuff() );
this thing's stuff = null
From the ConsoleMonitor output, it looks like a new instance of
t.poke();
t.poke();
t.poke();
PicoContainer: instantiating picotest.model.impl.DefaultThing()
PicoContainer: instantiated picotest.model.impl.DefaultThing() [0 ms], component picotest.model.impl.DefaultThing, injected []
PicoContainer: instantiating picotest.model.impl.DefaultThing()
PicoContainer: instantiated picotest.model.impl.DefaultThing() [0 ms], component picotest.model.impl.DefaultThing, injected []
PicoContainer: instantiating picotest.model.impl.DefaultThing()
PicoContainer: instantiated picotest.model.impl.DefaultThing() [0 ms], component picotest.model.impl.DefaultThing, injected []
Is this the intended behavior? It doesn't seem right, to me at least. :)

I poked around in the 2.2 source, and this:

http://picocontainer.codehaus.org/javadoc/core/src-html/org/picocontainer/behaviors/HiddenImplementation.html#line.83

seems to be the important method. On line 84, it does look like a new
Object componentInstance = getDelegate().getComponentInstance(container);
Apologies in advance if I'm missing something obvious. :)

-mgs


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Konstantin Priblouda
2008-05-16 05:37:46 UTC
Permalink
Object componentInstance =
getDelegate().getComponentInstance(container);
Apologies in advance if I'm missing something
obvious. :)
Caching behaviour? ;)

regards,

----[ Konstantin Pribluda http://www.pribluda.de ]----------------
JTec quality components: http://www.pribluda.de/projects/




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Mike Simpson
2008-05-16 12:39:40 UTC
Permalink
Post by Konstantin Priblouda
Caching behaviour? ;)
"Anti-caching", maybe? One instance per method call rather than one
instance no matter how many calls. :)

Maybe "multipleton". >;)

-mgs


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Konstantin Priblouda
2008-05-16 13:12:10 UTC
Permalink
Post by Mike Simpson
Post by Konstantin Priblouda
Caching behaviour? ;)
"Anti-caching", maybe? One instance per method call
rather than one
instance no matter how many calls. :)
I meant hat you would need caching behaviour ;)
If there isno caching, new instance will be provided
on every invocation

regards,

----[ Konstantin Pribluda http://www.pribluda.de ]----------------
JTec quality components: http://www.pribluda.de/projects/




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Mike Simpson
2008-05-16 13:26:34 UTC
Permalink
Post by Konstantin Priblouda
I meant hat you would need caching behaviour ;)
If there isno caching, new instance will be provided
on every invocation
Ah, my mistake. I think I see, but now I'm confused because I thought
caching means I only ever have one instance of a component, no matter
how many times I request it from PicoContainer. So, under caching
behavior:

Thing t1 = (Thing) mpc.getComponent( picotest.model.Thing.class );
Thing t2 = (Thing) mpc.getComponent( picotest.model.Thing.class );
Thing t3 = (Thing) mpc.getComponent( picotest.model.Thing.class );

are t1, t2, and t3 all the same object instantiation? So if I do:

t1.setStuff( "foo" );
String s = t3.getStuff();

then s will have the value "foo"? That's not the behavior I want either.

Under the non-implementation-hiding behavior (i.e. if I just leave off
.withHiddenImplementations() ) things seem to behave as I would
expect: t1, t2, and t3 all behave as separate object instances, but
multiple method invocations on one of them don't result in new
instances being created. It's the addition of the
implementation-hiding behavior that seems to get me into a strange
place, where calling

t2.someMethod();
t2.someMethod();
t2.someMethod();

winds up talking to a different, newly-created instance of a Thing
object in each method call.

-mgs

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Paul Hammant
2008-05-16 14:55:53 UTC
Permalink
Mike,

Do you mean that ..

Thing t = (Thing) mpc.getComponent( picotest.model.Thing.class );

.. give you a new instance every time ? or ..

t.poke();

.. is giving you a new instance each poke ?

- Paul
Post by Konstantin Priblouda
Post by Mike Simpson
Post by Konstantin Priblouda
Caching behaviour? ;)
"Anti-caching", maybe? One instance per method call
rather than one
instance no matter how many calls. :)
I meant hat you would need caching behaviour ;)
If there isno caching, new instance will be provided
on every invocation
regards,
----[ Konstantin Pribluda http://www.pribluda.de ]----------------
JTec quality components: http://www.pribluda.de/projects/
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Paul Hammant
2008-05-16 15:27:50 UTC
Permalink
Yup,

Can confirm, its the latter :-(

@Test public void shouldNotInstantiateForEveryMethodCall() {
DefaultPicoContainer parent = new DefaultPicoContainer(new
Caching());
parent.addComponent(StringBuilder.class);
DefaultPicoContainer pico =
new DefaultPicoContainer(new ImplementationHiding(),
parent);
pico.addComponent(NeedsStringBuilder.class,
NeedsStringBuilderImpl.class);
NeedsStringBuilder nsb =
pico.getComponent(NeedsStringBuilder.class);
assertNotNull(nsb);
nsb.foo();
nsb.foo();
StringBuilder sb = pico.getComponent(StringBuilder.class);
assertEquals("<init>foo()foo()", sb.toString());
}
Post by Paul Hammant
Mike,
Do you mean that ..
Thing t = (Thing) mpc.getComponent( picotest.model.Thing.class );
.. give you a new instance every time ? or ..
t.poke();
.. is giving you a new instance each poke ?
- Paul
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Mike Simpson
2008-05-16 15:28:40 UTC
Permalink
Post by Paul Hammant
Do you mean that ..
Thing t = (Thing) mpc.getComponent( picotest.model.Thing.class );
.. give you a new instance every time ? or ..
t.poke();
.. is giving you a new instance each poke ?
The latter. The ComponentMonitor logs seem to indicate that there's
an instantiation event occuring every time I call t.poke(), and that
the method invocation is being passed to the newly-instantiated object
each time.

-mgs


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
Paul Hammant
2008-05-16 16:00:30 UTC
Permalink
Fixed in the trunk.

I'll try to issue a release at the weekend.

- Paul
Post by Mike Simpson
Post by Paul Hammant
Do you mean that ..
Thing t = (Thing) mpc.getComponent( picotest.model.Thing.class );
.. give you a new instance every time ? or ..
t.poke();
.. is giving you a new instance each poke ?
The latter. The ComponentMonitor logs seem to indicate that there's
an instantiation event occuring every time I call t.poke(), and that
the method invocation is being passed to the newly-instantiated
object each time.
-mgs
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Loading...