7 Replies Latest reply on Jul 3, 2009 10:40 AM by Pete Muir

    Injection into Enums!

    Nirav Shah Newbie

      Hello!


      I have more of a design problem rather than a Webbeans one!
      Wanted to bounce it off the people in this community and see if I can get a different brainwave!


      In our app, we use a state machine thats made up of an Enum.
      Inside the enum we have helper classes that implement specific functionality for each type defined in the enum!


      This functionality needs to differ from a customer to customer basis and hence we were thinking of harnessing the power of Webbeans here with help of Binding Types and Deployment Types.


      Would WebBeans work with Enums?
      (I guess not. We do not want to move to using Java classes cause we'd than need to do a lot of ugly switch statements.)


      Would appreciate any different ideas / help on this !


      Cheers!
      Nirav

        • 1. Re: Injection into Enums!
          Pete Muir Master

          I guess it is possible to support injection into static fields in Enums - please file a feature request in WBRI...

          • 2. Re: Injection into Enums!
            Gavin King Master

            Would you mind showing us an example of what your DSL looks like? I've recently been toying with some ideas like that for a typesafe navigation facility for Seam2. I'd like to see what you guys have come up with.

            • 3. Re: Injection into Enums!
              Elias Ross Master

              I would assume the injection might work along the lines of this:


              public enum ColorPreferences {
              
                BACKGROUND,
                FOREGROUND;
              
                @Current
                private EntityManager em;
              
                @Current
                private Customer customer;
                
                /**
                 * Returns the color for the customer for this component.
                 */
                public Color getColor() {
                  em.query(...).getSingleResult();
                }
              
              }



              • 4. Re: Injection into Enums!
                Elias Ross Master

                Just tested this, and it doesn't work. The deployment scanner skips enum classes. Is this something specified in JSR 299?


                I'm not really sure how an enum type or its enumerated values might be used as a bean.


                One idea:


                For every enumerated type, a bean is created for each of the enumerated values. (Expensive?)


                Each bean automatically has scope @ApplicationScope


                Each bean has a default name, which is the name of the unqualified class name, plus the name. (For example, com.foo.ColorPreferences.BACKGROUND.)


                To reference an enumerated type as a bean, you can use EL, or you can of course inject it using a binding type, like @Current.

                • 5. Re: Injection into Enums!
                  Pete Muir Master

                  Enum's aren't managed beans (see 3.1.1, It has an appropriate constructor... - Enum's don't have callable constructors), so 299 doesn't require them to be available for injection etc.


                  Atm I haven't quite finished implementing the third party SPI for injection (done either early next week, or mid July as I am on PTO in early July), but once that is done, you should prototype a third party extension that supports your ideas and see if people like it.

                  • 6. Re: Injection into Enums!
                    Elias Ross Master

                    Would like to toy around with this, but my new employer's prohibiting open source contributions ATM. We'll see.


                    The comprehension problem I see with enum values is they would need to be always referenced through some sort of injection or the EL, and not through the name. This is quite non-intuitive.


                    The main benefit is a developer can easily/quickly create a number of grouped, named, singleton beans and also wouldn't need to use @Produces or create them through XML.

                    • 7. Re: Injection into Enums!
                      Pete Muir Master

                      Elias Ross wrote on Jul 02, 2009 19:43:


                      The comprehension problem I see with enum values is they would need to be always referenced through some sort of injection or the EL, and not through the name. This is quite non-intuitive.



                      Right, this is why I feel it's something not to support.