所以在我的观点中,我注入了我的演示者,因为我需要这样做才能使用我的活动。我的观点如下:
public partial class ImpositionForm : Form, IImpositionFormView {
private ImpositionFormPresenter presenter;
private readonly ISignatureSizeManager signatureSizeManager;
private readonly ISystemVariablesManager systemVariablesManager;
private readonly string expectedPathToAppSettings = $"{AppDomain.CurrentDomain.BaseDirectory}/PrintAppSettings.txt";
public event EventHandler<EventArgs> SetSheetSizeAcross;
public event EventHandler<EventArgs> SetSheetSizeAround;
public event EventHandler<ErrorEventArgs> Error;
public event EventHandler<EventArgs> ClearPage;
public event EventHandler<EventArgs> SetSignatureSize;
public event EventHandler<EventArgs> ShowSystemVariablesForm;
public ImpositionForm(ImpositionFormPresenter _presenter, ISignatureSizeManager _signatureSizeManager,
ISystemVariablesManager _systemVariablesManager) {
presenter = _presenter;
systemVariablesManager = _systemVariablesManager;
signatureSizeManager = _signatureSizeManager;
InitializeComponent();
}
//Other code
}
我在使用构建我的演示者时注入视图:
public class ImpositionFormPresenter {
private readonly ISignatureSizeManager signatureSizeManager;
private readonly ISystemVariablesManager systemVariablesManager;
private readonly SystemVariablesFormPresenter systemVariablesFormPresenter;
private readonly string pathToAppSettings = $"{AppDomain.CurrentDomain.BaseDirectory}/PrintAppSettings.txt";
private readonly IImpositionFormView view;
public ImpositionFormPresenter(IImpositionFormView _view, ISignatureSizeManager _signatureSizeManager,
ISystemVariablesManager _systemVariablesManager, SystemVariablesFormPresenter _systemVariablesFormPresenter) {
view = _view;
signatureSizeManager = _signatureSizeManager;
systemVariablesManager = _systemVariablesManager;
systemVariablesFormPresenter = _systemVariablesFormPresenter;
InitialiseEvents();
}
private void InitialiseEvents() {
view.SetSheetSizeAcross += SetSheetSizeAcross;
view.SetSheetSizeAround += SetSheetSizeAround;
view.Error += LogErrorToView;
view.SetSignatureSize += SetSignatureSizeValues;
view.ShowSystemVariablesForm += OpenSystemVariablesForm;
}
//Other code
}
这是一个问题,因为我收到一个循环,其中视图依赖于演示者,演示者依赖于视图。
但是,我不确定如何解决此问题,创建我的演示者和视图的新实例而不是注入它们。任何人都可以帮助我,所以我可以继续使用依赖注入吗?
答案 0 :(得分:0)
视图/表单完全不应该引用演示者。与演示者的任何通信均应通过演示者订阅的事件来完成。
从那里,您可以让Form实例化具体的演示者并传递对自身的引用,也可以让您的合成根目录/ IOC容器创建两者并将视图传递给演示者。在前一种情况下,具体视图将需要以某种方式获得演示者需要的所有其他依赖项。
有关合成词根,请参见this answer。
这里要注意的是,订阅创建了一个循环引用,该引用防止垃圾回收。因此,您应该让视图触发一个事件,指示该事件正在关闭。然后可以正常地对演示者进行垃圾收集。