Recursive binding in Guice

711 views
Skip to first unread message

Adio

unread,
Apr 28, 2012, 2:21:08 PM4/28/12
to google-guice
Hi every one, i am totally new to Guice - In DI frameworks in general
- . Now i am facing a dilemma witch is how to bind and create with
guice object that has dependency injection ? and here is what i mean

@Inject
public class A {
private B b ;

public A(B b){
this.b=b;
}
}

public class B {

private A a ;

@Inject
public B(A a){
this.a = a;
}


and here is a real situation of my problem, actually it is the MVP GWT
pattern design using UiBinding.

public class LoginPresenterImpl implements LoginPresenter {

private HasHandlers eventBus;
private LoginFormErrorMessages errorMessages;
private UserAccountServiceAsync async;
private LoginView loginDisplay;

@Inject
public LoginPresenterImpl(UserAccountServiceAsync serviceAsync,
LoginView loginview, HasHandlers handlerManager,
LoginFormErrorMessages loginFormErrorMessages) {
async = serviceAsync;
loginDisplay = loginview;
eventBus = handlerManager;
errorMessages = loginFormErrorMessages;
}

......

}

and the LoginView is :

public class LoginViewImpl extends Composite implements LoginView {


@UiTemplate("LoginView.ui.xml")
interface LoginViewUiBinder extends UiBinder<DecoratorPanel,
LoginViewImpl> {
}

@UiField
TextBox username;
@UiField
PasswordTextBox password;
@UiField
Button login;
@UiField
Button newRegistration;
@UiField
Label warningLabel;


private LoginViewUiBinder binder =
GWT.create(LoginViewUiBinder.class);

@Inject
private LoginPresenter loginPresenter;


public LoginViewImpl() {
initWidget(binder.createAndBindUi(this));
}
......

}

how to binde the view ??? and the Presenter ?


Fred Faber

unread,
Apr 29, 2012, 11:17:59 AM4/29/12
to google...@googlegroups.com
This is a problem we've dealt with by using either assisted injection or a setter method.

To use the former, the pattern is this:

// Some common marker interfaces
interface View { }
interface Presenter { }

// A common factory interface
interface ViewFactory<P extends Presenter, V extends View> {
  V create(P presenter);
}

// You can also do this based on a subclass, etc 
interface YourView extends View {
 //...define its methods
 
 interface YourViewFactory extends ViewFactory<YourPresenter, YourView> { }
}

class YourPresenter {
  private final YourView view;

  @Inject(YourViewFactory factory, ...) {
     this.view = factory.create(this);
   }
  ...
}

Depending on your abstraction needs, you can collapse / pull up the interface definitions as you wish.

The most important thing here IMO is to keep the presenter free of GWT logic, and as long as you do that, your unit testing of the presenter can be done using plain testing code (i.e., no GWT testing code is necessary).

Some folks don't mind having the "presenter" variable non-final in the view, and define a setter() method on the view:

class YourPresenter {
  private final YourView view;

  @Inject(YourView view, ...) {
     this.view = view;
     view.setPresenter(this);
   }
  ...
}

There is more detail in this GWT issue, fwiw:

Fred



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


Adio

unread,
Apr 30, 2012, 4:50:49 AM4/30/12
to google-guice

Thanks very much ...

Adio

unread,
Apr 30, 2012, 4:25:02 AM4/30/12
to google-guice

Thank you very much, this setter method is what i use currently and yo
just told me another trick. and yes my presenters do not have any GWT
logic and all my presenters unite test are only Junit, not GWTTest
Case !
Reply all
Reply to author
Forward
0 new messages