castle IOC - 解决循环引用
关于我的 MVP 实现的简单问题:
目前我有下面的代码,其中演示者和视图都是通过容器解析的。
然后演示者调用 View.Init 将自己传递给视图。
然而,我想知道是否有一种方法可以让容器修复我的循环引用(视图 -> 演示者,演示者 -> 视图)。
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;
}
}
亲切的问候
弗雷德里克
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用属性设置器,而不是将引用传递到构造函数中。
You could use a property setter instead of passing the reference into the constructor.
只要将Presenter和View放在同一个csproject中,就不应该有任何循环引用
As long as you put both Presenter and View inside the same csproject, there shouldn't be any circular reference