2 Replies Latest reply on May 30, 2009 8:03 PM by Gavin King

    applying custom binding types

    Arbi Sookazian Master

      So I'm trying to understand binding types concept in 299.


      I'm following the example code in 34.2 Component Injection section of Yuan et al Seam fwk, 2nd ed.


      If I understand this concept correctly, there appears to be a mistake (or two) in the sample code on pg. 413.


      Specifically, on pg. 413, the injection of paymentProcessor instance in the HotelBookingAction component via @Current or @CreditCard should be paymentService.


      Am I correct?


      Essentially the idea behind binding types is to be able to inject an instance of an interface and thus not create a direct dependency on the implementation class.  But the only interface mentioned in this section of the book is PaymentService.


      The PaymentProcessor reference on pg. 413 seems to come from 299:


      class SynchronousPaymentProcessor implements PaymentProcessor {
      ...
      }
      
      class AsynchronousPaymentProcessor implements PaymentProcessor {
      ...
      }



      Also, what is the advantage of injecting interface instance with metadata:


      @Synchronous PaymentProcessor paymentProcessor;



      vs. injecting the implementation class instance:


      @Current SynchronousPaymentProcessor synchronousPaymentProcessor;

      ??


      Either way, if the implementation of the interface you want to inject changes, you will have to change some code in the class...

        • 1. Re: applying custom binding types
          Arbi Sookazian Master

          I think I realized the answer.  Binding types are (at least part of) the solution for loose typing in Web Beans.


          This approach seems to be similar to the best practice used in Spring to design to interfaces so you can switch out the implementation class without breaking your client code.


          So in this context of JCDI/Web Beans, if the implementation class changes, then you simply change the annotation (binding type) and then nothing is broken in your client code.  Like using java.util.List and then switching from ArrayList to LinkedList but an annotation is now required.


          Correct?

          • 2. Re: applying custom binding types
            Gavin King Master

            Well, not exactly. If I was going to change the binding type every time I changed the implementation of the interface, I might as well just use the concrete class at the injection point. Nothing's gained by using an annotation instead.


            No, the real use case is if I have multiple beans with the same type, for example @UserDatabase EntityManager, @ProductDatabase EntityManager, etc.


            For the case where you are trying to achieve polymorphism, you are supposed to use deployment types, or producer methods.