5 Replies Latest reply on Mar 31, 2010 10:45 PM by chasetec

    Are binding types there just to avoid injection by name?

    phantasmo

      I am reading some examples that demonstrate the use of CDI beans, and I can't escape the feeling that the whole binding type thing is unnecessarily complicated. It seems to me it's there just to avoid injection by name (like the one we have in Seam).


      Am I under the wrong impression or is this really the case? I'd truly appreciate some insight...

        • 1. Re: Are binding types there just to avoid injection by name?
          phantasmo

          Just to clarify what I mean. With CDI, I would have something like this (examples taken from Prentice Hall's book on Seam):


          @Produces @ConversationScoped
            public List<Hotel> getHotels()
            {
              // query for hotels and return result
            }



          and this:


          @Produces @ConversationScoped @MostPopular
            public List<Hotel> getMostPopularHotels()
            {
              // query for most popular hotels and return result
            }



          while in Seam I'd have something like this:


          @Factory(value="hotels", scope=ScopeType.CONVERSATION)
          public List<Hotel> getHotels()
          {
              //query for hotels and return result
          }



          and this:


          @Factory(value="mostPopular", scope=ScopeType.CONVERSATION)
          public List<Hotel> getMostPopularHotels()
          {
              //query for hotels and return result
          }



          Now, apart from the first injecting by type and binding type and the second inject by type and name, both achieve the same thing, but Seam's version is easier to work with as there's no need to implement a custom annotation. What was wrong with injection by name, and do binding types provide some new capability that would warrant them?

          • 2. Re: Are binding types there just to avoid injection by name?
            nickarls

            Well you can have multiple qualifiers. And alternatives. And use them for event observing. And bind interceptors and decorators with it. Those things would be a little tricky to do with named binding, right?

            • 3. Re: Are binding types there just to avoid injection by name?
              phantasmo

              I understand some of the arguments and hope to understand the rest after I further familiarize myself with the spec. Anyway, you gave me the info I was looking for, and I am thankful :)

              • 4. Re: Are binding types there just to avoid injection by name?
                nickarls

                Have a look at the Weld reference docs behind the Documentation link, it should be mostly easy reading if you're already familiar with Seam 2.

                • 5. Re: Are binding types there just to avoid injection by name?
                  chasetec

                  The most basic reason is that binding by name (a String value) will not be validated by the compiler, only the runtime environment. Which means it is really easy to screw up. Using annotations as the binding meta-data allows for a compile-time validation. While you can still specify a different binding than you meant to, you can not specify an incorrect (non-existent) value.