On Feb 27, 11:03 am, Fred Faber <[email protected]> wrote:
> Pls look here:
>  http://code.google.com/p/google-guice/wiki/AssistedInject

I've faced a similar dynamic discovery problem, and have taken the
general approach (for better or worse), of
 - Have a registry class which can look up the instance I want based
on some parameter
 - Either use Multibinder or classpath scanning to find Providers of
each at bind-time (also using my own @Ordered(int) annotation to
transform to a list - using a Set for this sort of thing is a recipe
for accidentally depending on things being called in a particular,
arbitrary order
 - Inject the registry object where I need it - i.e.

abstract class MyAbstractModule extends AbstractModule {
   protected final List<Provider<T>> providers (Class<?> type) { //
just get all the providers and sort }
}

abstract class HashType {
  abstract boolean accept (ArgType arg);
  abstract void doSomething(ArgType arg);
}

class HashTypes {
  private final List<Provider<HashType>> providers;
  ...
  public final HashType forObject(ArgType arg) {
     for (Provider<HashType> p : providers) {
         if (p.get().accept(arg)) return p.get();
     }
  }
}

final class MyModule extends MyAbstractModule {
   public void configure() {
     bind (HashTypes.class).toInstance(new
HashTypes(providers(HashTypes.class));
   }
}

@Ordered (100)
final class HashType1 extends HashType {
  public boolean accept (ArgType arg) {  return arg.getFoo() <
whatever; }
  ...
}

@Ordered(Integer.MAX_VALUE)
final class UnknownHashType extends HashType {
  public boolean accept (ArgType arg) {  return true; }
}

(another pattern I use is, instead of an accept() method,
parameterize, e.g., HashType<T> on a particular type w/ the class
object passed in the constructor, and then look for, e.g., an
AccountFactory<EmailAddress>).

I looked at the AssistedInject stuff before, but it always has seemed
verbose and, to anyone reading the code who is not already steeped in
Guiciness, pretty non-obvious.  This is verbose too, but it seems more
straightforward to figure out what's going on (although I suspect
Guice die-hards might tell me it's not the Guice Way).  I may just not
be comfortable enough with magical annotations.

I'm using this fairly heavily in a codebase in which lots of files of
arbitrary types will be uploaded, with certain parameters that,
combined with the MIME type, determine how they're handled.  The
natural future of such code is to end up in a spaghetti-code mess of
if/then/else's for special cases.  Factoring each case into an object
makes it trivial both to add new cases, and to reorder the logic as
necessary.

-Tim

-- 
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