castle IOC - resolving circular references

本秂侑毒 提交于 2019-12-12 13:45:41

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/1783124/castle-ioc-resolving-circular-references

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!