castle IOC - resolving circular references

2019-07-02 10:42发布

quick question for my MVP implementation:

currently I have the code below, in which both the presenter and view are resolved via the container.
Then the presenter calls View.Init to pass himself to the view.

I was wondering however if there is a way to let the container fix my circular reference (view -> presenter, presenter -> view).

class Presenter : IPresenter {
   private View _view; 

   public Presenter(IView view, ...){
    _view = view;
    _view.Init(this)
   }
}

class View : IView {
 private IPresenter _presenter;
 public void Init(IPresenter presenter){
  _presenter = presenter;
 }
}

Kind regards

Frederik

2条回答
Root(大扎)
2楼-- · 2019-07-02 11:26

You could use a property setter instead of passing the reference into the constructor.

查看更多
劳资没心,怎么记你
3楼-- · 2019-07-02 11:40

As long as you put both Presenter and View inside the same csproject, there shouldn't be any circular reference

查看更多
登录 后发表回答