2 Replies Latest reply on Jun 6, 2008 4:44 PM by joaobmonteiro

    How do you inject diferent implementations in Seam?

    joaobmonteiro

      Hi guys,


      'I´m facing an interesting question in Seam. I have many services that I inject in my managed beans, for example:


      
      //my service interface
      
      public interface IncidenteService {
      
      ...
      
      }
      
      
      //my service implementation
      
      @Name("incidenteService")
      
      public class IncidenteServiceDdsiImpl implements IncidenteService {
      
      ...
      
      }
      
      
      //my service implementation
      
      @Name("incidenteService")
      
      public class IncidenteServiceDajImpl implements IncidenteService {
      
      ...
      
      }
      
      //my managedBean
      
      ...
      
      @In(create=true)
      
      private IncidenteService incidenteService;
      
      ...
      
      



      In my case, I have to decide according to user profile wich implementation should be injected. If user profile is DDSI, I should use IncidenteServiceDdsiImpl and so on.


      Note that I am trying to apply the Strategy Design Pattern and I want Seam to inject the correct implemantion for me.


      The question is how can I use Seam to inject the correct implementation?


      Of course I can´t have components with same name and same precedence. And in this case, I think that changing precedene will not help at all since I have to choose the right component according to some user information.


      So, guys, have you ever resolve this problem?


      Thanks!




        • 1. Re: How do you inject diferent implementations in Seam?
          bravocharlie.seam.signup.benny.me.uk

          How about


          @Name("incidenteServiceDdsi")
          public class IncidenteServiceDdsiImpl implements IncidenteService { }
          
          @Name("incidenteServiceXyz")
          public class IncidenteServiceXyzImpl implements IncidenteService { }
          
          
          @Name("incidenteService")
          public class IncidenteServiceManager {
          
             @In
             UserProfile userProfile
          
             @Unwrap
             public IncidenteService getIncidenteService(){
                if (userProfile.getType() == ...){
                   return Component.getInstance("incidenteServiceDdsi");
                } else {
                   ...
                }
             }
          
          }

          • 2. Re: How do you inject diferent implementations in Seam?
            joaobmonteiro

            Hi Ben,


            Thanks for this sweet solution! I was not aware of using @Unwrap.


            Looking further in documentation, I wonder if I could use


            @Factory('incidenteService') in somewhere like this:


            
            @Name("serviceFactory")
            
            public class ServiceFactory {
            
                 
            
                 @Factory("incidenteService")
            
                 public IncidenteService getIncidenteService(){
            
                         //some business logic to decide
            
                         //which implementation          
            
                 }
            
            }
            
            


            I will try both approaches.


            Thanks again!