List of named components as arg to constructor param

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

List of named components as arg to constructor param

by joe panico-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I'm using PicoContainer 2.8.3.

I would like Pico to explicitly wire specific named (keyed) components
into a constructor arg for a List type param. For example:

class Element{
   private final String config;
   public Element(String config){
      this.config = config
   }
}

class Composition{
   private final List<Element> elements;
   public Composition(List<Element> elements){
      this.elements = elements
   }
}

pico.addComponent("elementA", Element.class, new ConstantParameter("configA"))
pico.addComponent("elementB", Element.class, new ConstantParameter("configB"))
pico.addComponent("elementC", Element.class, new ConstantParameter("configC"))

pico.addComponent("compositionAB", Composition.class, ?tell it to use
keys "elementA" + "elementB"?)
pico.addComponent("compositionBC", Composition.class, ?tell it to use
keys "elementB" + "elementC"?)

How do I achieve those last two lines of pseudo code? I don't care if
the solution is not typesafe with respect to generics.

thanks for any pointers.

Joe

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

    http://xircles.codehaus.org/manage_email



Re: List of named components as arg to constructor param

by Paul Hammant-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

pico = new DefaultPicoContainer(new NullComponentMonitor() {

     public Object noComponentFound(MutablePicoContainer container,  
Object componentKey);
          String[] keys = componentKey.split(",");
          List<Element>() elements = new ArrayList<Element>();
          for (String key in keys) {
              elements.add(container.get(key));
          }
          return new Composition(elements);
      }

});


pico.addComponent("elementA", Element.class, new ConstantParameter
("configA"))
pico.addComponent("elementB", Element.class, new ConstantParameter
("configB"))
pico.addComponent("elementC", Element.class, new ConstantParameter
("configC"))

abComposition = pico.getComponent("elementA,elementB");
bcComposition = pico.getComponent("elementB,elementC");

-----

No need to add Composition to the container for this specialized case,  
it'll be cached anyway for the key in question, unless you don't do  
caching in the PicoContainer setup.  You obviously have to be  
comfortable with participating in the List creation.

You could aim at some smaller language in the missing key definition ,  
of you were prepared to do more string processing in the  
noComponentFound(..).

     abComposition = pico.getComponent("A,B");
     abComposition = pico.getComponent("AB");
     abComposition = pico.getComponent("compositionAB");
     abComposition = pico.getComponent("elems(A,B)");

Regards,

- Paul


On Sep 11, 2009, at 5:38 AM, joe panico wrote:

> Hello,
>
> I'm using PicoContainer 2.8.3.
>
> I would like Pico to explicitly wire specific named (keyed) components
> into a constructor arg for a List type param. For example:
>
> class Element{
>   private final String config;
>   public Element(String config){
>      this.config = config
>   }
> }
>
> class Composition{
>   private final List<Element> elements;
>   public Composition(List<Element> elements){
>      this.elements = elements
>   }
> }
>
> pico.addComponent("elementA", Element.class, new ConstantParameter
> ("configA"))
> pico.addComponent("elementB", Element.class, new ConstantParameter
> ("configB"))
> pico.addComponent("elementC", Element.class, new ConstantParameter
> ("configC"))
>
> pico.addComponent("compositionAB", Composition.class, ?tell it to use
> keys "elementA" + "elementB"?)
> pico.addComponent("compositionBC", Composition.class, ?tell it to use
> keys "elementB" + "elementC"?)
>
> How do I achieve those last two lines of pseudo code? I don't care if
> the solution is not typesafe with respect to generics.
>
> thanks for any pointers.
>
> Joe
>
> ---------------------------------------------------------------------
> 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: List of named components as arg to constructor param

by Paul Hammant-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Actually, a bug in PicoContainer itself would prevent that from  
working.  There's a SNAPSHOT that's been uploaded that will work  
better ( picocontainer 2.9-20090915.120308-1 )

Watch for some new architecture in the next release that will allow  
for regex to be used in component gathering for situations like this.

Regards,

- Paul

On Sep 12, 2009, at 9:57 AM, Paul Hammant wrote:

> pico = new DefaultPicoContainer(new NullComponentMonitor() {
>
>    public Object noComponentFound(MutablePicoContainer container,  
> Object componentKey);
>         String[] keys = componentKey.split(",");
>         List<Element>() elements = new ArrayList<Element>();
>         for (String key in keys) {
>             elements.add(container.get(key));
>         }
>         return new Composition(elements);
>     }
>
> });
>
>
> pico.addComponent("elementA", Element.class, new ConstantParameter
> ("configA"))
> pico.addComponent("elementB", Element.class, new ConstantParameter
> ("configB"))
> pico.addComponent("elementC", Element.class, new ConstantParameter
> ("configC"))
>
> abComposition = pico.getComponent("elementA,elementB");
> bcComposition = pico.getComponent("elementB,elementC");
>
> -----
>
> No need to add Composition to the container for this specialized  
> case, it'll be cached anyway for the key in question, unless you  
> don't do caching in the PicoContainer setup.  You obviously have to  
> be comfortable with participating in the List creation.
>
> You could aim at some smaller language in the missing key  
> definition , of you were prepared to do more string processing in  
> the noComponentFound(..).
>
>    abComposition = pico.getComponent("A,B");
>    abComposition = pico.getComponent("AB");
>    abComposition = pico.getComponent("compositionAB");
>    abComposition = pico.getComponent("elems(A,B)");
>
> Regards,
>
> - Paul
>
>
> On Sep 11, 2009, at 5:38 AM, joe panico wrote:
>
>> Hello,
>>
>> I'm using PicoContainer 2.8.3.
>>
>> I would like Pico to explicitly wire specific named (keyed)  
>> components
>> into a constructor arg for a List type param. For example:
>>
>> class Element{
>>  private final String config;
>>  public Element(String config){
>>     this.config = config
>>  }
>> }
>>
>> class Composition{
>>  private final List<Element> elements;
>>  public Composition(List<Element> elements){
>>     this.elements = elements
>>  }
>> }
>>
>> pico.addComponent("elementA", Element.class, new ConstantParameter
>> ("configA"))
>> pico.addComponent("elementB", Element.class, new ConstantParameter
>> ("configB"))
>> pico.addComponent("elementC", Element.class, new ConstantParameter
>> ("configC"))
>>
>> pico.addComponent("compositionAB", Composition.class, ?tell it to use
>> keys "elementA" + "elementB"?)
>> pico.addComponent("compositionBC", Composition.class, ?tell it to use
>> keys "elementB" + "elementC"?)
>>
>> How do I achieve those last two lines of pseudo code? I don't care if
>> the solution is not typesafe with respect to generics.
>>
>> thanks for any pointers.
>>
>> Joe
>>
>> ---------------------------------------------------------------------
>> 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
>
>


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

    http://xircles.codehaus.org/manage_email