MVP 流程问题
我正在使用 Windows 窗体实现 MVP 模式,并且我对当前的实现有一个疑问,因为我试图将其融入到更复杂的体系结构中。 现在,我有一个完全不可知的视图,其属性、一个在构造函数中注入视图的演示者以及具有演示者实例的视图。这是代码: View
public class MyView : IMyView
{
public MyView()
{
var presenter = new MyPresenter(this);
presenter.Init();
}
}
这是Presenter
public class MyPresenter
{
private IMyView view;
private MyModel model;
//
public MyPresenter(IMyView view)
{
// injection
this.view = view;
}
}
通过这种方式,我可以完成两项任务:
- 从 View 调用 Presenter 上的方法
- 从 Presenter 与视图交互 现在,我有两个问题:
编排我使用 IoC 容器的所有内容,以便我可以轻松编写如下代码:
var view = ioc.Resolve
(); var Presenter = ioc.Resolve (); //查看注入的内容 NavigationService.Show(演示者.View); 到目前为止一切顺利。
第一个问题:工作完成后如何从演示者那里取回模型? View 使用的 Presenter 与我在 IoC 容器中使用的 Presenter 不同,因为 View 自己实例化了一个新的 Presenter ...因此 Presenter 公开的模型与 View 中实例化的 Presenter 使用的模型不同
第二个问题:当我有一个 MVP 三元组时,如何将现有模型传递给这个 MVP 三元组?例如,如何使此代码适用于模型来自存储库的详细信息视图?
I am implementing the MVP pattern with Windows Form and I have a question about the current implementation as I am trying to fit this into a more complex architecture.
Right now I have a total agnostic view with properties, a presenter that get injected the view in the constructor and the view which has an instance of the presenter. This is the code:
View
public class MyView : IMyView
{
public MyView()
{
var presenter = new MyPresenter(this);
presenter.Init();
}
}
This is the Presenter
public class MyPresenter
{
private IMyView view;
private MyModel model;
//
public MyPresenter(IMyView view)
{
// injection
this.view = view;
}
}
In this way I can accomplish two tasks:
- Call methods on the Presenter from the View
- Interact with the View from the Presenter
Now, I have two questions: To orchestrate everything I am using the IoC container so that I can easily write code like this one:
var view = ioc.Resolve<IMyView>(); var presenter = ioc.Resolve<MyPresenter>(); //view injected NavigationService.Show(presenter.View);
So far so good.
First question: how I can get back the Model from the Presenter when the job is done? The presenter used by the View is not the same I am using from the IoC container as the View instantiate a new Presenter by itself ... so the model exposed by the Presenter is not the same used by the Presenter instantiated in the View
Second question: how I can pass an existing model to this MVP triad when I have one? For example how can I make this code working for a Details View where the Model is coming from a Repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了采用更简洁的方法,也将模型注入到演示器中
通过这样做,您始终可以在创建模型的演示器之外引用模型。
当您这样做时,您始终可以选择要使用的模型。
例如,如果您的后端(您的模型)尚未完成,您可以编写一个模拟模型(当您使用模型的接口时)来测试您的演示者和视图。
希望这有帮助
To have a more clean approach inject the model to the presenter, too
By doing this you always have a reference to the model outside of the presenter where you created it.
When you are doing it like this, you can always choose which model you want to use.
For example if your Backend (your model) is not finished, you could write a Mock-Model (when you use an interface for the model) to test your presenter and your view.
Hope this helped
基于此,我将通过反转依赖关系来改变一切。
现在,视图不会创建演示者的实例,但它会通过演示者本身以以下方式注入实例:
到目前为止,一切顺利。
现在在视图方面,Presenter 被公开为只写属性,因此视图无法更改它,但它只能使用:
到目前为止一切顺利。
现在,国际奥委会将这样做:
对我来说,这看起来不错,但我只是想确保我没有打破 MVP 逻辑。
Based on that I will change everything by inverting the dependencies.
The view now doesn't create an instance of a presenter but it gets one injected by the presenter itself in the following way:
So far, so good.
Now on the View side, the Presenter is exposed as a Write Only property so that it can't be changed by the View, but it can be only used:
So far so good.
Now the IoC will do this:
For me, it looks nice but I just want to be sure that I am not breaking the MVP logic.