Okay, I think I've got it.

My Dao.class does not need to be @ImplementedBy anything. So I removed
this annotation.


Next I created 2 Modules:
JDBCDAOModule.java - only handles impementations of my DAO by JDBC
LDAPDAOModule.java - likewise only handles implementations of my DAO
by LDAP

Instantiate the Injector with the required Module

Then use a setter method on my ManagerImpl to set the DAO.

Is this standard practice?




On Sep 27, 2:18 pm, Yaxin Liu 刘亚新 <[email protected]> wrote:
> @ImplementedBy.  It only works with a single available implementation.  You
> need module definitions and explicit bindings.
>
>
>
>
>
> On Sun, Sep 26, 2010 at 8:54 PM, Anthony <[email protected]> wrote:
> > I'm trying to work out the right way to perform the following:
>
> > I have a Manager and ManagerImpl. The ManagerImpl uses a Dao with 2
> > possible implementations (JDBCDao and LDAPDao)
>
> > So...
>
> > @ImplementedBy(JDBCDao.class)
> > public interface Dao {
> >     public String getString();
> > }
>
> > public class JDBCDao implements Dao {
> >     public String getString() {
> >          return "JDBC implementation called";
> >     }
> > }
>
> > public class LDAPDao implements Dao {
> >     private String mConfig;
>
> >     public LDAPDao(String config) {
> >          mConfig = config;
>
> >     }
>
> >     public String getString() {
> >          return "LDAP impementation called with config ["+mConfig
> > +"]");
> >     }
> > }
>
> > public interface Manager {
> >     public String doDao();
> >     public void setDao(Dao aDao);
> > }
>
> > public class ManagerImpl implements Manager {
> >     @Inject
> >     Dao mDao;
>
> >     public String doDao() {
> >          return mDao.getString();
> >     }
>
> >     public void setDao(Dao aDao) {
> >          mDao = aDao;
> >     }
> > }
>
> > I'm trying to work out how to properly set this up in my UnitTest to
> > test it out. I created a DAOFactory like so:
>
> > public interface DAOFactory {
> >     JDBCDao getJDBCDao();
> >     LDAPDao getLDAPDao(String config);
> > }
>
> > And a DAOModule:
>
> > public class DAOModule implements Module {
> >     public void configure(Binder binder) {
>
> > binder.bind(DAOFactory.class).toProvider(FactoryProvider.newFactory(DAOFact 
> > ory.class,
> > Dao.class));
> >     }
> > }
>
> > And create the following test (in a unit test class):
>
> > DAOModule module = new DAOModule();
> > Injector injector = Guice.createInjector(module);
>
> > Provider<DAOFactory> provider =
> > injector.getProvider(DAOFactory.class);
>
> > Dao aDAO = provider.get().getLDAPDao("config_file.txt");
>
> > Manager dManager = injector.getInstance(Manager.class);
>
> > dManager.setDAO(aDAO);
> > System.out.println(dManager.doDao());
>
> > But I get a ClassCastException on the line:
> > Dao aDAO = provider.get().getLDAPDao("config_file.txt");
>
> > What am I doing wrong?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "google-guice" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<google-guice%2bunsubscr...@google 
> > groups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/google-guice?hl=en.
>
> --
> Yaxin

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to