With the code you show, the errors are expected.
You won't be able to inject:
@Inject @StyleA private MyInterface interfaceTypeA;
@Inject @StyleB private MyInterface interfaceTypeB;
Because implementations of MyInterface should be created by
MyInterfaceFactory:
interface MyInterfaceFactory {
@StyleA MyInterface createStyleA(SomeParam someParam);
@StyleB MyInterface createStyleB(SomeParam someParam);
}
You should then:
@Inject MyInterfaceFactory myInterfaceFactory;
and the logic to create @StyleA or @StyleB will look like:
MyInterface createMyInterface(SomeParam someParam) {
if (...) {
return myInterface.createStyleA(someParam);
} else {
return myInterface.createStyleB(someParam);
}
The bindings you show in your mail should work within this model.
Fred
On Wed, May 4, 2011 at 2:57 PM, mhgrove <[email protected]> wrote:
> Or not, maybe the assisted inject extension is the wrong way to go
> about this. Hopefully someone who understands guice better than I do
> can clarify for me.
>
> (I'm using Guice 3.0 btw)
>
> I have an interface, MyInterface and two concrete implementations
> ImplementationA and ImplementationB and a factory interface
> MyInterfaceFactory for creating implementations of MyInterface. I
> also have two annotations, @StyleA and @StyleB which correspond
> roughly to the two implementations.
>
> I'd like to have a class created by Guice that has two members like
> so:
>
> @Inject @StyleA private MyInterface interfaceTypeA;
> @Inject @StyleB private MyInterface interfaceTypeB;
>
> This class is also a factory and contains the logic to determine
> whether or not A or B is more suitable for the thing it happens to be
> creating.
>
> I was reading about the AssistedInject stuff and I thought I could do
> something like:
>
> install(new FactoryModuleBuilder()
> .implement(MyInterface.class, StyleA.class,
> ImplementationA.class)
> .implement(MyInterface.class, StyleB.class,
> ImplementationB.class)
> .build(MyInterfaceFactory.class));
>
> But at runtime i get guice errors telling me there's no implementation
> of MyInterfaceFactory annotated with @StyleA (or @StyleB) bound when
> I'm trying to get values injected into the aforementioned fields. It
> also tells me that MyInterface is an interface, not a concrete class,
> which is true, and that it's unable to create an AssistedInject
> factory.
>
> I thought the above use of FactoryModuleBuilder was equivalent to
> having done:
>
>
> bind(MyInterfaceFactory.class).annotatedWith(StyleA.class).toProvider(FactoryProvider.newFactory(MyInterfaceFactory.class,
> ImplementationA.class));
>
> bind(MyInterfaceFactory.class).annotatedWith(StyleB.class).toProvider(FactoryProvider.newFactory(MyInterfaceFactory.class,
> ImplementationB.class));
>
> Maybe it is, maybe it isn't. I'm going to try that next, but I see
> that FactoryProvider is deprecated, and I'd rather not rely on it if
> there is a more appropriate way to make this happen.
>
> Am I misunderstanding how these are to be used and/or using them
> incorrectly? Is there a better way to accomplish this?
>
> Thanks for any help.
>
> Mike
>
> --
> 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.
>
>
--
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.