question about expected behavior for implementation hiding container

View: New views
9 Messages — Rating Filter:   Alert me  

question about expected behavior for implementation hiding container

by Mike Simpson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi --

I'm trying to use an implementation hiding PicoContainer:

> 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 );

but simple getters and setters don't seem to work, i.e.:

>  t.setStuff( "foo" );
>  System.out.println( "this thing's stuff = " + t.getStuff() );

outputs:

> this thing's stuff = null

 From the ConsoleMonitor output, it looks like a new instance of
DefaultThing is being created for each method invocation, i.e.:

> t.poke();
> t.poke();
> t.poke();

outputs:

> PicoContainer: instantiating picotest.model.impl.DefaultThing()
> PicoContainer: instantiated picotest.model.impl.DefaultThing() [0 ms], component picotest.model.impl.DefaultThing, injected []
> PicoContainer: invoking poke() on picotest.model.impl.DefaultThing@f66cff
> PicoContainer: invoked poke() on picotest.model.impl.DefaultThing@f66cff [0 ms]
> PicoContainer: instantiating picotest.model.impl.DefaultThing()
> PicoContainer: instantiated picotest.model.impl.DefaultThing() [0 ms], component picotest.model.impl.DefaultThing, injected []
> PicoContainer: invoking poke() on picotest.model.impl.DefaultThing@6de49c
> PicoContainer: invoked poke() on picotest.model.impl.DefaultThing@6de49c [0 ms]
> PicoContainer: instantiating picotest.model.impl.DefaultThing()
> PicoContainer: instantiated picotest.model.impl.DefaultThing() [0 ms], component picotest.model.impl.DefaultThing, injected []
> PicoContainer: invoking poke() on picotest.model.impl.DefaultThing@bbf1ca
> PicoContainer: invoked poke() on picotest.model.impl.DefaultThing@bbf1ca [0 ms]

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
instance is being created for each method invocation:

> 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



Re: question about expected behavior for implementation hiding container

by Konstantin Priblouda :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


--- Mike Simpson <mike.simpson@...> wrote:


> > 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



Re: question about expected behavior for implementation hiding container

by Mike Simpson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> 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



Re: question about expected behavior for implementation hiding container

by Konstantin Priblouda :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


--- Mike Simpson <mike.simpson@...> wrote:

> > 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



Re: question about expected behavior for implementation hiding container

by Mike Simpson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> 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



Re: question about expected behavior for implementation hiding container

by Paul Hammant-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

On May 16, 2008, at 6:12 AM, Konstantin Priblouda wrote:

>
> --- Mike Simpson <mike.simpson@...> wrote:
>
>>> 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
>
>


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

    http://xircles.codehaus.org/manage_email



Re: question about expected behavior for implementation hiding container

by Paul Hammant-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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());
     }


On May 16, 2008, at 7:55 AM, Paul Hammant wrote:

> 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



Re: question about expected behavior for implementation hiding container

by Mike Simpson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> 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



Re: question about expected behavior for implementation hiding container

by Paul Hammant-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Fixed in the trunk.

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

- Paul


On May 16, 2008, at 8:28 AM, Mike Simpson wrote:

>> 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
>
>


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

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price!