简体   繁体   English

在视图之间交流

[英]Communicate between views

I'm having quite a bit of trouble trying to understand this pattern. 我在尝试理解这种模式时遇到了很多麻烦。 At work we are implementing MVP with passive views. 在工作中,我们正在使用被动视图来实现MVP。 We are using WinForms. 我们正在使用WinForms。

  • So we have a View (which is a windows form), which implements an interface and has a presenter. 因此,我们有一个View(它是Windows窗体),它实现了一个接口并有一个演示者。
  • The presenter gets an instance of the interface which the view implements, and manages the view throw the interface. 演示者获得该视图实现的接口的实例,并通过该接口管理视图。
  • The presenter can see the model, and a manager. 演示者可以看到模型和经理。
  • The manager handles the database persistance of several models, but it does not add them manually, it uses a separate layer, called Data, so it tells that layer to persist data 管理器处理几种模型的数据库持久性,但是它不是手动添加的,它使用一个单独的层,称为数据,因此告诉该层持久化数据
  • The data layer then persists the data to the database, using Entity Framework 然后,数据层使用实体框架将数据持久化到数据库中

Say we have a view of Clients, which reperesents a Model Clients and we have a table in our database called Clients. 假设我们有一个“客户”视图,它代表一个“模型客户”,并且在数据库中有一个名为“客户”的表。 Clients can have name, and city. 客户可以有名字和城市。

We also have a view for Cities, which is a simple add, edit and remove, it also represents a model City, and a table City. 我们还有一个城市视图,它是一个简单的添加,编辑和删除操作,它还代表一个模型城市和一个表城市。

Now in our view of Clients, we want to let the user add a new city, so in the view, we open another view, the Cities view, the user then creates a new city. 现在,在“客户”视图中,我们希望允许用户添加一个新城市,因此在该视图中,我们打开另一个视图,即“城市”视图,然后用户创建一个新城市。

From the Clients view, I want to see what City the user added, how is that possible? 从“客户”视图,我想查看用户添加的城市,这怎么可能? Shall the view return something? 视图会返回一些东西吗?

(I want to have it in memory, not persist anything to the database when adding a city, I just want the model of the city) (我想将其存储在内存中,而不在添加城市时将任何内容持久化到数据库中,我只需要城市的模型)

I think returning a model and viewing models from the view is not MVP. 我认为返回模型并从视图中查看模型不是MVP。 How can I do it then? 那我该怎么办呢?

One view opening up another is perfectly reasonable, but I personally don't favour the view returning a value to be used by the caller. 一个视图打开另一个视图是完全合理的,但是我个人不赞成该视图返回供调用方使用的值。 What if other views needed to have access to this new city? 如果需要其他视图来访问这个新城市怎么办?

See my answer here: MVP Communication between presenters? 在这里查看我的答案: 主持人之间的MVP沟通? for an example of using pub/sub to achieve your goal. 有关使用pub / sub实现目标的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM