3 Replies Latest reply on Mar 15, 2010 2:05 PM by sebek64

    promoting beans to higher scopes

    sebek64

      Is it possible to promote scope of a bean like in this example?




      @Name("conv")
      @Scope(CONVERSATION)
      class Conv {
              List<CustomEntry> listOfEntries;
      
              public void addEntry(CustomEntry e) {
                      listOfEntries.add(e);
              }
      }
      






      @Name("entry")
      @Scope(EVENT)
      class Entry {
              // some data supplied by user in a JSF form
      }
      




      I've noticed that in Weld this is
      forbidden.

        • 1. Re: promoting beans to higher scopes
          sebek64

          It seems that the code above is invalid. I found this solution. The data class is not a seam component itself. Instead, there is a component in request scope that only instantiates the data class and holds a reference to it. When the component is destroyed, it's still valid to hold reference to the data class in a collection.


          @Name("conv")
          @Scope(CONVERSATION)
          class Conv {
                  List<CustomEntry> listOfEntries;
          
                  public void addEntry(EntryRef e) {
                          listOfEntries.add(e.getRef());
                  }
          }
          


          @Name("entryRef")
          @Scope(EVENT)
          class EntryRef {
                  private CustomEntry ref = new CustomEntry();
          
                  public CustomEntry getRef() { return ref; }
          }
          


          class CustomEntry {
                  // some data supplied by user in a JSF form
          }
          

          • 2. Re: promoting beans to higher scopes
            sebek64

            And when I think about it, the same result could be achieved using @Factory:


            @Name("conv")
            @Scope(CONVERSATION)
            class Conv {
                    List<CustomEntry> listOfEntries;
            
                    public void addEntry(CustomEntry e) {
                            listOfEntries.add(e);
                    }
            
                    @Factory
                    public void createEntry() { return new CustomEntry e; }
            }
            

            • 3. Re: promoting beans to higher scopes
              sebek64

              oops, there should be @Factory("entry",scope=EVENT).