Hello,
I would like to create a 1 to 1 relationship between two components.
Here is the "old-fashion" code that represent what I want to have.
import static org.junit.Assert.*;
import org.junit.Test;
public class OneToOneRelationshipTest {
static class A {
final B b;
A () { this.b = new B(this); }
}
static class B {
final A a;
B (A a) { this.a = a; }
}
@Test public void testOneToOneDependency() {
A a = new A();
assertEquals(a, a.b.a); // check identity
}
}
I've thought about using AssistedInject to transfer a factory of B to
A:
import static org.junit.Assert.*;
import org.junit.Test;
import com.google.inject.*;
import com.google.inject.assistedinject.*;
public class OneToOneRelationshipWithGuiceTest {
static interface Factory { public B create(A a); }
static class A {
final B b;
@Inject A (Factory f) { this.b = f.create(this); }
}
static class B {
final A a;
@Inject B (@Assisted A a) { this.a = a; }
}
static class Module extends AbstractModule {
public void configure() {
bind(A.class);
bind(Factory.class).toProvider(FactoryProvider.newFactory
(Factory.class, B.class));
}
}
@Test public void testOneToOneDependency() {
A a = Guice.createInjector(new Module()).getInstance(A.class);
assertEquals(a, a.b.a); // check identity
}
}
However I'm wondering whether this is "too much" or not because my
expectations are quite simple, but I feel like a more simple @Provides
can help me. I just don't know how I could correctly modelize it,
because I didn't see an example on the wiki.
Can anyone help me getting rid of AssistedInject if possible?
Olivier
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---