@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(DAOFactory.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%[email protected]>
> .
> 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