MVP 流程问题

发布于 2024-10-05 10:34:24 字数 1219 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攀登最高峰 2024-10-12 10:34:24

为了采用更简洁的方法,也将模型注入到演示器中

public class MyPresenter
{
   private IMyView view;
   private MyModel model;

   public MyPresenter(IMyView view, MyModel model)
   {
      this.view = view;
      this.model = model
   }
}

通过这样做,您始终可以在创建模型的演示器之外引用模型。

当您这样做时,您始终可以选择要使用的模型。
例如,如果您的后端(您的模型)尚未完成,您可以编写一个模拟模型(当您使用模型的接口时)来测试您的演示者和视图。

希望这有帮助

To have a more clean approach inject the model to the presenter, too

public class MyPresenter
{
   private IMyView view;
   private MyModel model;

   public MyPresenter(IMyView view, MyModel model)
   {
      this.view = view;
      this.model = model
   }
}

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

饭团 2024-10-12 10:34:24

基于此,我将通过反转依赖关系来改变一切。
现在,视图不会创建演示者的实例,但它会通过演示者本身以以下方式注入实例:

public MyPresenter(IView myView, Model myModel)
{
   this.View = myView;
   this.View.Presenter = this;
   this.Model = myModel;
}

到目前为止,一切顺利。
现在在视图方面,Presenter 被公开为只写属性,因此视图无法更改它,但它只能使用:

public class MyView : IView
{
   public MyPresenter Presenter { get; private set; }
}

到目前为止一切顺利。
现在,国际奥委会将这样做:

var view = IoC.Resolve<IView>();
var model = repository.GetModel(); // or new Model();
var presenter = IoC.Resolve<MyPresenter>(); //view and model injected
presenter.ShowView();
var result = presenter.Model;

对我来说,这看起来不错,但我只是想确保我没有打破 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:

public MyPresenter(IView myView, Model myModel)
{
   this.View = myView;
   this.View.Presenter = this;
   this.Model = myModel;
}

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:

public class MyView : IView
{
   public MyPresenter Presenter { get; private set; }
}

So far so good.
Now the IoC will do this:

var view = IoC.Resolve<IView>();
var model = repository.GetModel(); // or new Model();
var presenter = IoC.Resolve<MyPresenter>(); //view and model injected
presenter.ShowView();
var result = presenter.Model;

For me, it looks nice but I just want to be sure that I am not breaking the MVP logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文